; LLVM IR from chirag-singhal/Coding-Interview-101 :: Miscellaneous/Codechef/declong (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 [4 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [5 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i64 @min(i64 noundef, i64 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca [26 x i32], align 16
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #5
  %13 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %2)
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  store i64 0, ptr %3, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %71, %0
  %15 = load i64, ptr %3, align 8, !tbaa !5
  %16 = load i64, ptr %2, align 8, !tbaa !5
  %17 = icmp slt i64 %15, %16
  br i1 %17, label %19, label %18

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

19:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %20 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %5)
  %21 = load i64, ptr %5, align 8, !tbaa !5
  %22 = add nsw i64 %21, 1
  %23 = call ptr @llvm.stacksave.p0()
  store ptr %23, ptr %6, align 8
  %24 = alloca i8, i64 %22, align 16
  store i64 %22, ptr %7, align 8
  %25 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %24)
  call void @llvm.lifetime.start.p0(i64 104, ptr %8) #5
  call void @llvm.memset.p0.i64(ptr align 16 %8, i8 0, i64 104, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  store i64 0, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %26 = load i64, ptr %5, align 8, !tbaa !5
  store i64 %26, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  store i32 1, ptr %11, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %61, %19
  %28 = load i32, ptr %11, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = load i64, ptr %5, align 8, !tbaa !5
  %31 = icmp sle i64 %29, %30
  br i1 %31, label %33, label %32

32:                                               ; preds = %27
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  br label %64

33:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  %34 = load i32, ptr %11, align 4, !tbaa !9
  %35 = sub nsw i32 %34, 1
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds i8, ptr %24, i64 %36
  %38 = load i8, ptr %37, align 1, !tbaa !11
  %39 = sext i8 %38 to i32
  %40 = sub nsw i32 %39, 97
  store i32 %40, ptr %12, align 4, !tbaa !9
  %41 = load i32, ptr %12, align 4, !tbaa !9
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [26 x i32], ptr %8, i64 0, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !9
  %45 = icmp sgt i32 %44, 0
  br i1 %45, label %46, label %56

46:                                               ; preds = %33
  %47 = load i64, ptr %10, align 8, !tbaa !5
  %48 = load i32, ptr %11, align 4, !tbaa !9
  %49 = load i32, ptr %12, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [26 x i32], ptr %8, i64 0, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !9
  %53 = sub nsw i32 %48, %52
  %54 = sext i32 %53 to i64
  %55 = call i64 @min(i64 noundef %47, i64 noundef %54)
  store i64 %55, ptr %10, align 8, !tbaa !5
  br label %56

56:                                               ; preds = %46, %33
  %57 = load i32, ptr %11, align 4, !tbaa !9
  %58 = load i32, ptr %12, align 4, !tbaa !9
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [26 x i32], ptr %8, i64 0, i64 %59
  store i32 %57, ptr %60, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  br label %61

61:                                               ; preds = %56
  %62 = load i32, ptr %11, align 4, !tbaa !9
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %11, align 4, !tbaa !9
  br label %27, !llvm.loop !12

64:                                               ; preds = %32
  %65 = load i64, ptr %5, align 8, !tbaa !5
  %66 = load i64, ptr %10, align 8, !tbaa !5
  %67 = sub nsw i64 %65, %66
  store i64 %67, ptr %9, align 8, !tbaa !5
  %68 = load i64, ptr %9, align 8, !tbaa !5
  %69 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %68)
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 104, ptr %8) #5
  %70 = load ptr, ptr %6, align 8
  call void @llvm.stackrestore.p0(ptr %70)
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  br label %71

71:                                               ; preds = %64
  %72 = load i64, ptr %3, align 8, !tbaa !5
  %73 = add nsw i64 %72, 1
  store i64 %73, ptr %3, align 8, !tbaa !5
  br label %14, !llvm.loop !14

74:                                               ; preds = %18
  store i32 0, ptr %1, align 4
  store i32 1, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #5
  %75 = load i32, ptr %1, align 4
  ret i32 %75
}

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

; 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


; 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 [4 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [5 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i64 @min(i64 noundef, i64 noundef) local_unnamed_addr #0

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

._crit_edge25:                                    ; preds = %._crit_edge, %0
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #5
  ret i32 0

.lr.ph24:                                         ; preds = %0, %._crit_edge
  %.01522 = phi i64 [ %16, %._crit_edge ], [ 0, %0 ]
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #5
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %2)
  %8 = load i64, ptr %2, align 8, !tbaa !5
  %9 = add nsw i64 %8, 1
  %10 = call ptr @llvm.stacksave.p0()
  %11 = alloca i8, i64 %9, align 16
  %12 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %11)
  call void @llvm.lifetime.start.p0(i64 104, ptr nonnull %3) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(104) %3, i8 0, i64 104, i1 false)
  %13 = load i64, ptr %2, align 8, !tbaa !5
  %invariant.gep = getelementptr i8, ptr %11, i64 -1
  %.not18 = icmp slt i64 %13, 1
  br i1 %.not18, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph._crit_edge, %.lr.ph24
  %.014.lcssa = phi i64 [ %13, %.lr.ph24 ], [ %.1, %.lr.ph._crit_edge ]
  %.lcssa = phi i64 [ %13, %.lr.ph24 ], [ %31, %.lr.ph._crit_edge ]
  %14 = sub nsw i64 %.lcssa, %.014.lcssa
  %15 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %14)
  call void @llvm.lifetime.end.p0(i64 104, ptr nonnull %3) #5
  call void @llvm.stackrestore.p0(ptr %10)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #5
  %16 = add nuw nsw i64 %.01522, 1
  %17 = load i64, ptr %1, align 8, !tbaa !5
  %18 = icmp slt i64 %16, %17
  br i1 %18, label %.lr.ph24, label %._crit_edge25, !llvm.loop !9

.lr.ph:                                           ; preds = %.lr.ph24, %.lr.ph._crit_edge
  %19 = phi i64 [ %31, %.lr.ph._crit_edge ], [ %13, %.lr.ph24 ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph._crit_edge ], [ 1, %.lr.ph24 ]
  %.01419 = phi i64 [ %.1, %.lr.ph._crit_edge ], [ %13, %.lr.ph24 ]
  %gep = getelementptr i8, ptr %invariant.gep, i64 %indvars.iv
  %20 = load i8, ptr %gep, align 1, !tbaa !11
  %21 = sext i8 %20 to i64
  %22 = add nsw i64 %21, -97
  %23 = getelementptr inbounds [26 x i32], ptr %3, i64 0, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !12
  %25 = icmp sgt i32 %24, 0
  %26 = trunc nuw nsw i64 %indvars.iv to i32
  br i1 %25, label %27, label %.lr.ph._crit_edge

27:                                               ; preds = %.lr.ph
  %28 = sub nsw i32 %26, %24
  %29 = sext i32 %28 to i64
  %30 = call i64 @min(i64 noundef %.01419, i64 noundef %29)
  %.pre = load i64, ptr %2, align 8, !tbaa !5
  br label %.lr.ph._crit_edge

.lr.ph._crit_edge:                                ; preds = %.lr.ph, %27
  %31 = phi i64 [ %.pre, %27 ], [ %19, %.lr.ph ]
  %.1 = phi i64 [ %30, %27 ], [ %.01419, %.lr.ph ]
  store i32 %26, ptr %23, align 4, !tbaa !12
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %.not.not = icmp sgt i64 %31, %indvars.iv
  br i1 %.not.not, label %.lr.ph, label %._crit_edge, !llvm.loop !14
}

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

; 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

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


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