; LLVM IR from begeekmyfriend/kdtree :: kdtree (kdtree_rebuild)

; 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.kdtree = type { ptr, i64, i64, ptr, ptr, ptr, ptr, ptr, %struct.knn_list, i32, i32 }
%struct.knn_list = type { ptr, double, ptr, ptr }

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local void @kdtree_rebuild(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.kdtree, ptr %6, i32 0, i32 9
  %8 = load i32, ptr %7, align 8, !tbaa !9
  %9 = sext i32 %8 to i64
  %10 = mul i64 %9, 8
  store i64 %10, ptr %5, align 8, !tbaa !15
  store i64 0, ptr %3, align 8, !tbaa !15
  store i64 0, ptr %4, align 8, !tbaa !15
  br label %11

11:                                               ; preds = %68, %1
  %12 = load i64, ptr %4, align 8, !tbaa !15
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.kdtree, ptr %13, i32 0, i32 1
  %15 = load i64, ptr %14, align 8, !tbaa !16
  %16 = icmp ult i64 %12, %15
  br i1 %16, label %17, label %73

17:                                               ; preds = %11
  br label %18

18:                                               ; preds = %35, %17
  %19 = load i64, ptr %4, align 8, !tbaa !15
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.kdtree, ptr %20, i32 0, i32 1
  %22 = load i64, ptr %21, align 8, !tbaa !16
  %23 = icmp ult i64 %19, %22
  br i1 %23, label %24, label %33

24:                                               ; preds = %18
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.kdtree, ptr %25, i32 0, i32 6
  %27 = load ptr, ptr %26, align 8, !tbaa !17
  %28 = load i64, ptr %4, align 8, !tbaa !15
  %29 = getelementptr inbounds i8, ptr %27, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !18
  %31 = zext i8 %30 to i32
  %32 = icmp ne i32 %31, 0
  br label %33

33:                                               ; preds = %24, %18
  %34 = phi i1 [ false, %18 ], [ %32, %24 ]
  br i1 %34, label %35, label %38

35:                                               ; preds = %33
  %36 = load i64, ptr %4, align 8, !tbaa !15
  %37 = add nsw i64 %36, 1
  store i64 %37, ptr %4, align 8, !tbaa !15
  br label %18, !llvm.loop !19

38:                                               ; preds = %33
  %39 = load i64, ptr %3, align 8, !tbaa !15
  %40 = load i64, ptr %4, align 8, !tbaa !15
  %41 = icmp ne i64 %39, %40
  br i1 %41, label %42, label %67

42:                                               ; preds = %38
  %43 = load i64, ptr %4, align 8, !tbaa !15
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.kdtree, ptr %44, i32 0, i32 1
  %46 = load i64, ptr %45, align 8, !tbaa !16
  %47 = icmp ult i64 %43, %46
  br i1 %47, label %48, label %67

48:                                               ; preds = %42
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.kdtree, ptr %49, i32 0, i32 4
  %51 = load ptr, ptr %50, align 8, !tbaa !21
  %52 = load i64, ptr %3, align 8, !tbaa !15
  %53 = getelementptr inbounds ptr, ptr %51, i64 %52
  %54 = load ptr, ptr %53, align 8, !tbaa !5
  %55 = load ptr, ptr %2, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.kdtree, ptr %55, i32 0, i32 4
  %57 = load ptr, ptr %56, align 8, !tbaa !21
  %58 = load i64, ptr %4, align 8, !tbaa !15
  %59 = getelementptr inbounds ptr, ptr %57, i64 %58
  %60 = load ptr, ptr %59, align 8, !tbaa !5
  %61 = load i64, ptr %5, align 8, !tbaa !15
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %54, ptr align 8 %60, i64 %61, i1 false)
  %62 = load ptr, ptr %2, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.kdtree, ptr %62, i32 0, i32 6
  %64 = load ptr, ptr %63, align 8, !tbaa !17
  %65 = load i64, ptr %3, align 8, !tbaa !15
  %66 = getelementptr inbounds i8, ptr %64, i64 %65
  store i8 0, ptr %66, align 1, !tbaa !18
  br label %67

67:                                               ; preds = %48, %42, %38
  br label %68

68:                                               ; preds = %67
  %69 = load i64, ptr %3, align 8, !tbaa !15
  %70 = add nsw i64 %69, 1
  store i64 %70, ptr %3, align 8, !tbaa !15
  %71 = load i64, ptr %4, align 8, !tbaa !15
  %72 = add nsw i64 %71, 1
  store i64 %72, ptr %4, align 8, !tbaa !15
  br label %11, !llvm.loop !22

73:                                               ; preds = %11
  %74 = load i64, ptr %3, align 8, !tbaa !15
  %75 = load ptr, ptr %2, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.kdtree, ptr %75, i32 0, i32 1
  store i64 %74, ptr %76, align 8, !tbaa !16
  %77 = load ptr, ptr %2, align 8, !tbaa !5
  call void @coord_index_reset(ptr noundef %77)
  %78 = load ptr, ptr %2, align 8, !tbaa !5
  call void @kdtree_build(ptr noundef %78)
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @coord_index_reset(ptr noundef) #3

; Function Attrs: nounwind uwtable
declare hidden void @kdtree_build(ptr noundef) #2

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { inlinehint 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 #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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !14, i64 96}
!10 = !{!"kdtree", !6, i64 0, !11, i64 8, !11, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !12, i64 64, !14, i64 96, !14, i64 100}
!11 = !{!"long", !7, i64 0}
!12 = !{!"knn_list", !6, i64 0, !13, i64 8, !6, i64 16, !6, i64 24}
!13 = !{!"double", !7, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!11, !11, i64 0}
!16 = !{!10, !11, i64 8}
!17 = !{!10, !6, i64 48}
!18 = !{!7, !7, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!10, !6, i64 32}
!22 = distinct !{!22, !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 nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

; Function Attrs: nounwind uwtable
define dso_local void @kdtree_rebuild(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 96
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = sext i32 %3 to i64
  %5 = shl nsw i64 %4, 3
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !14
  %.not32 = icmp eq i64 %7, 0
  br i1 %.not32, label %._crit_edge, label %.preheader.lr.ph

.preheader.lr.ph:                                 ; preds = %1
  %8 = getelementptr inbounds i8, ptr %0, i64 48
  %9 = getelementptr inbounds i8, ptr %0, i64 32
  br label %.preheader

.preheader:                                       ; preds = %.preheader.lr.ph, %.thread
  %10 = phi i64 [ %7, %.preheader.lr.ph ], [ %27, %.thread ]
  %.031 = phi i64 [ 0, %.preheader.lr.ph ], [ %28, %.thread ]
  %.02430 = phi i64 [ 0, %.preheader.lr.ph ], [ %29, %.thread ]
  %11 = load ptr, ptr %8, align 8, !tbaa !15
  %12 = add nuw i64 %.02430, 1
  %umax = tail call i64 @llvm.umax.i64(i64 %10, i64 %12)
  br label %13

13:                                               ; preds = %.preheader, %16
  %.129 = phi i64 [ %.02430, %.preheader ], [ %17, %16 ]
  %14 = getelementptr inbounds i8, ptr %11, i64 %.129
  %15 = load i8, ptr %14, align 1, !tbaa !16
  %.not = icmp eq i8 %15, 0
  br i1 %.not, label %.critedge, label %16

16:                                               ; preds = %13
  %17 = add nuw nsw i64 %.129, 1
  %18 = icmp ult i64 %17, %10
  br i1 %18, label %13, label %.thread, !llvm.loop !17

.critedge:                                        ; preds = %13
  %.not25 = icmp eq i64 %.031, %.129
  br i1 %.not25, label %.thread, label %19

19:                                               ; preds = %.critedge
  %20 = load ptr, ptr %9, align 8, !tbaa !19
  %21 = getelementptr inbounds ptr, ptr %20, i64 %.031
  %22 = load ptr, ptr %21, align 8, !tbaa !20
  %23 = getelementptr inbounds ptr, ptr %20, i64 %.129
  %24 = load ptr, ptr %23, align 8, !tbaa !20
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 8 %22, ptr align 8 %24, i64 %5, i1 false)
  %25 = load ptr, ptr %8, align 8, !tbaa !15
  %26 = getelementptr inbounds i8, ptr %25, i64 %.031
  store i8 0, ptr %26, align 1, !tbaa !16
  %.pre = load i64, ptr %6, align 8, !tbaa !14
  br label %.thread

.thread:                                          ; preds = %16, %.critedge, %19
  %27 = phi i64 [ %10, %.critedge ], [ %.pre, %19 ], [ %10, %16 ]
  %.128 = phi i64 [ %.031, %.critedge ], [ %.129, %19 ], [ %umax, %16 ]
  %28 = add nuw nsw i64 %.031, 1
  %29 = add nsw i64 %.128, 1
  %30 = icmp ult i64 %29, %27
  br i1 %30, label %.preheader, label %._crit_edge, !llvm.loop !21

._crit_edge:                                      ; preds = %.thread, %1
  %.0.lcssa = phi i64 [ 0, %1 ], [ %28, %.thread ]
  store i64 %.0.lcssa, ptr %6, align 8, !tbaa !14
  tail call void @coord_index_reset(ptr noundef nonnull %0)
  tail call void @kdtree_build(ptr noundef nonnull %0)
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @coord_index_reset(ptr noundef) local_unnamed_addr #2

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #3

attributes #0 = { mustprogress nocallback nofree 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 = { inlinehint 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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, !13, i64 96}
!6 = !{!"kdtree", !7, i64 0, !10, i64 8, !10, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !11, i64 64, !13, i64 96, !13, i64 100}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"knn_list", !7, i64 0, !12, i64 8, !7, i64 16, !7, i64 24}
!12 = !{!"double", !8, i64 0}
!13 = !{!"int", !8, i64 0}
!14 = !{!6, !10, i64 8}
!15 = !{!6, !7, i64 48}
!16 = !{!8, !8, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!6, !7, i64 32}
!20 = !{!7, !7, i64 0}
!21 = distinct !{!21, !18}


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