// File: /workspace/github_c_makefile_star2_O2/mesbahamin[P]star-sim/src/sim.c
// Function: sim_render
void sim_render(struct OffscreenBuffer *buffer, float dt, struct SimState *sim_state)
{
    if (!buffer || !sim_state)
    {
        // TODO: handle invalid pointer error
        return;
    }

    int num_stars = sim_state->num_stars;
    struct Star *stars = sim_state->stars;
    struct QuadTree *qt = sim_state->qt;

    float dx = sim_state->view.dx;
    float dy = sim_state->view.dy;
    float zoom = sim_state->view.zoom;

    for (int i = 0; i < num_stars; ++i)
    {
        uint32_t color;
        if (BRUTE_FORCE)
        {
            color = COLOR_CYAN;
        }
        else
        {
            color = stars[i].color;
        }
        sim_set_pixel(
            buffer,
            sim_calc_render_offset(zoom, dx, stars[i].x + (sinf(stars[i].angle) * stars[i].speed) * dt, buffer->width/2),
            sim_calc_render_offset(zoom, dy, stars[i].y - (cosf(stars[i].angle) * stars[i].speed) * dt, buffer->height/2),
            color);
    }

    if (RENDER_GRID)
    {
        sim_grid_render(buffer, qt->root, COLOR_GREEN, &sim_state->view);
    }
    if (RENDER_BOUNDING_BOX)
    {
        sim_bounding_box_render(buffer, &sim_state->bounding_box, COLOR_RED, &sim_state->view);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movss %xmm0, 0xc(%rsp)
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
jne 0x2077
movq %rsi, %rbx
movq %rdi, %r14
movl (%rsi), %eax
movq 0x20(%rsi), %rcx
movq %rcx, 0x20(%rsp)
leaq 0x14(%rsi), %rcx
movq %rcx, 0x18(%rsp)
movss 0x14(%rsi), %xmm0
movss %xmm0, 0x14(%rsp)
movss 0x18(%rsi), %xmm0
movss %xmm0, 0x10(%rsp)
movss 0x1c(%rsi), %xmm0
movss 0x10e1(%rip), %xmm1     # 0x3014
movss %xmm0, 0x8(%rsp)
subss %xmm0, %xmm1
movss %xmm1, 0x4(%rsp)
xorl %r13d, %r13d
testl %eax, %eax
cmovlel %r13d, %eax
imulq $0x1c, %rax, %r15
cmpq %r13, %r15
je 0x2024
movl $0x2aa198, %ebp         # imm = 0x2AA198
cmpb $0x0, 0x31ec(%rip)      # 0x5151
jne 0x1f6c
movl 0x40(%rbx,%r13), %ebp
movss 0x28(%rbx,%r13), %xmm0
movss 0x38(%rbx,%r13), %xmm1
movss %xmm1, (%rsp)
callq 0x1130
mulss 0x2c(%rbx,%r13), %xmm0
mulss 0xc(%rsp), %xmm0
addss (%rsp), %xmm0
movl 0x8(%r14), %eax
shrl %eax
cvtsi2ss %eax, %xmm1
addss 0x14(%rsp), %xmm0
mulss 0x8(%rsp), %xmm0
mulss 0x4(%rsp), %xmm1
addss %xmm0, %xmm1
cvttss2si %xmm1, %r12
movss 0x28(%rbx,%r13), %xmm0
movss 0x3c(%rbx,%r13), %xmm1
movss %xmm1, (%rsp)
callq 0x10f0
mulss 0x2c(%rbx,%r13), %xmm0
mulss 0xc(%rsp), %xmm0
movss (%rsp), %xmm1
subss %xmm0, %xmm1
addss 0x10(%rsp), %xmm1
movl 0xc(%r14), %eax
shrl %eax
cvtsi2ss %eax, %xmm0
mulss 0x8(%rsp), %xmm1
mulss 0x4(%rsp), %xmm0
addss %xmm1, %xmm0
cvttss2si %xmm0, %rdx
movq %r14, %rdi
movl %r12d, %esi
movl %ebp, %ecx
callq 0x2086
addq $0x1c, %r13
jmp 0x1f50
cmpb $0x0, 0x3127(%rip)      # 0x5152
je 0x2047
movq 0x20(%rsp), %rax
movq (%rax), %rsi
movq %r14, %rdi
movl $0x859900, %edx         # imm = 0x859900
movq 0x18(%rsp), %rcx
callq 0x20c0
cmpb $0x1, 0x3105(%rip)      # 0x5153
jne 0x2077
addq $0x4, %rbx
movq %r14, %rdi
movq %rbx, %rsi
movl $0xdc322f, %edx         # imm = 0xDC322F
movq 0x18(%rsp), %rcx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2280
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq