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

; Function Attrs: nounwind uwtable
declare dso_local ptr @tree_new_custom(ptr noundef, ptr noundef, ptr noundef) #0

; 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 uwtable
declare dso_local zeroext i1 @tree_insert(ptr noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @tree_copy_of(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.BinaryTree, ptr %9, i32 0, i32 3
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.BinaryTree, ptr %12, i32 0, i32 4
  %14 = load ptr, ptr %13, align 8, !tbaa !13
  %15 = call ptr @tree_new_custom(ptr noundef %11, ptr noundef %14, ptr noundef null)
  store ptr %15, ptr %4, align 8, !tbaa !5
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %21, label %18

18:                                               ; preds = %1
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.BinaryTree, ptr %19, i32 0, i32 2
  store i32 7, ptr %20, align 8, !tbaa !14
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %120

21:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.BinaryTree, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !15
  store ptr %24, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #2
  store i8 0, ptr %7, align 1, !tbaa !16
  br label %25

25:                                               ; preds = %115, %21
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = icmp ne ptr %26, null
  br i1 %27, label %28, label %116

28:                                               ; preds = %25
  %29 = load i8, ptr %7, align 1, !tbaa !16, !range !18, !noundef !19
  %30 = trunc i8 %29 to i1
  br i1 %30, label %42, label %31

31:                                               ; preds = %28
  br label %32

32:                                               ; preds = %37, %31
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.BinaryTreeNode, ptr %33, i32 0, i32 3
  %35 = load ptr, ptr %34, align 8, !tbaa !20
  %36 = icmp ne ptr %35, null
  br i1 %36, label %37, label %41

37:                                               ; preds = %32
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.BinaryTreeNode, ptr %38, i32 0, i32 3
  %40 = load ptr, ptr %39, align 8, !tbaa !20
  store ptr %40, ptr %6, align 8, !tbaa !5
  br label %32, !llvm.loop !22

41:                                               ; preds = %32
  br label %42

42:                                               ; preds = %41, %28
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.BinaryTree, ptr %43, i32 0, i32 3
  %45 = load ptr, ptr %44, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.BinaryTreeFVal, ptr %45, i32 0, i32 1
  %47 = load ptr, ptr %46, align 8, !tbaa !24
  %48 = icmp ne ptr %47, null
  br i1 %48, label %49, label %59

49:                                               ; preds = %42
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.BinaryTree, ptr %50, i32 0, i32 3
  %52 = load ptr, ptr %51, align 8, !tbaa !9
  %53 = getelementptr inbounds %struct.BinaryTreeFVal, ptr %52, i32 0, i32 1
  %54 = load ptr, ptr %53, align 8, !tbaa !24
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.BinaryTreeNode, ptr %55, i32 0, i32 0
  %57 = load i32, ptr %56, align 8, !tbaa !26
  %58 = call i32 %54(i32 noundef %57)
  store i32 %58, ptr %8, align 4, !tbaa !27
  br label %63

59:                                               ; preds = %42
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.BinaryTreeNode, ptr %60, i32 0, i32 0
  %62 = load i32, ptr %61, align 8, !tbaa !26
  store i32 %62, ptr %8, align 4, !tbaa !27
  br label %63

63:                                               ; preds = %59, %49
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = load i32, ptr %8, align 4, !tbaa !27
  %66 = call zeroext i1 @tree_insert(ptr noundef %64, i32 noundef %65)
  store i8 1, ptr %7, align 1, !tbaa !16
  %67 = load ptr, ptr %6, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.BinaryTreeNode, ptr %67, i32 0, i32 2
  %69 = load ptr, ptr %68, align 8, !tbaa !28
  %70 = icmp ne ptr %69, null
  br i1 %70, label %71, label %75

71:                                               ; preds = %63
  store i8 0, ptr %7, align 1, !tbaa !16
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.BinaryTreeNode, ptr %72, i32 0, i32 2
  %74 = load ptr, ptr %73, align 8, !tbaa !28
  store ptr %74, ptr %6, align 8, !tbaa !5
  br label %112

75:                                               ; preds = %63
  %76 = load ptr, ptr %6, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.BinaryTreeNode, ptr %76, i32 0, i32 4
  %78 = load ptr, ptr %77, align 8, !tbaa !29
  %79 = icmp ne ptr %78, null
  br i1 %79, label %80, label %110

80:                                               ; preds = %75
  br label %81

81:                                               ; preds = %96, %80
  %82 = load ptr, ptr %6, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.BinaryTreeNode, ptr %82, i32 0, i32 4
  %84 = load ptr, ptr %83, align 8, !tbaa !29
  %85 = icmp ne ptr %84, null
  br i1 %85, label %86, label %94

86:                                               ; preds = %81
  %87 = load ptr, ptr %6, align 8, !tbaa !5
  %88 = load ptr, ptr %6, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.BinaryTreeNode, ptr %88, i32 0, i32 4
  %90 = load ptr, ptr %89, align 8, !tbaa !29
  %91 = getelementptr inbounds %struct.BinaryTreeNode, ptr %90, i32 0, i32 2
  %92 = load ptr, ptr %91, align 8, !tbaa !28
  %93 = icmp eq ptr %87, %92
  br label %94

94:                                               ; preds = %86, %81
  %95 = phi i1 [ false, %81 ], [ %93, %86 ]
  br i1 %95, label %96, label %100

96:                                               ; preds = %94
  %97 = load ptr, ptr %6, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.BinaryTreeNode, ptr %97, i32 0, i32 4
  %99 = load ptr, ptr %98, align 8, !tbaa !29
  store ptr %99, ptr %6, align 8, !tbaa !5
  br label %81, !llvm.loop !30

100:                                              ; preds = %94
  %101 = load ptr, ptr %6, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.BinaryTreeNode, ptr %101, i32 0, i32 4
  %103 = load ptr, ptr %102, align 8, !tbaa !29
  %104 = icmp ne ptr %103, null
  br i1 %104, label %106, label %105

105:                                              ; preds = %100
  store i32 3, ptr %5, align 4
  br label %113

106:                                              ; preds = %100
  %107 = load ptr, ptr %6, align 8, !tbaa !5
  %108 = getelementptr inbounds %struct.BinaryTreeNode, ptr %107, i32 0, i32 4
  %109 = load ptr, ptr %108, align 8, !tbaa !29
  store ptr %109, ptr %6, align 8, !tbaa !5
  br label %111

110:                                              ; preds = %75
  store i32 3, ptr %5, align 4
  br label %113

111:                                              ; preds = %106
  br label %112

112:                                              ; preds = %111, %71
  store i32 0, ptr %5, align 4
  br label %113

113:                                              ; preds = %112, %110, %105
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %114 = load i32, ptr %5, align 4
  switch i32 %114, label %122 [
    i32 0, label %115
    i32 3, label %116
  ]

115:                                              ; preds = %113
  br label %25, !llvm.loop !31

116:                                              ; preds = %113, %25
  %117 = load ptr, ptr %3, align 8, !tbaa !5
  %118 = getelementptr inbounds %struct.BinaryTree, ptr %117, i32 0, i32 2
  store i32 0, ptr %118, align 8, !tbaa !14
  %119 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %119, ptr %2, align 8
  store i32 1, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %120

120:                                              ; preds = %116, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %121 = load ptr, ptr %2, align 8
  ret ptr %121

122:                                              ; preds = %113
  unreachable
}

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 }

!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, !6, i64 24}
!10 = !{!"BinaryTree", !6, i64 0, !11, i64 8, !12, i64 16, !6, i64 24, !6, i64 32}
!11 = !{!"long", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !6, i64 32}
!14 = !{!10, !12, i64 16}
!15 = !{!10, !6, i64 0}
!16 = !{!17, !17, i64 0}
!17 = !{!"_Bool", !7, i64 0}
!18 = !{i8 0, i8 2}
!19 = !{}
!20 = !{!21, !6, i64 16}
!21 = !{!"BinaryTreeNode", !12, i64 0, !7, i64 4, !6, i64 8, !6, i64 16, !6, i64 24}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}
!24 = !{!25, !6, i64 8}
!25 = !{!"BinaryTreeFVal", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!26 = !{!21, !12, i64 0}
!27 = !{!12, !12, i64 0}
!28 = !{!21, !6, i64 8}
!29 = !{!21, !6, i64 24}
!30 = distinct !{!30, !23}
!31 = distinct !{!31, !23}


; 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
declare dso_local ptr @tree_new_custom(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local ptr @tree_copy_of(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 24
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 32
  %5 = load ptr, ptr %4, align 8, !tbaa !12
  %6 = tail call ptr @tree_new_custom(ptr noundef %3, ptr noundef %5, ptr noundef null)
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %.critedge.thread45, label %7

7:                                                ; preds = %1
  %8 = load ptr, ptr %0, align 8, !tbaa !13
  %.not3953 = icmp eq ptr %8, null
  br i1 %.not3953, label %.critedge.thread45, label %.lr.ph

.lr.ph:                                           ; preds = %7, %.lr.ph.backedge
  %.02755 = phi i1 [ %21, %.lr.ph.backedge ], [ false, %7 ]
  %.02854 = phi ptr [ %.02854.be, %.lr.ph.backedge ], [ %8, %7 ]
  br i1 %.02755, label %.loopexit, label %.preheader50

.preheader50:                                     ; preds = %.lr.ph, %.preheader50
  %.129 = phi ptr [ %10, %.preheader50 ], [ %.02854, %.lr.ph ]
  %9 = getelementptr inbounds i8, ptr %.129, i64 16
  %10 = load ptr, ptr %9, align 8, !tbaa !14
  %.not40 = icmp eq ptr %10, null
  br i1 %.not40, label %.loopexit, label %.preheader50, !llvm.loop !16

.loopexit:                                        ; preds = %.preheader50, %.lr.ph
  %.230 = phi ptr [ %.02854, %.lr.ph ], [ %.129, %.preheader50 ]
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %11, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !18
  %.not41 = icmp eq ptr %13, null
  %14 = load i32, ptr %.230, align 8, !tbaa !20
  br i1 %.not41, label %17, label %15

15:                                               ; preds = %.loopexit
  %16 = tail call i32 %13(i32 noundef %14) #1
  br label %17

17:                                               ; preds = %.loopexit, %15
  %.0 = phi i32 [ %16, %15 ], [ %14, %.loopexit ]
  %18 = tail call zeroext i1 @tree_insert(ptr noundef nonnull %6, i32 noundef %.0)
  %19 = getelementptr inbounds i8, ptr %.230, i64 8
  %20 = load ptr, ptr %19, align 8, !tbaa !21
  %21 = icmp eq ptr %20, null
  br i1 %21, label %22, label %.lr.ph.backedge

.lr.ph.backedge:                                  ; preds = %27, %17
  %.02854.be = phi ptr [ %20, %17 ], [ %26, %27 ]
  br label %.lr.ph

22:                                               ; preds = %17
  %23 = getelementptr inbounds i8, ptr %.230, i64 24
  %24 = load ptr, ptr %23, align 8, !tbaa !22
  %.not42 = icmp eq ptr %24, null
  br i1 %.not42, label %.critedge.thread45, label %.preheader

.preheader:                                       ; preds = %22, %27
  %.3 = phi ptr [ %26, %27 ], [ %.230, %22 ]
  %25 = getelementptr inbounds i8, ptr %.3, i64 24
  %26 = load ptr, ptr %25, align 8, !tbaa !22
  %.not43 = icmp eq ptr %26, null
  br i1 %.not43, label %.critedge.thread45, label %27

27:                                               ; preds = %.preheader
  %28 = getelementptr inbounds i8, ptr %26, i64 8
  %29 = load ptr, ptr %28, align 8, !tbaa !21
  %30 = icmp eq ptr %.3, %29
  br i1 %30, label %.preheader, label %.lr.ph.backedge, !llvm.loop !23

.critedge.thread45:                               ; preds = %22, %.preheader, %7, %1
  %.sink = phi i32 [ 7, %1 ], [ 0, %7 ], [ 0, %.preheader ], [ 0, %22 ]
  %31 = getelementptr inbounds i8, ptr %0, i64 16
  store i32 %.sink, ptr %31, align 8, !tbaa !24
  ret ptr %6
}

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 24}
!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 32}
!13 = !{!6, !7, i64 0}
!14 = !{!15, !7, i64 16}
!15 = !{!"BinaryTreeNode", !11, i64 0, !8, i64 4, !7, i64 8, !7, i64 16, !7, i64 24}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!19, !7, i64 8}
!19 = !{!"BinaryTreeFVal", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40}
!20 = !{!15, !11, i64 0}
!21 = !{!15, !7, i64 8}
!22 = !{!15, !7, i64 24}
!23 = distinct !{!23, !17}
!24 = !{!6, !11, i64 16}


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