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

@nus_octree_handles = external hidden global ptr, align 8
@nus_octree_count = external hidden global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @nus_octree_next_handle() #0 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = load i32, ptr @nus_octree_count, align 4, !tbaa !5
  %7 = zext i32 %6 to i64
  %8 = call ptr @llvm.stacksave.p0()
  store ptr %8, ptr %2, align 8
  %9 = alloca i32, i64 %7, align 16
  store i64 %7, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %30, %0
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = load i32, ptr @nus_octree_count, align 4, !tbaa !5
  %13 = icmp ult i32 %11, %12
  br i1 %13, label %14, label %33

14:                                               ; preds = %10
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = zext i32 %15 to i64
  %17 = getelementptr inbounds i32, ptr %9, i64 %16
  store i32 0, ptr %17, align 4, !tbaa !5
  %18 = load ptr, ptr @nus_octree_handles, align 8, !tbaa !9
  %19 = load i32, ptr %4, align 4, !tbaa !5
  %20 = zext i32 %19 to i64
  %21 = getelementptr inbounds i32, ptr %18, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = load i32, ptr @nus_octree_count, align 4, !tbaa !5
  %24 = icmp ult i32 %22, %23
  br i1 %24, label %25, label %29

25:                                               ; preds = %14
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = zext i32 %26 to i64
  %28 = getelementptr inbounds i32, ptr %9, i64 %27
  store i32 1, ptr %28, align 4, !tbaa !5
  br label %33

29:                                               ; preds = %14
  br label %30

30:                                               ; preds = %29
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = add i32 %31, 1
  store i32 %32, ptr %4, align 4, !tbaa !5
  br label %10, !llvm.loop !11

33:                                               ; preds = %25, %10
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %34

34:                                               ; preds = %47, %33
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = load i32, ptr @nus_octree_count, align 4, !tbaa !5
  %37 = icmp ult i32 %35, %36
  br i1 %37, label %38, label %50

38:                                               ; preds = %34
  %39 = load i32, ptr %4, align 4, !tbaa !5
  %40 = zext i32 %39 to i64
  %41 = getelementptr inbounds i32, ptr %9, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = icmp eq i32 0, %42
  br i1 %43, label %44, label %46

44:                                               ; preds = %38
  %45 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %45, ptr %1, align 4
  store i32 1, ptr %5, align 4
  br label %52

46:                                               ; preds = %38
  br label %47

47:                                               ; preds = %46
  %48 = load i32, ptr %4, align 4, !tbaa !5
  %49 = add i32 %48, 1
  store i32 %49, ptr %4, align 4, !tbaa !5
  br label %34, !llvm.loop !13

50:                                               ; preds = %34
  %51 = load i32, ptr @nus_octree_count, align 4, !tbaa !5
  store i32 %51, ptr %1, align 4
  store i32 1, ptr %5, align 4
  br label %52

52:                                               ; preds = %50, %44
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %53 = load ptr, ptr %2, align 8
  call void @llvm.stackrestore.p0(ptr %53)
  %54 = load i32, ptr %1, align 4
  ret i32 %54
}

; 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: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind willreturn }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

@nus_octree_handles = external hidden local_unnamed_addr global ptr, align 8
@nus_octree_count = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local i32 @nus_octree_next_handle() local_unnamed_addr #0 {
  %1 = load i32, ptr @nus_octree_count, align 4, !tbaa !5
  %2 = zext i32 %1 to i64
  %3 = alloca i32, i64 %2, align 16
  %.not = icmp eq i32 %1, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0
  %4 = load ptr, ptr @nus_octree_handles, align 8, !tbaa !9
  br label %6

5:                                                ; preds = %6
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %2
  br i1 %exitcond.not, label %.loopexit, label %6, !llvm.loop !11

6:                                                ; preds = %.lr.ph, %5
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %5 ]
  %7 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv
  store i32 0, ptr %7, align 4, !tbaa !5
  %8 = getelementptr inbounds i32, ptr %4, i64 %indvars.iv
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = icmp ult i32 %9, %1
  br i1 %10, label %.loopexit.thread26, label %5

.loopexit.thread26:                               ; preds = %6
  %11 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv
  store i32 1, ptr %11, align 4, !tbaa !5
  br label %.lr.ph15.preheader

.loopexit:                                        ; preds = %5
  br i1 %.not, label %._crit_edge, label %.lr.ph15.preheader

.lr.ph15.preheader:                               ; preds = %.loopexit.thread26, %.loopexit
  br label %.lr.ph15

.lr.ph15:                                         ; preds = %.lr.ph15.preheader, %15
  %indvars.iv21 = phi i64 [ %indvars.iv.next22, %15 ], [ 0, %.lr.ph15.preheader ]
  %12 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv21
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %._crit_edge.loopexit.split.loop.exit28, label %15

15:                                               ; preds = %.lr.ph15
  %indvars.iv.next22 = add nuw nsw i64 %indvars.iv21, 1
  %exitcond25.not = icmp eq i64 %indvars.iv.next22, %2
  br i1 %exitcond25.not, label %._crit_edge, label %.lr.ph15, !llvm.loop !13

._crit_edge.loopexit.split.loop.exit28:           ; preds = %.lr.ph15
  %16 = trunc nuw i64 %indvars.iv21 to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %15, %._crit_edge.loopexit.split.loop.exit28, %0, %.loopexit
  %.010 = phi i32 [ 0, %.loopexit ], [ 0, %0 ], [ %16, %._crit_edge.loopexit.split.loop.exit28 ], [ %1, %15 ]
  ret i32 %.010
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) 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, !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 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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