; LLVM IR from Shah-Shishir/Codeforces-Solutions :: 320A. Magic Numbers (main)

; 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 dso_local global [12 x i8], align 1
@len = external dso_local global i32, align 4
@i = external dso_local global i32, align 4
@k = 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 [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 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
  store i32 0, ptr %1, align 4
  br label %2

2:                                                ; preds = %76, %0
  %3 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @str)
  %4 = icmp ne i32 %3, -1
  br i1 %4, label %5, label %78

5:                                                ; preds = %2
  %6 = call i64 @strlen(ptr noundef @str) #3
  %7 = trunc i64 %6 to i32
  store i32 %7, ptr @len, align 4, !tbaa !5
  store i32 1, ptr @k, align 4, !tbaa !5
  store i32 0, ptr @i, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %68, %5
  %9 = load i32, ptr @i, align 4, !tbaa !5
  %10 = load i32, ptr @len, align 4, !tbaa !5
  %11 = icmp slt i32 %9, %10
  br i1 %11, label %12, label %71

12:                                               ; preds = %8
  %13 = load i32, ptr @i, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [12 x i8], ptr @str, i64 0, i64 %14
  %16 = load i8, ptr %15, align 1, !tbaa !9
  %17 = sext i8 %16 to i32
  %18 = icmp eq i32 %17, 49
  br i1 %18, label %19, label %38

19:                                               ; preds = %12
  %20 = load i32, ptr @i, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [12 x i8], ptr @str, i64 0, i64 %22
  %24 = load i8, ptr %23, align 1, !tbaa !9
  %25 = sext i8 %24 to i32
  %26 = icmp eq i32 %25, 52
  br i1 %26, label %27, label %38

27:                                               ; preds = %19
  %28 = load i32, ptr @i, align 4, !tbaa !5
  %29 = add nsw i32 %28, 2
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [12 x i8], ptr @str, i64 0, i64 %30
  %32 = load i8, ptr %31, align 1, !tbaa !9
  %33 = sext i8 %32 to i32
  %34 = icmp eq i32 %33, 52
  br i1 %34, label %35, label %38

35:                                               ; preds = %27
  %36 = load i32, ptr @i, align 4, !tbaa !5
  %37 = add nsw i32 %36, 2
  store i32 %37, ptr @i, align 4, !tbaa !5
  br label %67

38:                                               ; preds = %27, %19, %12
  %39 = load i32, ptr @i, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [12 x i8], ptr @str, i64 0, i64 %40
  %42 = load i8, ptr %41, align 1, !tbaa !9
  %43 = sext i8 %42 to i32
  %44 = icmp eq i32 %43, 49
  br i1 %44, label %45, label %56

45:                                               ; preds = %38
  %46 = load i32, ptr @i, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [12 x i8], ptr @str, i64 0, i64 %48
  %50 = load i8, ptr %49, align 1, !tbaa !9
  %51 = sext i8 %50 to i32
  %52 = icmp eq i32 %51, 52
  br i1 %52, label %53, label %56

53:                                               ; preds = %45
  %54 = load i32, ptr @i, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr @i, align 4, !tbaa !5
  br label %66

56:                                               ; preds = %45, %38
  %57 = load i32, ptr @i, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [12 x i8], ptr @str, i64 0, i64 %58
  %60 = load i8, ptr %59, align 1, !tbaa !9
  %61 = sext i8 %60 to i32
  %62 = icmp eq i32 %61, 49
  br i1 %62, label %63, label %64

63:                                               ; preds = %56
  br label %68

64:                                               ; preds = %56
  store i32 0, ptr @k, align 4, !tbaa !5
  %65 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  br label %71

66:                                               ; preds = %53
  br label %67

67:                                               ; preds = %66, %35
  br label %68

68:                                               ; preds = %67, %63
  %69 = load i32, ptr @i, align 4, !tbaa !5
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr @i, align 4, !tbaa !5
  br label %8, !llvm.loop !10

71:                                               ; preds = %64, %8
  %72 = load i32, ptr @k, align 4, !tbaa !5
  %73 = icmp ne i32 %72, 0
  br i1 %73, label %74, label %76

74:                                               ; preds = %71
  %75 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  br label %76

76:                                               ; preds = %74, %71
  %77 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  br label %2, !llvm.loop !12

78:                                               ; preds = %2
  ret i32 0
}

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

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #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 = { nounwind willreturn memory(read) "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 willreturn memory(read) }

!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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


; 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 dso_local global [12 x i8], align 1
@len = external dso_local local_unnamed_addr global i32, align 4
@i = external dso_local local_unnamed_addr global i32, align 4
@k = 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 [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 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 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @str)
  %.not6 = icmp eq i32 %1, -1
  br i1 %.not6, label %._crit_edge, label %.lr.ph7

.lr.ph7:                                          ; preds = %0, %27
  %2 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) @str) #3
  %3 = trunc i64 %2 to i32
  store i32 %3, ptr @len, align 4, !tbaa !5
  store i32 1, ptr @k, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %.thread

.lr.ph:                                           ; preds = %.lr.ph7, %.thread2
  %storemerge5 = phi i32 [ %21, %.thread2 ], [ 0, %.lr.ph7 ]
  %5 = sext i32 %storemerge5 to i64
  %6 = getelementptr inbounds [12 x i8], ptr @str, i64 0, i64 %5
  %7 = load i8, ptr %6, align 1, !tbaa !9
  %8 = icmp eq i8 %7, 49
  br i1 %8, label %9, label %23

9:                                                ; preds = %.lr.ph
  %10 = add nsw i32 %storemerge5, 1
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [12 x i8], ptr @str, i64 0, i64 %11
  %13 = load i8, ptr %12, align 1, !tbaa !9
  %14 = icmp eq i8 %13, 52
  br i1 %14, label %15, label %.thread2

15:                                               ; preds = %9
  %16 = add nsw i32 %storemerge5, 2
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [12 x i8], ptr @str, i64 0, i64 %17
  %19 = load i8, ptr %18, align 1, !tbaa !9
  %20 = icmp eq i8 %19, 52
  %spec.select = select i1 %20, i32 %16, i32 %10
  br label %.thread2

.thread2:                                         ; preds = %15, %9
  %storemerge3 = phi i32 [ %storemerge5, %9 ], [ %spec.select, %15 ]
  %21 = add nsw i32 %storemerge3, 1
  %22 = icmp slt i32 %21, %3
  br i1 %22, label %.lr.ph, label %.thread, !llvm.loop !10

.thread:                                          ; preds = %.thread2, %.lr.ph7
  %storemerge.lcssa = phi i32 [ 0, %.lr.ph7 ], [ %21, %.thread2 ]
  store i32 %storemerge.lcssa, ptr @i, align 4, !tbaa !5
  br label %25

23:                                               ; preds = %.lr.ph
  store i32 %storemerge5, ptr @i, align 4, !tbaa !5
  store i32 0, ptr @k, align 4, !tbaa !5
  %24 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
  %.pre = load i32, ptr @k, align 4, !tbaa !5
  %.not1 = icmp eq i32 %.pre, 0
  br i1 %.not1, label %27, label %25

25:                                               ; preds = %.thread, %23
  %26 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  br label %27

27:                                               ; preds = %25, %23
  %28 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  %29 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @str)
  %.not = icmp eq i32 %29, -1
  br i1 %.not, label %._crit_edge, label %.lr.ph7, !llvm.loop !12

._crit_edge:                                      ; preds = %27, %0
  ret i32 0
}

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #2

; 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" }
attributes #2 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 willreturn memory(read) }

!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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


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