; LLVM IR from DimitrisJim/leetcode_solutions :: C/Algorithms (1221)

; 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"

; Function Attrs: nounwind uwtable
define dso_local i32 @balancedStringSplit(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i8, align 1
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #2
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %8, i32 1
  store ptr %9, ptr %2, align 8, !tbaa !5
  %10 = load i8, ptr %8, align 1, !tbaa !11
  store i8 %10, ptr %5, align 1, !tbaa !11
  br label %11

11:                                               ; preds = %48, %1
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  %13 = load i8, ptr %12, align 1, !tbaa !11
  %14 = icmp ne i8 %13, 0
  br i1 %14, label %15, label %49

15:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #2
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %16, i32 1
  store ptr %17, ptr %2, align 8, !tbaa !5
  %18 = load i8, ptr %16, align 1, !tbaa !11
  store i8 %18, ptr %6, align 1, !tbaa !11
  %19 = load i8, ptr %5, align 1, !tbaa !11
  %20 = sext i8 %19 to i32
  %21 = load i8, ptr %6, align 1, !tbaa !11
  %22 = sext i8 %21 to i32
  %23 = icmp ne i32 %20, %22
  br i1 %23, label %24, label %42

24:                                               ; preds = %15
  %25 = load i32, ptr %4, align 4, !tbaa !9
  %26 = add nsw i32 %25, -1
  store i32 %26, ptr %4, align 4, !tbaa !9
  %27 = load i32, ptr %4, align 4, !tbaa !9
  %28 = icmp eq i32 %27, 0
  br i1 %28, label %29, label %41

29:                                               ; preds = %24
  %30 = load i32, ptr %3, align 4, !tbaa !9
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr %3, align 4, !tbaa !9
  %32 = load ptr, ptr %2, align 8, !tbaa !5
  %33 = load i8, ptr %32, align 1, !tbaa !11
  %34 = icmp ne i8 %33, 0
  br i1 %34, label %35, label %39

35:                                               ; preds = %29
  %36 = load ptr, ptr %2, align 8, !tbaa !5
  %37 = getelementptr inbounds i8, ptr %36, i32 1
  store ptr %37, ptr %2, align 8, !tbaa !5
  %38 = load i8, ptr %36, align 1, !tbaa !11
  store i8 %38, ptr %5, align 1, !tbaa !11
  br label %40

39:                                               ; preds = %29
  store i32 3, ptr %7, align 4
  br label %46

40:                                               ; preds = %35
  store i32 1, ptr %4, align 4, !tbaa !9
  br label %41

41:                                               ; preds = %40, %24
  br label %45

42:                                               ; preds = %15
  %43 = load i32, ptr %4, align 4, !tbaa !9
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %4, align 4, !tbaa !9
  br label %45

45:                                               ; preds = %42, %41
  store i32 0, ptr %7, align 4
  br label %46

46:                                               ; preds = %45, %39
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #2
  %47 = load i32, ptr %7, align 4
  switch i32 %47, label %51 [
    i32 0, label %48
    i32 3, label %49
  ]

48:                                               ; preds = %46
  br label %11, !llvm.loop !12

49:                                               ; preds = %46, %11
  %50 = load i32, ptr %3, align 4, !tbaa !9
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %50

51:                                               ; preds = %46
  unreachable
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; 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 = { 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 = !{!"any pointer", !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"}


; 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"

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local i32 @balancedStringSplit(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 1
  %3 = load i8, ptr %2, align 1, !tbaa !5
  %.not34 = icmp eq i8 %3, 0
  br i1 %.not34, label %.thread, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %4 = load i8, ptr %0, align 1, !tbaa !5
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %17
  %5 = phi i8 [ %18, %17 ], [ %3, %.lr.ph.preheader ]
  %.01238 = phi ptr [ %.2, %17 ], [ %2, %.lr.ph.preheader ]
  %.01337 = phi i8 [ %.215, %17 ], [ %4, %.lr.ph.preheader ]
  %.01636 = phi i32 [ %.218, %17 ], [ 1, %.lr.ph.preheader ]
  %.01935 = phi i32 [ %.221, %17 ], [ 0, %.lr.ph.preheader ]
  %6 = getelementptr inbounds i8, ptr %.01238, i64 1
  %.not26 = icmp eq i8 %.01337, %5
  br i1 %.not26, label %15, label %7

7:                                                ; preds = %.lr.ph
  %8 = add nsw i32 %.01636, -1
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %10, label %17

10:                                               ; preds = %7
  %11 = add nsw i32 %.01935, 1
  %12 = load i8, ptr %6, align 1, !tbaa !5
  %.not27 = icmp eq i8 %12, 0
  br i1 %.not27, label %.thread, label %13

13:                                               ; preds = %10
  %14 = getelementptr inbounds i8, ptr %.01238, i64 2
  br label %17

15:                                               ; preds = %.lr.ph
  %16 = add nsw i32 %.01636, 1
  br label %17

17:                                               ; preds = %15, %13, %7
  %.221 = phi i32 [ %11, %13 ], [ %.01935, %7 ], [ %.01935, %15 ]
  %.218 = phi i32 [ 1, %13 ], [ %8, %7 ], [ %16, %15 ]
  %.215 = phi i8 [ %12, %13 ], [ %.01337, %7 ], [ %5, %15 ]
  %.2 = phi ptr [ %14, %13 ], [ %6, %7 ], [ %6, %15 ]
  %18 = load i8, ptr %.2, align 1, !tbaa !5
  %.not = icmp eq i8 %18, 0
  br i1 %.not, label %.thread, label %.lr.ph

.thread:                                          ; preds = %17, %10, %1
  %.3 = phi i32 [ 0, %1 ], [ %11, %10 ], [ %.221, %17 ]
  ret i32 %.3
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) 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" }

!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"}


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