; LLVM IR from PyCee/NUS_library :: src/math (NUS_octree)

; 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.NUS_vector = type { double, double, double }
%struct.NUS_octree_t = type { i32, i32, [8 x i32], double, %struct.NUS_vector, i8 }

@nus_octree_handles = external hidden global ptr, align 8
@nus_octree_count = external hidden global i32, align 4
@nus_octrees = external hidden global ptr, align 8
@constinit = external hidden constant [8 x i32], align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @nus_octree_build(i32 noundef %0, double noundef %1, ptr noundef byval(%struct.NUS_vector) align 8 %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca double, align 8
  %6 = alloca %struct.NUS_octree_t, align 8
  store i32 %0, ptr %4, align 4, !tbaa !5
  store double %1, ptr %5, align 8, !tbaa !9
  %7 = load ptr, ptr @nus_octree_handles, align 8, !tbaa !11
  %8 = load i32, ptr @nus_octree_count, align 4, !tbaa !5
  %9 = add i32 %8, 1
  %10 = zext i32 %9 to i64
  %11 = mul i64 4, %10
  %12 = call ptr @realloc(ptr noundef %7, i64 noundef %11) #5
  store ptr %12, ptr @nus_octree_handles, align 8, !tbaa !11
  %13 = call i32 @nus_octree_next_handle()
  %14 = load ptr, ptr @nus_octree_handles, align 8, !tbaa !11
  %15 = load i32, ptr @nus_octree_count, align 4, !tbaa !5
  %16 = zext i32 %15 to i64
  %17 = getelementptr inbounds i32, ptr %14, i64 %16
  store i32 %13, ptr %17, align 4, !tbaa !5
  %18 = load ptr, ptr @nus_octrees, align 8, !tbaa !11
  %19 = load i32, ptr @nus_octree_count, align 4, !tbaa !5
  %20 = add i32 %19, 1
  store i32 %20, ptr @nus_octree_count, align 4, !tbaa !5
  %21 = zext i32 %20 to i64
  %22 = mul i64 8, %21
  %23 = call ptr @realloc(ptr noundef %18, i64 noundef %22) #5
  store ptr %23, ptr @nus_octrees, align 8, !tbaa !11
  %24 = call noalias ptr @malloc(i64 noundef 80) #6
  %25 = load ptr, ptr @nus_octrees, align 8, !tbaa !11
  %26 = load i32, ptr @nus_octree_count, align 4, !tbaa !5
  %27 = sub i32 %26, 1
  %28 = zext i32 %27 to i64
  %29 = getelementptr inbounds ptr, ptr %25, i64 %28
  store ptr %24, ptr %29, align 8, !tbaa !11
  %30 = load ptr, ptr @nus_octrees, align 8, !tbaa !11
  %31 = load i32, ptr @nus_octree_count, align 4, !tbaa !5
  %32 = sub i32 %31, 1
  %33 = zext i32 %32 to i64
  %34 = getelementptr inbounds ptr, ptr %30, i64 %33
  %35 = load ptr, ptr %34, align 8, !tbaa !11
  %36 = getelementptr inbounds %struct.NUS_octree_t, ptr %6, i32 0, i32 0
  store i32 0, ptr %36, align 8, !tbaa !13
  %37 = getelementptr inbounds %struct.NUS_octree_t, ptr %6, i32 0, i32 1
  %38 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %38, ptr %37, align 4, !tbaa !16
  %39 = getelementptr inbounds %struct.NUS_octree_t, ptr %6, i32 0, i32 2
  call void @llvm.memset.p0.i64(ptr align 8 %39, i8 0, i64 32, i1 false)
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %39, ptr align 4 @constinit, i64 32, i1 false), !tbaa.struct !17
  %40 = getelementptr inbounds %struct.NUS_octree_t, ptr %6, i32 0, i32 3
  %41 = load double, ptr %5, align 8, !tbaa !9
  store double %41, ptr %40, align 8, !tbaa !19
  %42 = getelementptr inbounds %struct.NUS_octree_t, ptr %6, i32 0, i32 4
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %42, ptr align 8 %2, i64 24, i1 false), !tbaa.struct !20
  %43 = getelementptr inbounds %struct.NUS_octree_t, ptr %6, i32 0, i32 5
  store i8 0, ptr %43, align 8, !tbaa !21
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %35, ptr align 8 %6, i64 80, i1 false), !tbaa.struct !22
  %44 = load ptr, ptr @nus_octree_handles, align 8, !tbaa !11
  %45 = load i32, ptr @nus_octree_count, align 4, !tbaa !5
  %46 = sub i32 %45, 1
  %47 = zext i32 %46 to i64
  %48 = getelementptr inbounds i32, ptr %44, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !5
  ret i32 %49
}

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @nus_octree_next_handle() #0

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

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

