; LLVM IR from MostafaTwfiq/C-DataStructures-And-Algorithms :: DataStructure/Trees/Sources (BinaryTree)

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

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

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

9:                                                ; preds = %3
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.BinaryTree, ptr %10, i32 0, i32 1
  %12 = load i32, ptr %11, align 8, !tbaa !9
  %13 = add nsw i32 %12, 1
  store i32 %13, ptr %11, align 8, !tbaa !9
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = call ptr @createBinaryTreeNode(ptr noundef %14)
  store ptr %15, ptr %5, align 8, !tbaa !5
  br label %66

16:                                               ; preds = %3
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.BinaryTree, ptr %17, i32 0, i32 2
  %19 = load ptr, ptr %18, align 8, !tbaa !12
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.BinaryTreeNode, ptr %21, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8, !tbaa !13
  %24 = call i32 %19(ptr noundef %20, ptr noundef %23)
  %25 = icmp sgt i32 %24, 0
  br i1 %25, label %36, label %26

26:                                               ; preds = %16
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.BinaryTree, ptr %27, i32 0, i32 2
  %29 = load ptr, ptr %28, align 8, !tbaa !12
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.BinaryTreeNode, ptr %31, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !13
  %34 = call i32 %29(ptr noundef %30, ptr noundef %33)
  %35 = icmp eq i32 %34, 0
  br i1 %35, label %36, label %45

36:                                               ; preds = %26, %16
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.BinaryTreeNode, ptr %38, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !15
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = call ptr @binaryTreeInsertR(ptr noundef %37, ptr noundef %40, ptr noundef %41)
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.BinaryTreeNode, ptr %43, i32 0, i32 1
  store ptr %42, ptr %44, align 8, !tbaa !15
  br label %65

45:                                               ; preds = %26
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.BinaryTree, ptr %46, i32 0, i32 2
  %48 = load ptr, ptr %47, align 8, !tbaa !12
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.BinaryTreeNode, ptr %50, i32 0, i32 0
  %52 = load ptr, ptr %51, align 8, !tbaa !13
  %53 = call i32 %48(ptr noundef %49, ptr noundef %52)
  %54 = icmp slt i32 %53, 0
  br i1 %54, label %55, label %64

55:                                               ; preds = %45
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.BinaryTreeNode, ptr %57, i32 0, i32 2
  %59 = load ptr, ptr %58, align 8, !tbaa !16
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = call ptr @binaryTreeInsertR(ptr noundef %56, ptr noundef %59, ptr noundef %60)
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.BinaryTreeNode, ptr %62, i32 0, i32 2
  store ptr %61, ptr %63, align 8, !tbaa !16
  br label %64

64:                                               ; preds = %55, %45
  br label %65

65:                                               ; preds = %64, %36
  br label %66

66:                                               ; preds = %65, %9
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  ret ptr %67
}

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

!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, !11, i64 8}
!10 = !{!"BinaryTree", !6, i64 0, !11, i64 8, !6, i64 16, !6, i64 24}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 16}
!13 = !{!14, !6, i64 0}
!14 = !{!"BinaryTreeNode", !6, 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: nounwind uwtable
declare dso_local ptr @createBinaryTreeNode(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @binaryTreeInsertR(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq ptr %1, null
  br i1 %4, label %5, label %10

5:                                                ; preds = %3
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = load i32, ptr %6, align 8, !tbaa !5
  %8 = add nsw i32 %7, 1
  store i32 %8, ptr %6, align 8, !tbaa !5
  %9 = tail call ptr @createBinaryTreeNode(ptr noundef %2)
  br label %common.ret22

10:                                               ; preds = %3
  %11 = getelementptr inbounds i8, ptr %0, i64 16
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %13 = load ptr, ptr %1, align 8, !tbaa !12
  %14 = tail call i32 %12(ptr noundef %2, ptr noundef %13) #1
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %21, label %16

16:                                               ; preds = %10
  %17 = load ptr, ptr %11, align 8, !tbaa !11
  %18 = load ptr, ptr %1, align 8, !tbaa !12
  %19 = tail call i32 %17(ptr noundef %2, ptr noundef %18) #1
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %25

common.ret22:                                     ; preds = %25, %5, %30, %21
  %common.ret22.op = phi ptr [ %1, %21 ], [ %1, %30 ], [ %9, %5 ], [ %1, %25 ]
  ret ptr %common.ret22.op

21:                                               ; preds = %16, %10
  %22 = getelementptr inbounds i8, ptr %1, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !14
  %24 = tail call ptr @binaryTreeInsertR(ptr noundef nonnull %0, ptr noundef %23, ptr noundef %2)
  store ptr %24, ptr %22, align 8, !tbaa !14
  br label %common.ret22

25:                                               ; preds = %16
  %26 = load ptr, ptr %11, align 8, !tbaa !11
  %27 = load ptr, ptr %1, align 8, !tbaa !12
  %28 = tail call i32 %26(ptr noundef %2, ptr noundef %27) #1
  %29 = icmp slt i32 %28, 0
  br i1 %29, label %30, label %common.ret22

30:                                               ; preds = %25
  %31 = getelementptr inbounds i8, ptr %1, i64 16
  %32 = load ptr, ptr %31, align 8, !tbaa !15
  %33 = tail call ptr @binaryTreeInsertR(ptr noundef nonnull %0, ptr noundef %32, ptr noundef %2)
  store ptr %33, ptr %31, align 8, !tbaa !15
  br label %common.ret22
}

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, !10, i64 8}
!6 = !{!"BinaryTree", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 16}
!12 = !{!13, !7, i64 0}
!13 = !{!"BinaryTreeNode", !7, i64 0, !7, i64 8, !7, i64 16}
!14 = !{!13, !7, i64 8}
!15 = !{!13, !7, i64 16}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass