; LLVM IR from TLE-MEC/Hack-CP-DSA :: Hackerrank/Birthday Cake Candles (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

; Function Attrs: nounwind uwtable
define dso_local 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 ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %10 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %4)
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = zext i32 %11 to i64
  %13 = call ptr @llvm.stacksave.p0()
  store ptr %13, ptr %5, align 8
  %14 = alloca i32, i64 %12, align 16
  store i64 %12, ptr %6, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %15

15:                                               ; preds = %25, %0
  %16 = load i32, ptr %7, align 4, !tbaa !5
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = icmp slt i32 %16, %17
  br i1 %18, label %20, label %19

19:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %28

20:                                               ; preds = %15
  %21 = load i32, ptr %7, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds i32, ptr %14, i64 %22
  %24 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %23)
  br label %25

25:                                               ; preds = %20
  %26 = load i32, ptr %7, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %7, align 4, !tbaa !5
  br label %15, !llvm.loop !9

28:                                               ; preds = %19
  %29 = getelementptr inbounds i32, ptr %14, i64 0
  %30 = load i32, ptr %29, align 16, !tbaa !5
  store i32 %30, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %49, %28
  %32 = load i32, ptr %8, align 4, !tbaa !5
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = icmp slt i32 %32, %33
  br i1 %34, label %36, label %35

35:                                               ; preds = %31
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %52

36:                                               ; preds = %31
  %37 = load i32, ptr %8, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds i32, ptr %14, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = load i32, ptr %2, align 4, !tbaa !5
  %42 = icmp sgt i32 %40, %41
  br i1 %42, label %43, label %48

43:                                               ; preds = %36
  %44 = load i32, ptr %8, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds i32, ptr %14, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  store i32 %47, ptr %2, align 4, !tbaa !5
  br label %48

48:                                               ; preds = %43, %36
  br label %49

49:                                               ; preds = %48
  %50 = load i32, ptr %8, align 4, !tbaa !5
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %8, align 4, !tbaa !5
  br label %31, !llvm.loop !11

52:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %53

53:                                               ; preds = %69, %52
  %54 = load i32, ptr %9, align 4, !tbaa !5
  %55 = load i32, ptr %4, align 4, !tbaa !5
  %56 = icmp slt i32 %54, %55
  br i1 %56, label %58, label %57

57:                                               ; preds = %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %72

58:                                               ; preds = %53
  %59 = load i32, ptr %9, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds i32, ptr %14, i64 %60
  %62 = load i32, ptr %61, align 4, !tbaa !5
  %63 = load i32, ptr %2, align 4, !tbaa !5
  %64 = icmp eq i32 %62, %63
  br i1 %64, label %65, label %68

65:                                               ; preds = %58
  %66 = load i32, ptr %3, align 4, !tbaa !5
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %3, align 4, !tbaa !5
  br label %68

68:                                               ; preds = %65, %58
  br label %69

69:                                               ; preds = %68
  %70 = load i32, ptr %9, align 4, !tbaa !5
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %9, align 4, !tbaa !5
  br label %53, !llvm.loop !12

72:                                               ; preds = %57
  %73 = load i32, ptr %3, align 4, !tbaa !5
  %74 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %73)
  store i32 0, ptr %1, align 4
  %75 = load ptr, ptr %5, align 8
  call void @llvm.stackrestore.p0(ptr %75)
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  %76 = load i32, ptr %1, align 4
  ret i32 %76
}

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #3

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #3

attributes #0 = { 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 = { "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 willreturn }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


; 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

; Function Attrs: nofree nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #5
  %2 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %3 = load i32, ptr %1, align 4, !tbaa !5
  %4 = zext i32 %3 to i64
  %5 = call ptr @llvm.stacksave.p0()
  %6 = alloca i32, i64 %4, align 16
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %.lr.ph, label %._crit_edge31

._crit_edge:                                      ; preds = %.lr.ph
  %9 = icmp sgt i32 %18, 0
  br i1 %9, label %.lr.ph26.preheader, label %._crit_edge31

.lr.ph26.preheader:                               ; preds = %._crit_edge
  %.pre = load i32, ptr %6, align 16, !tbaa !5
  %wide.trip.count = zext nneg i32 %18 to i64
  %min.iters.check = icmp ult i32 %18, 8
  br i1 %min.iters.check, label %.lr.ph26.preheader67, label %vector.ph

.lr.ph26.preheader67:                             ; preds = %middle.block, %.lr.ph26.preheader
  %indvars.iv35.ph = phi i64 [ 0, %.lr.ph26.preheader ], [ %n.vec, %middle.block ]
  %.01723.ph = phi i32 [ %.pre, %.lr.ph26.preheader ], [ %15, %middle.block ]
  br label %.lr.ph26

vector.ph:                                        ; preds = %.lr.ph26.preheader
  %n.vec = and i64 %wide.trip.count, 2147483640
  %minmax.ident.splatinsert = insertelement <4 x i32> poison, i32 %.pre, i64 0
  %minmax.ident.splat = shufflevector <4 x i32> %minmax.ident.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ %minmax.ident.splat, %vector.ph ], [ %12, %vector.body ]
  %vec.phi47 = phi <4 x i32> [ %minmax.ident.splat, %vector.ph ], [ %13, %vector.body ]
  %10 = getelementptr inbounds i32, ptr %6, i64 %index
  %11 = getelementptr inbounds i8, ptr %10, i64 16
  %wide.load = load <4 x i32>, ptr %10, align 16, !tbaa !5
  %wide.load48 = load <4 x i32>, ptr %11, align 16, !tbaa !5
  %12 = call <4 x i32> @llvm.smax.v4i32(<4 x i32> %wide.load, <4 x i32> %vec.phi)
  %13 = call <4 x i32> @llvm.smax.v4i32(<4 x i32> %wide.load48, <4 x i32> %vec.phi47)
  %index.next = add nuw i64 %index, 8
  %14 = icmp eq i64 %index.next, %n.vec
  br i1 %14, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %rdx.minmax = call <4 x i32> @llvm.smax.v4i32(<4 x i32> %12, <4 x i32> %13)
  %15 = call i32 @llvm.vector.reduce.smax.v4i32(<4 x i32> %rdx.minmax)
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %.preheader, label %.lr.ph26.preheader67

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %16 = getelementptr inbounds i32, ptr %6, i64 %indvars.iv
  %17 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %16)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %18 = load i32, ptr %1, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = icmp slt i64 %indvars.iv.next, %19
  br i1 %20, label %.lr.ph, label %._crit_edge, !llvm.loop !13

.preheader:                                       ; preds = %.lr.ph26, %middle.block
  %spec.select.lcssa = phi i32 [ %15, %middle.block ], [ %spec.select, %.lr.ph26 ]
  br i1 %9, label %.lr.ph30.preheader, label %._crit_edge31

.lr.ph30.preheader:                               ; preds = %.preheader
  %wide.trip.count41 = zext nneg i32 %18 to i64
  %min.iters.check51 = icmp ult i32 %18, 8
  br i1 %min.iters.check51, label %.lr.ph30.preheader65, label %vector.ph52

.lr.ph30.preheader65:                             ; preds = %middle.block49, %.lr.ph30.preheader
  %indvars.iv38.ph = phi i64 [ 0, %.lr.ph30.preheader ], [ %n.vec54, %middle.block49 ]
  %.01828.ph = phi i32 [ 0, %.lr.ph30.preheader ], [ %30, %middle.block49 ]
  br label %.lr.ph30

vector.ph52:                                      ; preds = %.lr.ph30.preheader
  %n.vec54 = and i64 %wide.trip.count, 2147483640
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %spec.select.lcssa, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  br label %vector.body56

