; LLVM IR from Shah-Shishir/Codeforces-Solutions :: 20A. BerOS file system (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 [105 x i8], align 16
@.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 [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
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  br label %6

6:                                                ; preds = %98, %0
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @str)
  %8 = icmp ne i32 %7, -1
  br i1 %8, label %9, label %100

9:                                                ; preds = %6
  %10 = call i64 @strlen(ptr noundef @str) #5
  %11 = trunc i64 %10 to i32
  store i32 %11, ptr %2, align 4, !tbaa !5
  store i32 0, ptr %4, align 4, !tbaa !5
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %43, %9
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %16, label %46

16:                                               ; preds = %12
  %17 = load i32, ptr %3, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [105 x i8], ptr @str, i64 0, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !9
  %21 = sext i8 %20 to i32
  %22 = call i32 @isalpha(i32 noundef %21) #5
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %24, label %25

24:                                               ; preds = %16
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %24, %16
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [105 x i8], ptr @str, i64 0, i64 %27
  %29 = load i8, ptr %28, align 1, !tbaa !9
  %30 = sext i8 %29 to i32
  %31 = icmp eq i32 %30, 47
  br i1 %31, label %32, label %42

32:                                               ; preds = %25
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr %4, align 4, !tbaa !5
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = icmp sgt i32 %35, 1
  br i1 %36, label %37, label %41

37:                                               ; preds = %32
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [105 x i8], ptr @str, i64 0, i64 %39
  store i8 32, ptr %40, align 1, !tbaa !9
  br label %41

41:                                               ; preds = %37, %32
  br label %42

42:                                               ; preds = %41, %25
  br label %43

43:                                               ; preds = %42
  %44 = load i32, ptr %3, align 4, !tbaa !5
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %3, align 4, !tbaa !5
  br label %12, !llvm.loop !10

46:                                               ; preds = %12
  %47 = load i32, ptr %4, align 4, !tbaa !5
  %48 = load i32, ptr %2, align 4, !tbaa !5
  %49 = icmp eq i32 %47, %48
  br i1 %49, label %50, label %52

50:                                               ; preds = %46
  %51 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  store i32 0, ptr %1, align 4
  store i32 1, ptr %5, align 4
  br label %101

52:                                               ; preds = %46
  %53 = load i32, ptr %2, align 4, !tbaa !5
  %54 = sub nsw i32 %53, 1
  store i32 %54, ptr %3, align 4, !tbaa !5
  br label %55

55:                                               ; preds = %71, %52
  %56 = load i32, ptr %3, align 4, !tbaa !5
  %57 = icmp sge i32 %56, 0
  br i1 %57, label %58, label %74

58:                                               ; preds = %55
  %59 = load i32, ptr %3, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [105 x i8], ptr @str, i64 0, i64 %60
  %62 = load i8, ptr %61, align 1, !tbaa !9
  %63 = sext i8 %62 to i32
  %64 = call i32 @isalpha(i32 noundef %63) #5
  %65 = icmp ne i32 %64, 0
  br i1 %65, label %66, label %67

66:                                               ; preds = %58
  br label %74

67:                                               ; preds = %58
  %68 = load i32, ptr %3, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds [105 x i8], ptr @str, i64 0, i64 %69
  store i8 32, ptr %70, align 1, !tbaa !9
  br label %71

71:                                               ; preds = %67
  %72 = load i32, ptr %3, align 4, !tbaa !5
  %73 = add nsw i32 %72, -1
  store i32 %73, ptr %3, align 4, !tbaa !5
  br label %55, !llvm.loop !12

74:                                               ; preds = %66, %55
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %75

75:                                               ; preds = %95, %74
  %76 = load i32, ptr %3, align 4, !tbaa !5
  %77 = load i32, ptr %2, align 4, !tbaa !5
  %78 = icmp slt i32 %76, %77
  br i1 %78, label %79, label %98

79:                                               ; preds = %75
  %80 = load i32, ptr %3, align 4, !tbaa !5
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds [105 x i8], ptr @str, i64 0, i64 %81
  %83 = load i8, ptr %82, align 1, !tbaa !9
  %84 = sext i8 %83 to i32
  %85 = call i32 @isspace(i32 noundef %84) #5
  %86 = icmp ne i32 %85, 0
  br i1 %86, label %87, label %88

87:                                               ; preds = %79
  br label %95

88:                                               ; preds = %79
  %89 = load i32, ptr %3, align 4, !tbaa !5
  %90 = sext i32 %89 to i64
  %91 = getelementptr inbounds [105 x i8], ptr @str, i64 0, i64 %90
  %92 = load i8, ptr %91, align 1, !tbaa !9
  %93 = sext i8 %92 to i32
  %94 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %93)
  br label %95

95:                                               ; preds = %88, %87
  %96 = load i32, ptr %3, align 4, !tbaa !5
  %97 = add nsw i32 %96, 1
  store i32 %97, ptr %3, align 4, !tbaa !5
  br label %75, !llvm.loop !13

98:                                               ; preds = %75
  %99 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  br label %6, !llvm.loop !14

100:                                              ; preds = %6
  store i32 0, ptr %1, align 4
  store i32 1, ptr %5, align 4
  br label %101

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

; 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: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #3

; Function Attrs: nounwind willreturn memory(read)
declare i32 @isalpha(i32 noundef) #3

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

; Function Attrs: nounwind willreturn memory(read)
declare i32 @isspace(i32 noundef) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { 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 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 #4 = { nounwind }
attributes #5 = { 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}
!13 = distinct !{!13, !11}
!14 = distinct !{!14, !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 [105 x i8], align 16
@.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 [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 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @str)
  %.not42 = icmp eq i32 %1, -1
  br i1 %.not42, label %.loopexit, label %.lr.ph44

.lr.ph44:                                         ; preds = %0, %._crit_edge41
  %2 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) @str) #4
  %3 = trunc i64 %2 to i32
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph.preheader, label %._crit_edge.thread

.lr.ph.preheader:                                 ; preds = %.lr.ph44
  %wide.trip.count = and i64 %2, 2147483647
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %14
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %14 ]
  %.032 = phi i32 [ 0, %.lr.ph.preheader ], [ %.2, %14 ]
  %5 = getelementptr inbounds [105 x i8], ptr @str, i64 0, i64 %indvars.iv
  %6 = load i8, ptr %5, align 1, !tbaa !5
  %7 = sext i8 %6 to i32
  %8 = tail call i32 @isalpha(i32 noundef %7) #4
  %.not30 = icmp eq i32 %8, 0
  %spec.select = select i1 %.not30, i32 %.032, i32 0
  %9 = icmp eq i8 %6, 47
  br i1 %9, label %10, label %14

10:                                               ; preds = %.lr.ph
  %11 = add nsw i32 %spec.select, 1
  %12 = icmp sgt i32 %spec.select, 0
  br i1 %12, label %13, label %14

13:                                               ; preds = %10
  store i8 32, ptr %5, align 1, !tbaa !5
  br label %14

14:                                               ; preds = %.lr.ph, %13, %10
  %.2 = phi i32 [ %11, %13 ], [ %11, %10 ], [ %spec.select, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !8

._crit_edge:                                      ; preds = %14
  %15 = icmp eq i32 %.2, %3
  br i1 %15, label %18, label %.preheader

._crit_edge.thread:                               ; preds = %.lr.ph44
  %16 = icmp eq i32 %3, 0
  br i1 %16, label %18, label %._crit_edge41

.preheader:                                       ; preds = %._crit_edge
  br i1 %4, label %.lr.ph35.preheader, label %._crit_edge41

.lr.ph35.preheader:                               ; preds = %.preheader
  %17 = and i64 %2, 2147483647
  br label %.lr.ph35

18:                                               ; preds = %._crit_edge.thread, %._crit_edge
  %19 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
  br label %.loopexit

.lr.ph35:                                         ; preds = %.lr.ph35.preheader, %24
  %indvars.iv46 = phi i64 [ %17, %.lr.ph35.preheader ], [ %indvars.iv.next47, %24 ]
  %indvars.iv.next47 = add nsw i64 %indvars.iv46, -1
  %20 = getelementptr inbounds [105 x i8], ptr @str, i64 0, i64 %indvars.iv.next47
  %21 = load i8, ptr %20, align 1, !tbaa !5
  %22 = sext i8 %21 to i32
  %23 = tail call i32 @isalpha(i32 noundef %22) #4
  %.not28 = icmp eq i32 %23, 0
  br i1 %.not28, label %24, label %._crit_edge36

24:                                               ; preds = %.lr.ph35
  store i8 32, ptr %20, align 1, !tbaa !5
  %25 = icmp ugt i64 %indvars.iv46, 1
  br i1 %25, label %.lr.ph35, label %._crit_edge36, !llvm.loop !10

._crit_edge36:                                    ; preds = %24, %.lr.ph35
  br i1 %4, label %.lr.ph40.preheader, label %._crit_edge41

.lr.ph40.preheader:                               ; preds = %._crit_edge36
  %wide.trip.count52 = and i64 %2, 2147483647
  br label %.lr.ph40

.lr.ph40:                                         ; preds = %.lr.ph40.preheader, %32
  %indvars.iv49 = phi i64 [ 0, %.lr.ph40.preheader ], [ %indvars.iv.next50, %32 ]
  %26 = getelementptr inbounds [105 x i8], ptr @str, i64 0, i64 %indvars.iv49
  %27 = load i8, ptr %26, align 1, !tbaa !5
  %28 = sext i8 %27 to i32
  %29 = tail call i32 @isspace(i32 noundef %28) #4
  %.not29 = icmp eq i32 %29, 0
  br i1 %.not29, label %30, label %32

30:                                               ; preds = %.lr.ph40
  %31 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %28)
  br label %32

32:                                               ; preds = %.lr.ph40, %30
  %indvars.iv.next50 = add nuw nsw i64 %indvars.iv49, 1
  %exitcond53.not = icmp eq i64 %indvars.iv.next50, %wide.trip.count52
  br i1 %exitcond53.not, label %._crit_edge41, label %.lr.ph40, !llvm.loop !11

._crit_edge41:                                    ; preds = %32, %._crit_edge.thread, %.preheader, %._crit_edge36
  %33 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  %34 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @str)
  %.not = icmp eq i32 %34, -1
  br i1 %.not, label %.loopexit, label %.lr.ph44, !llvm.loop !12

.loopexit:                                        ; preds = %._crit_edge41, %0, %18
  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: mustprogress nofree nounwind willreturn memory(read)
declare i32 @isalpha(i32 noundef) local_unnamed_addr #3

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(read)
declare i32 @isspace(i32 noundef) local_unnamed_addr #3

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 = { mustprogress nofree 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 #4 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !9}
!11 = distinct !{!11, !9}
!12 = distinct !{!12, !9}


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