; LLVM IR from wuzhouhui/leetcode :: 99_recover_bst (recoverTree)

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

@trave = external hidden global ptr, align 8
@p = external hidden global i32, align 4

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

8:                                                ; preds = %1
  br label %79

9:                                                ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store ptr null, ptr @trave, align 8, !tbaa !5
  store ptr null, ptr %5, align 8, !tbaa !5
  store ptr null, ptr %4, align 8, !tbaa !5
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  call void @in_order(ptr noundef %10)
  store i32 1, ptr %3, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %55, %9
  %12 = load i32, ptr %3, align 4, !tbaa !9
  %13 = load i32, ptr @p, align 4, !tbaa !9
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %15, label %58

15:                                               ; preds = %11
  %16 = load ptr, ptr @trave, align 8, !tbaa !5
  %17 = load i32, ptr %3, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds ptr, ptr %16, i64 %18
  %20 = load ptr, ptr %19, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.TreeNode, ptr %20, i32 0, i32 0
  %22 = load i32, ptr %21, align 8, !tbaa !11
  %23 = load ptr, ptr @trave, align 8, !tbaa !5
  %24 = load i32, ptr %3, align 4, !tbaa !9
  %25 = sub nsw i32 %24, 1
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds ptr, ptr %23, i64 %26
  %28 = load ptr, ptr %27, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.TreeNode, ptr %28, i32 0, i32 0
  %30 = load i32, ptr %29, align 8, !tbaa !11
  %31 = icmp slt i32 %22, %30
  br i1 %31, label %32, label %54

32:                                               ; preds = %15
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = icmp ne ptr %33, null
  br i1 %34, label %47, label %35

35:                                               ; preds = %32
  %36 = load ptr, ptr @trave, align 8, !tbaa !5
  %37 = load i32, ptr %3, align 4, !tbaa !9
  %38 = sub nsw i32 %37, 1
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds ptr, ptr %36, i64 %39
  %41 = load ptr, ptr %40, align 8, !tbaa !5
  store ptr %41, ptr %4, align 8, !tbaa !5
  %42 = load ptr, ptr @trave, align 8, !tbaa !5
  %43 = load i32, ptr %3, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds ptr, ptr %42, i64 %44
  %46 = load ptr, ptr %45, align 8, !tbaa !5
  store ptr %46, ptr %5, align 8, !tbaa !5
  br label %53

47:                                               ; preds = %32
  %48 = load ptr, ptr @trave, align 8, !tbaa !5
  %49 = load i32, ptr %3, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds ptr, ptr %48, i64 %50
  %52 = load ptr, ptr %51, align 8, !tbaa !5
  store ptr %52, ptr %5, align 8, !tbaa !5
  br label %58

53:                                               ; preds = %35
  br label %54

54:                                               ; preds = %53, %15
  br label %55

55:                                               ; preds = %54
  %56 = load i32, ptr %3, align 4, !tbaa !9
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %3, align 4, !tbaa !9
  br label %11, !llvm.loop !13

58:                                               ; preds = %47, %11
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = icmp ne ptr %59, null
  br i1 %60, label %61, label %73

61:                                               ; preds = %58
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.TreeNode, ptr %62, i32 0, i32 0
  %64 = load i32, ptr %63, align 8, !tbaa !11
  store i32 %64, ptr %3, align 4, !tbaa !9
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.TreeNode, ptr %65, i32 0, i32 0
  %67 = load i32, ptr %66, align 8, !tbaa !11
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.TreeNode, ptr %68, i32 0, i32 0
  store i32 %67, ptr %69, align 8, !tbaa !11
  %70 = load i32, ptr %3, align 4, !tbaa !9
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.TreeNode, ptr %71, i32 0, i32 0
  store i32 %70, ptr %72, align 8, !tbaa !11
  br label %73

73:                                               ; preds = %61, %58
  %74 = load ptr, ptr @trave, align 8, !tbaa !5
  %75 = icmp ne ptr %74, null
  br i1 %75, label %76, label %78

76:                                               ; preds = %73
  %77 = load ptr, ptr @trave, align 8, !tbaa !5
  call void @free(ptr noundef %77) #3
  br label %78

78:                                               ; preds = %76, %73
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %79

79:                                               ; preds = %78, %8
  ret void
}

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

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

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

; 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 "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 = { 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 = !{!12, !10, i64 0}
!12 = !{!"TreeNode", !10, i64 0, !6, i64 8, !6, i64 16}
!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"

@trave = external hidden local_unnamed_addr global ptr, align 8
@p = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @recoverTree(ptr noundef %0) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %20, label %2

2:                                                ; preds = %1
  store ptr null, ptr @trave, align 8, !tbaa !5
  tail call void @in_order(ptr noundef nonnull %0)
  %3 = load i32, ptr @p, align 4, !tbaa !9
  %4 = icmp sgt i32 %3, 1
  br i1 %4, label %.lr.ph, label %._crit_edge.thread

.lr.ph:                                           ; preds = %2
  %5 = load ptr, ptr @trave, align 8, !tbaa !5
  %wide.trip.count = zext nneg i32 %3 to i64
  br label %6

6:                                                ; preds = %.lr.ph, %15
  %indvars.iv = phi i64 [ 1, %.lr.ph ], [ %indvars.iv.next, %15 ]
  %.031 = phi ptr [ null, %.lr.ph ], [ %.1, %15 ]
  %.01530 = phi ptr [ null, %.lr.ph ], [ %.116, %15 ]
  %7 = getelementptr inbounds ptr, ptr %5, i64 %indvars.iv
  %8 = load ptr, ptr %7, align 8, !tbaa !5
  %9 = load i32, ptr %8, align 8, !tbaa !11
  %10 = getelementptr i8, ptr %7, i64 -8
  %11 = load ptr, ptr %10, align 8, !tbaa !5
  %12 = load i32, ptr %11, align 8, !tbaa !11
  %13 = icmp slt i32 %9, %12
  br i1 %13, label %14, label %15

14:                                               ; preds = %6
  %.not20 = icmp eq ptr %.01530, null
  br i1 %.not20, label %15, label %.thread

15:                                               ; preds = %14, %6
  %.116 = phi ptr [ %.01530, %6 ], [ %11, %14 ]
  %.1 = phi ptr [ %.031, %6 ], [ %8, %14 ]
  %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 %._crit_edge, label %6, !llvm.loop !13

._crit_edge:                                      ; preds = %15
  %.not21 = icmp eq ptr %.116, null
  br i1 %.not21, label %._crit_edge.thread, label %._crit_edge..thread_crit_edge

._crit_edge..thread_crit_edge:                    ; preds = %._crit_edge
  %.pre = load i32, ptr %.1, align 8, !tbaa !11
  br label %.thread

.thread:                                          ; preds = %14, %._crit_edge..thread_crit_edge
  %16 = phi i32 [ %.pre, %._crit_edge..thread_crit_edge ], [ %9, %14 ]
  %.01528 = phi ptr [ %.116, %._crit_edge..thread_crit_edge ], [ %.01530, %14 ]
  %.225 = phi ptr [ %.1, %._crit_edge..thread_crit_edge ], [ %8, %14 ]
  %17 = load i32, ptr %.01528, align 8, !tbaa !11
  store i32 %16, ptr %.01528, align 8, !tbaa !11
  store i32 %17, ptr %.225, align 8, !tbaa !11
  br label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %2, %.thread, %._crit_edge
  %18 = load ptr, ptr @trave, align 8, !tbaa !5
  %.not22 = icmp eq ptr %18, null
  br i1 %.not22, label %20, label %19

19:                                               ; preds = %._crit_edge.thread
  tail call void @free(ptr noundef nonnull %18) #2
  br label %20

20:                                               ; preds = %._crit_edge.thread, %19, %1
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @in_order(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 #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 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 #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 = !{!12, !10, i64 0}
!12 = !{!"TreeNode", !10, i64 0, !6, i64 8, !6, i64 16}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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