; LLVM IR from hutusi/rethink-c :: src (bstree)

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

; 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
declare dso_local ptr @bs_tree_leftmost_node(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @bs_tree_remove_node(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %6 = load ptr, ptr %4, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct._BSTreeNode, ptr %6, i32 0, i32 2
  %8 = load ptr, ptr %7, align 8, !tbaa !9
  %9 = icmp eq ptr %8, null
  br i1 %9, label %10, label %19

10:                                               ; preds = %2
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct._BSTreeNode, ptr %11, i32 0, i32 3
  %13 = load ptr, ptr %12, align 8, !tbaa !11
  %14 = icmp eq ptr %13, null
  br i1 %14, label %15, label %19

15:                                               ; preds = %10
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = call ptr @bs_tree_replace_node(ptr noundef %16, ptr noundef %17, ptr noundef null)
  br label %87

19:                                               ; preds = %10, %2
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct._BSTreeNode, ptr %20, i32 0, i32 2
  %22 = load ptr, ptr %21, align 8, !tbaa !9
  %23 = icmp eq ptr %22, null
  br i1 %23, label %24, label %31

24:                                               ; preds = %19
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct._BSTreeNode, ptr %27, i32 0, i32 3
  %29 = load ptr, ptr %28, align 8, !tbaa !11
  %30 = call ptr @bs_tree_replace_node(ptr noundef %25, ptr noundef %26, ptr noundef %29)
  br label %86

31:                                               ; preds = %19
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct._BSTreeNode, ptr %32, i32 0, i32 3
  %34 = load ptr, ptr %33, align 8, !tbaa !11
  %35 = icmp eq ptr %34, null
  br i1 %35, label %36, label %43

36:                                               ; preds = %31
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct._BSTreeNode, ptr %39, i32 0, i32 2
  %41 = load ptr, ptr %40, align 8, !tbaa !9
  %42 = call ptr @bs_tree_replace_node(ptr noundef %37, ptr noundef %38, ptr noundef %41)
  br label %85

43:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct._BSTreeNode, ptr %44, i32 0, i32 3
  %46 = load ptr, ptr %45, align 8, !tbaa !11
  %47 = call ptr @bs_tree_leftmost_node(ptr noundef %46)
  store ptr %47, ptr %5, align 8, !tbaa !5
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct._BSTreeNode, ptr %49, i32 0, i32 3
  %51 = load ptr, ptr %50, align 8, !tbaa !11
  %52 = icmp ne ptr %48, %51
  br i1 %52, label %53, label %70

53:                                               ; preds = %43
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct._BSTreeNode, ptr %56, i32 0, i32 3
  %58 = load ptr, ptr %57, align 8, !tbaa !11
  %59 = call ptr @bs_tree_replace_node(ptr noundef %54, ptr noundef %55, ptr noundef %58)
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct._BSTreeNode, ptr %60, i32 0, i32 3
  %62 = load ptr, ptr %61, align 8, !tbaa !11
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct._BSTreeNode, ptr %63, i32 0, i32 3
  store ptr %62, ptr %64, align 8, !tbaa !11
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct._BSTreeNode, ptr %66, i32 0, i32 3
  %68 = load ptr, ptr %67, align 8, !tbaa !11
  %69 = getelementptr inbounds %struct._BSTreeNode, ptr %68, i32 0, i32 1
  store ptr %65, ptr %69, align 8, !tbaa !12
  br label %70

70:                                               ; preds = %53, %43
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = load ptr, ptr %4, align 8, !tbaa !5
  %73 = load ptr, ptr %5, align 8, !tbaa !5
  %74 = call ptr @bs_tree_replace_node(ptr noundef %71, ptr noundef %72, ptr noundef %73)
  %75 = load ptr, ptr %4, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct._BSTreeNode, ptr %75, i32 0, i32 2
  %77 = load ptr, ptr %76, align 8, !tbaa !9
  %78 = load ptr, ptr %5, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct._BSTreeNode, ptr %78, i32 0, i32 2
  store ptr %77, ptr %79, align 8, !tbaa !9
  %80 = load ptr, ptr %5, align 8, !tbaa !5
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct._BSTreeNode, ptr %81, i32 0, i32 2
  %83 = load ptr, ptr %82, align 8, !tbaa !9
  %84 = getelementptr inbounds %struct._BSTreeNode, ptr %83, i32 0, i32 1
  store ptr %80, ptr %84, align 8, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %85

85:                                               ; preds = %70, %36
  br label %86

86:                                               ; preds = %85, %24
  br label %87

87:                                               ; preds = %86, %15
  %88 = load ptr, ptr %3, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct._BSTree, ptr %88, i32 0, i32 2
  %90 = load i32, ptr %89, align 8, !tbaa !13
  %91 = add i32 %90, -1
  store i32 %91, ptr %89, align 8, !tbaa !13
  %92 = load ptr, ptr %4, align 8, !tbaa !5
  ret ptr %92
}

; Function Attrs: nounwind uwtable
declare hidden ptr @bs_tree_replace_node(ptr noundef, 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, !6, i64 16}
!10 = !{!"_BSTreeNode", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!11 = !{!10, !6, i64 24}
!12 = !{!10, !6, i64 8}
!13 = !{!14, !15, i64 16}
!14 = !{!"_BSTree", !6, i64 0, !6, i64 8, !15, i64 16}
!15 = !{!"int", !7, i64 0}


; 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 @bs_tree_leftmost_node(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @bs_tree_remove_node(ptr noundef %0, ptr noundef returned %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %1, i64 16
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = icmp eq ptr %4, null
  %6 = getelementptr inbounds i8, ptr %1, i64 24
  %7 = load ptr, ptr %6, align 8, !tbaa !10
  %8 = icmp eq ptr %7, null
  br i1 %5, label %9, label %14

9:                                                ; preds = %2
  br i1 %8, label %10, label %12

10:                                               ; preds = %9
  %11 = tail call ptr @bs_tree_replace_node(ptr noundef %0, ptr noundef nonnull %1, ptr noundef null)
  br label %31

12:                                               ; preds = %9
  %13 = tail call ptr @bs_tree_replace_node(ptr noundef %0, ptr noundef nonnull %1, ptr noundef nonnull %7)
  br label %31

14:                                               ; preds = %2
  br i1 %8, label %15, label %17

15:                                               ; preds = %14
  %16 = tail call ptr @bs_tree_replace_node(ptr noundef %0, ptr noundef nonnull %1, ptr noundef nonnull %4)
  br label %31

17:                                               ; preds = %14
  %18 = tail call ptr @bs_tree_leftmost_node(ptr noundef nonnull %7)
  %19 = load ptr, ptr %6, align 8, !tbaa !10
  %.not = icmp eq ptr %18, %19
  br i1 %.not, label %26, label %20

20:                                               ; preds = %17
  %21 = getelementptr inbounds i8, ptr %18, i64 24
  %22 = load ptr, ptr %21, align 8, !tbaa !10
  %23 = tail call ptr @bs_tree_replace_node(ptr noundef %0, ptr noundef %18, ptr noundef %22)
  %24 = load ptr, ptr %6, align 8, !tbaa !10
  store ptr %24, ptr %21, align 8, !tbaa !10
  %25 = getelementptr inbounds i8, ptr %24, i64 8
  store ptr %18, ptr %25, align 8, !tbaa !11
  br label %26

26:                                               ; preds = %20, %17
  %27 = tail call ptr @bs_tree_replace_node(ptr noundef %0, ptr noundef nonnull %1, ptr noundef %18)
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %18, i64 16
  store ptr %28, ptr %29, align 8, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %28, i64 8
  store ptr %18, ptr %30, align 8, !tbaa !11
  br label %31

31:                                               ; preds = %12, %26, %15, %10
  %32 = getelementptr inbounds i8, ptr %0, i64 16
  %33 = load i32, ptr %32, align 8, !tbaa !12
  %34 = add i32 %33, -1
  store i32 %34, ptr %32, align 8, !tbaa !12
  ret ptr %1
}

; Function Attrs: nounwind uwtable
declare hidden ptr @bs_tree_replace_node(ptr noundef, 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" }

!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 16}
!6 = !{!"_BSTreeNode", !7, i64 0, !7, 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 = !{!6, !7, i64 24}
!11 = !{!6, !7, i64 8}
!12 = !{!13, !14, i64 16}
!13 = !{!"_BSTree", !7, i64 0, !7, i64 8, !14, i64 16}
!14 = !{!"int", !8, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass