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

@.str = external hidden unnamed_addr constant [2 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [63 x i8], align 1
@__PRETTY_FUNCTION__.bs_tree_insert = external hidden unnamed_addr constant [50 x i8], align 1

; 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 ptr @bs_tree_insert(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct._BSTree, ptr %11, i32 0, i32 0
  %13 = load ptr, ptr %12, align 8, !tbaa !9
  store ptr %13, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store ptr null, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !12
  br label %14

14:                                               ; preds = %43, %2
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %44

17:                                               ; preds = %14
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct._BSTree, ptr %18, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct._BSTreeNode, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !14
  %25 = call i32 %20(ptr noundef %21, ptr noundef %24)
  store i32 %25, ptr %8, align 4, !tbaa !12
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %26, ptr %7, align 8, !tbaa !5
  %27 = load i32, ptr %8, align 4, !tbaa !12
  %28 = icmp slt i32 %27, 0
  br i1 %28, label %29, label %33

29:                                               ; preds = %17
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct._BSTreeNode, ptr %30, i32 0, i32 2
  %32 = load ptr, ptr %31, align 8, !tbaa !16
  store ptr %32, ptr %6, align 8, !tbaa !5
  br label %43

33:                                               ; preds = %17
  %34 = load i32, ptr %8, align 4, !tbaa !12
  %35 = icmp sgt i32 %34, 0
  br i1 %35, label %36, label %40

36:                                               ; preds = %33
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct._BSTreeNode, ptr %37, i32 0, i32 3
  %39 = load ptr, ptr %38, align 8, !tbaa !17
  store ptr %39, ptr %6, align 8, !tbaa !5
  br label %42

40:                                               ; preds = %33
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %41, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %79

42:                                               ; preds = %36
  br label %43

43:                                               ; preds = %42, %29
  br label %14, !llvm.loop !18

44:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = call ptr @bs_tree_node_new(ptr noundef %45)
  store ptr %46, ptr %10, align 8, !tbaa !5
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = load ptr, ptr %10, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct._BSTreeNode, ptr %48, i32 0, i32 1
  store ptr %47, ptr %49, align 8, !tbaa !20
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct._BSTree, ptr %50, i32 0, i32 2
  %52 = load i32, ptr %51, align 8, !tbaa !21
  %53 = add i32 %52, 1
  store i32 %53, ptr %51, align 8, !tbaa !21
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  %55 = icmp eq ptr %54, null
  br i1 %55, label %56, label %60

56:                                               ; preds = %44
  %57 = load ptr, ptr %10, align 8, !tbaa !5
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct._BSTree, ptr %58, i32 0, i32 0
  store ptr %57, ptr %59, align 8, !tbaa !9
  br label %77

60:                                               ; preds = %44
  %61 = load i32, ptr %8, align 4, !tbaa !12
  %62 = icmp slt i32 %61, 0
  br i1 %62, label %63, label %67

63:                                               ; preds = %60
  %64 = load ptr, ptr %10, align 8, !tbaa !5
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct._BSTreeNode, ptr %65, i32 0, i32 2
  store ptr %64, ptr %66, align 8, !tbaa !16
  br label %76

67:                                               ; preds = %60
  %68 = load i32, ptr %8, align 4, !tbaa !12
  %69 = icmp sgt i32 %68, 0
  br i1 %69, label %70, label %74

70:                                               ; preds = %67
  %71 = load ptr, ptr %10, align 8, !tbaa !5
  %72 = load ptr, ptr %7, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct._BSTreeNode, ptr %72, i32 0, i32 3
  store ptr %71, ptr %73, align 8, !tbaa !17
  br label %75

74:                                               ; preds = %67
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 79, ptr noundef @__PRETTY_FUNCTION__.bs_tree_insert) #4
  unreachable

75:                                               ; preds = %70
  br label %76

76:                                               ; preds = %75, %63
  br label %77

77:                                               ; preds = %76, %56
  %78 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %78, ptr %3, align 8
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %79

79:                                               ; preds = %77, %40
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %80 = load ptr, ptr %3, align 8
  ret ptr %80
}

; Function Attrs: nounwind uwtable
declare hidden ptr @bs_tree_node_new(ptr noundef) #1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #2

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 = { noreturn nounwind "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 = { noreturn 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 0}
!10 = !{!"_BSTree", !6, i64 0, !6, i64 8, !11, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!10, !6, i64 8}
!14 = !{!15, !6, i64 0}
!15 = !{!"_BSTreeNode", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!16 = !{!15, !6, i64 16}
!17 = !{!15, !6, i64 24}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!15, !6, i64 8}
!21 = !{!10, !11, 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"

@.str = external hidden unnamed_addr constant [2 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [63 x i8], align 1
@__PRETTY_FUNCTION__.bs_tree_insert = external hidden unnamed_addr constant [50 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @bs_tree_insert(ptr nocapture noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %.02734 = load ptr, ptr %0, align 8, !tbaa !5
  %.not35 = icmp eq ptr %.02734, null
  br i1 %.not35, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  br label %4

4:                                                ; preds = %.lr.ph, %10
  %.02736 = phi ptr [ %.02734, %.lr.ph ], [ %.027, %10 ]
  %5 = load ptr, ptr %3, align 8, !tbaa !9
  %6 = load ptr, ptr %.02736, align 8, !tbaa !12
  %7 = tail call i32 %5(ptr noundef %1, ptr noundef %6) #2
  %8 = icmp slt i32 %7, 0
  br i1 %8, label %10, label %9

9:                                                ; preds = %4
  %.not30 = icmp eq i32 %7, 0
  br i1 %.not30, label %.loopexit, label %10

10:                                               ; preds = %9, %4
  %.sink = phi i64 [ 16, %4 ], [ 24, %9 ]
  %11 = getelementptr inbounds i8, ptr %.02736, i64 %.sink
  %.027 = load ptr, ptr %11, align 8, !tbaa !5
  %.not = icmp eq ptr %.027, null
  br i1 %.not, label %._crit_edge, label %4, !llvm.loop !14

._crit_edge:                                      ; preds = %10, %2
  %.026.lcssa = phi ptr [ null, %2 ], [ %.02736, %10 ]
  %.025.lcssa = phi i32 [ 0, %2 ], [ %7, %10 ]
  %12 = tail call ptr @bs_tree_node_new(ptr noundef %1)
  %13 = getelementptr inbounds i8, ptr %12, i64 8
  store ptr %.026.lcssa, ptr %13, align 8, !tbaa !16
  %14 = getelementptr inbounds i8, ptr %0, i64 16
  %15 = load i32, ptr %14, align 8, !tbaa !17
  %16 = add i32 %15, 1
  store i32 %16, ptr %14, align 8, !tbaa !17
  %17 = icmp eq ptr %.026.lcssa, null
  br i1 %17, label %.loopexit.sink.split, label %18

18:                                               ; preds = %._crit_edge
  %19 = icmp slt i32 %.025.lcssa, 0
  br i1 %19, label %20, label %22

20:                                               ; preds = %18
  %21 = getelementptr inbounds i8, ptr %.026.lcssa, i64 16
  br label %.loopexit.sink.split

22:                                               ; preds = %18
  %.not29 = icmp eq i32 %.025.lcssa, 0
  br i1 %.not29, label %25, label %23

23:                                               ; preds = %22
  %24 = getelementptr inbounds i8, ptr %.026.lcssa, i64 24
  br label %.loopexit.sink.split

25:                                               ; preds = %22
  tail call void @__assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 79, ptr noundef nonnull @__PRETTY_FUNCTION__.bs_tree_insert) #3
  unreachable

.loopexit.sink.split:                             ; preds = %._crit_edge, %20, %23
  %.sink42 = phi ptr [ %24, %23 ], [ %21, %20 ], [ %0, %._crit_edge ]
  store ptr %12, ptr %.sink42, align 8, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %9, %.loopexit.sink.split
  %.0 = phi ptr [ %12, %.loopexit.sink.split ], [ %.02736, %9 ]
  ret ptr %.0
}

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

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

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 = { noreturn nounwind "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 }
attributes #3 = { noreturn 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 8}
!10 = !{!"_BSTree", !6, i64 0, !6, i64 8, !11, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !6, i64 0}
!13 = !{!"_BSTreeNode", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!13, !6, i64 8}
!17 = !{!10, !11, i64 16}


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