vector.body56:                                    ; preds = %vector.body56, %vector.ph52
  %index57 = phi i64 [ 0, %vector.ph52 ], [ %index.next62, %vector.body56 ]
  %vec.phi58 = phi <4 x i32> [ zeroinitializer, %vector.ph52 ], [ %27, %vector.body56 ]
  %vec.phi59 = phi <4 x i32> [ zeroinitializer, %vector.ph52 ], [ %28, %vector.body56 ]
  %21 = getelementptr inbounds i32, ptr %6, i64 %index57
  %22 = getelementptr inbounds i8, ptr %21, i64 16
  %wide.load60 = load <4 x i32>, ptr %21, align 16, !tbaa !5
  %wide.load61 = load <4 x i32>, ptr %22, align 16, !tbaa !5
  %23 = icmp eq <4 x i32> %wide.load60, %broadcast.splat
  %24 = icmp eq <4 x i32> %wide.load61, %broadcast.splat
  %25 = zext <4 x i1> %23 to <4 x i32>
  %26 = zext <4 x i1> %24 to <4 x i32>
  %27 = add <4 x i32> %vec.phi58, %25
  %28 = add <4 x i32> %vec.phi59, %26
  %index.next62 = add nuw i64 %index57, 8
  %29 = icmp eq i64 %index.next62, %n.vec54
  br i1 %29, label %middle.block49, label %vector.body56, !llvm.loop !14

middle.block49:                                   ; preds = %vector.body56
  %bin.rdx = add <4 x i32> %28, %27
  %30 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n63 = icmp eq i64 %n.vec54, %wide.trip.count
  br i1 %cmp.n63, label %._crit_edge31, label %.lr.ph30.preheader65

.lr.ph26:                                         ; preds = %.lr.ph26.preheader67, %.lr.ph26
  %indvars.iv35 = phi i64 [ %indvars.iv.next36, %.lr.ph26 ], [ %indvars.iv35.ph, %.lr.ph26.preheader67 ]
  %.01723 = phi i32 [ %spec.select, %.lr.ph26 ], [ %.01723.ph, %.lr.ph26.preheader67 ]
  %31 = getelementptr inbounds i32, ptr %6, i64 %indvars.iv35
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %spec.select = call i32 @llvm.smax.i32(i32 %32, i32 %.01723)
  %indvars.iv.next36 = add nuw nsw i64 %indvars.iv35, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next36, %wide.trip.count
  br i1 %exitcond.not, label %.preheader, label %.lr.ph26, !llvm.loop !15

._crit_edge31:                                    ; preds = %.lr.ph30, %middle.block49, %0, %._crit_edge, %.preheader
  %.018.lcssa = phi i32 [ 0, %.preheader ], [ 0, %._crit_edge ], [ 0, %0 ], [ %30, %middle.block49 ], [ %spec.select21, %.lr.ph30 ]
  %33 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %.018.lcssa)
  call void @llvm.stackrestore.p0(ptr %5)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #5
  ret i32 0

.lr.ph30:                                         ; preds = %.lr.ph30.preheader65, %.lr.ph30
  %indvars.iv38 = phi i64 [ %indvars.iv.next39, %.lr.ph30 ], [ %indvars.iv38.ph, %.lr.ph30.preheader65 ]
  %.01828 = phi i32 [ %spec.select21, %.lr.ph30 ], [ %.01828.ph, %.lr.ph30.preheader65 ]
  %34 = getelementptr inbounds i32, ptr %6, i64 %indvars.iv38
  %35 = load i32, ptr %34, align 4, !tbaa !5
  %36 = icmp eq i32 %35, %spec.select.lcssa
  %37 = zext i1 %36 to i32
  %spec.select21 = add nuw nsw i32 %.01828, %37
  %indvars.iv.next39 = add nuw nsw i64 %indvars.iv38, 1
  %exitcond42.not = icmp eq i64 %indvars.iv.next39, %wide.trip.count41
  br i1 %exitcond42.not, label %._crit_edge31, label %.lr.ph30, !llvm.loop !16
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.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: mustprogress nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #3

; 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 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #3

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <4 x i32> @llvm.smax.v4i32(<4 x i32>, <4 x i32>) #4

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.smax.v4i32(<4 x i32>) #4

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #4

attributes #0 = { nofree 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 = { mustprogress nocallback nofree nosync nounwind willreturn }
attributes #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10, !11, !12}
!15 = distinct !{!15, !10, !12, !11}
!16 = distinct !{!16, !10, !12, !11}


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