; LLVM IR from LeoVen/C-Macro-Collections :: examples (dfs)

; 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.BinaryTree = type { ptr, i64, i32, ptr, ptr }
%struct.BinaryTreeFVal = type { ptr, ptr, ptr, ptr, ptr, ptr }
%struct.BinaryTreeNode = type { i32, i8, ptr, ptr, ptr }

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

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @tree_insert(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca i1, align 1
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = call zeroext i1 @tree_empty(ptr noundef %10)
  br i1 %11, label %12, label %26

12:                                               ; preds = %2
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = call ptr @tree_impl_new_node(ptr noundef %13, i32 noundef %14)
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.BinaryTree, ptr %16, i32 0, i32 0
  store ptr %15, ptr %17, align 8, !tbaa !11
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.BinaryTree, ptr %18, i32 0, i32 0
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  %21 = icmp ne ptr %20, null
  br i1 %21, label %25, label %22

22:                                               ; preds = %12
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.BinaryTree, ptr %23, i32 0, i32 2
  store i32 1, ptr %24, align 8, !tbaa !14
  store i1 false, ptr %3, align 1
  br label %142

25:                                               ; preds = %12
  br label %135

26:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.BinaryTree, ptr %27, i32 0, i32 0
  %29 = load ptr, ptr %28, align 8, !tbaa !11
  store ptr %29, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %30, ptr %7, align 8, !tbaa !5
  br label %31

31:                                               ; preds = %71, %26
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = icmp ne ptr %32, null
  br i1 %33, label %34, label %72

34:                                               ; preds = %31
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %35, ptr %7, align 8, !tbaa !5
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.BinaryTree, ptr %36, i32 0, i32 3
  %38 = load ptr, ptr %37, align 8, !tbaa !15
  %39 = getelementptr inbounds %struct.BinaryTreeFVal, ptr %38, i32 0, i32 0
  %40 = load ptr, ptr %39, align 8, !tbaa !16
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.BinaryTreeNode, ptr %41, i32 0, i32 0
  %43 = load i32, ptr %42, align 8, !tbaa !18
  %44 = load i32, ptr %5, align 4, !tbaa !9
  %45 = call i32 %40(i32 noundef %43, i32 noundef %44)
  %46 = icmp sgt i32 %45, 0
  br i1 %46, label %47, label %51

47:                                               ; preds = %34
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.BinaryTreeNode, ptr %48, i32 0, i32 3
  %50 = load ptr, ptr %49, align 8, !tbaa !20
  store ptr %50, ptr %6, align 8, !tbaa !5
  br label %71

51:                                               ; preds = %34
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.BinaryTree, ptr %52, i32 0, i32 3
  %54 = load ptr, ptr %53, align 8, !tbaa !15
  %55 = getelementptr inbounds %struct.BinaryTreeFVal, ptr %54, i32 0, i32 0
  %56 = load ptr, ptr %55, align 8, !tbaa !16
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.BinaryTreeNode, ptr %57, i32 0, i32 0
  %59 = load i32, ptr %58, align 8, !tbaa !18
  %60 = load i32, ptr %5, align 4, !tbaa !9
  %61 = call i32 %56(i32 noundef %59, i32 noundef %60)
  %62 = icmp slt i32 %61, 0
  br i1 %62, label %63, label %67

63:                                               ; preds = %51
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.BinaryTreeNode, ptr %64, i32 0, i32 2
  %66 = load ptr, ptr %65, align 8, !tbaa !21
  store ptr %66, ptr %6, align 8, !tbaa !5
  br label %70

67:                                               ; preds = %51
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.BinaryTree, ptr %68, i32 0, i32 2
  store i32 6, ptr %69, align 8, !tbaa !14
  store i1 false, ptr %3, align 1
  store i32 1, ptr %8, align 4
  br label %132

70:                                               ; preds = %63
  br label %71

71:                                               ; preds = %70, %47
  br label %31, !llvm.loop !22

72:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.BinaryTree, ptr %73, i32 0, i32 3
  %75 = load ptr, ptr %74, align 8, !tbaa !15
  %76 = getelementptr inbounds %struct.BinaryTreeFVal, ptr %75, i32 0, i32 0
  %77 = load ptr, ptr %76, align 8, !tbaa !16
  %78 = load ptr, ptr %7, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.BinaryTreeNode, ptr %78, i32 0, i32 0
  %80 = load i32, ptr %79, align 8, !tbaa !18
  %81 = load i32, ptr %5, align 4, !tbaa !9
  %82 = call i32 %77(i32 noundef %80, i32 noundef %81)
  %83 = icmp sgt i32 %82, 0
  br i1 %83, label %84, label %106

84:                                               ; preds = %72
  %85 = load ptr, ptr %4, align 8, !tbaa !5
  %86 = load i32, ptr %5, align 4, !tbaa !9
  %87 = call ptr @tree_impl_new_node(ptr noundef %85, i32 noundef %86)
  %88 = load ptr, ptr %7, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.BinaryTreeNode, ptr %88, i32 0, i32 3
  store ptr %87, ptr %89, align 8, !tbaa !20
  %90 = load ptr, ptr %7, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.BinaryTreeNode, ptr %90, i32 0, i32 3
  %92 = load ptr, ptr %91, align 8, !tbaa !20
  %93 = icmp ne ptr %92, null
  br i1 %93, label %97, label %94

94:                                               ; preds = %84
  %95 = load ptr, ptr %4, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct.BinaryTree, ptr %95, i32 0, i32 2
  store i32 1, ptr %96, align 8, !tbaa !14
  store i1 false, ptr %3, align 1
  store i32 1, ptr %8, align 4
  br label %131

97:                                               ; preds = %84
  %98 = load ptr, ptr %7, align 8, !tbaa !5
  %99 = load ptr, ptr %7, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.BinaryTreeNode, ptr %99, i32 0, i32 3
  %101 = load ptr, ptr %100, align 8, !tbaa !20
  %102 = getelementptr inbounds %struct.BinaryTreeNode, ptr %101, i32 0, i32 4
  store ptr %98, ptr %102, align 8, !tbaa !24
  %103 = load ptr, ptr %7, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct.BinaryTreeNode, ptr %103, i32 0, i32 3
  %105 = load ptr, ptr %104, align 8, !tbaa !20
  store ptr %105, ptr %9, align 8, !tbaa !5
  br label %128

106:                                              ; preds = %72
  %107 = load ptr, ptr %4, align 8, !tbaa !5
  %108 = load i32, ptr %5, align 4, !tbaa !9
  %109 = call ptr @tree_impl_new_node(ptr noundef %107, i32 noundef %108)
  %110 = load ptr, ptr %7, align 8, !tbaa !5
  %111 = getelementptr inbounds %struct.BinaryTreeNode, ptr %110, i32 0, i32 2
  store ptr %109, ptr %111, align 8, !tbaa !21
  %112 = load ptr, ptr %7, align 8, !tbaa !5
  %113 = getelementptr inbounds %struct.BinaryTreeNode, ptr %112, i32 0, i32 2
  %114 = load ptr, ptr %113, align 8, !tbaa !21
  %115 = icmp ne ptr %114, null
  br i1 %115, label %119, label %116

116:                                              ; preds = %106
  %117 = load ptr, ptr %4, align 8, !tbaa !5
  %118 = getelementptr inbounds %struct.BinaryTree, ptr %117, i32 0, i32 2
  store i32 1, ptr %118, align 8, !tbaa !14
  store i1 false, ptr %3, align 1
  store i32 1, ptr %8, align 4
  br label %131

119:                                              ; preds = %106
  %120 = load ptr, ptr %7, align 8, !tbaa !5
  %121 = load ptr, ptr %7, align 8, !tbaa !5
  %122 = getelementptr inbounds %struct.BinaryTreeNode, ptr %121, i32 0, i32 2
  %123 = load ptr, ptr %122, align 8, !tbaa !21
  %124 = getelementptr inbounds %struct.BinaryTreeNode, ptr %123, i32 0, i32 4
  store ptr %120, ptr %124, align 8, !tbaa !24
  %125 = load ptr, ptr %7, align 8, !tbaa !5
  %126 = getelementptr inbounds %struct.BinaryTreeNode, ptr %125, i32 0, i32 2
  %127 = load ptr, ptr %126, align 8, !tbaa !21
  store ptr %127, ptr %9, align 8, !tbaa !5
  br label %128

128:                                              ; preds = %119, %97
  %129 = load ptr, ptr %4, align 8, !tbaa !5
  %130 = load ptr, ptr %9, align 8, !tbaa !5
  call void @tree_impl_rebalance(ptr noundef %129, ptr noundef %130)
  store i32 0, ptr %8, align 4
  br label %131

131:                                              ; preds = %128, %116, %94
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  br label %132

132:                                              ; preds = %131, %67
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %133 = load i32, ptr %8, align 4
  switch i32 %133, label %144 [
    i32 0, label %134
    i32 1, label %142
  ]

134:                                              ; preds = %132
  br label %135

135:                                              ; preds = %134, %25
  %136 = load ptr, ptr %4, align 8, !tbaa !5
  %137 = getelementptr inbounds %struct.BinaryTree, ptr %136, i32 0, i32 1
  %138 = load i64, ptr %137, align 8, !tbaa !25
  %139 = add i64 %138, 1
  store i64 %139, ptr %137, align 8, !tbaa !25
  %140 = load ptr, ptr %4, align 8, !tbaa !5
  %141 = getelementptr inbounds %struct.BinaryTree, ptr %140, i32 0, i32 2
  store i32 0, ptr %141, align 8, !tbaa !14
  store i1 true, ptr %3, align 1
  br label %142

142:                                              ; preds = %135, %132, %22
  %143 = load i1, ptr %3, align 1
  ret i1 %143

144:                                              ; preds = %132
  unreachable
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @tree_empty(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden ptr @tree_impl_new_node(ptr noundef, i32 noundef) #1

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !6, i64 0}
!12 = !{!"BinaryTree", !6, i64 0, !13, i64 8, !10, i64 16, !6, i64 24, !6, i64 32}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !10, i64 16}
!15 = !{!12, !6, i64 24}
!16 = !{!17, !6, i64 0}
!17 = !{!"BinaryTreeFVal", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!18 = !{!19, !10, i64 0}
!19 = !{!"BinaryTreeNode", !10, i64 0, !7, i64 4, !6, i64 8, !6, i64 16, !6, i64 24}
!20 = !{!19, !6, i64 16}
!21 = !{!19, !6, i64 8}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}
!24 = !{!19, !6, i64 24}
!25 = !{!12, !13, i64 8}


; 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: nounwind uwtable
define dso_local noundef zeroext i1 @tree_insert(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call zeroext i1 @tree_empty(ptr noundef %0)
  br i1 %3, label %4, label %6

4:                                                ; preds = %2
  %5 = tail call ptr @tree_impl_new_node(ptr noundef %0, i32 noundef %1)
  store ptr %5, ptr %0, align 8, !tbaa !5
  %.not53 = icmp eq ptr %5, null
  br i1 %.not53, label %.thread, label %36

6:                                                ; preds = %2
  %7 = load ptr, ptr %0, align 8, !tbaa !5
  %.not57 = icmp eq ptr %7, null
  br i1 %.not57, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %6
  %8 = getelementptr inbounds i8, ptr %0, i64 24
  br label %9

9:                                                ; preds = %.lr.ph, %21
  %.04558 = phi ptr [ %7, %.lr.ph ], [ %.146, %21 ]
  %10 = load ptr, ptr %8, align 8, !tbaa !12
  %11 = load ptr, ptr %10, align 8, !tbaa !13
  %12 = load i32, ptr %.04558, align 8, !tbaa !15
  %13 = tail call i32 %11(i32 noundef %12, i32 noundef %1) #1
  %14 = icmp sgt i32 %13, 0
  br i1 %14, label %21, label %15

15:                                               ; preds = %9
  %16 = load ptr, ptr %8, align 8, !tbaa !12
  %17 = load ptr, ptr %16, align 8, !tbaa !13
  %18 = load i32, ptr %.04558, align 8, !tbaa !15
  %19 = tail call i32 %17(i32 noundef %18, i32 noundef %1) #1
  %20 = icmp slt i32 %19, 0
  br i1 %20, label %21, label %.thread

21:                                               ; preds = %15, %9
  %.sink = phi i64 [ 16, %9 ], [ 8, %15 ]
  %22 = getelementptr inbounds i8, ptr %.04558, i64 %.sink
  %.146 = load ptr, ptr %22, align 8, !tbaa !17
  %.not = icmp eq ptr %.146, null
  br i1 %.not, label %._crit_edge, label %9, !llvm.loop !18

._crit_edge:                                      ; preds = %21, %6
  %.044.lcssa = phi ptr [ null, %6 ], [ %.04558, %21 ]
  %23 = getelementptr inbounds i8, ptr %0, i64 24
  %24 = load ptr, ptr %23, align 8, !tbaa !12
  %25 = load ptr, ptr %24, align 8, !tbaa !13
  %26 = load i32, ptr %.044.lcssa, align 8, !tbaa !15
  %27 = tail call i32 %25(i32 noundef %26, i32 noundef %1) #1
  %28 = icmp sgt i32 %27, 0
  %29 = tail call ptr @tree_impl_new_node(ptr noundef nonnull %0, i32 noundef %1)
  %.not52 = icmp eq ptr %29, null
  br i1 %28, label %30, label %32

30:                                               ; preds = %._crit_edge
  %31 = getelementptr inbounds i8, ptr %.044.lcssa, i64 16
  store ptr %29, ptr %31, align 8, !tbaa !20
  br i1 %.not52, label %.thread, label %34

32:                                               ; preds = %._crit_edge
  %33 = getelementptr inbounds i8, ptr %.044.lcssa, i64 8
  store ptr %29, ptr %33, align 8, !tbaa !21
  br i1 %.not52, label %.thread, label %34

34:                                               ; preds = %32, %30
  %.0.in = phi ptr [ %31, %30 ], [ %33, %32 ]
  %35 = getelementptr inbounds i8, ptr %29, i64 24
  store ptr %.044.lcssa, ptr %35, align 8, !tbaa !22
  %.0 = load ptr, ptr %.0.in, align 8, !tbaa !17
  tail call void @tree_impl_rebalance(ptr noundef nonnull %0, ptr noundef %.0)
  br label %36

36:                                               ; preds = %34, %4
  %37 = getelementptr inbounds i8, ptr %0, i64 8
  %38 = load i64, ptr %37, align 8, !tbaa !23
  %39 = add i64 %38, 1
  store i64 %39, ptr %37, align 8, !tbaa !23
  br label %.thread

.thread:                                          ; preds = %15, %32, %30, %4, %36
  %.sink62 = phi i32 [ 0, %36 ], [ 1, %4 ], [ 1, %30 ], [ 1, %32 ], [ 6, %15 ]
  %.2 = phi i1 [ true, %36 ], [ false, %4 ], [ false, %30 ], [ false, %32 ], [ false, %15 ]
  %40 = getelementptr inbounds i8, ptr %0, i64 16
  store i32 %.sink62, ptr %40, align 8, !tbaa !24
  ret i1 %.2
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @tree_empty(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare hidden void @tree_impl_rebalance(ptr noundef, ptr noundef) 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 = { 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, !7, i64 0}
!6 = !{!"BinaryTree", !7, i64 0, !10, i64 8, !11, i64 16, !7, i64 24, !7, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!6, !7, i64 24}
!13 = !{!14, !7, i64 0}
!14 = !{!"BinaryTreeFVal", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40}
!15 = !{!16, !11, i64 0}
!16 = !{!"BinaryTreeNode", !11, i64 0, !8, i64 4, !7, i64 8, !7, i64 16, !7, i64 24}
!17 = !{!7, !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!16, !7, i64 16}
!21 = !{!16, !7, i64 8}
!22 = !{!16, !7, i64 24}
!23 = !{!6, !10, i64 8}
!24 = !{!6, !11, i64 16}


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