; LLVM IR from reyadussalahin/problem-solving :: CONTEST/hackercup/Y2019/round01/FClassTreasurer (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"

@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [16 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca [1000010 x i64], align 16
  %3 = alloca [1000010 x i8], align 16
  %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
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 8000080, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 1000010, ptr %3) #3
  %13 = getelementptr inbounds [1000010 x i64], ptr %2, i64 0, i64 0
  store i64 1, ptr %13, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 1, ptr %4, align 4, !tbaa !9
  br label %14

14:                                               ; preds = %29, %0
  %15 = load i32, ptr %4, align 4, !tbaa !9
  %16 = icmp sle i32 %15, 1000000
  br i1 %16, label %18, label %17

17:                                               ; preds = %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %32

18:                                               ; preds = %14
  %19 = load i32, ptr %4, align 4, !tbaa !9
  %20 = sub nsw i32 %19, 1
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [1000010 x i64], ptr %2, i64 0, i64 %21
  %23 = load i64, ptr %22, align 8, !tbaa !5
  %24 = mul nsw i64 %23, 2
  %25 = srem i64 %24, 1000000007
  %26 = load i32, ptr %4, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [1000010 x i64], ptr %2, i64 0, i64 %27
  store i64 %25, ptr %28, align 8, !tbaa !5
  br label %29

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

32:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %33 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %5)
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %87, %32
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = icmp slt i32 %35, %36
  br i1 %37, label %39, label %38

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

39:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %40 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %8, ptr noundef %9)
  %41 = getelementptr inbounds [1000010 x i8], ptr %3, i64 0, i64 0
  %42 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.2, ptr noundef %41)
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  store i64 0, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %43 = load i32, ptr %8, align 4, !tbaa !9
  %44 = sub nsw i32 %43, 1
  store i32 %44, ptr %12, align 4, !tbaa !9
  br label %45

45:                                               ; preds = %79, %39
  %46 = load i32, ptr %12, align 4, !tbaa !9
  %47 = icmp sge i32 %46, 0
  br i1 %47, label %49, label %48

48:                                               ; preds = %45
  store i32 8, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %82

49:                                               ; preds = %45
  %50 = load i32, ptr %10, align 4, !tbaa !9
  %51 = load i32, ptr %12, align 4, !tbaa !9
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [1000010 x i8], ptr %3, i64 0, i64 %52
  %54 = load i8, ptr %53, align 1, !tbaa !13
  %55 = sext i8 %54 to i32
  %56 = icmp eq i32 %55, 66
  %57 = zext i1 %56 to i64
  %58 = select i1 %56, i32 1, i32 -1
  %59 = add nsw i32 %50, %58
  store i32 %59, ptr %10, align 4, !tbaa !9
  %60 = load i32, ptr %10, align 4, !tbaa !9
  %61 = load i32, ptr %9, align 4, !tbaa !9
  %62 = icmp sgt i32 %60, %61
  br i1 %62, label %63, label %74

63:                                               ; preds = %49
  %64 = load i64, ptr %11, align 8, !tbaa !5
  %65 = load i32, ptr %12, align 4, !tbaa !9
  %66 = add nsw i32 %65, 1
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [1000010 x i64], ptr %2, i64 0, i64 %67
  %69 = load i64, ptr %68, align 8, !tbaa !5
  %70 = add nsw i64 %64, %69
  %71 = srem i64 %70, 1000000007
  store i64 %71, ptr %11, align 8, !tbaa !5
  %72 = load i32, ptr %10, align 4, !tbaa !9
  %73 = sub nsw i32 %72, 2
  store i32 %73, ptr %10, align 4, !tbaa !9
  br label %74

74:                                               ; preds = %63, %49
  %75 = load i32, ptr %10, align 4, !tbaa !9
  %76 = icmp slt i32 %75, 0
  br i1 %76, label %77, label %78

77:                                               ; preds = %74
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %78

78:                                               ; preds = %77, %74
  br label %79

79:                                               ; preds = %78
  %80 = load i32, ptr %12, align 4, !tbaa !9
  %81 = add nsw i32 %80, -1
  store i32 %81, ptr %12, align 4, !tbaa !9
  br label %45, !llvm.loop !14