; 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) #4

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 = { nounwind allocsize(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 = { 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { nounwind allocsize(1) }
attributes #6 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"NUS_octree_t", !6, i64 0, !6, i64 4, !7, i64 8, !10, i64 40, !15, i64 48, !7, i64 72}
!15 = !{!"NUS_vector", !10, i64 0, !10, i64 8, !10, i64 16}
!16 = !{!14, !6, i64 4}
!17 = !{i64 0, i64 32, !18}
!18 = !{!7, !7, i64 0}
!19 = !{!14, !10, i64 40}
!20 = !{i64 0, i64 8, !9, i64 8, i64 8, !9, i64 16, i64 8, !9}
!21 = !{!14, !7, i64 72}
!22 = !{i64 0, i64 4, !5, i64 4, i64 4, !5, i64 8, i64 32, !18, i64 40, i64 8, !9, i64 48, i64 8, !9, i64 56, i64 8, !9, i64 64, i64 8, !9, i64 72, i64 1, !18}


; 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"

%struct.NUS_vector = type { double, double, double }

@nus_octree_handles = external hidden local_unnamed_addr global ptr, align 8
@nus_octree_count = external hidden local_unnamed_addr global i32, align 4
@nus_octrees = external hidden local_unnamed_addr global ptr, align 8
@constinit = external hidden local_unnamed_addr constant [8 x i32], align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @nus_octree_build(i32 noundef %0, double noundef %1, ptr nocapture noundef readonly byval(%struct.NUS_vector) align 8 %2) local_unnamed_addr #0 {
  %4 = load ptr, ptr @nus_octree_handles, align 8, !tbaa !5
  %5 = load i32, ptr @nus_octree_count, align 4, !tbaa !9
  %6 = add i32 %5, 1
  %7 = zext i32 %6 to i64
  %8 = shl nuw nsw i64 %7, 2
  %9 = tail call ptr @realloc(ptr noundef %4, i64 noundef %8) #4
  store ptr %9, ptr @nus_octree_handles, align 8, !tbaa !5
  %10 = tail call i32 @nus_octree_next_handle()
  %11 = load ptr, ptr @nus_octree_handles, align 8, !tbaa !5
  %12 = load i32, ptr @nus_octree_count, align 4, !tbaa !9
  %13 = zext i32 %12 to i64
  %14 = getelementptr inbounds i32, ptr %11, i64 %13
  store i32 %10, ptr %14, align 4, !tbaa !9
  %15 = load ptr, ptr @nus_octrees, align 8, !tbaa !5
  %16 = load i32, ptr @nus_octree_count, align 4, !tbaa !9
  %17 = add i32 %16, 1
  store i32 %17, ptr @nus_octree_count, align 4, !tbaa !9
  %18 = zext i32 %17 to i64
  %19 = shl nuw nsw i64 %18, 3
  %20 = tail call ptr @realloc(ptr noundef %15, i64 noundef %19) #4
  store ptr %20, ptr @nus_octrees, align 8, !tbaa !5
  %21 = tail call noalias dereferenceable_or_null(80) ptr @malloc(i64 noundef 80) #5
  %22 = load i32, ptr @nus_octree_count, align 4, !tbaa !9
  %23 = add i32 %22, -1
  %24 = zext i32 %23 to i64
  %25 = getelementptr inbounds ptr, ptr %20, i64 %24
  store ptr %21, ptr %25, align 8, !tbaa !5
  store i32 0, ptr %21, align 8, !tbaa !9
  %.sroa.2.0..sroa_idx = getelementptr inbounds i8, ptr %21, i64 4
  store i32 %0, ptr %.sroa.2.0..sroa_idx, align 4, !tbaa !9
  %.sroa.3.0..sroa_idx = getelementptr inbounds i8, ptr %21, i64 8
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %.sroa.3.0..sroa_idx, ptr noundef nonnull align 4 dereferenceable(32) @constinit, i64 32, i1 false)
  %.sroa.5.0..sroa_idx = getelementptr inbounds i8, ptr %21, i64 40
  store double %1, ptr %.sroa.5.0..sroa_idx, align 8, !tbaa !11
  %.sroa.6.0..sroa_idx = getelementptr inbounds i8, ptr %21, i64 48
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %.sroa.6.0..sroa_idx, ptr noundef nonnull align 8 dereferenceable(24) %2, i64 24, i1 false)
  %.sroa.7.0..sroa_idx = getelementptr inbounds i8, ptr %21, i64 72
  store i8 0, ptr %.sroa.7.0..sroa_idx, align 8, !tbaa !13
  %26 = load ptr, ptr @nus_octree_handles, align 8, !tbaa !5
  %27 = getelementptr inbounds i32, ptr %26, i64 %24
  %28 = load i32, ptr %27, align 4, !tbaa !9
  ret i32 %28
}

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @nus_octree_next_handle() local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #2

; 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) #3

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 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, 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 #2 = { 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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nounwind allocsize(1) }
attributes #5 = { 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = !{!7, !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,DSEPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,MemCpyOptPass,PostOrderFunctionAttrsPass,SROAPass,TailCallElimPass