// File: /workspace/github_c_makefile_star2_O2/mesbahamin[P]star-sim/src/sim.c
// Function: sim_update
void sim_update(struct SimState *sim_state, int field_width, int field_height)
{
    if (!sim_state)
    {
        // TODO: handle invalid pointer error
        return;
    }

    float min_x = sim_state->bounding_box.center_x;
    float max_x = sim_state->bounding_box.center_x;
    float min_y = sim_state->bounding_box.center_y;
    float max_y = sim_state->bounding_box.center_y;

    int num_stars = sim_state->num_stars;
    struct Star *stars = sim_state->stars;
    struct QuadTree *qt = sim_state->qt;


    if (BRUTE_FORCE)
    {
        for (int i = 0; i < num_stars; ++i)
        {
            for (int j = i + 1; j < num_stars; ++j)
            {
                star_attract(&stars[i], &stars[j]);
            }
        }
    }
    else
    {
        quad_tree_node_free(qt->root);
        qt->root = quad_tree_node_init(
                sim_state->bounding_box.center_x,
                sim_state->bounding_box.center_y,
                sim_state->bounding_box.side_length_x / 2,
                sim_state->bounding_box.side_length_y / 2);

        for (int i = 0; i < num_stars; ++i)
        {
            quad_tree_node_insert_star(qt->root, &(stars[i]));
        }
        for (int i = 0; i < num_stars; ++i)
        {
            // TODO: Will this result in stars being attracted to each other
            // twice?
            quad_tree_calc_force_on_star(qt->root, &(stars[i]));
        }
    }
    for (int i = 0; i < num_stars; ++i)
    {
        stars[i].x += sinf(stars[i].angle) * stars[i].speed;
        stars[i].y -= cosf(stars[i].angle) * stars[i].speed;
        stars[i].speed *= DRAG;

#ifdef RESPAWN_STARS
        if (stars[i].x < 0 || stars[i].x > field_width
            || stars[i].y < 0 || stars[i].y > field_height)
        {
            stars[i].x = rand() % field_width;
            stars[i].y = rand() % field_height;
            stars[i].angle = 0;
            stars[i].speed = 0;
        }
#endif

        if (stars[i].x <= min_x)
        {
            min_x = stars[i].x;
        }
        else if (stars[i].x >= max_x)
        {
            max_x = stars[i].x;
        }
        if (stars[i].y <= min_y)
        {
            min_y = stars[i].y;
        }
        else if (stars[i].y >= max_y)
        {
            max_y = stars[i].y;
        }
    }

    sim_bounding_box_update(&sim_state->bounding_box, min_x, min_y, max_x, max_y);
}

// Assembly:
testq %rdi, %rdi
je 0x1ecd
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movsd 0x4(%rdi), %xmm2
movl (%rdi), %ebp
leaq 0x28(%rdi), %r13
cmpb $0x1, 0x347b(%rip)      # 0x5151
movaps %xmm2, 0x10(%rsp)
jne 0x1d4c
movq %r13, 0x30(%rsp)
xorl %r13d, %r13d
testl %ebp, %ebp
movl $0x0, %eax
cmovgl %ebp, %eax
movq %rax, (%rsp)
leaq 0x44(%rbx), %r12
pushq $0x1
popq %r14
cmpq (%rsp), %r13
je 0x1dcb
imulq $0x1c, %r13, %r15
addq 0x30(%rsp), %r15
movl %r14d, 0xc(%rsp)
movq %r12, 0x20(%rsp)
cmpl %r14d, %ebp
jle 0x1d36
movq %r15, %rdi
movq %r12, %rsi
callq 0x274f
movaps 0x10(%rsp), %xmm2
addq $0x1c, %r12
incl %r14d
jmp 0x1d18
incq %r13
movq 0x20(%rsp), %r12
addq $0x1c, %r12
movl 0xc(%rsp), %r14d
incl %r14d
jmp 0x1cfb
movq 0x20(%rbx), %r12
movq (%r12), %rdi
callq 0x29f8
movss 0x4(%rbx), %xmm0
movss 0x8(%rbx), %xmm1
movss 0x1349(%rip), %xmm3     # 0x30b4
movss 0xc(%rbx), %xmm2
mulss %xmm3, %xmm2
mulss 0x10(%rbx), %xmm3
callq 0x2999
movq %rax, (%r12)
xorl %r14d, %r14d
testl %ebp, %ebp
cmovgl %ebp, %r14d
movq %r13, %r15
movq %r14, (%rsp)
subq $0x1, %r14
jb 0x1daa
movq (%r12), %rdi
movq %r15, %rsi
callq 0x2b3d
addq $0x1c, %r15
jmp 0x1d92
movq (%rsp), %r14
subq $0x1, %r14
jb 0x1dc6
movq (%r12), %rdi
movq %r13, %rsi
callq 0x2ce8
addq $0x1c, %r13
jmp 0x1dae
movaps 0x10(%rsp), %xmm2
imulq $0x1c, (%rsp), %r14
xorl %r15d, %r15d
movaps %xmm2, %xmm3
cmpq %r15, %r14
movaps %xmm2, 0x10(%rsp)
movaps %xmm3, 0x20(%rsp)
je 0x1e8e
movss 0x28(%rbx,%r15), %xmm0
callq 0x1130
mulss 0x2c(%rbx,%r15), %xmm0
addss 0x38(%rbx,%r15), %xmm0
movss %xmm0, 0x38(%rbx,%r15)
movss 0x28(%rbx,%r15), %xmm0
callq 0x10f0
movaps 0x20(%rsp), %xmm5
movss 0x38(%rbx,%r15), %xmm3
movss 0x3c(%rbx,%r15), %xmm1
mulss 0x2c(%rbx,%r15), %xmm0
subss %xmm0, %xmm1
movss %xmm1, 0x3c(%rbx,%r15)
ucomiss %xmm3, %xmm5
jae 0x1e52
movaps %xmm5, %xmm0
movaps 0x10(%rsp), %xmm4
ucomiss %xmm4, %xmm3
jae 0x1e5f
movaps %xmm4, %xmm2
jmp 0x1e62
movaps 0x10(%rsp), %xmm4
movaps %xmm4, %xmm2
movaps %xmm3, %xmm0
jmp 0x1e62
movaps %xmm3, %xmm2
shufps $0x55, %xmm5, %xmm5     # xmm5 = xmm5[1,1,1,1]
ucomiss %xmm1, %xmm5
shufps $0x55, %xmm4, %xmm4     # xmm4 = xmm4[1,1,1,1]
jae 0x1e79
ucomiss %xmm4, %xmm1
jb 0x1e7c
movaps %xmm1, %xmm4
jmp 0x1e7c
movaps %xmm1, %xmm5
unpcklps %xmm5, %xmm0            # xmm0 = xmm0[0],xmm5[0],xmm0[1],xmm5[1]
unpcklps %xmm4, %xmm2            # xmm2 = xmm2[0],xmm4[0],xmm2[1],xmm4[1]
addq $0x1c, %r15
movaps %xmm0, %xmm3
jmp 0x1dd6
movaps %xmm2, %xmm0
subps %xmm3, %xmm0
movaps %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm1     # xmm1 = xmm1[1,1],xmm0[1,1]
callq 0x1190
movss %xmm0, 0xc(%rbx)
movss %xmm0, 0x10(%rbx)
movaps 0x10(%rsp), %xmm0
addps 0x20(%rsp), %xmm0
mulps 0x11d5(%rip), %xmm0     # 0x3090
movlps %xmm0, 0x4(%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq