// File: /workspace/github_c_makefile_star2_O0/AlexCharlton[P]Hyperscene/src/aabb-tree.c
// Function: splitTree
static void splitTree(AABBtree *tree){
    HPSvector *nodes = &tree->nodes;
    int nCurrentNodes = nodes->size;
    int i;
    setSplitLocation(tree);
    setSplitDirection(tree);
    for (i = 0; i < nCurrentNodes; i++){
	Node *node = nodes->data[i];
	AABBtree *branch = whichBranch(tree, node->boundingSphere);
	if (branch !=tree){
            addNode(node, branch);
            nodes->data[i] = NULL;
	}
    }
#ifdef DEBUG
    printf("Split tree %p at (%f, %f, %f) along: ", tree, tree->splitPoint.x,
           tree->splitPoint.y, tree->splitPoint.x);
    if (tree->split & SPLIT_X) printf("x-axis ");
    if (tree->split & SPLIT_Y) printf("y-axis ");
    if (tree->split & SPLIT_Z) printf("z-axis ");
    printf("\n");
#endif
    for (i = 0; i < nodes->size;){
	if (!nodes->data[i]){
	    hpsRemoveNth(nodes, i);
	} else {
	    i++;
	}
    }
    for (i = 0; i < 27; i++){
	AABBtree *child = tree->children[i];
	if (child){
	    if (child->nodes.size == nCurrentNodes)
		goto abort;
	}
    }
    for (i = 0; i < 27; i++){
	AABBtree *child = tree->children[i];
	if (child){
	    updateExtents(child);
	}
    }
    return;
abort:
#ifdef DEBUG
    printf("Aborting split of tree %p\n", tree);
#endif
    for (i = 0; i < 27; i++){
	AABBtree *child = tree->children[i];
	if (child){
	    Node *n;
	    while ((n = hpsPop(&child->nodes))){
                addNode(n, tree);
	    }
	}
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x118, %rax            # imm = 0x118
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rdi
callq 0xafa0
movq -0x8(%rbp), %rdi
callq 0xb0b0
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0xae64
movq -0x10(%rbp), %rax
movq (%rax), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
callq 0x9b30
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x8(%rbp), %rax
je 0xae57
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x43f0
movq -0x10(%rbp), %rax
movq (%rax), %rax
movslq -0x18(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
jmp 0xae59
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xadfe
movl $0x0, -0x18(%rbp)
movslq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0xaea5
movq -0x10(%rbp), %rax
movq (%rax), %rax
movslq -0x18(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0xae9a
movq -0x10(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x40b0
jmp 0xaea3
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xae6b
movl $0x0, -0x18(%rbp)
cmpl $0x1b, -0x18(%rbp)
jge 0xaeef
movq -0x8(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xaee2
movq -0x30(%rbp), %rax
movq 0x120(%rax), %rax
movslq -0x14(%rbp), %rcx
cmpq %rcx, %rax
jne 0xaee0
jmp 0xaf2c
jmp 0xaee2
jmp 0xaee4
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xaeac
movl $0x0, -0x18(%rbp)
cmpl $0x1b, -0x18(%rbp)
jge 0xaf2a
movq -0x8(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0xaf1d
movq -0x38(%rbp), %rdi
callq 0xa970
jmp 0xaf1f
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xaef6
jmp 0xaf8b
movl $0x0, -0x18(%rbp)
cmpl $0x1b, -0x18(%rbp)
jge 0xaf8b
movq -0x8(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0xaf7e
jmp 0xaf53
movq -0x40(%rbp), %rdi
addq $0x118, %rdi            # imm = 0x118
callq 0x4390
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
je 0xaf7c
movq -0x48(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x43f0
jmp 0xaf53
jmp 0xaf7e
jmp 0xaf80
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xaf33
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)