; LLVM IR from wuzhouhui/leetcode :: 20_valid_parenthese (isValid)

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

@stack = external hidden global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @isValid(ptr noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = icmp ne ptr %7, null
  br i1 %8, label %10, label %9

9:                                                ; preds = %1
  store i32 1, ptr %2, align 4
  br label %69

10:                                               ; preds = %1
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = call i64 @strlen(ptr noundef %11) #3
  %13 = urem i64 %12, 2
  %14 = icmp ne i64 %13, 0
  br i1 %14, label %15, label %16

15:                                               ; preds = %10
  store i32 0, ptr %2, align 4
  br label %69

16:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 0, ptr %4, align 4, !tbaa !9
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %17

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

24:                                               ; preds = %17
  %25 = load i32, ptr %6, align 4, !tbaa !9
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %28, label %27

27:                                               ; preds = %24
  br label %63

28:                                               ; preds = %24
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = load i32, ptr %4, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i8, ptr %29, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !11
  %34 = sext i8 %33 to i32
  switch i32 %34, label %59 [
    i32 40, label %35
    i32 123, label %35
    i32 91, label %35
    i32 41, label %41
    i32 125, label %41
    i32 93, label %41
  ]

35:                                               ; preds = %28, %28, %28
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = load i32, ptr %4, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds i8, ptr %36, i64 %38
  %40 = load i8, ptr %39, align 1, !tbaa !11
  call void @push(i8 noundef signext %40)
  br label %59

41:                                               ; preds = %28, %28, %28
  %42 = call signext i8 @pop()
  %43 = sext i8 %42 to i32
  store i32 %43, ptr %5, align 4, !tbaa !9
  %44 = load i32, ptr %5, align 4, !tbaa !9
  switch i32 %44, label %48 [
    i32 40, label %45
    i32 123, label %46
    i32 91, label %47
  ]

45:                                               ; preds = %41
  store i32 41, ptr %5, align 4, !tbaa !9
  br label %48

46:                                               ; preds = %41
  store i32 125, ptr %5, align 4, !tbaa !9
  br label %48

47:                                               ; preds = %41
  store i32 93, ptr %5, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %47, %46, %45, %41
  %49 = load i32, ptr %5, align 4, !tbaa !9
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = load i32, ptr %4, align 4, !tbaa !9
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds i8, ptr %50, i64 %52
  %54 = load i8, ptr %53, align 1, !tbaa !11
  %55 = sext i8 %54 to i32
  %56 = icmp ne i32 %49, %55
  br i1 %56, label %57, label %58

57:                                               ; preds = %48
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %58

58:                                               ; preds = %57, %48
  br label %59

59:                                               ; preds = %58, %35, %28
  br label %60

60:                                               ; preds = %59
  %61 = load i32, ptr %4, align 4, !tbaa !9
  %62 = add nsw i32 %61, 1
  store i32 %62, ptr %4, align 4, !tbaa !9
  br label %17, !llvm.loop !12

63:                                               ; preds = %27, %17
  %64 = load ptr, ptr @stack, align 8, !tbaa !5
  %65 = icmp ne ptr %64, null
  br i1 %65, label %66, label %67

66:                                               ; preds = %63
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %67

67:                                               ; preds = %66, %63
  call void @free_stack()
  %68 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %68, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %69

69:                                               ; preds = %67, %15, %9
  %70 = load i32, ptr %2, align 4
  ret i32 %70
}

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

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

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

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

; Function Attrs: nounwind uwtable
declare hidden signext i8 @pop() #0

; Function Attrs: nounwind uwtable
declare hidden void @free_stack() #0

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 = { 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nounwind willreturn memory(read) }
attributes #4 = { 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"

@stack = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @isValid(ptr noundef readonly %0) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %24, label %2

2:                                                ; preds = %1
  %3 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #2
  %4 = and i64 %3, 1
  %.not21 = icmp eq i64 %4, 0
  br i1 %.not21, label %.preheader, label %24

.preheader:                                       ; preds = %2
  %5 = load i8, ptr %0, align 1, !tbaa !5
  %.not26 = icmp eq i8 %5, 0
  br i1 %.not26, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %18
  %indvars.iv = phi i64 [ %indvars.iv.next, %18 ], [ 0, %.preheader ]
  %6 = phi i8 [ %20, %18 ], [ %5, %.preheader ]
  %7 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv
  switch i8 %6, label %18 [
    i8 40, label %8
    i8 123, label %8
    i8 91, label %8
    i8 41, label %9
    i8 125, label %9
    i8 93, label %9
  ]

8:                                                ; preds = %.lr.ph, %.lr.ph, %.lr.ph
  tail call void @push(i8 noundef signext %6)
  br label %18

9:                                                ; preds = %.lr.ph, %.lr.ph, %.lr.ph
  %10 = tail call signext i8 @pop()
  %11 = sext i8 %10 to i32
  switch i8 %10, label %15 [
    i8 40, label %12
    i8 123, label %13
    i8 91, label %14
  ]

12:                                               ; preds = %9
  br label %15

13:                                               ; preds = %9
  br label %15

14:                                               ; preds = %9
  br label %15

15:                                               ; preds = %14, %13, %12, %9
  %.016 = phi i32 [ %11, %9 ], [ 93, %14 ], [ 125, %13 ], [ 41, %12 ]
  %16 = load i8, ptr %7, align 1, !tbaa !5
  %17 = sext i8 %16 to i32
  %.not23 = icmp eq i32 %.016, %17
  %spec.select = zext i1 %.not23 to i32
  br label %18

18:                                               ; preds = %15, %.lr.ph, %8
  %.1 = phi i32 [ 1, %.lr.ph ], [ 1, %8 ], [ %spec.select, %15 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %19 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next
  %20 = load i8, ptr %19, align 1, !tbaa !5
  %21 = icmp ne i8 %20, 0
  %22 = icmp ne i32 %.1, 0
  %or.cond = and i1 %22, %21
  br i1 %or.cond, label %.lr.ph, label %._crit_edge, !llvm.loop !8

._crit_edge:                                      ; preds = %18, %.preheader
  %.0.lcssa = phi i32 [ 1, %.preheader ], [ %.1, %18 ]
  %23 = load ptr, ptr @stack, align 8, !tbaa !10
  %.not22 = icmp eq ptr %23, null
  %spec.select24 = select i1 %.not22, i32 %.0.lcssa, i32 0
  tail call void @free_stack()
  br label %24

24:                                               ; preds = %2, %1, %._crit_edge
  %.018 = phi i32 [ %spec.select24, %._crit_edge ], [ 1, %1 ], [ 0, %2 ]
  ret i32 %.018
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #1

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

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

; Function Attrs: nounwind uwtable
declare hidden void @free_stack() local_unnamed_addr #0

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


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