; LLVM IR from chirag-singhal/Coding-Interview-101 :: Miscellaneous/Codechef/novlong (1)

; 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 [4 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 [128 x i32], align 16
  %4 = alloca [128 x i32], align 16
  %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 i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  %12 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %2)
  call void @llvm.lifetime.start.p0(i64 512, ptr %3) #4
  call void @llvm.memset.p0.i64(ptr align 16 %3, i8 0, i64 512, i1 false)
  %13 = getelementptr inbounds [128 x i32], ptr %3, i64 0, i64 1
  store i32 0, ptr %13, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 512, ptr %4) #4
  call void @llvm.memset.p0.i64(ptr align 16 %4, i8 0, i64 512, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 2, ptr %5, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %50, %0
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = icmp slt i32 %15, 128
  br i1 %16, label %18, label %17

17:                                               ; preds = %14
  store i32 2, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %53

18:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = sub nsw i32 %19, 2
  store i32 %20, ptr %7, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %45, %18
  %22 = load i32, ptr %7, align 4, !tbaa !5
  %23 = icmp sgt i32 %22, -1
  br i1 %23, label %25, label %24

24:                                               ; preds = %21
  store i32 5, ptr %6, align 4
  br label %48

25:                                               ; preds = %21
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = sub nsw i32 %26, 1
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [128 x i32], ptr %3, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [128 x i32], ptr %3, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = icmp eq i32 %30, %34
  br i1 %35, label %36, label %44

36:                                               ; preds = %25
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = sub nsw i32 %37, 1
  %39 = load i32, ptr %7, align 4, !tbaa !5
  %40 = sub nsw i32 %38, %39
  %41 = load i32, ptr %5, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [128 x i32], ptr %3, i64 0, i64 %42
  store i32 %40, ptr %43, align 4, !tbaa !5
  store i32 5, ptr %6, align 4
  br label %48

44:                                               ; preds = %25
  br label %45

45:                                               ; preds = %44
  %46 = load i32, ptr %7, align 4, !tbaa !5
  %47 = add nsw i32 %46, -1
  store i32 %47, ptr %7, align 4, !tbaa !5
  br label %21, !llvm.loop !9

48:                                               ; preds = %36, %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %49

49:                                               ; preds = %48
  br label %50

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

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

54:                                               ; preds = %87, %53
  %55 = load i32, ptr %8, align 4, !tbaa !5
  %56 = load i32, ptr %2, align 4, !tbaa !5
  %57 = icmp slt i32 %55, %56
  br i1 %57, label %59, label %58

58:                                               ; preds = %54
  store i32 8, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %90

59:                                               ; preds = %54
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  store i32 0, ptr %10, align 4, !tbaa !5
  %60 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %9)
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  store i32 0, ptr %11, align 4, !tbaa !5
  br label %61

61:                                               ; preds = %81, %59
  %62 = load i32, ptr %11, align 4, !tbaa !5
  %63 = load i32, ptr %9, align 4, !tbaa !5
  %64 = icmp slt i32 %62, %63
  br i1 %64, label %66, label %65

65:                                               ; preds = %61
  store i32 11, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  br label %84

66:                                               ; preds = %61
  %67 = load i32, ptr %11, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [128 x i32], ptr %3, i64 0, i64 %68
  %70 = load i32, ptr %69, align 4, !tbaa !5
  %71 = load i32, ptr %9, align 4, !tbaa !5
  %72 = sub nsw i32 %71, 1
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds [128 x i32], ptr %3, i64 0, i64 %73
  %75 = load i32, ptr %74, align 4, !tbaa !5
  %76 = icmp eq i32 %70, %75
  br i1 %76, label %77, label %80

77:                                               ; preds = %66
  %78 = load i32, ptr %10, align 4, !tbaa !5
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr %10, align 4, !tbaa !5
  br label %80

80:                                               ; preds = %77, %66
  br label %81

81:                                               ; preds = %80
  %82 = load i32, ptr %11, align 4, !tbaa !5
  %83 = add nsw i32 %82, 1
  store i32 %83, ptr %11, align 4, !tbaa !5
  br label %61, !llvm.loop !12

84:                                               ; preds = %65
  %85 = load i32, ptr %10, align 4, !tbaa !5
  %86 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %85)
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %87

87:                                               ; preds = %84
  %88 = load i32, ptr %8, align 4, !tbaa !5
  %89 = add nsw i32 %88, 1
  store i32 %89, ptr %8, align 4, !tbaa !5
  br label %54, !llvm.loop !13

90:                                               ; preds = %58
  store i32 0, ptr %1, align 4
  store i32 1, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 512, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 512, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  %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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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

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 nounwind willreturn memory(argmem: write) }
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}
!13 = distinct !{!13, !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
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: nofree nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = alloca [128 x i32], align 16
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #5
  %4 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  call void @llvm.lifetime.start.p0(i64 512, ptr nonnull %2) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(512) %2, i8 0, i64 512, i1 false)
  br label %.lr.ph

.preheader:                                       ; preds = %.loopexit
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph31, label %._crit_edge32

.lr.ph:                                           ; preds = %.loopexit, %0
  %indvars.iv = phi i64 [ 2, %0 ], [ %indvars.iv.next, %.loopexit ]
  %7 = add nsw i64 %indvars.iv, -1
  %8 = getelementptr inbounds [128 x i32], ptr %2, i64 0, i64 %7
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = trunc i64 %indvars.iv to i32
  %11 = add i32 %10, -2
  br label %12

