; LLVM IR from reyadussalahin/problem-solving :: CONTEST/random_contest/Y2019/practice_for_preli/FLifeOfPhi (Solution)

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

@phi = external dso_local global [1000010 x i64], align 16
@sum = external dso_local global [1000010 x i64], align 16
@mark = external dso_local global [1000010 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1

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

9:                                                ; preds = %30, %0
  %10 = load i32, ptr %2, align 4, !tbaa !9
  %11 = icmp sle i32 %10, 1000000
  br i1 %11, label %13, label %12

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

13:                                               ; preds = %9
  %14 = load i32, ptr %2, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = load i32, ptr %2, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [1000010 x i64], ptr @phi, i64 0, i64 %17
  store i64 %15, ptr %18, align 8, !tbaa !5
  %19 = load i32, ptr %2, align 4, !tbaa !9
  %20 = sub nsw i32 %19, 1
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [1000010 x i64], ptr @sum, i64 0, i64 %21
  %23 = load i64, ptr %22, align 8, !tbaa !5
  %24 = load i32, ptr %2, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = add nsw i64 %23, %25
  %27 = load i32, ptr %2, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [1000010 x i64], ptr @sum, i64 0, i64 %28
  store i64 %26, ptr %29, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %13
  %31 = load i32, ptr %2, align 4, !tbaa !9
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %2, align 4, !tbaa !9
  br label %9, !llvm.loop !11

33:                                               ; preds = %12
  store i64 1, ptr getelementptr inbounds ([1000010 x i64], ptr @phi, i64 0, i64 1), align 8, !tbaa !5
  store i32 1, ptr getelementptr inbounds ([1000010 x i32], ptr @mark, i64 0, i64 1), align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 2, ptr %3, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %74, %33
  %35 = load i32, ptr %3, align 4, !tbaa !9
  %36 = icmp sle i32 %35, 1000000
  br i1 %36, label %38, label %37

37:                                               ; preds = %34
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %77

38:                                               ; preds = %34
  %39 = load i32, ptr %3, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [1000010 x i32], ptr @mark, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !9
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %73, label %44

44:                                               ; preds = %38
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %45 = load i32, ptr %3, align 4, !tbaa !9
  store i32 %45, ptr %5, align 4, !tbaa !9
  br label %46

46:                                               ; preds = %68, %44
  %47 = load i32, ptr %5, align 4, !tbaa !9
  %48 = icmp sle i32 %47, 1000000
  br i1 %48, label %50, label %49

49:                                               ; preds = %46
  store i32 8, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %72

50:                                               ; preds = %46
  %51 = load i32, ptr %5, align 4, !tbaa !9
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [1000010 x i32], ptr @mark, i64 0, i64 %52
  store i32 1, ptr %53, align 4, !tbaa !9
  %54 = load i32, ptr %3, align 4, !tbaa !9
  %55 = sub nsw i32 %54, 1
  %56 = sext i32 %55 to i64
  %57 = load i32, ptr %5, align 4, !tbaa !9
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [1000010 x i64], ptr @phi, i64 0, i64 %58
  %60 = load i64, ptr %59, align 8, !tbaa !5
  %61 = mul nsw i64 %56, %60
  %62 = load i32, ptr %3, align 4, !tbaa !9
  %63 = sext i32 %62 to i64
  %64 = sdiv i64 %61, %63
  %65 = load i32, ptr %5, align 4, !tbaa !9
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [1000010 x i64], ptr @phi, i64 0, i64 %66
  store i64 %64, ptr %67, align 8, !tbaa !5
  br label %68

68:                                               ; preds = %50
  %69 = load i32, ptr %3, align 4, !tbaa !9
  %70 = load i32, ptr %5, align 4, !tbaa !9
  %71 = add nsw i32 %70, %69
  store i32 %71, ptr %5, align 4, !tbaa !9
  br label %46, !llvm.loop !13

72:                                               ; preds = %49
  br label %73

73:                                               ; preds = %72, %38
  br label %74

74:                                               ; preds = %73
  %75 = load i32, ptr %3, align 4, !tbaa !9
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %3, align 4, !tbaa !9
  br label %34, !llvm.loop !14

77:                                               ; preds = %37
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %78 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %6)
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %79

