// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movl %esi, 0x4c(%rsp)
movl %edx, 0x48(%rsp)
cmpq $0x0, 0x50(%rsp)
jne 0x22ee
jmp 0x26bf
movq 0x50(%rsp), %rax
movss 0x4(%rax), %xmm0
movss %xmm0, 0x44(%rsp)
movq 0x50(%rsp), %rax
movss 0x4(%rax), %xmm0
movss %xmm0, 0x40(%rsp)
movq 0x50(%rsp), %rax
movss 0x8(%rax), %xmm0
movss %xmm0, 0x3c(%rsp)
movq 0x50(%rsp), %rax
movss 0x8(%rax), %xmm0
movss %xmm0, 0x38(%rsp)
movq 0x50(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x34(%rsp)
movq 0x50(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x28(%rsp)
movq 0x50(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x20(%rsp)
testb $0x1, 0x3de5(%rip)      # 0x6141
je 0x23cd
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0x23c8
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
movl 0x18(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0x23b9
movq 0x28(%rsp), %rdi
movslq 0x1c(%rsp), %rax
imulq $0x1c, %rax, %rax
addq %rax, %rdi
movq 0x28(%rsp), %rsi
movslq 0x18(%rsp), %rax
imulq $0x1c, %rax, %rax
addq %rax, %rsi
callq 0x33c0
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x237b
jmp 0x23bb
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x2366
jmp 0x24a6
movq 0x20(%rsp), %rax
movq (%rax), %rdi
callq 0x3880
movq 0x50(%rsp), %rax
movss 0x4(%rax), %xmm0
movq 0x50(%rsp), %rax
movss 0x8(%rax), %xmm1
movq 0x50(%rsp), %rax
movss 0xc(%rax), %xmm2
movss 0x1c88(%rip), %xmm3     # 0x4088
divss %xmm3, %xmm2
movq 0x50(%rsp), %rax
movss 0x10(%rax), %xmm3
movss 0x1c72(%rip), %xmm4     # 0x4088
divss %xmm4, %xmm3
callq 0x37e0
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, (%rax)
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0x2467
movq 0x20(%rsp), %rax
movq (%rax), %rdi
movq 0x28(%rsp), %rsi
movslq 0x14(%rsp), %rax
imulq $0x1c, %rax, %rax
addq %rax, %rsi
callq 0x3ad0
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x2432
movl $0x0, 0x10(%rsp)
movl 0x10(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0x24a4
movq 0x20(%rsp), %rax
movq (%rax), %rdi
movq 0x28(%rsp), %rsi
movslq 0x10(%rsp), %rax
imulq $0x1c, %rax, %rax
addq %rax, %rsi
callq 0x3e50
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x246f
jmp 0x24a6
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0x2699
movq 0x28(%rsp), %rax
movslq 0xc(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
callq 0x1130
movq 0x28(%rsp), %rax
movslq 0xc(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm2
movq 0x28(%rsp), %rax
movslq 0xc(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x10(%rax), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x10(%rax)
movq 0x28(%rsp), %rax
movslq 0xc(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
callq 0x10f0
movq 0x28(%rsp), %rax
movslq 0xc(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm2
movq 0x28(%rsp), %rax
movslq 0xc(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x14(%rax), %xmm1
movd %xmm0, %ecx
xorl $0x80000000, %ecx       # imm = 0x80000000
movd %ecx, %xmm0
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x14(%rax)
movq 0x28(%rsp), %rax
movslq 0xc(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x1a87(%rip), %xmm0     # 0x4010
mulss 0x4(%rax), %xmm0
movss %xmm0, 0x4(%rax)
movq 0x28(%rsp), %rax
movslq 0xc(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x10(%rax), %xmm1
movss 0x44(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jb 0x25d2
movq 0x28(%rsp), %rax
movslq 0xc(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x10(%rax), %xmm0
movss %xmm0, 0x44(%rsp)
jmp 0x260d
movq 0x28(%rsp), %rax
movslq 0xc(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x10(%rax), %xmm0
ucomiss 0x40(%rsp), %xmm0
jb 0x260b
movq 0x28(%rsp), %rax
movslq 0xc(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x10(%rax), %xmm0
movss %xmm0, 0x40(%rsp)
jmp 0x260d
movq 0x28(%rsp), %rax
movslq 0xc(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x14(%rax), %xmm1
movss 0x3c(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jb 0x264c
movq 0x28(%rsp), %rax
movslq 0xc(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x14(%rax), %xmm0
movss %xmm0, 0x3c(%rsp)
jmp 0x2687
movq 0x28(%rsp), %rax
movslq 0xc(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x14(%rax), %xmm0
ucomiss 0x38(%rsp), %xmm0
jb 0x2685
movq 0x28(%rsp), %rax
movslq 0xc(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x14(%rax), %xmm0
movss %xmm0, 0x38(%rsp)
jmp 0x2687
jmp 0x2689
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x24ae
movq 0x50(%rsp), %rdi
addq $0x4, %rdi
movss 0x44(%rsp), %xmm0
movss 0x3c(%rsp), %xmm1
movss 0x40(%rsp), %xmm2
movss 0x38(%rsp), %xmm3
callq 0x2200
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)