12:                                               ; preds = %.lr.ph, %21
  %.02024 = phi i32 [ %11, %.lr.ph ], [ %22, %21 ]
  %13 = zext nneg i32 %.02024 to i64
  %14 = getelementptr inbounds [128 x i32], ptr %2, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = icmp eq i32 %9, %15
  br i1 %16, label %17, label %21

17:                                               ; preds = %12
  %18 = trunc nuw nsw i64 %7 to i32
  %19 = sub nsw i32 %18, %.02024
  %20 = getelementptr inbounds [128 x i32], ptr %2, i64 0, i64 %indvars.iv
  store i32 %19, ptr %20, align 4, !tbaa !5
  br label %.loopexit

21:                                               ; preds = %12
  %22 = add nsw i32 %.02024, -1
  %23 = icmp sgt i32 %.02024, 0
  br i1 %23, label %12, label %.loopexit, !llvm.loop !9

.loopexit:                                        ; preds = %21, %17
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 128
  br i1 %exitcond.not, label %.preheader, label %.lr.ph, !llvm.loop !11

._crit_edge32:                                    ; preds = %._crit_edge, %.preheader
  call void @llvm.lifetime.end.p0(i64 512, ptr nonnull %2) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #5
  ret i32 0

.lr.ph31:                                         ; preds = %.preheader, %._crit_edge
  %.01930 = phi i32 [ %42, %._crit_edge ], [ 0, %.preheader ]
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #5
  %24 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %3)
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = icmp sgt i32 %25, 0
  br i1 %26, label %.lr.ph29, label %._crit_edge

.lr.ph29:                                         ; preds = %.lr.ph31
  %27 = add nsw i32 %25, -1
  %28 = zext nneg i32 %27 to i64
  %29 = getelementptr inbounds [128 x i32], ptr %2, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %wide.trip.count = zext nneg i32 %25 to i64
  %min.iters.check = icmp ult i32 %25, 8
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph29
  %indvars.iv35.ph = phi i64 [ 0, %.lr.ph29 ], [ %n.vec, %middle.block ]
  %.01827.ph = phi i32 [ 0, %.lr.ph29 ], [ %40, %middle.block ]
  br label %scalar.ph

vector.ph:                                        ; preds = %.lr.ph29
  %n.vec = and i64 %wide.trip.count, 2147483640
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %30, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.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> [ zeroinitializer, %vector.ph ], [ %37, %vector.body ]
  %vec.phi41 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %38, %vector.body ]
  %31 = getelementptr inbounds [128 x i32], ptr %2, i64 0, i64 %index
  %32 = getelementptr inbounds i8, ptr %31, i64 16
  %wide.load = load <4 x i32>, ptr %31, align 16, !tbaa !5
  %wide.load42 = load <4 x i32>, ptr %32, align 16, !tbaa !5
  %33 = icmp eq <4 x i32> %wide.load, %broadcast.splat
  %34 = icmp eq <4 x i32> %wide.load42, %broadcast.splat
  %35 = zext <4 x i1> %33 to <4 x i32>
  %36 = zext <4 x i1> %34 to <4 x i32>
  %37 = add <4 x i32> %vec.phi, %35
  %38 = add <4 x i32> %vec.phi41, %36
  %index.next = add nuw i64 %index, 8
  %39 = icmp eq i64 %index.next, %n.vec
  br i1 %39, label %middle.block, label %vector.body, !llvm.loop !12

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %38, %37
  %40 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %._crit_edge, label %scalar.ph.preheader

._crit_edge:                                      ; preds = %scalar.ph, %middle.block, %.lr.ph31
  %.018.lcssa = phi i32 [ 0, %.lr.ph31 ], [ %40, %middle.block ], [ %spec.select, %scalar.ph ]
  %41 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %.018.lcssa)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #5
  %42 = add nuw nsw i32 %.01930, 1
  %43 = load i32, ptr %1, align 4, !tbaa !5
  %44 = icmp slt i32 %42, %43
  br i1 %44, label %.lr.ph31, label %._crit_edge32, !llvm.loop !15

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv35 = phi i64 [ %indvars.iv.next36, %scalar.ph ], [ %indvars.iv35.ph, %scalar.ph.preheader ]
  %.01827 = phi i32 [ %spec.select, %scalar.ph ], [ %.01827.ph, %scalar.ph.preheader ]
  %45 = getelementptr inbounds [128 x i32], ptr %2, i64 0, i64 %indvars.iv35
  %46 = load i32, ptr %45, align 4, !tbaa !5
  %47 = icmp eq i32 %46, %30
  %48 = zext i1 %47 to i32
  %spec.select = add nuw nsw i32 %.01827, %48
  %indvars.iv.next36 = add nuw nsw i64 %indvars.iv35, 1
  %exitcond38.not = icmp eq i64 %indvars.iv.next36, %wide.trip.count
  br i1 %exitcond38.not, label %._crit_edge, label %scalar.ph, !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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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: 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 nounwind willreturn memory(argmem: write) }
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}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10, !13, !14}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !10}
!16 = distinct !{!16, !10, !14, !13}


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