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

    sim_state->num_stars = NUM_STARS;

    sim_state->bounding_box.center_x = field_width / 2;
    sim_state->bounding_box.center_y = field_height / 2;
    sim_state->bounding_box.side_length_x = 0;
    sim_state->bounding_box.side_length_y = 0;

    sim_state->view.dx = 0;
    sim_state->view.dy = 0;
    sim_state->view.zoom = 1;

    float min_x = field_width / 2;
    float max_x = field_width / 2;
    float min_y = field_height / 2;
    float max_y = field_height / 2;

    for (int i = 0; i < sim_state->num_stars; ++i)
    {
        int star_x = rand() % field_width;
        int star_y = rand() % field_height;

        struct Star *star = &sim_state->stars[i];
        star->x = star_x;
        star->y = star_y;
        star->angle = ((float)rand()/(float)(RAND_MAX)) * 2 * M_PI;
        star->speed = 0;
        star->mass = 5;
        star->size = star_calc_size(star->mass);
        star->color = COLOR_WHITE;
        //printf("%f, %f, %f\n", star->angle, star->speed, star->mass);

        if (star_x <= min_x)
        {
            min_x = star_x;
        }
        else if (star_x >= max_x)
        {
            max_x = star_x;
        }
        if (star_y <= min_y)
        {
            min_y = star_y;
        }
        else if (star_y >= max_y)
        {
            max_y = star_y;
        }
    }
    sim_bounding_box_update(&sim_state->bounding_box, min_x, min_y, max_x, max_y);

    sim_state->qt = quad_tree_init();
}

// Assembly:
testq %rdi, %rdi
je 0x1c40
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %ebp
movl %esi, %r14d
movq %rdi, %rbx
movl $0x64, (%rdi)
movd %edx, %xmm0
movd %esi, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movdqa %xmm1, %xmm0
psrld $0x1f, %xmm0
paddd %xmm1, %xmm0
psrad $0x1, %xmm0
cvtdq2ps %xmm0, %xmm2
movlps %xmm2, 0x4(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0xc(%rdi)
movl $0x3f800000, 0x1c(%rdi) # imm = 0x3F800000
leaq 0x40(%rdi), %r12
pushq $0x64
popq %rax
xorl %r13d, %r13d
movaps %xmm2, %xmm3
cltq
cmpq %rax, %r13
movaps %xmm2, 0x10(%rsp)
movaps %xmm3, 0x20(%rsp)
jge 0x1bf8
callq 0x1090
cltd
idivl %r14d
movl %edx, %r15d
callq 0x1090
cvtsi2ss %r15d, %xmm0
movss %xmm0, 0xc(%rsp)
cltd
idivl %ebp
movss %xmm0, -0x8(%r12)
cvtsi2ss %edx, %xmm0
movss %xmm0, 0x8(%rsp)
movss %xmm0, -0x4(%r12)
callq 0x1090
cvtsi2ss %eax, %xmm0
mulss 0x156a(%rip), %xmm0     # 0x30b0
addss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
mulsd 0x156a(%rip), %xmm0     # 0x30c0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x18(%r12)
movsd 0x1537(%rip), %xmm0     # 0x30a0
movsd %xmm0, -0x14(%r12)
movss 0x1494(%rip), %xmm0     # 0x300c
callq 0x2737
movss 0xc(%rsp), %xmm4
movaps 0x20(%rsp), %xmm3
movss %xmm0, -0xc(%r12)
movl $0xffffff, (%r12)       # imm = 0xFFFFFF
ucomiss %xmm4, %xmm3
jae 0x1bae
movaps %xmm3, %xmm0
movaps 0x10(%rsp), %xmm2
ucomiss %xmm2, %xmm4
jae 0x1bbb
movaps %xmm2, %xmm1
jmp 0x1bbe
movaps 0x10(%rsp), %xmm2
movaps %xmm2, %xmm1
movaps %xmm4, %xmm0
jmp 0x1bbe
movaps %xmm4, %xmm1
movss 0x8(%rsp), %xmm4
shufps $0x55, %xmm3, %xmm3     # xmm3 = xmm3[1,1,1,1]
ucomiss %xmm4, %xmm3
shufps $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
jae 0x1bdb
ucomiss %xmm2, %xmm4
jb 0x1bde
movaps %xmm4, %xmm2
jmp 0x1bde
movaps %xmm4, %xmm3
unpcklps %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
unpcklps %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
incq %r13
movl (%rbx), %eax
addq $0x1c, %r12
movaps %xmm1, %xmm2
movaps %xmm0, %xmm3
jmp 0x1ae9
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 0x146b(%rip), %xmm0     # 0x3090
movlps %xmm0, 0x4(%rbx)
callq 0x2c73
movq %rax, 0x20(%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq