; LLVM IR from VastoLorde95/Competitive-Programming :: Hackerrank/Week of Code/Week 15/Problem B (B)

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

@n = external dso_local global i32, align 4
@l = external dso_local global i32, align 4
@h = external dso_local global i32, align 4
@d = external dso_local global [300001 x i32], align 16
@s = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 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
  store i32 0, ptr %1, align 4
  %5 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %28, %0
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = load i32, ptr @n, align 4, !tbaa !5
  %9 = icmp slt i32 %7, %8
  br i1 %9, label %11, label %10

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

11:                                               ; preds = %6
  %12 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef @l, ptr noundef @h)
  %13 = load i32, ptr @l, align 4, !tbaa !5
  %14 = add nsw i32 %13, 1
  store i32 %14, ptr @l, align 4, !tbaa !5
  %15 = load i32, ptr @h, align 4, !tbaa !5
  %16 = add nsw i32 %15, 1
  store i32 %16, ptr @h, align 4, !tbaa !5
  %17 = load i32, ptr @l, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [300001 x i32], ptr @d, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %19, align 4, !tbaa !5
  %22 = load i32, ptr @h, align 4, !tbaa !5
  %23 = add nsw i32 %22, 1
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [300001 x i32], ptr @d, i64 0, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = add nsw i32 %26, -1
  store i32 %27, ptr %25, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %11
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %2, align 4, !tbaa !5
  br label %6, !llvm.loop !9

31:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 1, ptr %4, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %50, %31
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = load i32, ptr @n, align 4, !tbaa !5
  %35 = icmp sle i32 %33, %34
  br i1 %35, label %37, label %36

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

37:                                               ; preds = %32
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [300001 x i32], ptr @d, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = load i32, ptr @s, align 4, !tbaa !5
  %43 = add nsw i32 %42, %41
  store i32 %43, ptr @s, align 4, !tbaa !5
  %44 = load i32, ptr @s, align 4, !tbaa !5
  %45 = load i32, ptr %4, align 4, !tbaa !5
  %46 = icmp sge i32 %44, %45
  br i1 %46, label %47, label %49

47:                                               ; preds = %37
  %48 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %48, ptr %3, align 4, !tbaa !5
  br label %49

49:                                               ; preds = %47, %37
  br label %50

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

53:                                               ; preds = %36
  %54 = load i32, ptr %3, align 4, !tbaa !5
  %55 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %54)
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret i32 0
}

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

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

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

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

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 = { "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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !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"

@n = external dso_local global i32, align 4
@l = external dso_local global i32, align 4
@h = external dso_local global i32, align 4
@d = external dso_local local_unnamed_addr global [300001 x i32], align 16
@s = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph, label %.preheader.thread

.preheader:                                       ; preds = %.lr.ph
  %.not12 = icmp slt i32 %21, 1
  br i1 %.not12, label %.preheader.thread, label %.lr.ph15.preheader

.lr.ph15.preheader:                               ; preds = %.preheader
  %s.promoted = load i32, ptr @s, align 4, !tbaa !5
  %4 = zext nneg i32 %21 to i64
  %xtraiter = and i64 %4, 3
  %5 = icmp ult i32 %21, 4
  br i1 %5, label %._crit_edge.unr-lcssa, label %.lr.ph15.preheader.new

.lr.ph15.preheader.new:                           ; preds = %.lr.ph15.preheader
  %unroll_iter = and i64 %4, 2147483644
  br label %.lr.ph15

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.0911 = phi i32 [ %20, %.lr.ph ], [ 0, %0 ]
  %6 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @l, ptr noundef nonnull @h)
  %7 = load i32, ptr @l, align 4, !tbaa !5
  %8 = add nsw i32 %7, 1
  store i32 %8, ptr @l, align 4, !tbaa !5
  %9 = load i32, ptr @h, align 4, !tbaa !5
  %10 = add nsw i32 %9, 1
  store i32 %10, ptr @h, align 4, !tbaa !5
  %11 = sext i32 %8 to i64
  %12 = getelementptr inbounds [300001 x i32], ptr @d, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = add nsw i32 %13, 1
  store i32 %14, ptr %12, align 4, !tbaa !5
  %15 = add nsw i32 %9, 2
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [300001 x i32], ptr @d, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = add nsw i32 %18, -1
  store i32 %19, ptr %17, align 4, !tbaa !5
  %20 = add nuw nsw i32 %.0911, 1
  %21 = load i32, ptr @n, align 4, !tbaa !5
  %22 = icmp slt i32 %20, %21
  br i1 %22, label %.lr.ph, label %.preheader, !llvm.loop !9

._crit_edge.unr-lcssa:                            ; preds = %.lr.ph15, %.lr.ph15.preheader
  %.lcssa.ph = phi i32 [ poison, %.lr.ph15.preheader ], [ %48, %.lr.ph15 ]
  %spec.select.lcssa.ph = phi i32 [ poison, %.lr.ph15.preheader ], [ %spec.select.3, %.lr.ph15 ]
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph15.preheader ], [ %indvars.iv.next.3, %.lr.ph15 ]
  %.0813.unr = phi i32 [ 0, %.lr.ph15.preheader ], [ %spec.select.3, %.lr.ph15 ]
  %.unr = phi i32 [ %s.promoted, %.lr.ph15.preheader ], [ %48, %.lr.ph15 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph15.epil

.lr.ph15.epil:                                    ; preds = %._crit_edge.unr-lcssa, %.lr.ph15.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph15.epil ], [ %indvars.iv.unr, %._crit_edge.unr-lcssa ]
  %.0813.epil = phi i32 [ %spec.select.epil, %.lr.ph15.epil ], [ %.0813.unr, %._crit_edge.unr-lcssa ]
  %23 = phi i32 [ %26, %.lr.ph15.epil ], [ %.unr, %._crit_edge.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph15.epil ], [ 0, %._crit_edge.unr-lcssa ]
  %24 = getelementptr inbounds [300001 x i32], ptr @d, i64 0, i64 %indvars.iv.epil
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = add nsw i32 %23, %25
  %27 = sext i32 %26 to i64
  %.not10.epil = icmp sgt i64 %indvars.iv.epil, %27
  %28 = trunc nuw nsw i64 %indvars.iv.epil to i32
  %spec.select.epil = select i1 %.not10.epil, i32 %.0813.epil, i32 %28
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph15.epil, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph15.epil, %._crit_edge.unr-lcssa
  %.lcssa = phi i32 [ %.lcssa.ph, %._crit_edge.unr-lcssa ], [ %26, %.lr.ph15.epil ]
  %spec.select.lcssa = phi i32 [ %spec.select.lcssa.ph, %._crit_edge.unr-lcssa ], [ %spec.select.epil, %.lr.ph15.epil ]
  store i32 %.lcssa, ptr @s, align 4, !tbaa !5
  br label %.preheader.thread

.preheader.thread:                                ; preds = %0, %._crit_edge, %.preheader
  %.08.lcssa = phi i32 [ %spec.select.lcssa, %._crit_edge ], [ 0, %.preheader ], [ 0, %0 ]
  %29 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %.08.lcssa)
  ret i32 0

.lr.ph15:                                         ; preds = %.lr.ph15, %.lr.ph15.preheader.new
  %indvars.iv = phi i64 [ 1, %.lr.ph15.preheader.new ], [ %indvars.iv.next.3, %.lr.ph15 ]
  %.0813 = phi i32 [ 0, %.lr.ph15.preheader.new ], [ %spec.select.3, %.lr.ph15 ]
  %30 = phi i32 [ %s.promoted, %.lr.ph15.preheader.new ], [ %48, %.lr.ph15 ]
  %niter = phi i64 [ 0, %.lr.ph15.preheader.new ], [ %niter.next.3, %.lr.ph15 ]
  %31 = getelementptr inbounds [300001 x i32], ptr @d, i64 0, i64 %indvars.iv
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = add nsw i32 %30, %32
  %34 = sext i32 %33 to i64
  %.not10 = icmp sgt i64 %indvars.iv, %34
  %35 = trunc nuw nsw i64 %indvars.iv to i32
  %spec.select = select i1 %.not10, i32 %.0813, i32 %35
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %36 = getelementptr inbounds [300001 x i32], ptr @d, i64 0, i64 %indvars.iv.next
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = add nsw i32 %33, %37
  %39 = sext i32 %38 to i64
  %.not10.1.not = icmp slt i64 %indvars.iv, %39
  %40 = trunc nuw nsw i64 %indvars.iv.next to i32
  %spec.select.1 = select i1 %.not10.1.not, i32 %40, i32 %spec.select
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %41 = getelementptr inbounds [300001 x i32], ptr @d, i64 0, i64 %indvars.iv.next.1
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = add nsw i32 %38, %42
  %44 = sext i32 %43 to i64
  %.not10.2 = icmp sgt i64 %indvars.iv.next.1, %44
  %45 = trunc nuw nsw i64 %indvars.iv.next.1 to i32
  %spec.select.2 = select i1 %.not10.2, i32 %spec.select.1, i32 %45
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %46 = getelementptr inbounds [300001 x i32], ptr @d, i64 0, i64 %indvars.iv.next.2
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = add nsw i32 %43, %47
  %49 = sext i32 %48 to i64
  %.not10.3 = icmp sgt i64 %indvars.iv.next.2, %49
  %50 = trunc nuw nsw i64 %indvars.iv.next.2 to i32
  %spec.select.3 = select i1 %.not10.3, i32 %spec.select.2, i32 %50
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.unr-lcssa, label %.lr.ph15, !llvm.loop !13
}

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

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

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 = { 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" }

!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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.unroll.disable"}
!13 = distinct !{!13, !10}


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