; LLVM IR from VastoLorde95/Competitive-Programming :: Hackerrank/Codesprints 2016/Codesprint July/Problem D (D)

; 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"

@f = external dso_local global [500100 x i64], align 16
@inv = external dso_local global [500100 x i64], align 16
@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z4fastxx(i64 noundef, i64 noundef) #0

; 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

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z5solvev() #2

declare i32 @__isoc99_scanf(ptr noundef, ...) #3

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #4 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  store i64 1, ptr @f, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 1, ptr %2, align 4, !tbaa !9
  br label %5

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

8:                                                ; preds = %5
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  br label %25

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

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

25:                                               ; preds = %8
  %26 = load i64, ptr getelementptr inbounds ([500100 x i64], ptr @f, i64 0, i64 500099), align 8, !tbaa !5
  %27 = call noundef i64 @_Z4fastxx(i64 noundef %26, i64 noundef 1000000005)
  store i64 %27, ptr getelementptr inbounds ([500100 x i64], ptr @inv, i64 0, i64 500099), align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  store i32 500098, ptr %3, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %46, %25
  %29 = load i32, ptr %3, align 4, !tbaa !9
  %30 = icmp sge i32 %29, 0
  br i1 %30, label %32, label %31

31:                                               ; preds = %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  br label %49

32:                                               ; preds = %28
  %33 = load i32, ptr %3, align 4, !tbaa !9
  %34 = add nsw i32 %33, 1
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [500100 x i64], ptr @inv, i64 0, i64 %35
  %37 = load i64, ptr %36, align 8, !tbaa !5
  %38 = load i32, ptr %3, align 4, !tbaa !9
  %39 = add nsw i32 %38, 1
  %40 = sext i32 %39 to i64
  %41 = mul nsw i64 %37, %40
  %42 = srem i64 %41, 1000000007
  %43 = load i32, ptr %3, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [500100 x i64], ptr @inv, i64 0, i64 %44
  store i64 %42, ptr %45, align 8, !tbaa !5
  br label %46

46:                                               ; preds = %32
  %47 = load i32, ptr %3, align 4, !tbaa !9
  %48 = add nsw i32 %47, -1
  store i32 %48, ptr %3, align 4, !tbaa !9
  br label %28, !llvm.loop !13

49:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  %50 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.2, ptr noundef %4)
  br label %51

51:                                               ; preds = %55, %49
  %52 = load i32, ptr %4, align 4, !tbaa !9
  %53 = add nsw i32 %52, -1
  store i32 %53, ptr %4, align 4, !tbaa !9
  %54 = icmp ne i32 %52, 0
  br i1 %54, label %55, label %56

55:                                               ; preds = %51
  call void @_Z5solvev()
  br label %51, !llvm.loop !14

56:                                               ; preds = %51
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  ret i32 0
}

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 = { mustprogress 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 #3 = { "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 #4 = { mustprogress norecurse 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 #5 = { 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"

@f = external dso_local local_unnamed_addr global [500100 x i64], align 16
@inv = external dso_local local_unnamed_addr global [500100 x i64], align 16
@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z4fastxx(i64 noundef, i64 noundef) local_unnamed_addr #0

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z5solvev() local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #4 {
  %1 = alloca i32, align 4
  store i64 1, ptr @f, align 16, !tbaa !5
  br label %5

2:                                                ; preds = %5
  %3 = load i64, ptr getelementptr inbounds (i8, ptr @f, i64 4000792), align 8, !tbaa !5
  %4 = tail call noundef i64 @_Z4fastxx(i64 noundef %3, i64 noundef 1000000005)
  store i64 %4, ptr getelementptr inbounds (i8, ptr @inv, i64 4000792), align 8, !tbaa !5
  br label %18

5:                                                ; preds = %10, %0
  %6 = phi i64 [ 1, %0 ], [ %12, %10 ]
  %indvars.iv = phi i64 [ 1, %0 ], [ %indvars.iv.next.1, %10 ]
  %7 = mul nsw i64 %6, %indvars.iv
  %8 = srem i64 %7, 1000000007
  %9 = getelementptr inbounds [500100 x i64], ptr @f, i64 0, i64 %indvars.iv
  store i64 %8, ptr %9, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 500100
  br i1 %exitcond.not, label %2, label %10, !llvm.loop !9

10:                                               ; preds = %5
  %11 = mul nsw i64 %8, %indvars.iv.next
  %12 = srem i64 %11, 1000000007
  %13 = getelementptr inbounds [500100 x i64], ptr @f, i64 0, i64 %indvars.iv.next
  store i64 %12, ptr %13, align 8, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  br label %5

14:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #5
  %15 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.2, ptr noundef nonnull %1)
  %16 = load i32, ptr %1, align 4, !tbaa !11
  %17 = add nsw i32 %16, -1
  store i32 %17, ptr %1, align 4, !tbaa !11
  %.not13 = icmp eq i32 %16, 0
  br i1 %.not13, label %._crit_edge, label %.lr.ph

18:                                               ; preds = %24, %2
  %19 = phi i64 [ %4, %2 ], [ %26, %24 ]
  %indvars.iv15 = phi i64 [ 500098, %2 ], [ %indvars.iv.next16.1, %24 ]
  %20 = or disjoint i64 %indvars.iv15, 1
  %21 = mul nsw i64 %19, %20
  %22 = srem i64 %21, 1000000007
  %23 = getelementptr inbounds [500100 x i64], ptr @inv, i64 0, i64 %indvars.iv15
  store i64 %22, ptr %23, align 16, !tbaa !5
  %.not18 = icmp eq i64 %indvars.iv15, 0
  br i1 %.not18, label %14, label %24, !llvm.loop !13

24:                                               ; preds = %18
  %indvars.iv.next16 = add nsw i64 %indvars.iv15, -1
  %25 = mul nsw i64 %22, %indvars.iv15
  %26 = srem i64 %25, 1000000007
  %27 = getelementptr inbounds [500100 x i64], ptr @inv, i64 0, i64 %indvars.iv.next16
  store i64 %26, ptr %27, align 8, !tbaa !5
  %indvars.iv.next16.1 = add nsw i64 %indvars.iv15, -2
  br label %18

.lr.ph:                                           ; preds = %14, %.lr.ph
  call void @_Z5solvev()
  %28 = load i32, ptr %1, align 4, !tbaa !11
  %29 = add nsw i32 %28, -1
  store i32 %29, ptr %1, align 4, !tbaa !11
  %.not = icmp eq i32 %28, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #5
  ret i32 0
}

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress 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 #3 = { nofree nounwind "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 #4 = { mustprogress norecurse 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 #5 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass