// File: /workspace/github_c_makefile_star2_O3/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 0x1cd0
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %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, %xmm1
movlps %xmm1, 0x4(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0xc(%rdi)
movl $0x3f800000, 0x1c(%rdi) # imm = 0x3F800000
movaps %xmm1, %xmm0
shufps $0x55, %xmm1, %xmm0     # xmm0 = xmm0[1,1],xmm1[1,1]
leaq 0x40(%rdi), %r12
xorl %r13d, %r13d
movaps %xmm1, 0x40(%rsp)
movaps %xmm1, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x10(%rsp)
callq 0x1090
cltd
idivl %r14d
movl %edx, %r15d
callq 0x1090
xorps %xmm0, %xmm0
cvtsi2ss %r15d, %xmm0
movss %xmm0, 0x8(%rsp)
cltd
idivl %ebp
movss %xmm0, -0x8(%r12)
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
movss %xmm0, 0xc(%rsp)
movss %xmm0, -0x4(%r12)
callq 0x1090
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x24cd(%rip), %xmm0     # 0x408c
addss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
mulsd 0x24d1(%rip), %xmm0     # 0x40a0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x18(%r12)
movsd 0x24ce(%rip), %xmm0     # 0x40b0
movsd %xmm0, -0x14(%r12)
movss 0x241b(%rip), %xmm0     # 0x400c
callq 0x2920
movss %xmm0, -0xc(%r12)
movl $0xffffff, (%r12)       # imm = 0xFFFFFF
movaps 0x40(%rsp), %xmm0
movss 0x8(%rsp), %xmm1
ucomiss %xmm1, %xmm0
movaps %xmm1, %xmm0
jae 0x1c26
ucomiss 0x20(%rsp), %xmm0
jb 0x1c2b
movaps %xmm0, 0x20(%rsp)
jmp 0x1c2b
movaps %xmm0, 0x40(%rsp)
movaps 0x30(%rsp), %xmm0
movss 0xc(%rsp), %xmm1
ucomiss %xmm1, %xmm0
movaps %xmm1, %xmm0
jae 0x1c4c
ucomiss 0x10(%rsp), %xmm0
jb 0x1c51
movaps %xmm0, 0x10(%rsp)
jmp 0x1c51
movaps %xmm0, 0x30(%rsp)
incq %r13
movslq (%rbx), %rax
addq $0x1c, %r12
cmpq %rax, %r13
jl 0x1b6e
movaps 0x20(%rsp), %xmm5
movaps %xmm5, %xmm0
movaps 0x40(%rsp), %xmm3
subss %xmm3, %xmm0
movaps 0x10(%rsp), %xmm6
unpcklps %xmm6, %xmm5            # xmm5 = xmm5[0],xmm6[0],xmm5[1],xmm6[1]
movaps 0x30(%rsp), %xmm4
subss %xmm4, %xmm6
movaps %xmm0, %xmm1
cmpunordss %xmm0, %xmm1
movaps %xmm1, %xmm2
andps %xmm6, %xmm2
maxss %xmm0, %xmm6
andnps %xmm6, %xmm1
orps %xmm2, %xmm1
movss %xmm1, 0xc(%rbx)
movss %xmm1, 0x10(%rbx)
unpcklps %xmm4, %xmm3            # xmm3 = xmm3[0],xmm4[0],xmm3[1],xmm4[1]
addps %xmm5, %xmm3
mulps 0x240b(%rip), %xmm3     # 0x40c0
movlps %xmm3, 0x4(%rbx)
callq 0x2f57
movq %rax, 0x20(%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq