; LLVM IR from reyadussalahin/problem-solving :: data_structure/stack/codeforces/F5C (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"

@.str = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @flash_bstack() #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @flash_cstack() #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @bpush(i8 noundef signext) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @cpush(i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @bpop() #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @cpop() #0

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

14:                                               ; preds = %81, %0
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = add nsw i32 %15, 1
  store i32 %16, ptr %6, align 4, !tbaa !5
  %17 = sext i32 %15 to i64
  %18 = getelementptr inbounds [1000002 x i8], ptr %1, i64 0, i64 %17
  %19 = load i8, ptr %18, align 1, !tbaa !9
  store i8 %19, ptr %2, align 1, !tbaa !9
  %20 = icmp ne i8 %19, 0
  br i1 %20, label %21, label %82

21:                                               ; preds = %14
  %22 = load i8, ptr %2, align 1, !tbaa !9
  %23 = sext i8 %22 to i32
  %24 = icmp eq i32 %23, 40
  br i1 %24, label %25, label %28

25:                                               ; preds = %21
  %26 = load i8, ptr %2, align 1, !tbaa !9
  %27 = call i32 @bpush(i8 noundef signext %26)
  br label %81

28:                                               ; preds = %21
  %29 = call signext i8 @bpop()
  store i8 %29, ptr %3, align 1, !tbaa !9
  %30 = sext i8 %29 to i32
  %31 = icmp eq i32 %30, 40
  br i1 %31, label %32, label %47

32:                                               ; preds = %28
  store i32 2, ptr %7, align 4, !tbaa !5
  %33 = call signext i8 @bpop()
  store i8 %33, ptr %4, align 1, !tbaa !9
  %34 = sext i8 %33 to i32
  %35 = icmp eq i32 %34, 45
  br i1 %35, label %36, label %40

36:                                               ; preds = %32
  %37 = call i32 @cpop()
  %38 = load i32, ptr %7, align 4, !tbaa !5
  %39 = add nsw i32 %38, %37
  store i32 %39, ptr %7, align 4, !tbaa !5
  br label %43

40:                                               ; preds = %32
  %41 = load i8, ptr %4, align 1, !tbaa !9
  %42 = call i32 @bpush(i8 noundef signext %41)
  br label %43

43:                                               ; preds = %40, %36
  %44 = load i32, ptr %7, align 4, !tbaa !5
  %45 = call i32 @cpush(i32 noundef %44)
  %46 = call i32 @bpush(i8 noundef signext 45)
  br label %80

47:                                               ; preds = %28
  %48 = load i8, ptr %3, align 1, !tbaa !9
  %49 = sext i8 %48 to i32
  %50 = icmp eq i32 %49, 45
  br i1 %50, label %51, label %76

51:                                               ; preds = %47
  %52 = call signext i8 @bpop()
  store i8 %52, ptr %4, align 1, !tbaa !9
  %53 = sext i8 %52 to i32
  %54 = icmp eq i32 %53, 40
  br i1 %54, label %55, label %72

55:                                               ; preds = %51
  %56 = call i32 @cpop()
  %57 = add nsw i32 %56, 2
  store i32 %57, ptr %7, align 4, !tbaa !5
  %58 = call signext i8 @bpop()
  store i8 %58, ptr %5, align 1, !tbaa !9
  %59 = sext i8 %58 to i32
  %60 = icmp eq i32 %59, 45
  br i1 %60, label %61, label %65

61:                                               ; preds = %55
  %62 = call i32 @cpop()
  %63 = load i32, ptr %7, align 4, !tbaa !5
  %64 = add nsw i32 %63, %62
  store i32 %64, ptr %7, align 4, !tbaa !5
  br label %68

65:                                               ; preds = %55
  %66 = load i8, ptr %5, align 1, !tbaa !9
  %67 = call i32 @bpush(i8 noundef signext %66)
  br label %68

68:                                               ; preds = %65, %61
  %69 = load i32, ptr %7, align 4, !tbaa !5
  %70 = call i32 @cpush(i32 noundef %69)
  %71 = call i32 @bpush(i8 noundef signext 45)
  br label %75

72:                                               ; preds = %51
  %73 = load i8, ptr %4, align 1, !tbaa !9
  %74 = call i32 @bpush(i8 noundef signext %73)
  br label %75

75:                                               ; preds = %72, %68
  br label %79

76:                                               ; preds = %47
  %77 = load i8, ptr %3, align 1, !tbaa !9
  %78 = call i32 @bpush(i8 noundef signext %77)
  br label %79

79:                                               ; preds = %76, %75
  br label %80

80:                                               ; preds = %79, %43
  br label %81

81:                                               ; preds = %80, %25
  br label %14, !llvm.loop !10

82:                                               ; preds = %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 1000002, ptr %1) #3
  ret i32 0
}

; 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

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 = { 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/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; 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 [3 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @flash_bstack() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @flash_cstack() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @bpush(i8 noundef signext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @cpush(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @bpop() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @cpop() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @longest_sequence() local_unnamed_addr #0 {
  %1 = alloca [1000002 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 1000002, ptr nonnull %1) #3
  %2 = tail call i32 @flash_bstack()
  %3 = tail call i32 @flash_cstack()
  %4 = tail call i32 @bpush(i8 noundef signext 46)
  %5 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  br label %6

6:                                                ; preds = %33, %0
  %indvars.iv = phi i64 [ %indvars.iv.next, %33 ], [ 0, %0 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %7 = getelementptr inbounds [1000002 x i8], ptr %1, i64 0, i64 %indvars.iv
  %8 = load i8, ptr %7, align 1, !tbaa !5
  switch i8 %8, label %9 [
    i8 0, label %35
    i8 40, label %33
  ]

9:                                                ; preds = %6
  %10 = call signext i8 @bpop()
  switch i8 %10, label %33 [
    i8 40, label %11
    i8 45, label %19
  ]

11:                                               ; preds = %9
  %12 = call signext i8 @bpop()
  %13 = icmp eq i8 %12, 45
  br i1 %13, label %14, label %17

14:                                               ; preds = %11
  %15 = call i32 @cpop()
  %16 = add nsw i32 %15, 2
  br label %.sink.split

17:                                               ; preds = %11
  %18 = call i32 @bpush(i8 noundef signext %12)
  br label %.sink.split

19:                                               ; preds = %9
  %20 = call signext i8 @bpop()
  %21 = icmp eq i8 %20, 40
  br i1 %21, label %22, label %33

22:                                               ; preds = %19
  %23 = call i32 @cpop()
  %24 = add nsw i32 %23, 2
  %25 = call signext i8 @bpop()
  %26 = icmp eq i8 %25, 45
  br i1 %26, label %27, label %30

27:                                               ; preds = %22
  %28 = call i32 @cpop()
  %29 = add nsw i32 %28, %24
  br label %.sink.split

30:                                               ; preds = %22
  %31 = call i32 @bpush(i8 noundef signext %25)
  br label %.sink.split

.sink.split:                                      ; preds = %27, %30, %14, %17
  %.0.sink = phi i32 [ %16, %14 ], [ 2, %17 ], [ %29, %27 ], [ %24, %30 ]
  %32 = call i32 @cpush(i32 noundef %.0.sink)
  br label %33

33:                                               ; preds = %.sink.split, %9, %19, %6
  %.sink = phi i8 [ %8, %6 ], [ %20, %19 ], [ %10, %9 ], [ 45, %.sink.split ]
  %34 = call i32 @bpush(i8 noundef signext %.sink)
  br label %6, !llvm.loop !8

35:                                               ; preds = %6
  call void @llvm.lifetime.end.p0(i64 1000002, ptr nonnull %1) #3
  ret i32 0
}

; 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

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass