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

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

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

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

17:                                               ; preds = %14
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.kdnode, ptr %18, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !16
  %21 = icmp eq ptr %20, null
  br i1 %21, label %22, label %34

22:                                               ; preds = %17
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.kdnode, ptr %23, i32 0, i32 3
  %25 = load ptr, ptr %24, align 8, !tbaa !18
  %26 = getelementptr inbounds %struct.kdnode, ptr %25, i32 0, i32 1
  %27 = load ptr, ptr %26, align 8, !tbaa !16
  %28 = icmp eq ptr %27, null
  br i1 %28, label %29, label %30

29:                                               ; preds = %22
  br label %112

30:                                               ; preds = %22
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.kdnode, ptr %31, i32 0, i32 3
  %33 = load ptr, ptr %32, align 8, !tbaa !18
  store ptr %33, ptr %6, align 8, !tbaa !5
  br label %14, !llvm.loop !19

34:                                               ; preds = %17
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds double, ptr %35, i64 %37
  %39 = load double, ptr %38, align 8, !tbaa !21
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.kdnode, ptr %40, i32 0, i32 1
  %42 = load ptr, ptr %41, align 8, !tbaa !16
  %43 = load i32, ptr %5, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds double, ptr %42, i64 %44
  %46 = load double, ptr %45, align 8, !tbaa !21
  %47 = fcmp olt double %39, %46
  br i1 %47, label %48, label %53

48:                                               ; preds = %34
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %49, ptr %7, align 8, !tbaa !5
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.kdnode, ptr %50, i32 0, i32 2
  %52 = load ptr, ptr %51, align 8, !tbaa !22
  store ptr %52, ptr %6, align 8, !tbaa !5
  br label %105

53:                                               ; preds = %34
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = load i32, ptr %5, align 4, !tbaa !9
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds double, ptr %54, i64 %56
  %58 = load double, ptr %57, align 8, !tbaa !21
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.kdnode, ptr %59, i32 0, i32 1
  %61 = load ptr, ptr %60, align 8, !tbaa !16
  %62 = load i32, ptr %5, align 4, !tbaa !9
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds double, ptr %61, i64 %63
  %65 = load double, ptr %64, align 8, !tbaa !21
  %66 = fcmp ogt double %58, %65
  br i1 %66, label %67, label %72

67:                                               ; preds = %53
  %68 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %68, ptr %7, align 8, !tbaa !5
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.kdnode, ptr %69, i32 0, i32 3
  %71 = load ptr, ptr %70, align 8, !tbaa !18
  store ptr %71, ptr %6, align 8, !tbaa !5
  br label %104

72:                                               ; preds = %53
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  %74 = load ptr, ptr %6, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.kdnode, ptr %74, i32 0, i32 1
  %76 = load ptr, ptr %75, align 8, !tbaa !16
  %77 = load ptr, ptr %3, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.kdtree, ptr %77, i32 0, i32 9
  %79 = load i32, ptr %78, align 8, !tbaa !23
  %80 = call i32 @coord_cmp(ptr noundef %73, ptr noundef %76, i32 noundef %79)
  store i32 %80, ptr %8, align 4, !tbaa !9
  %81 = load i32, ptr %8, align 4, !tbaa !9
  %82 = icmp slt i32 %81, 0
  br i1 %82, label %83, label %88

83:                                               ; preds = %72
  %84 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %84, ptr %7, align 8, !tbaa !5
  %85 = load ptr, ptr %6, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.kdnode, ptr %85, i32 0, i32 2
  %87 = load ptr, ptr %86, align 8, !tbaa !22
  store ptr %87, ptr %6, align 8, !tbaa !5
  br label %100

88:                                               ; preds = %72
  %89 = load i32, ptr %8, align 4, !tbaa !9
  %90 = icmp sgt i32 %89, 0
  br i1 %90, label %91, label %96

91:                                               ; preds = %88
  %92 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %92, ptr %7, align 8, !tbaa !5
  %93 = load ptr, ptr %6, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.kdnode, ptr %93, i32 0, i32 3
  %95 = load ptr, ptr %94, align 8, !tbaa !18
  store ptr %95, ptr %6, align 8, !tbaa !5
  br label %99

96:                                               ; preds = %88
  %97 = load ptr, ptr %6, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.kdnode, ptr %97, i32 0, i32 1
  store ptr null, ptr %98, align 8, !tbaa !16
  store i32 3, ptr %9, align 4
  br label %101

99:                                               ; preds = %91
  br label %100

100:                                              ; preds = %99, %83
  store i32 0, ptr %9, align 4
  br label %101

101:                                              ; preds = %100, %96
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %102 = load i32, ptr %9, align 4
  switch i32 %102, label %113 [
    i32 0, label %103
    i32 3, label %112
  ]

103:                                              ; preds = %101
  br label %104

104:                                              ; preds = %103, %67
  br label %105

105:                                              ; preds = %104, %48
  %106 = load i32, ptr %5, align 4, !tbaa !9
  %107 = add nsw i32 %106, 1
  %108 = load ptr, ptr %3, align 8, !tbaa !5
  %109 = getelementptr inbounds %struct.kdtree, ptr %108, i32 0, i32 9
  %110 = load i32, ptr %109, align 8, !tbaa !23
  %111 = srem i32 %107, %110
  store i32 %111, ptr %5, align 4, !tbaa !9
  br label %14, !llvm.loop !19

112:                                              ; preds = %101, %29, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret void

113:                                              ; preds = %101
  unreachable
}

; Function Attrs: nounwind uwtable
declare hidden i32 @coord_cmp(ptr noundef, ptr noundef, i32 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 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"kdtree", !6, i64 0, !13, i64 8, !13, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !14, i64 64, !10, i64 96, !10, i64 100}
!13 = !{!"long", !7, i64 0}
!14 = !{!"knn_list", !6, i64 0, !15, i64 8, !6, i64 16, !6, i64 24}
!15 = !{!"double", !7, i64 0}
!16 = !{!17, !6, i64 8}
!17 = !{!"kdnode", !13, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32}
!18 = !{!17, !6, i64 24}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!15, !15, i64 0}
!22 = !{!17, !6, i64 16}
!23 = !{!12, !10, i64 96}


; 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: nounwind uwtable
define dso_local void @kdtree_delete(ptr nocapture noundef readonly %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = load ptr, ptr %0, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 96
  br label %.outer

.outer:                                           ; preds = %.thread, %2
  %.032.ph = phi ptr [ %.335, %.thread ], [ %3, %2 ]
  %.031.ph = phi ptr [ %.032, %.thread ], [ %3, %2 ]
  %.030.ph = phi i32 [ %35, %.thread ], [ 0, %2 ]
  %5 = getelementptr inbounds i8, ptr %.031.ph, i64 24
  br label %6

6:                                                ; preds = %.outer, %11
  %.032 = phi ptr [ %12, %11 ], [ %.032.ph, %.outer ]
  %.not = icmp eq ptr %.032, null
  br i1 %.not, label %.loopexit, label %7

7:                                                ; preds = %6
  %8 = getelementptr inbounds i8, ptr %.032, i64 8
  %9 = load ptr, ptr %8, align 8, !tbaa !14
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %16

11:                                               ; preds = %7
  %12 = load ptr, ptr %5, align 8, !tbaa !16
  %13 = getelementptr inbounds i8, ptr %12, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !14
  %15 = icmp eq ptr %14, null
  br i1 %15, label %.loopexit, label %6, !llvm.loop !17

16:                                               ; preds = %7
  %17 = sext i32 %.030.ph to i64
  %18 = getelementptr inbounds double, ptr %1, i64 %17
  %19 = load double, ptr %18, align 8, !tbaa !19
  %20 = getelementptr inbounds double, ptr %9, i64 %17
  %21 = load double, ptr %20, align 8, !tbaa !19
  %22 = fcmp olt double %19, %21
  br i1 %22, label %.thread, label %23

23:                                               ; preds = %16
  %24 = fcmp ogt double %19, %21
  br i1 %24, label %.thread, label %25

25:                                               ; preds = %23
  %26 = load i32, ptr %4, align 8, !tbaa !20
  %27 = tail call i32 @coord_cmp(ptr noundef nonnull %1, ptr noundef nonnull %9, i32 noundef %26)
  %28 = icmp slt i32 %27, 0
  br i1 %28, label %.thread, label %29

29:                                               ; preds = %25
  %.not40 = icmp eq i32 %27, 0
  br i1 %.not40, label %30, label %.thread

30:                                               ; preds = %29
  %31 = getelementptr inbounds i8, ptr %.032, i64 8
  store ptr null, ptr %31, align 8, !tbaa !14
  br label %.loopexit

.thread:                                          ; preds = %29, %25, %23, %16
  %.sink = phi i64 [ 16, %16 ], [ 24, %23 ], [ 16, %25 ], [ 24, %29 ]
  %32 = getelementptr inbounds i8, ptr %.032, i64 %.sink
  %.335 = load ptr, ptr %32, align 8, !tbaa !21
  %33 = add nsw i32 %.030.ph, 1
  %34 = load i32, ptr %4, align 8, !tbaa !20
  %35 = srem i32 %33, %34
  br label %.outer, !llvm.loop !17

.loopexit:                                        ; preds = %11, %6, %30
  ret void
}

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

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


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