// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movl %esi, 0x2c(%rsp)
movl %edx, 0x28(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0x1f6e
jmp 0x21f0
movq 0x30(%rsp), %rax
movl $0x64, (%rax)
movl 0x2c(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cvtsi2ss %eax, %xmm0
movq 0x30(%rsp), %rax
movss %xmm0, 0x4(%rax)
movl 0x28(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cvtsi2ss %eax, %xmm0
movq 0x30(%rsp), %rax
movss %xmm0, 0x8(%rax)
movq 0x30(%rsp), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0xc(%rax)
movq 0x30(%rsp), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x10(%rax)
movq 0x30(%rsp), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x14(%rax)
movq 0x30(%rsp), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x18(%rax)
movq 0x30(%rsp), %rax
movss 0x2022(%rip), %xmm0     # 0x4010
movss %xmm0, 0x1c(%rax)
movl 0x2c(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x24(%rsp)
movl 0x2c(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x20(%rsp)
movl 0x28(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x1c(%rsp)
movl 0x28(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
movq 0x30(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x21b9
callq 0x1090
cltd
idivl 0x2c(%rsp)
movl %edx, 0x10(%rsp)
callq 0x1090
cltd
idivl 0x28(%rsp)
movl %edx, 0xc(%rsp)
movq 0x30(%rsp), %rax
addq $0x28, %rax
movslq 0x14(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq %rax, (%rsp)
cvtsi2ssl 0x10(%rsp), %xmm0
movq (%rsp), %rax
movss %xmm0, 0x10(%rax)
cvtsi2ssl 0xc(%rsp), %xmm0
movq (%rsp), %rax
movss %xmm0, 0x14(%rax)
callq 0x1090
cvtsi2ss %eax, %xmm0
movss 0x1fc4(%rip), %xmm1     # 0x408c
divss %xmm1, %xmm0
movss 0x1fb4(%rip), %xmm1     # 0x4088
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x1fac(%rip), %xmm1     # 0x4090
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movq (%rsp), %rax
movss %xmm0, (%rax)
movq (%rsp), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x4(%rax)
movq (%rsp), %rax
movss 0x1f00(%rip), %xmm0     # 0x400c
movss %xmm0, 0x8(%rax)
movq (%rsp), %rax
movss 0x8(%rax), %xmm0
callq 0x3390
movq (%rsp), %rax
movss %xmm0, 0xc(%rax)
movq (%rsp), %rax
movl $0xffffff, 0x18(%rax)   # imm = 0xFFFFFF
cvtsi2ssl 0x10(%rsp), %xmm1
movss 0x24(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jb 0x2152
cvtsi2ssl 0x10(%rsp), %xmm0
movss %xmm0, 0x24(%rsp)
jmp 0x216d
cvtsi2ssl 0x10(%rsp), %xmm0
ucomiss 0x20(%rsp), %xmm0
jb 0x216b
cvtsi2ssl 0x10(%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
jmp 0x216d
cvtsi2ssl 0xc(%rsp), %xmm1
movss 0x1c(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jb 0x218c
cvtsi2ssl 0xc(%rsp), %xmm0
movss %xmm0, 0x1c(%rsp)
jmp 0x21a7
cvtsi2ssl 0xc(%rsp), %xmm0
ucomiss 0x18(%rsp), %xmm0
jb 0x21a5
cvtsi2ssl 0xc(%rsp), %xmm0
movss %xmm0, 0x18(%rsp)
jmp 0x21a7
jmp 0x21a9
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x2053
movq 0x30(%rsp), %rdi
addq $0x4, %rdi
movss 0x24(%rsp), %xmm0
movss 0x1c(%rsp), %xmm1
movss 0x20(%rsp), %xmm2
movss 0x18(%rsp), %xmm3
callq 0x2200
callq 0x3d50
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x20(%rax)
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)