; LLVM IR from asset-ai/simple-vector-db :: src (kdtree)

; 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.KDTreeNode = type { ptr, i64, ptr, ptr }

@.str.1 = external hidden unnamed_addr constant [51 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @kdtree_create_node(ptr noundef, i64 noundef, i64 noundef) #0

declare i32 @printf(ptr noundef, ...) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @kdtree_insert_rec(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3, i64 noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i64 %2, ptr %9, align 8, !tbaa !9
  store i64 %3, ptr %10, align 8, !tbaa !9
  store i64 %4, ptr %11, align 8, !tbaa !9
  %13 = load i64, ptr %10, align 8, !tbaa !9
  %14 = load i64, ptr %11, align 8, !tbaa !9
  %15 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i64 noundef %13, i64 noundef %14)
  %16 = load ptr, ptr %7, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %23, label %18

18:                                               ; preds = %5
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = load i64, ptr %9, align 8, !tbaa !9
  %21 = load i64, ptr %11, align 8, !tbaa !9
  %22 = call ptr @kdtree_create_node(ptr noundef %19, i64 noundef %20, i64 noundef %21)
  store ptr %22, ptr %6, align 8
  br label %64

23:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %24 = load i64, ptr %10, align 8, !tbaa !9
  %25 = load i64, ptr %11, align 8, !tbaa !9
  %26 = urem i64 %24, %25
  store i64 %26, ptr %12, align 8, !tbaa !9
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  %28 = load i64, ptr %12, align 8, !tbaa !9
  %29 = getelementptr inbounds double, ptr %27, i64 %28
  %30 = load double, ptr %29, align 8, !tbaa !11
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.KDTreeNode, ptr %31, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !13
  %34 = load i64, ptr %12, align 8, !tbaa !9
  %35 = getelementptr inbounds double, ptr %33, i64 %34
  %36 = load double, ptr %35, align 8, !tbaa !11
  %37 = fcmp olt double %30, %36
  br i1 %37, label %38, label %50

38:                                               ; preds = %23
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.KDTreeNode, ptr %39, i32 0, i32 2
  %41 = load ptr, ptr %40, align 8, !tbaa !15
  %42 = load ptr, ptr %8, align 8, !tbaa !5
  %43 = load i64, ptr %9, align 8, !tbaa !9
  %44 = load i64, ptr %10, align 8, !tbaa !9
  %45 = add i64 %44, 1
  %46 = load i64, ptr %11, align 8, !tbaa !9
  %47 = call ptr @kdtree_insert_rec(ptr noundef %41, ptr noundef %42, i64 noundef %43, i64 noundef %45, i64 noundef %46)
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.KDTreeNode, ptr %48, i32 0, i32 2
  store ptr %47, ptr %49, align 8, !tbaa !15
  br label %62

50:                                               ; preds = %23
  %51 = load ptr, ptr %7, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.KDTreeNode, ptr %51, i32 0, i32 3
  %53 = load ptr, ptr %52, align 8, !tbaa !16
  %54 = load ptr, ptr %8, align 8, !tbaa !5
  %55 = load i64, ptr %9, align 8, !tbaa !9
  %56 = load i64, ptr %10, align 8, !tbaa !9
  %57 = add i64 %56, 1
  %58 = load i64, ptr %11, align 8, !tbaa !9
  %59 = call ptr @kdtree_insert_rec(ptr noundef %53, ptr noundef %54, i64 noundef %55, i64 noundef %57, i64 noundef %58)
  %60 = load ptr, ptr %7, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.KDTreeNode, ptr %60, i32 0, i32 3
  store ptr %59, ptr %61, align 8, !tbaa !16
  br label %62

62:                                               ; preds = %50, %38
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %63, ptr %6, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %64

64:                                               ; preds = %62, %18
  %65 = load ptr, ptr %6, align 8
  ret ptr %65
}

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 = { "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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"KDTreeNode", !6, i64 0, !10, i64 8, !6, i64 16, !6, i64 24}
!15 = !{!14, !6, i64 16}
!16 = !{!14, !6, i64 24}


; 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.1 = external hidden unnamed_addr constant [51 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @kdtree_create_node(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local ptr @kdtree_insert_rec(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3, i64 noundef %4) local_unnamed_addr #0 {
  %6 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i64 noundef %3, i64 noundef %4)
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %common.ret, label %8

common.ret:                                       ; preds = %5
  %7 = tail call ptr @kdtree_create_node(ptr noundef %1, i64 noundef %2, i64 noundef %4)
  br label %common.ret26

8:                                                ; preds = %5
  %9 = urem i64 %3, %4
  %10 = getelementptr inbounds double, ptr %1, i64 %9
  %11 = load double, ptr %10, align 8, !tbaa !5
  %12 = load ptr, ptr %0, align 8, !tbaa !9
  %13 = getelementptr inbounds double, ptr %12, i64 %9
  %14 = load double, ptr %13, align 8, !tbaa !5
  %15 = fcmp olt double %11, %14
  %16 = add i64 %3, 1
  br i1 %15, label %17, label %21

common.ret26:                                     ; preds = %21, %17, %common.ret
  %common.ret26.op = phi ptr [ %7, %common.ret ], [ %0, %17 ], [ %0, %21 ]
  ret ptr %common.ret26.op

17:                                               ; preds = %8
  %18 = getelementptr inbounds i8, ptr %0, i64 16
  %19 = load ptr, ptr %18, align 8, !tbaa !13
  %20 = tail call ptr @kdtree_insert_rec(ptr noundef %19, ptr noundef nonnull %1, i64 noundef %2, i64 noundef %16, i64 noundef %4)
  store ptr %20, ptr %18, align 8, !tbaa !13
  br label %common.ret26

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

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 = { nofree 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" }

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


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