; LLVM IR from reyadussalahin/problem-solving :: math/combinatorics/loj/F1105 (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"

@stck = external dso_local global [128 x i32], align 16
@str = external dso_local global [128 x i8], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 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
  %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
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  %14 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %2)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %15

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

19:                                               ; preds = %15
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %100

20:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %21 = 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 !5
  br label %22

22:                                               ; preds = %38, %20
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = icmp sgt i32 %23, 0
  br i1 %24, label %25, label %48

25:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 1, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 2, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 1, ptr %9, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %30, %25
  %27 = load i32, ptr %8, align 4, !tbaa !5
  %28 = load i32, ptr %5, align 4, !tbaa !5
  %29 = icmp sle i32 %27, %28
  br i1 %29, label %30, label %38

30:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = load i32, ptr %8, align 4, !tbaa !5
  %33 = add nsw i32 %31, %32
  store i32 %33, ptr %10, align 4, !tbaa !5
  %34 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %34, ptr %7, align 4, !tbaa !5
  %35 = load i32, ptr %10, align 4, !tbaa !5
  store i32 %35, ptr %8, align 4, !tbaa !5
  %36 = load i32, ptr %9, align 4, !tbaa !5
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %26, !llvm.loop !9

38:                                               ; preds = %26
  %39 = load i32, ptr %9, align 4, !tbaa !5
  %40 = sub nsw i32 %39, 1
  %41 = load i32, ptr %6, align 4, !tbaa !5
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %6, align 4, !tbaa !5
  %43 = sext i32 %41 to i64
  %44 = getelementptr inbounds [128 x i32], ptr @stck, i64 0, i64 %43
  store i32 %40, ptr %44, align 4, !tbaa !5
  %45 = load i32, ptr %5, align 4, !tbaa !5
  %46 = load i32, ptr %7, align 4, !tbaa !5
  %47 = sub nsw i32 %45, %46
  store i32 %47, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %22, !llvm.loop !11

48:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %49 = load i32, ptr @stck, align 16, !tbaa !5
  store i32 %49, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  store i32 0, ptr %12, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %73, %48
  %51 = load i32, ptr %6, align 4, !tbaa !5
  %52 = icmp sgt i32 %51, 0
  br i1 %52, label %54, label %53

53:                                               ; preds = %50
  store i32 9, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %76

54:                                               ; preds = %50
  %55 = load i32, ptr %12, align 4, !tbaa !5
  %56 = load i32, ptr %6, align 4, !tbaa !5
  %57 = sub nsw i32 %56, 1
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [128 x i32], ptr @stck, i64 0, i64 %58
  %60 = load i32, ptr %59, align 4, !tbaa !5
  %61 = icmp eq i32 %55, %60
  br i1 %61, label %62, label %68

62:                                               ; preds = %54
  %63 = load i32, ptr %12, align 4, !tbaa !5
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [128 x i8], ptr @str, i64 0, i64 %64
  store i8 49, ptr %65, align 1, !tbaa !12
  %66 = load i32, ptr %6, align 4, !tbaa !5
  %67 = add nsw i32 %66, -1
  store i32 %67, ptr %6, align 4, !tbaa !5
  br label %72

68:                                               ; preds = %54
  %69 = load i32, ptr %12, align 4, !tbaa !5
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [128 x i8], ptr @str, i64 0, i64 %70
  store i8 48, ptr %71, align 1, !tbaa !12
  br label %72

72:                                               ; preds = %68, %62
  br label %73

73:                                               ; preds = %72
  %74 = load i32, ptr %12, align 4, !tbaa !5
  %75 = add nsw i32 %74, 1
  store i32 %75, ptr %12, align 4, !tbaa !5
  br label %50, !llvm.loop !13

76:                                               ; preds = %53
  %77 = load i32, ptr %3, align 4, !tbaa !5
  %78 = add nsw i32 %77, 1
  %79 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %78)
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %80 = load i32, ptr %11, align 4, !tbaa !5
  store i32 %80, ptr %13, align 4, !tbaa !5
  br label %81

81:                                               ; preds = %92, %76
  %82 = load i32, ptr %13, align 4, !tbaa !5
  %83 = icmp sge i32 %82, 0
  br i1 %83, label %85, label %84

84:                                               ; preds = %81
  store i32 12, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  br label %95

85:                                               ; preds = %81
  %86 = load i32, ptr %13, align 4, !tbaa !5
  %87 = sext i32 %86 to i64
  %88 = getelementptr inbounds [128 x i8], ptr @str, i64 0, i64 %87
  %89 = load i8, ptr %88, align 1, !tbaa !12
  %90 = sext i8 %89 to i32
  %91 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %90)
  br label %92

92:                                               ; preds = %85
  %93 = load i32, ptr %13, align 4, !tbaa !5
  %94 = add nsw i32 %93, -1
  store i32 %94, ptr %13, align 4, !tbaa !5
  br label %81, !llvm.loop !14

95:                                               ; preds = %84
  %96 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %97

97:                                               ; preds = %95
  %98 = load i32, ptr %3, align 4, !tbaa !5
  %99 = add nsw i32 %98, 1
  store i32 %99, ptr %3, align 4, !tbaa !5
  br label %15, !llvm.loop !15

100:                                              ; preds = %19
  store i32 0, ptr %1, align 4
  store i32 1, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  %101 = load i32, ptr %1, align 4
  ret i32 %101
}

