!http://web.agr.ehime-u.ac.jp/~kishou/Lecture/atmosphere/Fortran90/Chap4_answer.pdf
!-----------------------------------------
! エラトステネスのふるい
implicit none
logical, dimension(100000) :: flag ! ここでは論理型を使ったが、整数型でも可
integer, dimension(100000) :: count=0, nitakazu_count=0, ruiseki_count=0
integer :: i, n, nn, a, b
flag(:) = .false. ! すべて「偽」に初期化
n = 0 ! 素数の個数を数えるカウンター
do i = 2, 100000
    if (flag(i)) cycle ! 「偽」すなわち素数でなければ次の数へ
    count(i)=1
    if ( count(int((i+1)/2))==1 ) then
        nitakazu_count(i)=1
    endif
    !write(6,*) i ! 出力して
    n = n + 1 ! カウンタをひとつ増やし
    flag(i:100000:i) = .true. ! その倍数をすべて「真」にする
enddo

!nitakazu_count(2)=0

!write(6,*) nitakazu_count

do i = 2, 99999
  ruiseki_count(i+1)=ruiseki_count(i)+nitakazu_count(i+1)
enddo

read(5,*) nn
do i= 1, nn
  read(5,*) a, b
  write(6,*) ruiseki_count(b)-ruiseki_count(a-1)
  
enddo

stop
end

; ModuleID = 'FIRModule'
source_filename = "FIRModule"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

@_QFEcount = internal unnamed_addr global [100000 x i32] zeroinitializer
@_QFEflag = internal unnamed_addr global [100000 x i32] undef
@_QFEnitakazu_count = internal unnamed_addr global [100000 x i32] zeroinitializer
@_QFEruiseki_count = internal unnamed_addr global [100000 x i32] zeroinitializer
@_QQcl.4a6badf3fa2a1c8cdf54f8b3592a643c = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_6509/source.f\00"
@_QQEnvironmentDefaults = local_unnamed_addr constant ptr null

; Function Attrs: noreturn
define void @_QQmain() local_unnamed_addr #0 {
.preheader15.preheader:
  %0 = alloca i32, align 4
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(400000) @_QFEflag, i8 0, i64 400000, i1 false), !tbaa !3
  br label %.preheader15

.preheader15:                                     ; preds = %.preheader15.preheader, %.loopexit
  %indvars.iv = phi i64 [ 2, %.preheader15.preheader ], [ %indvars.iv.next.pre-phi, %.loopexit ]
  %3 = add nsw i64 %indvars.iv, -1
  %4 = getelementptr [100000 x i32], ptr @_QFEflag, i64 0, i64 %3
  %5 = load i32, ptr %4, align 4, !tbaa !3
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %6, label %.preheader15..loopexit_crit_edge

.preheader15..loopexit_crit_edge:                 ; preds = %.preheader15
  %.pre25 = add nuw nsw i64 %indvars.iv, 1
  br label %.loopexit

6:                                                ; preds = %.preheader15
  %7 = getelementptr [100000 x i32], ptr @_QFEcount, i64 0, i64 %3
  store i32 1, ptr %7, align 4, !tbaa !3
  %8 = add nuw nsw i64 %indvars.iv, 1
  %9 = trunc i64 %8 to i32
  %.udiv1926 = lshr i32 %9, 1
  %narrow = add nsw i32 %.udiv1926, -1
  %10 = sext i32 %narrow to i64
  %11 = getelementptr [100000 x i32], ptr @_QFEcount, i64 0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !3
  %13 = icmp eq i32 %12, 1
  br i1 %13, label %14, label %16

14:                                               ; preds = %6
  %15 = getelementptr [100000 x i32], ptr @_QFEnitakazu_count, i64 0, i64 %3
  store i32 1, ptr %15, align 4, !tbaa !3
  br label %16

16:                                               ; preds = %14, %6
  %17 = trunc i64 %indvars.iv to i32
  %.not18 = icmp ugt i32 %17, 100000
  br i1 %.not18, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %16
  %.udiv = udiv i32 100000, %17
  %18 = zext i32 %.udiv to i64
  %min.iters.check = icmp ult i32 %17, 12501
  %ident.check.not = icmp eq i64 %indvars.iv, 1
  %or.cond = and i1 %min.iters.check, %ident.check.not
  br i1 %or.cond, label %vector.ph, label %.lr.ph.preheader28

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %18, 131064
  %ind.end = and i64 %18, 7
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %19 = mul nuw nsw i64 %index, %indvars.iv
  %20 = add nuw nsw i64 %19, %3
  %21 = getelementptr i32, ptr @_QFEflag, i64 %20
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %21, align 4, !tbaa !3
  %22 = getelementptr i32, ptr %21, i64 4
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %22, align 4, !tbaa !3
  %index.next = add nuw i64 %index, 8
  %23 = icmp eq i64 %index.next, %n.vec
  br i1 %23, label %middle.block, label %vector.body, !llvm.loop !7

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %18
  br i1 %cmp.n, label %.loopexit, label %.lr.ph.preheader28

.lr.ph.preheader28:                               ; preds = %.lr.ph.preheader, %middle.block
  %.ph = phi i64 [ %18, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.ph29 = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader28, %.lr.ph
  %24 = phi i64 [ %30, %.lr.ph ], [ %.ph, %.lr.ph.preheader28 ]
  %25 = phi i64 [ %26, %.lr.ph ], [ %.ph29, %.lr.ph.preheader28 ]
  %26 = add nuw nsw i64 %25, 1
  %27 = mul nuw nsw i64 %25, %indvars.iv
  %28 = add nuw nsw i64 %27, %3
  %29 = getelementptr i32, ptr @_QFEflag, i64 %28
  store i32 1, ptr %29, align 4, !tbaa !3
  %30 = add nsw i64 %24, -1
  %31 = icmp ugt i64 %24, 1
  br i1 %31, label %.lr.ph, label %.loopexit, !llvm.loop !10

.loopexit:                                        ; preds = %.lr.ph, %middle.block, %.preheader15..loopexit_crit_edge, %16
  %indvars.iv.next.pre-phi = phi i64 [ %.pre25, %.preheader15..loopexit_crit_edge ], [ %8, %16 ], [ %8, %middle.block ], [ %8, %.lr.ph ]
  %exitcond.not = icmp eq i64 %indvars.iv.next.pre-phi, 100001
  br i1 %exitcond.not, label %.preheader.preheader, label %.preheader15

.preheader.preheader:                             ; preds = %.loopexit
  %.pre = load i32, ptr getelementptr inbounds ([100000 x i32], ptr @_QFEruiseki_count, i64 0, i64 1), align 4, !tbaa !3
  br label %.preheader

.preheader:                                       ; preds = %.preheader, %.preheader.preheader
  %32 = phi i32 [ %.pre, %.preheader.preheader ], [ %39, %.preheader ]
  %indvars.iv21 = phi i64 [ 2, %.preheader.preheader ], [ %indvars.iv.next22.1, %.preheader ]
  %indvars.iv.next22 = or i64 %indvars.iv21, 1
  %33 = getelementptr [100000 x i32], ptr @_QFEnitakazu_count, i64 0, i64 %indvars.iv21
  %34 = load i32, ptr %33, align 8, !tbaa !3
  %35 = add i32 %34, %32
  %36 = getelementptr [100000 x i32], ptr @_QFEruiseki_count, i64 0, i64 %indvars.iv21
  store i32 %35, ptr %36, align 8, !tbaa !3
  %indvars.iv.next22.1 = add nuw nsw i64 %indvars.iv21, 2
  %37 = getelementptr [100000 x i32], ptr @_QFEnitakazu_count, i64 0, i64 %indvars.iv.next22
  %38 = load i32, ptr %37, align 4, !tbaa !3
  %39 = add i32 %38, %35
  %40 = getelementptr [100000 x i32], ptr @_QFEruiseki_count, i64 0, i64 %indvars.iv.next22
  store i32 %39, ptr %40, align 4, !tbaa !3
  %exitcond24.not.1 = icmp eq i64 %indvars.iv.next22.1, 100000
  br i1 %exitcond24.not.1, label %41, label %.preheader

41:                                               ; preds = %.preheader
  %42 = tail call ptr @_FortranAioBeginExternalListInput(i32 5, ptr nonnull @_QQcl.4a6badf3fa2a1c8cdf54f8b3592a643c, i32 29)
  %43 = call i1 @_FortranAioInputInteger(ptr %42, ptr nonnull %2, i32 4)
  %44 = call i32 @_FortranAioEndIoStatement(ptr %42)
  %45 = load i32, ptr %2, align 4, !tbaa !3
  %46 = icmp sgt i32 %45, 0
  br i1 %46, label %.lr.ph17.preheader, label %._crit_edge

.lr.ph17.preheader:                               ; preds = %41
  %47 = zext i32 %45 to i64
  br label %.lr.ph17

.lr.ph17:                                         ; preds = %.lr.ph17.preheader, %.lr.ph17
  %48 = phi i64 [ %68, %.lr.ph17 ], [ %47, %.lr.ph17.preheader ]
  %49 = call ptr @_FortranAioBeginExternalListInput(i32 5, ptr nonnull @_QQcl.4a6badf3fa2a1c8cdf54f8b3592a643c, i32 31)
  %50 = call i1 @_FortranAioInputInteger(ptr %49, ptr nonnull %0, i32 4)
  %51 = call i1 @_FortranAioInputInteger(ptr %49, ptr nonnull %1, i32 4)
  %52 = call i32 @_FortranAioEndIoStatement(ptr %49)
  %53 = call ptr @_FortranAioBeginExternalListOutput(i32 6, ptr nonnull @_QQcl.4a6badf3fa2a1c8cdf54f8b3592a643c, i32 32)
  %54 = load i32, ptr %1, align 4, !tbaa !3
  %55 = sext i32 %54 to i64
  %56 = add nsw i64 %55, -1
  %57 = getelementptr [100000 x i32], ptr @_QFEruiseki_count, i64 0, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !3
  %59 = load i32, ptr %0, align 4, !tbaa !3
  %60 = add i32 %59, -1
  %61 = sext i32 %60 to i64
  %62 = add nsw i64 %61, -1
  %63 = getelementptr [100000 x i32], ptr @_QFEruiseki_count, i64 0, i64 %62
  %64 = load i32, ptr %63, align 4, !tbaa !3
  %65 = sub i32 %58, %64
  %66 = call i1 @_FortranAioOutputInteger32(ptr %53, i32 %65)
  %67 = call i32 @_FortranAioEndIoStatement(ptr %53)
  %68 = add nsw i64 %48, -1
  %69 = icmp ugt i64 %48, 1
  br i1 %69, label %.lr.ph17, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph17, %41
  %70 = call {} @_FortranAStopStatement(i32 0, i1 false, i1 false)
  unreachable
}

declare ptr @_FortranAioBeginExternalListInput(i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioInputInteger(ptr, ptr, i32) local_unnamed_addr

declare i32 @_FortranAioEndIoStatement(ptr) local_unnamed_addr

declare ptr @_FortranAioBeginExternalListOutput(i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioOutputInteger32(ptr, i32) local_unnamed_addr

declare {} @_FortranAStopStatement(i32, i1, i1) local_unnamed_addr

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { noreturn }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!llvm.module.flags = !{!0, !1, !2}

!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{!4, !4, i64 0}
!4 = !{!"any data access", !5, i64 0}
!5 = !{!"any access", !6, i64 0}
!6 = !{!"Flang Type TBAA Root"}
!7 = distinct !{!7, !8, !9}
!8 = !{!"llvm.loop.isvectorized", i32 1}
!9 = !{!"llvm.loop.unroll.runtime.disable"}
!10 = distinct !{!10, !8}
