// File: /workspace/github_c_makefile_star2_O1/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 0x1f8a
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movsd 0x4(%rdi), %xmm0
movaps %xmm0, 0x30(%rsp)
movl (%rdi), %r15d
leaq 0x28(%rdi), %rax
movq %rax, (%rsp)
cmpb $0x1, 0x43e5(%rip)      # 0x6149
jne 0x1ddc
testl %r15d, %r15d
jle 0x1e5e
leaq 0x44(%rbx), %rbp
movl $0x1, %r13d
xorl %ecx, %ecx
movq %r15, 0x10(%rsp)
movq %r15, %rax
movq %rcx, %r15
incq %rcx
movq %rcx, 0x20(%rsp)
cmpq %rax, %rcx
jae 0x1dc1
leaq (%r15,%r15,8), %rax
leaq (%rax,%rax,2), %rax
addq (%rsp), %r15
addq %rax, %r15
movq 0x10(%rsp), %r14
movq %rbp, %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x2918
addq $0x1c, %r12
decq %r14
cmpq %r14, %r13
jne 0x1daa
incq %r13
addq $0x1c, %rbp
movq 0x10(%rsp), %r15
movq 0x20(%rsp), %rcx
cmpq %r15, %rcx
jne 0x1d80
jmp 0x1e5e
movq 0x20(%rbx), %r12
movq (%r12), %rdi
callq 0x2c50
movss 0x4(%rbx), %xmm0
movss 0x8(%rbx), %xmm1
movss 0x2295(%rip), %xmm3     # 0x4090
movss 0xc(%rbx), %xmm2
mulss %xmm3, %xmm2
mulss 0x10(%rbx), %xmm3
callq 0x2bb7
movq %rax, (%r12)
movq %r15, %r13
testl %r15d, %r15d
jle 0x1e36
movq %r13, %r14
movq (%rsp), %r15
movq (%r12), %rdi
movq %r15, %rsi
callq 0x2da8
addq $0x1c, %r15
decq %r14
jne 0x1e21
movq %r13, %r15
testl %r15d, %r15d
jle 0x1e5e
movq %r15, %r14
movq (%r12), %rdi
movq (%rsp), %r13
movq %r13, %rsi
callq 0x2faf
addq $0x1c, %r13
movq %r13, (%rsp)
decq %r14
jne 0x1e41
testl %r15d, %r15d
jle 0x1f37
movaps 0x30(%rsp), %xmm3
movaps %xmm3, %xmm0
shufps $0x55, %xmm3, %xmm0     # xmm0 = xmm0[1,1],xmm3[1,1]
leaq (%r15,%r15,8), %rax
leaq (%rax,%rax,2), %r14
addq %r15, %r14
xorl %r15d, %r15d
movaps %xmm3, 0x20(%rsp)
movaps %xmm0, (%rsp)
movaps %xmm0, 0x10(%rsp)
movaps %xmm3, 0x30(%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 0x30(%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 0x1ef9
ucomiss 0x20(%rsp), %xmm2
jb 0x1efc
movaps %xmm2, 0x20(%rsp)
jmp 0x1efc
movaps %xmm2, %xmm3
movaps (%rsp), %xmm0
ucomiss %xmm1, %xmm0
jae 0x1f13
ucomiss 0x10(%rsp), %xmm1
jb 0x1f17
movaps %xmm1, 0x10(%rsp)
jmp 0x1f17
movaps %xmm1, (%rsp)
addq $0x1c, %r15
cmpq %r15, %r14
jne 0x1e8f
movaps %xmm3, %xmm0
unpcklps (%rsp), %xmm0           # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
movaps 0x20(%rsp), %xmm5
unpcklps 0x10(%rsp), %xmm5       # xmm5 = xmm5[0],mem[0],xmm5[1],mem[1]
jmp 0x1f3f
movaps 0x30(%rsp), %xmm5
movaps %xmm5, %xmm0
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 0x2148(%rip), %xmm5     # 0x40c0
movlps %xmm5, 0x4(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq