; LLVM IR from kazunetakahashi/atcoder :: 2018/0421_ARC096/E.ll (Z4initv)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@MOD = external dso_local global i64, align 8
@inv = external dso_local global [1000010 x i64], align 16
@fact = external dso_local global [1000010 x i64], align 16
@factinv = external dso_local global [1000010 x i64], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z4initv() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  store i64 1, ptr getelementptr inbounds ([1000010 x i64], ptr @inv, i64 0, i64 1), align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store i32 2, ptr %1, align 4, !tbaa !9
  br label %3

3:                                                ; preds = %26, %0
  %4 = load i32, ptr %1, align 4, !tbaa !9
  %5 = icmp slt i32 %4, 1000010
  br i1 %5, label %7, label %6

6:                                                ; preds = %3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  br label %29

7:                                                ; preds = %3
  %8 = load i64, ptr @MOD, align 8, !tbaa !5
  %9 = load i64, ptr @MOD, align 8, !tbaa !5
  %10 = load i32, ptr %1, align 4, !tbaa !9
  %11 = sext i32 %10 to i64
  %12 = srem i64 %9, %11
  %13 = getelementptr inbounds [1000010 x i64], ptr @inv, i64 0, i64 %12
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = sub nsw i64 %8, %14
  %16 = load i64, ptr @MOD, align 8, !tbaa !5
  %17 = load i32, ptr %1, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = sdiv i64 %16, %18
  %20 = mul nsw i64 %15, %19
  %21 = load i64, ptr @MOD, align 8, !tbaa !5
  %22 = srem i64 %20, %21
  %23 = load i32, ptr %1, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [1000010 x i64], ptr @inv, i64 0, i64 %24
  store i64 %22, ptr %25, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %7
  %27 = load i32, ptr %1, align 4, !tbaa !9
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %1, align 4, !tbaa !9
  br label %3, !llvm.loop !11

29:                                               ; preds = %6
  store i64 1, ptr @factinv, align 16, !tbaa !5
  store i64 1, ptr @fact, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  store i32 1, ptr %2, align 4, !tbaa !9
  br label %30

30:                                               ; preds = %63, %29
  %31 = load i32, ptr %2, align 4, !tbaa !9
  %32 = icmp slt i32 %31, 1000010
  br i1 %32, label %34, label %33

33:                                               ; preds = %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  br label %66

34:                                               ; preds = %30
  %35 = load i32, ptr %2, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = load i32, ptr %2, align 4, !tbaa !9
  %38 = sub nsw i32 %37, 1
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [1000010 x i64], ptr @fact, i64 0, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = mul nsw i64 %36, %41
  %43 = load i64, ptr @MOD, align 8, !tbaa !5
  %44 = srem i64 %42, %43
  %45 = load i32, ptr %2, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [1000010 x i64], ptr @fact, i64 0, i64 %46
  store i64 %44, ptr %47, align 8, !tbaa !5
  %48 = load i32, ptr %2, align 4, !tbaa !9
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [1000010 x i64], ptr @inv, i64 0, i64 %49
  %51 = load i64, ptr %50, align 8, !tbaa !5
  %52 = load i32, ptr %2, align 4, !tbaa !9
  %53 = sub nsw i32 %52, 1
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [1000010 x i64], ptr @factinv, i64 0, i64 %54
  %56 = load i64, ptr %55, align 8, !tbaa !5
  %57 = mul nsw i64 %51, %56
  %58 = load i64, ptr @MOD, align 8, !tbaa !5
  %59 = srem i64 %57, %58
  %60 = load i32, ptr %2, align 4, !tbaa !9
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [1000010 x i64], ptr @factinv, i64 0, i64 %61
  store i64 %59, ptr %62, align 8, !tbaa !5
  br label %63

63:                                               ; preds = %34
  %64 = load i32, ptr %2, align 4, !tbaa !9
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %2, align 4, !tbaa !9
  br label %30, !llvm.loop !13

66:                                               ; preds = %33
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { mustprogress nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind }

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

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@MOD = external dso_local local_unnamed_addr global i64, align 8
@inv = external dso_local local_unnamed_addr global [1000010 x i64], align 16
@fact = external dso_local local_unnamed_addr global [1000010 x i64], align 16
@factinv = external dso_local local_unnamed_addr global [1000010 x i64], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z4initv() local_unnamed_addr #0 {
  store i64 1, ptr getelementptr inbounds (i8, ptr @inv, i64 8), align 8, !tbaa !5
  %1 = load i64, ptr @MOD, align 8, !tbaa !5
  br label %3

2:                                                ; preds = %3
  store i64 1, ptr @factinv, align 16, !tbaa !5
  store i64 1, ptr @fact, align 16, !tbaa !5
  br label %13

3:                                                ; preds = %0, %3
  %indvars.iv = phi i64 [ 2, %0 ], [ %indvars.iv.next, %3 ]
  %4 = srem i64 %1, %indvars.iv
  %5 = getelementptr inbounds [1000010 x i64], ptr @inv, i64 0, i64 %4
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %7 = sub nsw i64 %1, %6
  %8 = sdiv i64 %1, %indvars.iv
  %9 = mul nsw i64 %7, %8
  %10 = srem i64 %9, %1
  %11 = getelementptr inbounds [1000010 x i64], ptr @inv, i64 0, i64 %indvars.iv
  store i64 %10, ptr %11, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 1000010
  br i1 %exitcond.not, label %2, label %3, !llvm.loop !9

12:                                               ; preds = %13
  ret void

13:                                               ; preds = %2, %13
  %14 = phi i64 [ 1, %2 ], [ %22, %13 ]
  %15 = phi i64 [ 1, %2 ], [ %17, %13 ]
  %indvars.iv17 = phi i64 [ 1, %2 ], [ %indvars.iv.next18, %13 ]
  %16 = mul nsw i64 %15, %indvars.iv17
  %17 = srem i64 %16, %1
  %18 = getelementptr inbounds [1000010 x i64], ptr @fact, i64 0, i64 %indvars.iv17
  store i64 %17, ptr %18, align 8, !tbaa !5
  %19 = getelementptr inbounds [1000010 x i64], ptr @inv, i64 0, i64 %indvars.iv17
  %20 = load i64, ptr %19, align 8, !tbaa !5
  %21 = mul nsw i64 %14, %20
  %22 = srem i64 %21, %1
  %23 = getelementptr inbounds [1000010 x i64], ptr @factinv, i64 0, i64 %indvars.iv17
  store i64 %22, ptr %23, align 8, !tbaa !5
  %indvars.iv.next18 = add nuw nsw i64 %indvars.iv17, 1
  %exitcond20.not = icmp eq i64 %indvars.iv.next18, 1000010
  br i1 %exitcond20.not, label %12, label %13, !llvm.loop !11
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

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

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass