; LLVM IR from kazunetakahashi/atcoder :: 2018/0325_ARC093/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"

@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
@union_find = external dso_local global [100010 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z4initv() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = 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 %4

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

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

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

23:                                               ; preds = %8
  %24 = load i32, ptr %1, align 4, !tbaa !9
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %1, align 4, !tbaa !9
  br label %4, !llvm.loop !11

26:                                               ; preds = %7
  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 %27

27:                                               ; preds = %58, %26
  %28 = load i32, ptr %2, align 4, !tbaa !9
  %29 = icmp slt i32 %28, 1000010
  br i1 %29, label %31, label %30

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

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

58:                                               ; preds = %31
  %59 = load i32, ptr %2, align 4, !tbaa !9
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %2, align 4, !tbaa !9
  br label %27, !llvm.loop !13

61:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %62

62:                                               ; preds = %71, %61
  %63 = load i32, ptr %3, align 4, !tbaa !9
  %64 = icmp slt i32 %63, 100010
  br i1 %64, label %66, label %65

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

66:                                               ; preds = %62
  %67 = load i32, ptr %3, align 4, !tbaa !9
  %68 = load i32, ptr %3, align 4, !tbaa !9
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds [100010 x i32], ptr @union_find, i64 0, i64 %69
  store i32 %67, ptr %70, align 4, !tbaa !9
  br label %71

71:                                               ; preds = %66
  %72 = load i32, ptr %3, align 4, !tbaa !9
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %3, align 4, !tbaa !9
  br label %62, !llvm.loop !14

74:                                               ; preds = %65
  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}
!14 = distinct !{!14, !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"

@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
@union_find = external dso_local local_unnamed_addr global [100010 x i32], 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
  br label %2

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

2:                                                ; preds = %0, %2
  %indvars.iv = phi i64 [ 2, %0 ], [ %indvars.iv.next, %2 ]
  %3 = trunc nuw nsw i64 %indvars.iv to i32
  %4 = urem i32 1000000007, %3
  %.zext = zext nneg i32 %4 to i64
  %5 = getelementptr inbounds [1000010 x i64], ptr @inv, i64 0, i64 %.zext
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %7 = sub nsw i64 1000000007, %6
  %8 = udiv i32 1000000007, %3
  %.zext20 = zext nneg i32 %8 to i64
  %9 = mul nsw i64 %7, %.zext20
  %10 = srem i64 %9, 1000000007
  %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 %1, label %2, !llvm.loop !9

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

vector.body:                                      ; preds = %12, %vector.body
  %index = phi i64 [ %index.next.2, %vector.body ], [ 0, %12 ]
  %vec.ind = phi <4 x i32> [ %vec.ind.next.2, %vector.body ], [ <i32 0, i32 1, i32 2, i32 3>, %12 ]
  %step.add = add <4 x i32> %vec.ind, <i32 4, i32 4, i32 4, i32 4>
  %23 = getelementptr inbounds [100010 x i32], ptr @union_find, i64 0, i64 %index
  %24 = getelementptr inbounds i8, ptr %23, i64 16
  store <4 x i32> %vec.ind, ptr %23, align 16, !tbaa !12
  store <4 x i32> %step.add, ptr %24, align 16, !tbaa !12
  %index.next = add nuw nsw i64 %index, 8
  %vec.ind.next = add <4 x i32> %vec.ind, <i32 8, i32 8, i32 8, i32 8>
  %step.add.1 = add <4 x i32> %vec.ind, <i32 12, i32 12, i32 12, i32 12>
  %25 = getelementptr inbounds [100010 x i32], ptr @union_find, i64 0, i64 %index.next
  %26 = getelementptr inbounds i8, ptr %25, i64 16
  store <4 x i32> %vec.ind.next, ptr %25, align 16, !tbaa !12
  store <4 x i32> %step.add.1, ptr %26, align 16, !tbaa !12
  %index.next.1 = add nuw nsw i64 %index, 16
  %vec.ind.next.1 = add <4 x i32> %vec.ind, <i32 16, i32 16, i32 16, i32 16>
  %step.add.2 = add <4 x i32> %vec.ind, <i32 20, i32 20, i32 20, i32 20>
  %27 = getelementptr inbounds [100010 x i32], ptr @union_find, i64 0, i64 %index.next.1
  %28 = getelementptr inbounds i8, ptr %27, i64 16
  store <4 x i32> %vec.ind.next.1, ptr %27, align 16, !tbaa !12
  store <4 x i32> %step.add.2, ptr %28, align 16, !tbaa !12
  %index.next.2 = add nuw nsw i64 %index, 24
  %vec.ind.next.2 = add <4 x i32> %vec.ind, <i32 24, i32 24, i32 24, i32 24>
  %29 = icmp eq i64 %index.next.2, 100008
  br i1 %29, label %.preheader, label %vector.body, !llvm.loop !14

.preheader:                                       ; preds = %vector.body
  store i32 100008, ptr getelementptr inbounds (i8, ptr @union_find, i64 400032), align 16, !tbaa !12
  store i32 100009, ptr getelementptr inbounds (i8, ptr @union_find, i64 400036), align 4, !tbaa !12
  ret void
}

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}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !10, !15, !16}
!15 = !{!"llvm.loop.isvectorized", i32 1}
!16 = !{!"llvm.loop.unroll.runtime.disable"}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass