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

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

@.str = external hidden unnamed_addr constant [2 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [64 x i8], align 1
@__PRETTY_FUNCTION__.avl_tree_insert = external hidden unnamed_addr constant [66 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 @avl_tree_insert(ptr noundef %0, ptr noundef %1, ptr noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct._AVLTree, ptr %13, i32 0, i32 0
  %15 = load ptr, ptr %14, align 8, !tbaa !9
  store ptr %15, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store ptr null, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !12
  br label %16

16:                                               ; preds = %58, %3
  %17 = load ptr, ptr %8, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %59

19:                                               ; preds = %16
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct._AVLTree, ptr %20, i32 0, i32 1
  %22 = load ptr, ptr %21, align 8, !tbaa !13
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct._AVLTreeNode, ptr %24, i32 0, i32 0
  %26 = load ptr, ptr %25, align 8, !tbaa !14
  %27 = call i32 %22(ptr noundef %23, ptr noundef %26)
  store i32 %27, ptr %10, align 4, !tbaa !12
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %28, ptr %9, align 8, !tbaa !5
  %29 = load i32, ptr %10, align 4, !tbaa !12
  %30 = icmp slt i32 %29, 0
  br i1 %30, label %31, label %35

31:                                               ; preds = %19
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct._AVLTreeNode, ptr %32, i32 0, i32 3
  %34 = load ptr, ptr %33, align 8, !tbaa !16
  store ptr %34, ptr %8, align 8, !tbaa !5
  br label %58

35:                                               ; preds = %19
  %36 = load i32, ptr %10, align 4, !tbaa !12
  %37 = icmp sgt i32 %36, 0
  br i1 %37, label %38, label %42

38:                                               ; preds = %35
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct._AVLTreeNode, ptr %39, i32 0, i32 4
  %41 = load ptr, ptr %40, align 8, !tbaa !17
  store ptr %41, ptr %8, align 8, !tbaa !5
  br label %57

42:                                               ; preds = %35
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct._AVLTree, ptr %43, i32 0, i32 2
  %45 = load ptr, ptr %44, align 8, !tbaa !18
  %46 = icmp ne ptr %45, null
  br i1 %46, label %47, label %52

47:                                               ; preds = %42
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct._AVLTree, ptr %48, i32 0, i32 2
  %50 = load ptr, ptr %49, align 8, !tbaa !18
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  call void %50(ptr noundef %51)
  br label %52

52:                                               ; preds = %47, %42
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  %55 = call ptr @avl_tree_node_append_value(ptr noundef %53, ptr noundef %54)
  %56 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %56, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %101

57:                                               ; preds = %38
  br label %58

58:                                               ; preds = %57, %31
  br label %16, !llvm.loop !19

59:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = call ptr @avl_tree_node_new(ptr noundef %60, ptr noundef %61)
  store ptr %62, ptr %12, align 8, !tbaa !5
  %63 = load ptr, ptr %9, align 8, !tbaa !5
  %64 = load ptr, ptr %12, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct._AVLTreeNode, ptr %64, i32 0, i32 2
  store ptr %63, ptr %65, align 8, !tbaa !21
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct._AVLTree, ptr %66, i32 0, i32 4
  %68 = load i32, ptr %67, align 8, !tbaa !22
  %69 = add i32 %68, 1
  store i32 %69, ptr %67, align 8, !tbaa !22
  %70 = load ptr, ptr %9, align 8, !tbaa !5
  %71 = icmp eq ptr %70, null
  br i1 %71, label %72, label %76

72:                                               ; preds = %59
  %73 = load ptr, ptr %12, align 8, !tbaa !5
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct._AVLTree, ptr %74, i32 0, i32 0
  store ptr %73, ptr %75, align 8, !tbaa !9
  br label %93

76:                                               ; preds = %59
  %77 = load i32, ptr %10, align 4, !tbaa !12
  %78 = icmp slt i32 %77, 0
  br i1 %78, label %79, label %83

79:                                               ; preds = %76
  %80 = load ptr, ptr %12, align 8, !tbaa !5
  %81 = load ptr, ptr %9, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct._AVLTreeNode, ptr %81, i32 0, i32 3
  store ptr %80, ptr %82, align 8, !tbaa !16
  br label %92

83:                                               ; preds = %76
  %84 = load i32, ptr %10, align 4, !tbaa !12
  %85 = icmp sgt i32 %84, 0
  br i1 %85, label %86, label %90

86:                                               ; preds = %83
  %87 = load ptr, ptr %12, align 8, !tbaa !5
  %88 = load ptr, ptr %9, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct._AVLTreeNode, ptr %88, i32 0, i32 4
  store ptr %87, ptr %89, align 8, !tbaa !17
  br label %91

90:                                               ; preds = %83
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 318, ptr noundef @__PRETTY_FUNCTION__.avl_tree_insert) #4
  unreachable

91:                                               ; preds = %86
  br label %92

92:                                               ; preds = %91, %79
  br label %93

93:                                               ; preds = %92, %72
  %94 = load ptr, ptr %12, align 8, !tbaa !5
  %95 = icmp ne ptr %94, null
  br i1 %95, label %96, label %99

96:                                               ; preds = %93
  %97 = load ptr, ptr %5, align 8, !tbaa !5
  %98 = load ptr, ptr %12, align 8, !tbaa !5
  call void @avl_tree_balance_tree(ptr noundef %97, ptr noundef %98)
  br label %99

99:                                               ; preds = %96, %93
  %100 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %100, ptr %4, align 8
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %101

101:                                              ; preds = %99, %52
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %102 = load ptr, ptr %4, align 8
  ret ptr %102
}

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

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

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

; Function Attrs: nounwind uwtable
declare hidden void @avl_tree_balance_tree(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 = { 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 = !{!"_AVLTree", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!10, !6, i64 8}
!14 = !{!15, !6, i64 0}
!15 = !{!"_AVLTreeNode", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !11, i64 40}
!16 = !{!15, !6, i64 24}
!17 = !{!15, !6, i64 32}
!18 = !{!10, !6, i64 16}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!15, !6, i64 16}
!22 = !{!10, !11, i64 32}


; 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 [64 x i8], align 1
@__PRETTY_FUNCTION__.avl_tree_insert = external hidden unnamed_addr constant [66 x i8], align 1

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

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

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

10:                                               ; preds = %5
  %.not42 = icmp eq i32 %8, 0
  br i1 %.not42, label %11, label %17

11:                                               ; preds = %10
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  %13 = load ptr, ptr %12, align 8, !tbaa !14
  %.not43 = icmp eq ptr %13, null
  br i1 %.not43, label %15, label %14

14:                                               ; preds = %11
  tail call void %13(ptr noundef %1) #2
  br label %15

15:                                               ; preds = %14, %11
  %16 = tail call ptr @avl_tree_node_append_value(ptr noundef nonnull %.03649, ptr noundef %2)
  br label %34

17:                                               ; preds = %10, %5
  %.sink = phi i64 [ 24, %5 ], [ 32, %10 ]
  %18 = getelementptr inbounds i8, ptr %.03649, i64 %.sink
  %.036 = load ptr, ptr %18, align 8, !tbaa !5
  %.not = icmp eq ptr %.036, null
  br i1 %.not, label %._crit_edge, label %5, !llvm.loop !15

._crit_edge:                                      ; preds = %17, %3
  %.035.lcssa = phi ptr [ null, %3 ], [ %.03649, %17 ]
  %.034.lcssa = phi i32 [ 0, %3 ], [ %8, %17 ]
  %19 = tail call ptr @avl_tree_node_new(ptr noundef %1, ptr noundef %2)
  %20 = getelementptr inbounds i8, ptr %19, i64 16
  store ptr %.035.lcssa, ptr %20, align 8, !tbaa !17
  %21 = getelementptr inbounds i8, ptr %0, i64 32
  %22 = load i32, ptr %21, align 8, !tbaa !18
  %23 = add i32 %22, 1
  store i32 %23, ptr %21, align 8, !tbaa !18
  %24 = icmp eq ptr %.035.lcssa, null
  br i1 %24, label %33, label %25

25:                                               ; preds = %._crit_edge
  %26 = icmp slt i32 %.034.lcssa, 0
  br i1 %26, label %27, label %29

27:                                               ; preds = %25
  %28 = getelementptr inbounds i8, ptr %.035.lcssa, i64 24
  br label %33

29:                                               ; preds = %25
  %.not40 = icmp eq i32 %.034.lcssa, 0
  br i1 %.not40, label %32, label %30

30:                                               ; preds = %29
  %31 = getelementptr inbounds i8, ptr %.035.lcssa, i64 32
  br label %33

32:                                               ; preds = %29
  tail call void @__assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 318, ptr noundef nonnull @__PRETTY_FUNCTION__.avl_tree_insert) #3
  unreachable

33:                                               ; preds = %._crit_edge, %30, %27
  %.sink55 = phi ptr [ %31, %30 ], [ %28, %27 ], [ %0, %._crit_edge ]
  store ptr %19, ptr %.sink55, align 8, !tbaa !5
  tail call void @avl_tree_balance_tree(ptr noundef nonnull %0, ptr noundef nonnull %19)
  br label %34

34:                                               ; preds = %33, %15
  %.0 = phi ptr [ %.03649, %15 ], [ %19, %33 ]
  ret ptr %.0
}

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

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

; Function Attrs: nounwind uwtable
declare hidden void @avl_tree_balance_tree(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 = { 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 = !{!"_AVLTree", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !6, i64 0}
!13 = !{!"_AVLTreeNode", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !11, i64 40}
!14 = !{!10, !6, i64 16}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!13, !6, i64 16}
!18 = !{!10, !11, i64 32}


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