; 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 memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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 = !{!"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}
!12 = !{!7, !7, i64 0}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !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"

@stck = external dso_local local_unnamed_addr global [128 x i32], align 16
@str = external dso_local local_unnamed_addr global [128 x i8], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #3
  %3 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %4 = load i32, ptr %1, align 4, !tbaa !5
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph49, label %._crit_edge50

._crit_edge50:                                    ; preds = %._crit_edge46, %0
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #3
  ret i32 0

.lr.ph49:                                         ; preds = %0, %._crit_edge46
  %.02547 = phi i32 [ %20, %._crit_edge46 ], [ 0, %0 ]
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #3
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %2)
  %.pr = load i32, ptr %2, align 4, !tbaa !5
  %7 = icmp sgt i32 %.pr, 0
  br i1 %7, label %.preheader, label %.thread

.thread:                                          ; preds = %.lr.ph49
  %8 = load i32, ptr @stck, align 16, !tbaa !5
  br label %._crit_edge42

.preheader:                                       ; preds = %.lr.ph49, %._crit_edge
  %indvars.iv = phi i64 [ %indvars.iv.next, %._crit_edge ], [ 0, %.lr.ph49 ]
  %9 = phi i32 [ %13, %._crit_edge ], [ %.pr, %.lr.ph49 ]
  %.not30 = icmp ult i32 %9, 2
  br i1 %.not30, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.02633 = phi i32 [ %11, %.lr.ph ], [ 1, %.preheader ]
  %.02732 = phi i32 [ %10, %.lr.ph ], [ 2, %.preheader ]
  %.02831 = phi i32 [ %.02732, %.lr.ph ], [ 1, %.preheader ]
  %10 = add nsw i32 %.02732, %.02831
  %11 = add nuw nsw i32 %.02633, 1
  %.not = icmp sgt i32 %10, %9
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %.028.lcssa = phi i32 [ 1, %.preheader ], [ %.02732, %.lr.ph ]
  %.026.lcssa = phi i32 [ 0, %.preheader ], [ %.02633, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %12 = getelementptr inbounds [128 x i32], ptr @stck, i64 0, i64 %indvars.iv
  store i32 %.026.lcssa, ptr %12, align 4, !tbaa !5
  %13 = sub nsw i32 %9, %.028.lcssa
  %14 = icmp sgt i32 %13, 0
  br i1 %14, label %.preheader, label %15, !llvm.loop !11

15:                                               ; preds = %._crit_edge
  %16 = trunc nuw i64 %indvars.iv.next to i32
  store i32 %13, ptr %2, align 4, !tbaa !5
  %17 = load i32, ptr @stck, align 16, !tbaa !5
  %18 = icmp sgt i32 %16, 0
  br i1 %18, label %.lr.ph41, label %._crit_edge42

._crit_edge42:                                    ; preds = %.lr.ph41, %.thread, %15
  %19 = phi i32 [ %8, %.thread ], [ %17, %15 ], [ %17, %.lr.ph41 ]
  %20 = add nuw nsw i32 %.02547, 1
  %21 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %20)
  %22 = icmp sgt i32 %19, -1
  br i1 %22, label %.lr.ph45.preheader, label %._crit_edge46

.lr.ph45.preheader:                               ; preds = %._crit_edge42
  %23 = zext nneg i32 %19 to i64
  br label %.lr.ph45

.lr.ph41:                                         ; preds = %15, %.lr.ph41
  %indvars.iv53 = phi i64 [ %indvars.iv.next54, %.lr.ph41 ], [ 0, %15 ]
  %.138 = phi i32 [ %spec.select60, %.lr.ph41 ], [ %16, %15 ]
  %24 = add nsw i32 %.138, -1
  %25 = zext nneg i32 %24 to i64
  %26 = getelementptr inbounds [128 x i32], ptr @stck, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = zext i32 %27 to i64
  %29 = icmp eq i64 %indvars.iv53, %28
  %spec.select = select i1 %29, i8 49, i8 48
  %spec.select60 = select i1 %29, i32 %24, i32 %.138
  %30 = getelementptr inbounds [128 x i8], ptr @str, i64 0, i64 %indvars.iv53
  store i8 %spec.select, ptr %30, align 1
  %indvars.iv.next54 = add nuw nsw i64 %indvars.iv53, 1
  %31 = icmp sgt i32 %spec.select60, 0
  br i1 %31, label %.lr.ph41, label %._crit_edge42, !llvm.loop !12

._crit_edge46:                                    ; preds = %.lr.ph45, %._crit_edge42
  %32 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #3
  %33 = load i32, ptr %1, align 4, !tbaa !5
  %34 = icmp slt i32 %20, %33
  br i1 %34, label %.lr.ph49, label %._crit_edge50, !llvm.loop !13

.lr.ph45:                                         ; preds = %.lr.ph45.preheader, %.lr.ph45
  %indvars.iv56 = phi i64 [ %23, %.lr.ph45.preheader ], [ %indvars.iv.next57, %.lr.ph45 ]
  %35 = getelementptr inbounds [128 x i8], ptr @str, i64 0, i64 %indvars.iv56
  %36 = load i8, ptr %35, align 1, !tbaa !14
  %37 = sext i8 %36 to i32
  %38 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %37)
  %indvars.iv.next57 = add nsw i64 %indvars.iv56, -1
  %.not61 = icmp eq i64 %indvars.iv56, 0
  br i1 %.not61, label %._crit_edge46, label %.lr.ph45, !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: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; 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

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 = { 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}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !10}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !10}


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