// File: /workspace/github_c_makefile_star2_O3/mesbahamin[P]star-sim/src/barnes_hut.c
// Function: quad_tree_node_insert_star
void quad_tree_node_insert_star(struct QuadTreeNode *node, struct Star *star)
{
    if (node && cell_contains_star(node->cell, star))
    {
        if (quad_tree_node_is_leaf(node))
        {
            if (cell_is_empty(node->cell))
            {
                node->cell->star = star;
            }
            else
            {
                quad_tree_node_subdivide(node);

                quad_tree_node_insert_star(node->ne, node->cell->star);
                quad_tree_node_insert_star(node->nw, node->cell->star);
                quad_tree_node_insert_star(node->sw, node->cell->star);
                quad_tree_node_insert_star(node->se, node->cell->star);

                quad_tree_node_insert_star(node->ne, star);
                quad_tree_node_insert_star(node->nw, star);
                quad_tree_node_insert_star(node->sw, star);
                quad_tree_node_insert_star(node->se, star);

                node->cell->star = NULL;
            }
        }
        else
        {
            node->cell->mass_total += star->mass;
            node->cell->stars_sum_x += star->x * star->mass;
            node->cell->stars_sum_y += star->y * star->mass;

            float x_avg = node->cell->stars_sum_x / node->cell->mass_total;
            float y_avg = node->cell->stars_sum_y / node->cell->mass_total;

            node->cell->mass_center_x = x_avg;
            node->cell->mass_center_y = y_avg;

            quad_tree_node_insert_star(node->ne, star);
            quad_tree_node_insert_star(node->nw, star);
            quad_tree_node_insert_star(node->sw, star);
            quad_tree_node_insert_star(node->se, star);
        }
    }
}

// Assembly:
testq %rdi, %rdi
je 0x2f56
pushq %r14
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
movq (%rbx), %rax
movss 0x10(%r14), %xmm0
movss (%rax), %xmm1
movss 0x8(%rax), %xmm2
movaps %xmm1, %xmm3
addss %xmm2, %xmm3
ucomiss %xmm0, %xmm3
jb 0x2f4f
subss %xmm2, %xmm1
ucomiss %xmm1, %xmm0
jb 0x2f4f
movss 0x14(%r14), %xmm1
movss 0x4(%rax), %xmm2
movss 0xc(%rax), %xmm3
movaps %xmm2, %xmm4
addss %xmm3, %xmm4
ucomiss %xmm1, %xmm4
jb 0x2f4f
subss %xmm3, %xmm2
ucomiss %xmm2, %xmm1
jb 0x2f4f
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x2ebb
cmpq $0x0, 0x10(%rbx)
je 0x2ebb
cmpq $0x0, 0x18(%rbx)
je 0x2ebb
cmpq $0x0, 0x20(%rbx)
je 0x2ebb
movss 0x8(%r14), %xmm2
movss 0x10(%rax), %xmm3
addss %xmm2, %xmm3
movss %xmm3, 0x10(%rax)
movsd 0x1c(%rax), %xmm4
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
shufps $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0]
mulps %xmm0, %xmm2
addps %xmm4, %xmm2
movlps %xmm2, 0x1c(%rax)
unpcklps %xmm3, %xmm3            # xmm3 = xmm3[0,0,1,1]
divps %xmm3, %xmm2
movlps %xmm2, 0x14(%rax)
movq %r14, %rsi
callq 0x2dc8
movq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x2dc8
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0x2dc8
movq 0x20(%rbx), %rbx
testq %rbx, %rbx
jne 0x2ddb
jmp 0x2f4f
cmpq $0x0, 0x28(%rax)
je 0x2f4b
movq %rbx, %rdi
callq 0x2cd7
movq (%rbx), %rax
movq 0x8(%rbx), %rdi
movq 0x28(%rax), %rsi
callq 0x2dc8
movq (%rbx), %rax
movq 0x10(%rbx), %rdi
movq 0x28(%rax), %rsi
callq 0x2dc8
movq (%rbx), %rax
movq 0x18(%rbx), %rdi
movq 0x28(%rax), %rsi
callq 0x2dc8
movq (%rbx), %rax
movq 0x20(%rbx), %rdi
movq 0x28(%rax), %rsi
callq 0x2dc8
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x2dc8
movq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x2dc8
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0x2dc8
movq 0x20(%rbx), %rdi
movq %r14, %rsi
callq 0x2dc8
movq (%rbx), %rax
movq $0x0, 0x28(%rax)
jmp 0x2f4f
movq %r14, 0x28(%rax)
addq $0x8, %rsp
popq %rbx
popq %r14
retq