// File: /workspace/github_c_makefile_star2_O3/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 0x1f94
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
movsd 0x4(%rdi), %xmm5
movl (%rdi), %r14d
leaq 0x28(%rdi), %rax
movq %rax, 0x10(%rsp)
cmpb $0x1, 0x4400(%rip)      # 0x6149
movq %r14, (%rsp)
jne 0x1dc1
testl %r14d, %r14d
jle 0x1f37
movaps %xmm5, 0x20(%rsp)
movq %rbx, 0x38(%rsp)
leaq 0x44(%rbx), %rbp
movl $0x1, %r13d
xorl %r14d, %r14d
movq (%rsp), %rbx
movq %r14, %r15
incq %r14
cmpq %rbx, %r14
jae 0x1dac
leaq (%r15,%r15,8), %rax
leaq (%rax,%rax,2), %rax
addq 0x10(%rsp), %r15
addq %rax, %r15
movq (%rsp), %rbx
movq %rbp, %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x2938
addq $0x1c, %r12
decq %rbx
cmpq %rbx, %r13
jne 0x1d95
incq %r13
addq $0x1c, %rbp
movq (%rsp), %rbx
cmpq %rbx, %r14
jne 0x1d73
jmp 0x1e55
movaps %xmm5, 0x20(%rsp)
movq 0x20(%rbx), %r12
movq (%r12), %rdi
callq 0x2c70
movss 0x4(%rbx), %xmm0
movss 0x8(%rbx), %xmm1
movss 0x22ab(%rip), %xmm3     # 0x4090
movss 0xc(%rbx), %xmm2
mulss %xmm3, %xmm2
mulss 0x10(%rbx), %xmm3
callq 0x2bd7
movq %rax, (%r12)
testl %r14d, %r14d
jle 0x1f32
movq 0x10(%rsp), %r15
movq (%r12), %rdi
movq %r15, %rsi
callq 0x2dc8
addq $0x1c, %r15
decq %r14
jne 0x1e0a
movq (%rsp), %rax
testl %eax, %eax
jle 0x1f32
movq %rbx, 0x38(%rsp)
movq %rax, %r14
movq %rax, %rbx
movq (%r12), %rdi
movq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x2fd1
addq $0x1c, %r15
movq %r15, 0x10(%rsp)
decq %r14
jne 0x1e36
testl %ebx, %ebx
jle 0x1f3c
movaps 0x20(%rsp), %xmm3
movaps %xmm3, %xmm0
shufps $0x55, %xmm3, %xmm0     # xmm0 = xmm0[1,1],xmm3[1,1]
leaq (%rbx,%rbx,8), %rax
leaq (%rax,%rax,2), %r14
addq %rbx, %r14
xorl %r15d, %r15d
movaps %xmm3, (%rsp)
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, 0x40(%rsp)
movq 0x38(%rsp), %rbx
movaps %xmm3, 0x20(%rsp)
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), %xmm3
movss 0x38(%rbx,%r15), %xmm2
movss 0x3c(%rbx,%r15), %xmm1
mulss 0x2c(%rbx,%r15), %xmm0
subss %xmm0, %xmm1
movss %xmm1, 0x3c(%rbx,%r15)
ucomiss %xmm2, %xmm3
jae 0x1ef2
ucomiss (%rsp), %xmm2
jb 0x1ef5
movaps %xmm2, (%rsp)
jmp 0x1ef5
movaps %xmm2, %xmm3
movaps 0x10(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jae 0x1f0d
ucomiss 0x40(%rsp), %xmm1
jb 0x1f12
movaps %xmm1, 0x40(%rsp)
jmp 0x1f12
movaps %xmm1, 0x10(%rsp)
addq $0x1c, %r15
cmpq %r15, %r14
jne 0x1e8a
movaps %xmm3, %xmm0
unpcklps 0x10(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
movaps (%rsp), %xmm5
unpcklps 0x40(%rsp), %xmm5       # xmm5 = xmm5[0],mem[0],xmm5[1],mem[1]
jmp 0x1f49
movaps 0x20(%rsp), %xmm5
movaps %xmm5, %xmm0
jmp 0x1f49
movaps 0x20(%rsp), %xmm5
movaps %xmm5, %xmm0
movq 0x38(%rsp), %rbx
movaps %xmm5, %xmm1
subps %xmm0, %xmm1
movaps %xmm1, %xmm2
shufps $0x55, %xmm1, %xmm2     # xmm2 = xmm2[1,1],xmm1[1,1]
movaps %xmm1, %xmm3
cmpunordss %xmm1, %xmm3
movaps %xmm3, %xmm4
andps %xmm2, %xmm4
maxss %xmm1, %xmm2
andnps %xmm2, %xmm3
orps %xmm4, %xmm3
movss %xmm3, 0xc(%rbx)
movss %xmm3, 0x10(%rbx)
addps %xmm0, %xmm5
mulps 0x213e(%rip), %xmm5     # 0x40c0
movlps %xmm5, 0x4(%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq