; LLVM IR from PacktPublishing/CPP-Data-Structures-and-Algorithms :: Chapter03/Parenthesis_Validity/Parenthesis_Validity.ll (Z7IsValidPc)

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

%class.Stack = type { i32, ptr }

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_Z7IsValidPc(ptr noundef %0) #0 {
  %2 = alloca i1, align 1
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca %class.Stack, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = call i64 @strlen(ptr noundef %8) #5
  %10 = trunc i64 %9 to i32
  store i32 %10, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %5) #4
  call void @_ZN5StackIcEC2Ev(ptr noundef nonnull align 8 dereferenceable(16) %5)
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %120, %1
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = load i32, ptr %4, align 4, !tbaa !9
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %11
  store i32 2, ptr %7, align 4
  br label %123

16:                                               ; preds = %11
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = load i32, ptr %6, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds i8, ptr %17, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !11
  %22 = sext i8 %21 to i32
  %23 = icmp eq i32 %22, 123
  br i1 %23, label %24, label %25

24:                                               ; preds = %16
  call void @_ZN5StackIcE4PushEc(ptr noundef nonnull align 8 dereferenceable(16) %5, i8 noundef signext 123)
  br label %119

25:                                               ; preds = %16
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = load i32, ptr %6, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i8, ptr %26, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !11
  %31 = sext i8 %30 to i32
  %32 = icmp eq i32 %31, 91
  br i1 %32, label %33, label %34

33:                                               ; preds = %25
  call void @_ZN5StackIcE4PushEc(ptr noundef nonnull align 8 dereferenceable(16) %5, i8 noundef signext 91)
  br label %118

34:                                               ; preds = %25
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = load i32, ptr %6, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds i8, ptr %35, i64 %37
  %39 = load i8, ptr %38, align 1, !tbaa !11
  %40 = sext i8 %39 to i32
  %41 = icmp eq i32 %40, 40
  br i1 %41, label %42, label %43

42:                                               ; preds = %34
  call void @_ZN5StackIcE4PushEc(ptr noundef nonnull align 8 dereferenceable(16) %5, i8 noundef signext 40)
  br label %117

43:                                               ; preds = %34
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = load i32, ptr %6, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i8, ptr %44, i64 %46
  %48 = load i8, ptr %47, align 1, !tbaa !11
  %49 = sext i8 %48 to i32
  %50 = icmp eq i32 %49, 125
  br i1 %50, label %67, label %51

51:                                               ; preds = %43
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = load i32, ptr %6, align 4, !tbaa !9
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds i8, ptr %52, i64 %54
  %56 = load i8, ptr %55, align 1, !tbaa !11
  %57 = sext i8 %56 to i32
  %58 = icmp eq i32 %57, 93
  br i1 %58, label %67, label %59

59:                                               ; preds = %51
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = load i32, ptr %6, align 4, !tbaa !9
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds i8, ptr %60, i64 %62
  %64 = load i8, ptr %63, align 1, !tbaa !11
  %65 = sext i8 %64 to i32
  %66 = icmp eq i32 %65, 41
  br i1 %66, label %67, label %116

67:                                               ; preds = %59, %51, %43
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = load i32, ptr %6, align 4, !tbaa !9
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds i8, ptr %68, i64 %70
  %72 = load i8, ptr %71, align 1, !tbaa !11
  %73 = sext i8 %72 to i32
  %74 = icmp eq i32 %73, 125
  br i1 %74, label %75, label %82

75:                                               ; preds = %67
  %76 = call noundef zeroext i1 @_ZN5StackIcE7IsEmptyEv(ptr noundef nonnull align 8 dereferenceable(16) %5)
  br i1 %76, label %81, label %77

77:                                               ; preds = %75
  %78 = call noundef signext i8 @_ZN5StackIcE3TopEv(ptr noundef nonnull align 8 dereferenceable(16) %5)
  %79 = sext i8 %78 to i32
  %80 = icmp ne i32 %79, 123
  br i1 %80, label %81, label %82

81:                                               ; preds = %77, %75
  store i1 false, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %123

82:                                               ; preds = %77, %67
  %83 = load ptr, ptr %3, align 8, !tbaa !5
  %84 = load i32, ptr %6, align 4, !tbaa !9
  %85 = sext i32 %84 to i64
  %86 = getelementptr inbounds i8, ptr %83, i64 %85
  %87 = load i8, ptr %86, align 1, !tbaa !11
  %88 = sext i8 %87 to i32
  %89 = icmp eq i32 %88, 93
  br i1 %89, label %90, label %97

90:                                               ; preds = %82
  %91 = call noundef zeroext i1 @_ZN5StackIcE7IsEmptyEv(ptr noundef nonnull align 8 dereferenceable(16) %5)
  br i1 %91, label %96, label %92

92:                                               ; preds = %90
  %93 = call noundef signext i8 @_ZN5StackIcE3TopEv(ptr noundef nonnull align 8 dereferenceable(16) %5)
  %94 = sext i8 %93 to i32
  %95 = icmp ne i32 %94, 91
  br i1 %95, label %96, label %97

96:                                               ; preds = %92, %90
  store i1 false, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %123

97:                                               ; preds = %92, %82
  %98 = load ptr, ptr %3, align 8, !tbaa !5
  %99 = load i32, ptr %6, align 4, !tbaa !9
  %100 = sext i32 %99 to i64
  %101 = getelementptr inbounds i8, ptr %98, i64 %100
  %102 = load i8, ptr %101, align 1, !tbaa !11
  %103 = sext i8 %102 to i32
  %104 = icmp eq i32 %103, 41
  br i1 %104, label %105, label %112

105:                                              ; preds = %97
  %106 = call noundef zeroext i1 @_ZN5StackIcE7IsEmptyEv(ptr noundef nonnull align 8 dereferenceable(16) %5)
  br i1 %106, label %111, label %107

107:                                              ; preds = %105
  %108 = call noundef signext i8 @_ZN5StackIcE3TopEv(ptr noundef nonnull align 8 dereferenceable(16) %5)
  %109 = sext i8 %108 to i32
  %110 = icmp ne i32 %109, 40
  br i1 %110, label %111, label %112

111:                                              ; preds = %107, %105
  store i1 false, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %123

112:                                              ; preds = %107, %97
  call void @_ZN5StackIcE3PopEv(ptr noundef nonnull align 8 dereferenceable(16) %5)
  br label %113

113:                                              ; preds = %112
  br label %114

114:                                              ; preds = %113
  br label %115

115:                                              ; preds = %114
  br label %116

116:                                              ; preds = %115, %59
  br label %117

117:                                              ; preds = %116, %42
  br label %118

118:                                              ; preds = %117, %33
  br label %119

119:                                              ; preds = %118, %24
  br label %120

120:                                              ; preds = %119
  %121 = load i32, ptr %6, align 4, !tbaa !9
  %122 = add nsw i32 %121, 1
  store i32 %122, ptr %6, align 4, !tbaa !9
  br label %11, !llvm.loop !12

123:                                              ; preds = %111, %96, %81, %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  %124 = load i32, ptr %7, align 4
  switch i32 %124, label %129 [
    i32 2, label %125
  ]

125:                                              ; preds = %123
  %126 = call noundef zeroext i1 @_ZN5StackIcE7IsEmptyEv(ptr noundef nonnull align 8 dereferenceable(16) %5)
  br i1 %126, label %127, label %128

127:                                              ; preds = %125
  store i1 true, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %129

128:                                              ; preds = %125
  store i1 false, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %129

129:                                              ; preds = %128, %127, %123
  call void @llvm.lifetime.end.p0(i64 16, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  %130 = load i1, ptr %2, align 1
  ret i1 %130
}

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

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN5StackIcEC2Ev(ptr noundef nonnull align 8 dereferenceable(16)) unnamed_addr #3 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN5StackIcE4PushEc(ptr noundef nonnull align 8 dereferenceable(16), i8 noundef signext) #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZN5StackIcE7IsEmptyEv(ptr noundef nonnull align 8 dereferenceable(16)) #3 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef signext i8 @_ZN5StackIcE3TopEv(ptr noundef nonnull align 8 dereferenceable(16)) #3 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN5StackIcE3PopEv(ptr noundef nonnull align 8 dereferenceable(16)) #3 align 2

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

attributes #0 = { mustprogress 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 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 = { mustprogress 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple 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"

%class.Stack = type { i32, ptr }

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_Z7IsValidPc(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = alloca %class.Stack, align 8
  %3 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #4
  %4 = trunc i64 %3 to i32
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %2) #5
  call void @_ZN5StackIcEC2Ev(ptr noundef nonnull align 8 dereferenceable(16) %2)
  %.not2829 = icmp sgt i32 %4, 0
  br i1 %.not2829, label %.lr.ph.preheader, label %.critedge

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

.lr.ph:                                           ; preds = %.lr.ph.preheader, %29
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %29 ]
  %5 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv
  %6 = load i8, ptr %5, align 1, !tbaa !5
  switch i8 %6, label %29 [
    i8 123, label %7
    i8 91, label %8
    i8 40, label %9
    i8 125, label %10
    i8 41, label %14
    i8 93, label %14
  ]

7:                                                ; preds = %.lr.ph
  call void @_ZN5StackIcE4PushEc(ptr noundef nonnull align 8 dereferenceable(16) %2, i8 noundef signext 123)
  br label %29

8:                                                ; preds = %.lr.ph
  call void @_ZN5StackIcE4PushEc(ptr noundef nonnull align 8 dereferenceable(16) %2, i8 noundef signext 91)
  br label %29

9:                                                ; preds = %.lr.ph
  call void @_ZN5StackIcE4PushEc(ptr noundef nonnull align 8 dereferenceable(16) %2, i8 noundef signext 40)
  br label %29

10:                                               ; preds = %.lr.ph
  %11 = call noundef zeroext i1 @_ZN5StackIcE7IsEmptyEv(ptr noundef nonnull align 8 dereferenceable(16) %2)
  br i1 %11, label %.loopexit, label %12

12:                                               ; preds = %10
  %13 = call noundef signext i8 @_ZN5StackIcE3TopEv(ptr noundef nonnull align 8 dereferenceable(16) %2)
  %.not = icmp eq i8 %13, 123
  br i1 %.not, label %._crit_edge, label %.loopexit

._crit_edge:                                      ; preds = %12
  %.pre = load i8, ptr %5, align 1, !tbaa !5
  br label %14

14:                                               ; preds = %._crit_edge, %.lr.ph, %.lr.ph
  %15 = phi i8 [ %.pre, %._crit_edge ], [ %6, %.lr.ph ], [ %6, %.lr.ph ]
  %16 = icmp eq i8 %15, 93
  br i1 %16, label %17, label %21

17:                                               ; preds = %14
  %18 = call noundef zeroext i1 @_ZN5StackIcE7IsEmptyEv(ptr noundef nonnull align 8 dereferenceable(16) %2)
  br i1 %18, label %.loopexit, label %19

19:                                               ; preds = %17
  %20 = call noundef signext i8 @_ZN5StackIcE3TopEv(ptr noundef nonnull align 8 dereferenceable(16) %2)
  %.not26 = icmp eq i8 %20, 91
  br i1 %.not26, label %thread-pre-split, label %.loopexit

thread-pre-split:                                 ; preds = %19
  %.pr = load i8, ptr %5, align 1, !tbaa !5
  br label %21

21:                                               ; preds = %thread-pre-split, %14
  %22 = phi i8 [ %.pr, %thread-pre-split ], [ %15, %14 ]
  %23 = icmp eq i8 %22, 41
  br i1 %23, label %24, label %28

24:                                               ; preds = %21
  %25 = call noundef zeroext i1 @_ZN5StackIcE7IsEmptyEv(ptr noundef nonnull align 8 dereferenceable(16) %2)
  br i1 %25, label %.loopexit, label %26

26:                                               ; preds = %24
  %27 = call noundef signext i8 @_ZN5StackIcE3TopEv(ptr noundef nonnull align 8 dereferenceable(16) %2)
  %.not27 = icmp eq i8 %27, 40
  br i1 %.not27, label %28, label %.loopexit

28:                                               ; preds = %26, %21
  call void @_ZN5StackIcE3PopEv(ptr noundef nonnull align 8 dereferenceable(16) %2)
  br label %29

29:                                               ; preds = %.lr.ph, %7, %9, %28, %8
  %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 %.critedge, label %.lr.ph, !llvm.loop !8

.critedge:                                        ; preds = %29, %1
  %30 = call noundef zeroext i1 @_ZN5StackIcE7IsEmptyEv(ptr noundef nonnull align 8 dereferenceable(16) %2)
  br label %.loopexit

.loopexit:                                        ; preds = %12, %10, %19, %17, %26, %24, %.critedge
  %.1 = phi i1 [ %30, %.critedge ], [ false, %24 ], [ false, %26 ], [ false, %17 ], [ false, %19 ], [ false, %10 ], [ false, %12 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %2) #5
  ret i1 %.1
}

; 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 nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN5StackIcEC2Ev(ptr noundef nonnull align 8 dereferenceable(16)) unnamed_addr #3 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN5StackIcE4PushEc(ptr noundef nonnull align 8 dereferenceable(16), i8 noundef signext) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZN5StackIcE7IsEmptyEv(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #3 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef signext i8 @_ZN5StackIcE3TopEv(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #3 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN5StackIcE3PopEv(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #3 align 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 = { mustprogress 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 = { 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 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 #4 = { nounwind willreturn memory(read) }
attributes #5 = { 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++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


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