; LLVM IR from KishanBGajera/2024-366-Days-Of-DSA :: Data Structures/Linear/Stack/Applications of Stack/Valid Parentheses/C (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"

%struct.Stack = type { i32, [100 x i8] }

; Function Attrs: nounwind uwtable
declare dso_local void @initialize(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @push(ptr noundef, i8 noundef signext) #0

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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @isOpening(i8 noundef signext) #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @isClosing(i8 noundef signext) #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @isMatching(i8 noundef signext, i8 noundef signext) #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @checkBalancedParentheses(ptr noundef %0) #0 {
  %2 = alloca i1, align 1
  %3 = alloca ptr, align 8
  %4 = alloca %struct.Stack, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 104, ptr %4) #2
  call void @initialize(ptr noundef %4)
  br label %6

6:                                                ; preds = %34, %1
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = load i8, ptr %7, align 1, !tbaa !9
  %9 = sext i8 %8 to i32
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %11, label %37

11:                                               ; preds = %6
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = load i8, ptr %12, align 1, !tbaa !9
  %14 = call zeroext i1 @isOpening(i8 noundef signext %13)
  br i1 %14, label %15, label %18

15:                                               ; preds = %11
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = load i8, ptr %16, align 1, !tbaa !9
  call void @push(ptr noundef %4, i8 noundef signext %17)
  br label %34

18:                                               ; preds = %11
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = load i8, ptr %19, align 1, !tbaa !9
  %21 = call zeroext i1 @isClosing(i8 noundef signext %20)
  br i1 %21, label %22, label %33

22:                                               ; preds = %18
  %23 = getelementptr inbounds %struct.Stack, ptr %4, i32 0, i32 0
  %24 = load i32, ptr %23, align 4, !tbaa !10
  %25 = icmp eq i32 %24, -1
  br i1 %25, label %31, label %26

26:                                               ; preds = %22
  %27 = call signext i8 @pop(ptr noundef %4)
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = load i8, ptr %28, align 1, !tbaa !9
  %30 = call zeroext i1 @isMatching(i8 noundef signext %27, i8 noundef signext %29)
  br i1 %30, label %32, label %31

31:                                               ; preds = %26, %22
  store i1 false, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %41

32:                                               ; preds = %26
  br label %33

33:                                               ; preds = %32, %18
  br label %34

34:                                               ; preds = %33, %15
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds i8, ptr %35, i32 1
  store ptr %36, ptr %3, align 8, !tbaa !5
  br label %6, !llvm.loop !13

37:                                               ; preds = %6
  %38 = getelementptr inbounds %struct.Stack, ptr %4, i32 0, i32 0
  %39 = load i32, ptr %38, align 4, !tbaa !10
  %40 = icmp eq i32 %39, -1
  store i1 %40, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %41

41:                                               ; preds = %37, %31
  call void @llvm.lifetime.end.p0(i64 104, ptr %4) #2
  %42 = load i1, ptr %2, align 1
  ret i1 %42
}

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

%struct.Stack = type { i32, [100 x i8] }

; Function Attrs: nounwind uwtable
declare dso_local void @initialize(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @push(ptr noundef, i8 noundef signext) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @isOpening(i8 noundef signext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @isClosing(i8 noundef signext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @isMatching(i8 noundef signext, i8 noundef signext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @checkBalancedParentheses(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = alloca %struct.Stack, align 4
  call void @llvm.lifetime.start.p0(i64 104, ptr nonnull %2) #2
  call void @initialize(ptr noundef nonnull %2)
  %3 = load i8, ptr %0, align 1, !tbaa !5
  %.not8 = icmp eq i8 %3, 0
  br i1 %.not8, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1, %17
  %4 = phi i8 [ %19, %17 ], [ %3, %1 ]
  %.09 = phi ptr [ %18, %17 ], [ %0, %1 ]
  %5 = call zeroext i1 @isOpening(i8 noundef signext %4)
  %6 = load i8, ptr %.09, align 1, !tbaa !5
  br i1 %5, label %7, label %8

7:                                                ; preds = %.lr.ph
  call void @push(ptr noundef nonnull %2, i8 noundef signext %6)
  br label %17

8:                                                ; preds = %.lr.ph
  %9 = call zeroext i1 @isClosing(i8 noundef signext %6)
  br i1 %9, label %10, label %17

10:                                               ; preds = %8
  %11 = load i32, ptr %2, align 4, !tbaa !8
  %12 = icmp eq i32 %11, -1
  br i1 %12, label %.loopexit, label %13

13:                                               ; preds = %10
  %14 = call signext i8 @pop(ptr noundef nonnull %2)
  %15 = load i8, ptr %.09, align 1, !tbaa !5
  %16 = call zeroext i1 @isMatching(i8 noundef signext %14, i8 noundef signext %15)
  br i1 %16, label %17, label %.loopexit

17:                                               ; preds = %8, %13, %7
  %18 = getelementptr inbounds i8, ptr %.09, i64 1
  %19 = load i8, ptr %18, align 1, !tbaa !5
  %.not = icmp eq i8 %19, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !11

._crit_edge:                                      ; preds = %17, %1
  %20 = load i32, ptr %2, align 4, !tbaa !8
  %21 = icmp eq i32 %20, -1
  br label %.loopexit

.loopexit:                                        ; preds = %10, %13, %._crit_edge
  %.07 = phi i1 [ %21, %._crit_edge ], [ false, %13 ], [ false, %10 ]
  call void @llvm.lifetime.end.p0(i64 104, ptr nonnull %2) #2
  ret i1 %.07
}

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

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


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