; LLVM IR from ksw2000/Data-Structure-in-C :: Tree (AVL-Tree)

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #1

; 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 void @stack_push(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local ptr @stack_pop(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @stack_empty(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @stack_init(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @treeNode_updateHeight(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @treeNode_reBalance(ptr noundef) #2

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

23:                                               ; preds = %2
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.avlTree, ptr %25, i32 0, i32 0
  store ptr %24, ptr %26, align 8, !tbaa !15
  store i32 1, ptr %6, align 4
  br label %94

27:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store ptr null, ptr %7, align 8, !tbaa !5
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.avlTree, ptr %28, i32 0, i32 4
  %30 = load ptr, ptr %29, align 8, !tbaa !17
  call void @stack_init(ptr noundef %30)
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.avlTree, ptr %31, i32 0, i32 4
  %33 = load ptr, ptr %32, align 8, !tbaa !17
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.avlTree, ptr %34, i32 0, i32 0
  call void @stack_push(ptr noundef %33, ptr noundef %35)
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.avlTree, ptr %36, i32 0, i32 0
  %38 = load ptr, ptr %37, align 8, !tbaa !15
  store ptr %38, ptr %7, align 8, !tbaa !5
  br label %39

39:                                               ; preds = %70, %27
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = icmp ne ptr %40, null
  br i1 %41, label %42, label %71

42:                                               ; preds = %39
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.avlTree, ptr %43, i32 0, i32 1
  %45 = load ptr, ptr %44, align 8, !tbaa !18
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.treeNode, ptr %47, i32 0, i32 0
  %49 = load ptr, ptr %48, align 8, !tbaa !9
  %50 = call i32 %45(ptr noundef %46, ptr noundef %49)
  %51 = icmp slt i32 %50, 0
  br i1 %51, label %52, label %61

52:                                               ; preds = %42
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.avlTree, ptr %53, i32 0, i32 4
  %55 = load ptr, ptr %54, align 8, !tbaa !17
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.treeNode, ptr %56, i32 0, i32 1
  call void @stack_push(ptr noundef %55, ptr noundef %57)
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.treeNode, ptr %58, i32 0, i32 1
  %60 = load ptr, ptr %59, align 8, !tbaa !12
  store ptr %60, ptr %7, align 8, !tbaa !5
  br label %70

61:                                               ; preds = %42
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.avlTree, ptr %62, i32 0, i32 4
  %64 = load ptr, ptr %63, align 8, !tbaa !17
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.treeNode, ptr %65, i32 0, i32 2
  call void @stack_push(ptr noundef %64, ptr noundef %66)
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.treeNode, ptr %67, i32 0, i32 2
  %69 = load ptr, ptr %68, align 8, !tbaa !13
  store ptr %69, ptr %7, align 8, !tbaa !5
  br label %70

70:                                               ; preds = %61, %52
  br label %39, !llvm.loop !19

71:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %72 = load ptr, ptr %3, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.avlTree, ptr %72, i32 0, i32 4
  %74 = load ptr, ptr %73, align 8, !tbaa !17
  %75 = call ptr @stack_pop(ptr noundef %74)
  store ptr %75, ptr %8, align 8, !tbaa !5
  %76 = load ptr, ptr %5, align 8, !tbaa !5
  %77 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %76, ptr %77, align 8, !tbaa !5
  br label %78

78:                                               ; preds = %85, %71
  %79 = load ptr, ptr %3, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.avlTree, ptr %79, i32 0, i32 4
  %81 = load ptr, ptr %80, align 8, !tbaa !17
  %82 = call i32 @stack_empty(ptr noundef %81)
  %83 = icmp ne i32 %82, 0
  %84 = xor i1 %83, true
  br i1 %84, label %85, label %93

85:                                               ; preds = %78
  %86 = load ptr, ptr %3, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.avlTree, ptr %86, i32 0, i32 4
  %88 = load ptr, ptr %87, align 8, !tbaa !17
  %89 = call ptr @stack_pop(ptr noundef %88)
  store ptr %89, ptr %8, align 8, !tbaa !5
  %90 = load ptr, ptr %8, align 8, !tbaa !5
  %91 = load ptr, ptr %90, align 8, !tbaa !5
  call void @treeNode_updateHeight(ptr noundef %91)
  %92 = load ptr, ptr %8, align 8, !tbaa !5
  call void @treeNode_reBalance(ptr noundef %92)
  br label %78, !llvm.loop !21

93:                                               ; preds = %78
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  store i32 0, ptr %6, align 4
  br label %94

94:                                               ; preds = %93, %23
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  %95 = load i32, ptr %6, align 4
  switch i32 %95, label %97 [
    i32 0, label %96
    i32 1, label %96
  ]

96:                                               ; preds = %94, %94
  ret void

97:                                               ; preds = %94
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind allocsize(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 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 #3 = { nounwind }
attributes #4 = { nounwind allocsize(0) }

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


; 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: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @stack_push(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @stack_pop(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @stack_empty(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @stack_init(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @treeNode_updateHeight(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @treeNode_reBalance(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @avlTree_insert(ptr noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = tail call noalias dereferenceable_or_null(32) ptr @malloc(i64 noundef 32) #3
  store ptr %1, ptr %3, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  %5 = getelementptr inbounds i8, ptr %3, i64 24
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %4, i8 0, i64 16, i1 false)
  store i32 1, ptr %5, align 8, !tbaa !11
  %6 = load ptr, ptr %0, align 8, !tbaa !12
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %7, label %8

7:                                                ; preds = %2
  store ptr %3, ptr %0, align 8, !tbaa !12
  br label %.loopexit

8:                                                ; preds = %2
  %9 = getelementptr inbounds i8, ptr %0, i64 32
  %10 = load ptr, ptr %9, align 8, !tbaa !14
  tail call void @stack_init(ptr noundef %10)
  %11 = load ptr, ptr %9, align 8, !tbaa !14
  tail call void @stack_push(ptr noundef %11, ptr noundef nonnull %0)
  %.031 = load ptr, ptr %0, align 8, !tbaa !15
  %.not2932 = icmp eq ptr %.031, null
  br i1 %.not2932, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %8
  %12 = getelementptr inbounds i8, ptr %0, i64 8
  br label %13

13:                                               ; preds = %.lr.ph, %13
  %.033 = phi ptr [ %.031, %.lr.ph ], [ %.0, %13 ]
  %14 = load ptr, ptr %12, align 8, !tbaa !16
  %15 = load ptr, ptr %.033, align 8, !tbaa !5
  %16 = tail call i32 %14(ptr noundef %1, ptr noundef %15) #4
  %17 = icmp slt i32 %16, 0
  %18 = load ptr, ptr %9, align 8, !tbaa !14
  %. = select i1 %17, i64 8, i64 16
  %19 = getelementptr inbounds i8, ptr %.033, i64 %.
  tail call void @stack_push(ptr noundef %18, ptr noundef nonnull %19)
  %.0 = load ptr, ptr %19, align 8, !tbaa !15
  %.not29 = icmp eq ptr %.0, null
  br i1 %.not29, label %._crit_edge, label %13, !llvm.loop !17

._crit_edge:                                      ; preds = %13, %8
  %20 = load ptr, ptr %9, align 8, !tbaa !14
  %21 = tail call ptr @stack_pop(ptr noundef %20)
  store ptr %3, ptr %21, align 8, !tbaa !15
  %22 = load ptr, ptr %9, align 8, !tbaa !14
  %23 = tail call i32 @stack_empty(ptr noundef %22)
  %.not3034 = icmp eq i32 %23, 0
  br i1 %.not3034, label %.lr.ph36, label %.loopexit

.lr.ph36:                                         ; preds = %._crit_edge, %.lr.ph36
  %24 = load ptr, ptr %9, align 8, !tbaa !14
  %25 = tail call ptr @stack_pop(ptr noundef %24)
  %26 = load ptr, ptr %25, align 8, !tbaa !15
  tail call void @treeNode_updateHeight(ptr noundef %26)
  tail call void @treeNode_reBalance(ptr noundef nonnull %25)
  %27 = load ptr, ptr %9, align 8, !tbaa !14
  %28 = tail call i32 @stack_empty(ptr noundef %27)
  %.not30 = icmp eq i32 %28, 0
  br i1 %.not30, label %.lr.ph36, label %.loopexit, !llvm.loop !19

.loopexit:                                        ; preds = %.lr.ph36, %._crit_edge, %7
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nounwind allocsize(0) }
attributes #4 = { 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 0}
!6 = !{!"treeNode", !7, i64 0, !7, i64 8, !7, i64 16, !10, 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, !10, i64 24}
!12 = !{!13, !7, i64 0}
!13 = !{!"avlTree", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32}
!14 = !{!13, !7, i64 32}
!15 = !{!7, !7, i64 0}
!16 = !{!13, !7, i64 8}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


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