79:                                               ; preds = %101, %77
  %80 = load i32, ptr %7, align 4, !tbaa !9
  %81 = load i32, ptr %6, align 4, !tbaa !9
  %82 = icmp slt i32 %80, %81
  br i1 %82, label %84, label %83

83:                                               ; preds = %79
  store i32 11, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %104

84:                                               ; preds = %79
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %85 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %8)
  %86 = load i32, ptr %8, align 4, !tbaa !9
  %87 = sub nsw i32 %86, 1
  %88 = sext i32 %87 to i64
  %89 = getelementptr inbounds [1000010 x i64], ptr @sum, i64 0, i64 %88
  %90 = load i64, ptr %89, align 8, !tbaa !5
  %91 = load i32, ptr %8, align 4, !tbaa !9
  %92 = sext i32 %91 to i64
  %93 = load i32, ptr %8, align 4, !tbaa !9
  %94 = sext i32 %93 to i64
  %95 = getelementptr inbounds [1000010 x i64], ptr @phi, i64 0, i64 %94
  %96 = load i64, ptr %95, align 8, !tbaa !5
  %97 = mul nsw i64 %92, %96
  %98 = sdiv i64 %97, 2
  %99 = sub nsw i64 %90, %98
  %100 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i64 noundef %99)
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %101

101:                                              ; preds = %84
  %102 = load i32, ptr %7, align 4, !tbaa !9
  %103 = add nsw i32 %102, 1
  store i32 %103, ptr %7, align 4, !tbaa !9
  br label %79, !llvm.loop !15

104:                                              ; preds = %83
  store i32 0, ptr %1, align 4
  store i32 1, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %105 = load i32, ptr %1, align 4
  ret i32 %105
}

; 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

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

declare i32 @printf(ptr noundef, ...) #2

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = { 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}
!15 = distinct !{!15, !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"

@phi = external dso_local local_unnamed_addr global [1000010 x i64], align 16
@sum = external dso_local local_unnamed_addr global [1000010 x i64], align 16
@mark = external dso_local local_unnamed_addr global [1000010 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1

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

3:                                                ; preds = %4
  store i64 1, ptr getelementptr inbounds (i8, ptr @phi, i64 8), align 8, !tbaa !5
  store i32 1, ptr getelementptr inbounds (i8, ptr @mark, i64 4), align 4, !tbaa !9
  br label %22

4:                                                ; preds = %4, %0
  %5 = phi i64 [ 0, %0 ], [ %16, %4 ]
  %indvars.iv = phi i64 [ 1, %0 ], [ %indvars.iv.next.3, %4 ]
  %6 = getelementptr inbounds [1000010 x i64], ptr @phi, i64 0, i64 %indvars.iv
  store i64 %indvars.iv, ptr %6, align 8, !tbaa !5
  %7 = add nuw nsw i64 %5, %indvars.iv
  %8 = getelementptr inbounds [1000010 x i64], ptr @sum, i64 0, i64 %indvars.iv
  store i64 %7, ptr %8, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %9 = getelementptr inbounds [1000010 x i64], ptr @phi, i64 0, i64 %indvars.iv.next
  store i64 %indvars.iv.next, ptr %9, align 8, !tbaa !5
  %10 = add nuw nsw i64 %7, %indvars.iv.next
  %11 = getelementptr inbounds [1000010 x i64], ptr @sum, i64 0, i64 %indvars.iv.next
  store i64 %10, ptr %11, align 8, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %12 = getelementptr inbounds [1000010 x i64], ptr @phi, i64 0, i64 %indvars.iv.next.1
  store i64 %indvars.iv.next.1, ptr %12, align 8, !tbaa !5
  %13 = add nuw nsw i64 %10, %indvars.iv.next.1
  %14 = getelementptr inbounds [1000010 x i64], ptr @sum, i64 0, i64 %indvars.iv.next.1
  store i64 %13, ptr %14, align 8, !tbaa !5
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %15 = getelementptr inbounds [1000010 x i64], ptr @phi, i64 0, i64 %indvars.iv.next.2
  store i64 %indvars.iv.next.2, ptr %15, align 8, !tbaa !5
  %16 = add nuw nsw i64 %13, %indvars.iv.next.2
  %17 = getelementptr inbounds [1000010 x i64], ptr @sum, i64 0, i64 %indvars.iv.next.2
  store i64 %16, ptr %17, align 8, !tbaa !5
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, 1000001
  br i1 %exitcond.not.3, label %3, label %4, !llvm.loop !11

18:                                               ; preds = %.loopexit
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #3
  %19 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %20 = load i32, ptr %1, align 4, !tbaa !9
  %21 = icmp sgt i32 %20, 0
  br i1 %21, label %.lr.ph30, label %._crit_edge

22:                                               ; preds = %3, %.loopexit
  %indvars.iv32 = phi i64 [ 2, %3 ], [ %indvars.iv.next33, %.loopexit ]
  %23 = getelementptr inbounds [1000010 x i32], ptr @mark, i64 0, i64 %indvars.iv32
  %24 = load i32, ptr %23, align 4, !tbaa !9
  %.not = icmp eq i32 %24, 0
  br i1 %.not, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %22
  %25 = add nsw i64 %indvars.iv32, -1
  br label %26

26:                                               ; preds = %.lr.ph, %26
  %indvars.iv34 = phi i64 [ %indvars.iv32, %.lr.ph ], [ %indvars.iv.next35, %26 ]
  %27 = getelementptr inbounds [1000010 x i32], ptr @mark, i64 0, i64 %indvars.iv34
  store i32 1, ptr %27, align 4, !tbaa !9
  %28 = getelementptr inbounds [1000010 x i64], ptr @phi, i64 0, i64 %indvars.iv34
  %29 = load i64, ptr %28, align 8, !tbaa !5
  %30 = mul nsw i64 %29, %25
  %31 = sdiv i64 %30, %indvars.iv32
  store i64 %31, ptr %28, align 8, !tbaa !5
  %indvars.iv.next35 = add nuw nsw i64 %indvars.iv34, %indvars.iv32
  %32 = icmp ult i64 %indvars.iv.next35, 1000001
  br i1 %32, label %26, label %.loopexit, !llvm.loop !13

.loopexit:                                        ; preds = %26, %22
  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
  %exitcond38.not = icmp eq i64 %indvars.iv.next33, 1000001
  br i1 %exitcond38.not, label %18, label %22, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph30, %18
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #3
  ret i32 0

.lr.ph30:                                         ; preds = %18, %.lr.ph30
  %.029 = phi i32 [ %45, %.lr.ph30 ], [ 0, %18 ]
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #3
  %33 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %2)
  %34 = load i32, ptr %2, align 4, !tbaa !9
  %35 = add nsw i32 %34, -1
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [1000010 x i64], ptr @sum, i64 0, i64 %36
  %38 = load i64, ptr %37, align 8, !tbaa !5
  %39 = sext i32 %34 to i64
  %40 = getelementptr inbounds [1000010 x i64], ptr @phi, i64 0, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = mul nsw i64 %41, %39
  %.neg = sdiv i64 %42, -2
  %43 = add i64 %.neg, %38
  %44 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i64 noundef %43)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #3
  %45 = add nuw nsw i32 %.029, 1
  %46 = load i32, ptr %1, align 4, !tbaa !9
  %47 = icmp slt i32 %45, %46
  br i1 %47, label %.lr.ph30, label %._crit_edge, !llvm.loop !15
}

; 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: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

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

attributes #0 = { mustprogress nofree norecurse 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 = { 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 #3 = { 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}
!15 = distinct !{!15, !12}


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