82:                                               ; preds = %48
  %83 = load i32, ptr %6, align 4, !tbaa !9
  %84 = add nsw i32 %83, 1
  %85 = load i64, ptr %11, align 8, !tbaa !5
  %86 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i32 noundef %84, i64 noundef %85)
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %87

87:                                               ; preds = %82
  %88 = load i32, ptr %6, align 4, !tbaa !9
  %89 = add nsw i32 %88, 1
  store i32 %89, ptr %6, align 4, !tbaa !9
  br label %34, !llvm.loop !15

90:                                               ; preds = %38
  store i32 0, ptr %1, align 4
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 1000010, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8000080, ptr %2) #3
  %91 = load i32, ptr %1, align 4
  ret i32 %91
}

; 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 = !{!7, !7, i64 0}
!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"

@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [16 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca [1000010 x i64], align 16
  %2 = alloca [1000010 x i8], align 16
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8000080, ptr nonnull %1) #4
  call void @llvm.lifetime.start.p0(i64 1000010, ptr nonnull %2) #4
  store i64 1, ptr %1, align 16, !tbaa !5
  br label %10

6:                                                ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #4
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %3)
  %8 = load i32, ptr %3, align 4, !tbaa !9
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %.lr.ph33, label %._crit_edge34

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

._crit_edge34:                                    ; preds = %._crit_edge, %6
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #4
  call void @llvm.lifetime.end.p0(i64 1000010, ptr nonnull %2) #4
  call void @llvm.lifetime.end.p0(i64 8000080, ptr nonnull %1) #4
  ret i32 0

.lr.ph33:                                         ; preds = %6, %._crit_edge
  %.02131 = phi i32 [ %24, %._crit_edge ], [ 0, %6 ]
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5) #4
  %18 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %4, ptr noundef nonnull %5)
  %19 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.2, ptr noundef nonnull %2)
  %20 = load i32, ptr %4, align 4, !tbaa !9
  %21 = icmp sgt i32 %20, 0
  br i1 %21, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.lr.ph33
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = zext nneg i32 %20 to i64
  br label %28

._crit_edge:                                      ; preds = %41, %.lr.ph33
  %.018.lcssa = phi i64 [ 0, %.lr.ph33 ], [ %.1, %41 ]
  %24 = add nuw nsw i32 %.02131, 1
  %25 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef %24, i64 noundef %.018.lcssa)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #4
  %26 = load i32, ptr %3, align 4, !tbaa !9
  %27 = icmp slt i32 %24, %26
  br i1 %27, label %.lr.ph33, label %._crit_edge34, !llvm.loop !13

28:                                               ; preds = %.lr.ph, %41
  %indvars.iv36 = phi i64 [ %23, %.lr.ph ], [ %indvars.iv.next37, %41 ]
  %.01828 = phi i64 [ 0, %.lr.ph ], [ %.1, %41 ]
  %.01927 = phi i32 [ 0, %.lr.ph ], [ %spec.store.select, %41 ]
  %indvars.iv.next37 = add nsw i64 %indvars.iv36, -1
  %29 = getelementptr inbounds [1000010 x i8], ptr %2, i64 0, i64 %indvars.iv.next37
  %30 = load i8, ptr %29, align 1, !tbaa !14
  %31 = icmp eq i8 %30, 66
  %32 = select i1 %31, i32 1, i32 -1
  %33 = add nsw i32 %32, %.01927
  %34 = icmp sgt i32 %33, %22
  br i1 %34, label %35, label %41

35:                                               ; preds = %28
  %36 = getelementptr inbounds [1000010 x i64], ptr %1, i64 0, i64 %indvars.iv36
  %37 = load i64, ptr %36, align 8, !tbaa !5
  %38 = add nsw i64 %37, %.01828
  %39 = srem i64 %38, 1000000007
  %40 = add nsw i32 %33, -2
  br label %41

41:                                               ; preds = %35, %28
  %.120 = phi i32 [ %40, %35 ], [ %33, %28 ]
  %.1 = phi i64 [ %39, %35 ], [ %.01828, %28 ]
  %spec.store.select = call i32 @llvm.smax.i32(i32 %.120, i32 0)
  %42 = icmp ugt i64 %indvars.iv36, 1
  br i1 %42, label %28, 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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #3

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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 = !{!7, !7, i64 0}
!15 = distinct !{!15, !12}


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