; LLVM IR from DimitrisJim/leetcode_solutions :: C/Algorithms (654)

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

; Function Attrs: nounwind uwtable
define dso_local ptr @build_tree(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = load i32, ptr %5, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds i32, ptr %14, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !9
  store i32 %18, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %19 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %19, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %9, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %40, %3
  %23 = load i32, ptr %9, align 4, !tbaa !9
  %24 = load i32, ptr %6, align 4, !tbaa !9
  %25 = icmp slt i32 %23, %24
  br i1 %25, label %27, label %26

26:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %43

27:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i32, ptr %28, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !9
  store i32 %32, ptr %10, align 4, !tbaa !9
  %33 = load i32, ptr %10, align 4, !tbaa !9
  %34 = load i32, ptr %7, align 4, !tbaa !9
  %35 = icmp sgt i32 %33, %34
  br i1 %35, label %36, label %39

36:                                               ; preds = %27
  %37 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %37, ptr %7, align 4, !tbaa !9
  %38 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %38, ptr %8, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %36, %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %40

40:                                               ; preds = %39
  %41 = load i32, ptr %9, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %9, align 4, !tbaa !9
  br label %22, !llvm.loop !11

43:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  store ptr null, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  store ptr null, ptr %12, align 8, !tbaa !5
  %44 = load i32, ptr %8, align 4, !tbaa !9
  %45 = load i32, ptr %5, align 4, !tbaa !9
  %46 = icmp sgt i32 %44, %45
  br i1 %46, label %47, label %52

47:                                               ; preds = %43
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = load i32, ptr %5, align 4, !tbaa !9
  %50 = load i32, ptr %8, align 4, !tbaa !9
  %51 = call ptr @build_tree(ptr noundef %48, i32 noundef %49, i32 noundef %50)
  store ptr %51, ptr %11, align 8, !tbaa !5
  br label %52

52:                                               ; preds = %47, %43
  %53 = load i32, ptr %6, align 4, !tbaa !9
  %54 = load i32, ptr %8, align 4, !tbaa !9
  %55 = add nsw i32 %54, 1
  %56 = icmp sgt i32 %53, %55
  br i1 %56, label %57, label %63

57:                                               ; preds = %52
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = load i32, ptr %8, align 4, !tbaa !9
  %60 = add nsw i32 %59, 1
  %61 = load i32, ptr %6, align 4, !tbaa !9
  %62 = call ptr @build_tree(ptr noundef %58, i32 noundef %60, i32 noundef %61)
  store ptr %62, ptr %12, align 8, !tbaa !5
  br label %63

63:                                               ; preds = %57, %52
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %64 = call noalias ptr @malloc(i64 noundef 24) #4
  store ptr %64, ptr %13, align 8, !tbaa !5
  %65 = load i32, ptr %7, align 4, !tbaa !9
  %66 = load ptr, ptr %13, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.TreeNode, ptr %66, i32 0, i32 0
  store i32 %65, ptr %67, align 8, !tbaa !13
  %68 = load ptr, ptr %11, align 8, !tbaa !5
  %69 = load ptr, ptr %13, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.TreeNode, ptr %69, i32 0, i32 1
  store ptr %68, ptr %70, align 8, !tbaa !15
  %71 = load ptr, ptr %12, align 8, !tbaa !5
  %72 = load ptr, ptr %13, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.TreeNode, ptr %72, i32 0, i32 2
  store ptr %71, ptr %73, align 8, !tbaa !16
  %74 = load ptr, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret ptr %74
}

; 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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind allocsize(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 #3 = { nounwind }
attributes #4 = { nounwind allocsize(0) }

!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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !10, i64 0}
!14 = !{!"TreeNode", !10, i64 0, !6, i64 8, !6, i64 16}
!15 = !{!14, !6, i64 8}
!16 = !{!14, !6, i64 16}


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

; Function Attrs: nofree nounwind memory(write, argmem: read, inaccessiblemem: readwrite) uwtable
define dso_local noalias noundef ptr @build_tree(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = sext i32 %1 to i64
  %5 = getelementptr inbounds i32, ptr %0, i64 %4
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %.03339 = add nsw i32 %1, 1
  %7 = icmp slt i32 %.03339, %2
  br i1 %7, label %.lr.ph.preheader, label %._crit_edge.thread

.lr.ph.preheader:                                 ; preds = %3
  %8 = add nsw i64 %4, 1
  %9 = xor i32 %1, -1
  %10 = add i32 %9, %2
  %11 = add i32 %2, -2
  %12 = sub i32 %11, %1
  %xtraiter = and i32 %10, 3
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader, %.lr.ph.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %.lr.ph.prol ], [ %8, %.lr.ph.preheader ]
  %.041.prol = phi i32 [ %spec.select38.prol, %.lr.ph.prol ], [ %6, %.lr.ph.preheader ]
  %.03140.prol = phi i32 [ %spec.select.prol, %.lr.ph.prol ], [ %1, %.lr.ph.preheader ]
  %prol.iter = phi i32 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %13 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.prol
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = icmp sgt i32 %14, %.041.prol
  %16 = trunc nsw i64 %indvars.iv.prol to i32
  %spec.select.prol = select i1 %15, i32 %16, i32 %.03140.prol
  %spec.select38.prol = tail call i32 @llvm.smax.i32(i32 %14, i32 %.041.prol)
  %indvars.iv.next.prol = add nsw i64 %indvars.iv.prol, 1
  %prol.iter.next = add i32 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i32 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !9

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %spec.select.lcssa.unr = phi i32 [ poison, %.lr.ph.preheader ], [ %spec.select.prol, %.lr.ph.prol ]
  %spec.select38.lcssa.unr = phi i32 [ poison, %.lr.ph.preheader ], [ %spec.select38.prol, %.lr.ph.prol ]
  %indvars.iv.unr = phi i64 [ %8, %.lr.ph.preheader ], [ %indvars.iv.next.prol, %.lr.ph.prol ]
  %.041.unr = phi i32 [ %6, %.lr.ph.preheader ], [ %spec.select38.prol, %.lr.ph.prol ]
  %.03140.unr = phi i32 [ %1, %.lr.ph.preheader ], [ %spec.select.prol, %.lr.ph.prol ]
  %17 = icmp ult i32 %12, 3
  br i1 %17, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph.prol.loopexit
  %spec.select.lcssa = phi i32 [ %spec.select.lcssa.unr, %.lr.ph.prol.loopexit ], [ %spec.select.3, %.lr.ph ]
  %spec.select38.lcssa = phi i32 [ %spec.select38.lcssa.unr, %.lr.ph.prol.loopexit ], [ %spec.select38.3, %.lr.ph ]
  %18 = icmp sgt i32 %spec.select.lcssa, %1
  br i1 %18, label %35, label %._crit_edge.thread

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %.lr.ph ], [ %indvars.iv.unr, %.lr.ph.prol.loopexit ]
  %.041 = phi i32 [ %spec.select38.3, %.lr.ph ], [ %.041.unr, %.lr.ph.prol.loopexit ]
  %.03140 = phi i32 [ %spec.select.3, %.lr.ph ], [ %.03140.unr, %.lr.ph.prol.loopexit ]
  %19 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = icmp sgt i32 %20, %.041
  %22 = trunc nsw i64 %indvars.iv to i32
  %spec.select = select i1 %21, i32 %22, i32 %.03140
  %spec.select38 = tail call i32 @llvm.smax.i32(i32 %20, i32 %.041)
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %23 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = icmp sgt i32 %24, %spec.select38
  %26 = trunc nsw i64 %indvars.iv.next to i32
  %spec.select.1 = select i1 %25, i32 %26, i32 %spec.select
  %spec.select38.1 = tail call i32 @llvm.smax.i32(i32 %24, i32 %spec.select38)
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, 2
  %27 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next.1
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = icmp sgt i32 %28, %spec.select38.1
  %30 = trunc nsw i64 %indvars.iv.next.1 to i32
  %spec.select.2 = select i1 %29, i32 %30, i32 %spec.select.1
  %spec.select38.2 = tail call i32 @llvm.smax.i32(i32 %28, i32 %spec.select38.1)
  %indvars.iv.next.2 = add nsw i64 %indvars.iv, 3
  %31 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next.2
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = icmp sgt i32 %32, %spec.select38.2
  %34 = trunc nsw i64 %indvars.iv.next.2 to i32
  %spec.select.3 = select i1 %33, i32 %34, i32 %spec.select.2
  %spec.select38.3 = tail call i32 @llvm.smax.i32(i32 %32, i32 %spec.select38.2)
  %indvars.iv.next.3 = add nsw i64 %indvars.iv, 4
  %lftr.wideiv.3 = trunc i64 %indvars.iv.next.3 to i32
  %exitcond.not.3 = icmp eq i32 %lftr.wideiv.3, %2
  br i1 %exitcond.not.3, label %._crit_edge, label %.lr.ph, !llvm.loop !11

35:                                               ; preds = %._crit_edge
  %36 = tail call ptr @build_tree(ptr noundef nonnull %0, i32 noundef %1, i32 noundef %spec.select.lcssa)
  br label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %3, %35, %._crit_edge
  %.0.lcssa48 = phi i32 [ %spec.select38.lcssa, %35 ], [ %spec.select38.lcssa, %._crit_edge ], [ %6, %3 ]
  %.031.lcssa47 = phi i32 [ %spec.select.lcssa, %35 ], [ %spec.select.lcssa, %._crit_edge ], [ %1, %3 ]
  %.030 = phi ptr [ %36, %35 ], [ null, %._crit_edge ], [ null, %3 ]
  %37 = add nsw i32 %.031.lcssa47, 1
  %38 = icmp slt i32 %37, %2
  br i1 %38, label %39, label %41

39:                                               ; preds = %._crit_edge.thread
  %40 = tail call ptr @build_tree(ptr noundef nonnull %0, i32 noundef %37, i32 noundef %2)
  br label %41

41:                                               ; preds = %39, %._crit_edge.thread
  %.029 = phi ptr [ %40, %39 ], [ null, %._crit_edge.thread ]
  %42 = tail call noalias dereferenceable_or_null(24) ptr @malloc(i64 noundef 24) #3
  store i32 %.0.lcssa48, ptr %42, align 8, !tbaa !13
  %43 = getelementptr inbounds i8, ptr %42, i64 8
  store ptr %.030, ptr %43, align 8, !tbaa !16
  %44 = getelementptr inbounds i8, ptr %42, i64 16
  store ptr %.029, ptr %44, align 8, !tbaa !17
  ret ptr %42
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

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

attributes #0 = { nofree nounwind memory(write, argmem: read, inaccessiblemem: readwrite) 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 allockind("alloc,uninitialized") allocsize(0) memory(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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { nounwind allocsize(0) }

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.unroll.disable"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !6, i64 0}
!14 = !{!"TreeNode", !6, i64 0, !15, i64 8, !15, i64 16}
!15 = !{!"any pointer", !7, i64 0}
!16 = !{!14, !15, i64 8}
!17 = !{!14, !15, i64 16}


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