// File: /workspace/github_c_makefile_star2_O1/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 0x1ce7
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, %xmm5
movlps %xmm5, 0x4(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0xc(%rdi)
movl $0x3f800000, 0x1c(%rdi) # imm = 0x3F800000
xorl %eax, %eax
movaps %xmm5, %xmm0
testb %al, %al
jne 0x1c93
shufps $0x55, %xmm5, %xmm0     # xmm0 = xmm0[1,1],xmm5[1,1]
leaq 0x40(%rbx), %r12
xorl %r13d, %r13d
movaps %xmm5, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x10(%rsp)
movaps %xmm5, 0x40(%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 0x24ab(%rip), %xmm0     # 0x408c
addss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
mulsd 0x24af(%rip), %xmm0     # 0x40a0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x18(%r12)
movsd 0x24ac(%rip), %xmm0     # 0x40b0
movsd %xmm0, -0x14(%r12)
movss 0x23f9(%rip), %xmm0     # 0x400c
callq 0x2900
movaps 0x40(%rsp), %xmm5
movss %xmm0, -0xc(%r12)
movss 0x8(%rsp), %xmm0
movl $0xffffff, (%r12)       # imm = 0xFFFFFF
ucomiss %xmm0, %xmm5
jae 0x1c45
ucomiss 0x20(%rsp), %xmm0
jb 0x1c48
movaps %xmm0, 0x20(%rsp)
jmp 0x1c48
movaps %xmm0, %xmm5
movaps 0x30(%rsp), %xmm0
movss 0xc(%rsp), %xmm1
ucomiss %xmm1, %xmm0
movaps %xmm1, %xmm0
jae 0x1c69
ucomiss 0x10(%rsp), %xmm0
jb 0x1c6e
movaps %xmm0, 0x10(%rsp)
jmp 0x1c6e
movaps %xmm0, 0x30(%rsp)
incq %r13
movslq (%rbx), %rax
addq $0x1c, %r12
cmpq %rax, %r13
jl 0x1b8b
movaps %xmm5, %xmm0
unpcklps 0x30(%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]
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 0x23f4(%rip), %xmm5     # 0x40c0
movlps %xmm5, 0x4(%rbx)
callq 0x2f35
movq %rax, 0x20(%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq