; LLVM IR from wuzhouhui/leetcode :: 32_longest_valid_par (longestValidParentheses)

; 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.node = type { i32, i8, ptr }

@stack = external hidden global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @longestValidParentheses(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
  %7 = alloca i32, align 4
  %8 = alloca i8, align 1
  %9 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = call i64 @strlen(ptr noundef %10) #4
  %12 = icmp ult i64 %11, 2
  br i1 %12, label %13, label %14

13:                                               ; preds = %1
  store i32 0, ptr %2, align 4
  br label %74

14:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  store ptr null, ptr @stack, align 8, !tbaa !5
  store i32 0, ptr %7, align 4, !tbaa !9
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %15

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

22:                                               ; preds = %15
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = load i32, ptr %4, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i8, ptr %23, i64 %25
  %27 = load i8, ptr %26, align 1, !tbaa !11
  %28 = sext i8 %27 to i32
  %29 = icmp eq i32 %28, 40
  br i1 %29, label %30, label %32

30:                                               ; preds = %22
  %31 = load i32, ptr %4, align 4, !tbaa !9
  call void @push(i32 noundef %31, i8 noundef signext 40)
  br label %59

32:                                               ; preds = %22
  call void @peek(ptr noundef %5, ptr noundef %8)
  %33 = load ptr, ptr @stack, align 8, !tbaa !5
  %34 = icmp ne ptr %33, null
  br i1 %34, label %35, label %39

35:                                               ; preds = %32
  %36 = load i8, ptr %8, align 1, !tbaa !11
  %37 = sext i8 %36 to i32
  %38 = icmp eq i32 %37, 41
  br i1 %38, label %39, label %41

39:                                               ; preds = %35, %32
  %40 = load i32, ptr %4, align 4, !tbaa !9
  call void @push(i32 noundef %40, i8 noundef signext 41)
  br label %58

41:                                               ; preds = %35
  store i32 0, ptr %6, align 4, !tbaa !9
  call void @pop(ptr noundef %5, ptr noundef %8)
  %42 = load ptr, ptr @stack, align 8, !tbaa !5
  %43 = icmp ne ptr %42, null
  br i1 %43, label %47, label %44

44:                                               ; preds = %41
  %45 = load i32, ptr %4, align 4, !tbaa !9
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %6, align 4, !tbaa !9
  br label %51

47:                                               ; preds = %41
  call void @peek(ptr noundef %5, ptr noundef %8)
  %48 = load i32, ptr %4, align 4, !tbaa !9
  %49 = load i32, ptr %5, align 4, !tbaa !9
  %50 = sub nsw i32 %48, %49
  store i32 %50, ptr %6, align 4, !tbaa !9
  br label %51

51:                                               ; preds = %47, %44
  %52 = load i32, ptr %6, align 4, !tbaa !9
  %53 = load i32, ptr %7, align 4, !tbaa !9
  %54 = icmp sgt i32 %52, %53
  br i1 %54, label %55, label %57

55:                                               ; preds = %51
  %56 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %56, ptr %7, align 4, !tbaa !9
  br label %57

57:                                               ; preds = %55, %51
  br label %58

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

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

62:                                               ; preds = %15
  br label %63

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

66:                                               ; preds = %63
  %67 = load ptr, ptr @stack, align 8, !tbaa !5
  store ptr %67, ptr %9, align 8, !tbaa !5
  %68 = load ptr, ptr @stack, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.node, ptr %68, i32 0, i32 2
  %70 = load ptr, ptr %69, align 8, !tbaa !14
  store ptr %70, ptr @stack, align 8, !tbaa !5
  %71 = load ptr, ptr %9, align 8, !tbaa !5
  call void @free(ptr noundef %71) #5
  br label %63, !llvm.loop !16

72:                                               ; preds = %63
  %73 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %73, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  br label %74

74:                                               ; preds = %72, %13
  %75 = load i32, ptr %2, align 4
  ret i32 %75
}

; 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(i32 noundef, i8 noundef signext) #0

; Function Attrs: nounwind uwtable
declare hidden void @peek(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @pop(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

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

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 "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 = !{!"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"}
!14 = !{!15, !6, i64 8}
!15 = !{!"node", !10, i64 0, !7, i64 4, !6, i64 8}
!16 = distinct !{!16, !13}


; 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, -2147483648) i32 @longestValidParentheses(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = alloca i32, align 4
  %3 = alloca i8, align 1
  %4 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #5
  %5 = icmp ult i64 %4, 2
  br i1 %5, label %35, label %6

6:                                                ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #6
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %3) #6
  store ptr null, ptr @stack, align 8, !tbaa !5
  br label %7

7:                                                ; preds = %30, %6
  %indvars.iv = phi i64 [ %indvars.iv.next, %30 ], [ 0, %6 ]
  %.017 = phi i32 [ %.1, %30 ], [ 0, %6 ]
  %8 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv
  %9 = load i8, ptr %8, align 1, !tbaa !9
  switch i8 %9, label %13 [
    i8 0, label %.preheader
    i8 40, label %11
  ]

.preheader:                                       ; preds = %7
  %10 = load ptr, ptr @stack, align 8, !tbaa !5
  %.not2224 = icmp eq ptr %10, null
  br i1 %.not2224, label %._crit_edge, label %.lr.ph

11:                                               ; preds = %7
  %12 = trunc nuw nsw i64 %indvars.iv to i32
  call void @push(i32 noundef %12, i8 noundef signext 40)
  br label %30

13:                                               ; preds = %7
  call void @peek(ptr noundef nonnull %2, ptr noundef nonnull %3)
  %14 = load ptr, ptr @stack, align 8, !tbaa !5
  %15 = icmp eq ptr %14, null
  %16 = load i8, ptr %3, align 1
  %17 = icmp eq i8 %16, 41
  %or.cond = select i1 %15, i1 true, i1 %17
  br i1 %or.cond, label %18, label %20

18:                                               ; preds = %13
  %19 = trunc nuw nsw i64 %indvars.iv to i32
  call void @push(i32 noundef %19, i8 noundef signext 41)
  br label %30

20:                                               ; preds = %13
  call void @pop(ptr noundef nonnull %2, ptr noundef nonnull %3)
  %21 = load ptr, ptr @stack, align 8, !tbaa !5
  %.not23 = icmp eq ptr %21, null
  br i1 %.not23, label %22, label %25

22:                                               ; preds = %20
  %23 = trunc i64 %indvars.iv to i32
  %24 = add i32 %23, 1
  br label %29

25:                                               ; preds = %20
  call void @peek(ptr noundef nonnull %2, ptr noundef nonnull %3)
  %26 = load i32, ptr %2, align 4, !tbaa !10
  %27 = trunc nuw nsw i64 %indvars.iv to i32
  %28 = sub nsw i32 %27, %26
  br label %29

29:                                               ; preds = %25, %22
  %.018 = phi i32 [ %28, %25 ], [ %24, %22 ]
  %spec.select = call i32 @llvm.smax.i32(i32 %.018, i32 %.017)
  br label %30

30:                                               ; preds = %29, %18, %11
  %.1 = phi i32 [ %.017, %11 ], [ %.017, %18 ], [ %spec.select, %29 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  br label %7, !llvm.loop !12

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %31 = phi ptr [ %34, %.lr.ph ], [ %10, %.preheader ]
  %32 = getelementptr inbounds i8, ptr %31, i64 8
  %33 = load ptr, ptr %32, align 8, !tbaa !14
  store ptr %33, ptr @stack, align 8, !tbaa !5
  call void @free(ptr noundef nonnull %31) #6
  %34 = load ptr, ptr @stack, align 8, !tbaa !5
  %.not22 = icmp eq ptr %34, null
  br i1 %.not22, label %._crit_edge, label %.lr.ph, !llvm.loop !16

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %3) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #6
  br label %35

35:                                               ; preds = %1, %._crit_edge
  %.0 = phi i32 [ %.017, %._crit_edge ], [ 0, %1 ]
  ret i32 %.0
}

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

; Function Attrs: mustprogress 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(i32 noundef, i8 noundef signext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @peek(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @pop(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #3

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #4

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { nounwind willreturn memory(read) }
attributes #6 = { 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, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !6, i64 8}
!15 = !{!"node", !11, i64 0, !7, i64 4, !6, i64 8}
!16 = distinct !{!16, !13}


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