// File: /workspace/github_c_makefile_star2_O1/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 $0x88, %rsp
movaps %xmm0, (%rsp)
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
jne 0x218b
movq %rsi, %rbx
movq %rdi, %r14
movslq (%rsi), %rax
movq 0x20(%rsi), %r12
testq %rax, %rax
jle 0x2139
movsd 0x14(%rbx), %xmm0
movaps %xmm0, 0x40(%rsp)
movss 0x1c(%rbx), %xmm0
movss 0x2033(%rip), %xmm1     # 0x4014
subss %xmm0, %xmm1
shufps $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
movaps %xmm1, 0x20(%rsp)
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movaps %xmm0, 0x30(%rsp)
movaps (%rsp), %xmm0
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movaps %xmm0, (%rsp)
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %r15
addq %rax, %r15
xorl %r13d, %r13d
movl $0x2aa198, %ebp         # imm = 0x2AA198
cmpb $0x0, 0x412c(%rip)      # 0x6149
jne 0x2024
movl 0x40(%rbx,%r13), %ebp
movss 0x28(%rbx,%r13), %xmm0
movss 0x38(%rbx,%r13), %xmm1
movaps %xmm1, 0x10(%rsp)
callq 0x1130
movaps %xmm0, 0x60(%rsp)
movss 0x28(%rbx,%r13), %xmm0
movss 0x2c(%rbx,%r13), %xmm1
movaps %xmm1, 0x50(%rsp)
movss 0x3c(%rbx,%r13), %xmm1
movaps 0x10(%rsp), %xmm2
unpcklps %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
movaps %xmm2, 0x10(%rsp)
movss 0x8(%r14), %xmm1
movaps %xmm1, 0x70(%rsp)
callq 0x10f0
movaps 0x2051(%rip), %xmm4     # 0x40d0
xorps %xmm4, %xmm0
movaps 0x50(%rsp), %xmm2
unpcklps %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0],xmm2[1],xmm0[1]
movss 0x2c(%rbx,%r13), %xmm0
movaps 0x60(%rsp), %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
mulps %xmm2, %xmm1
mulps (%rsp), %xmm1
addps 0x10(%rsp), %xmm1
addps 0x40(%rsp), %xmm1
mulps 0x30(%rsp), %xmm1
movaps %xmm1, %xmm2
movsd 0x8(%r14), %xmm1
movaps %xmm1, %xmm0
movaps 0x70(%rsp), %xmm3
movss %xmm3, %xmm0            # xmm0 = xmm3[0],xmm0[1,2,3]
psrld $0x1, %xmm0
cvtdq2ps %xmm0, %xmm0
mulps 0x20(%rsp), %xmm0
addps %xmm2, %xmm0
cvttps2dq %xmm0, %xmm2
movdqa %xmm2, %xmm3
psrad $0x1f, %xmm3
subps 0x1ff8(%rip), %xmm0     # 0x40e0
cvttps2dq %xmm0, %xmm0
pand %xmm3, %xmm0
por %xmm2, %xmm0
movdqa %xmm0, %xmm2
pxor %xmm4, %xmm2
xorps %xmm4, %xmm1
pcmpgtd %xmm2, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
movmskpd %xmm1, %eax
cmpl $0x3, %eax
jne 0x212c
movq (%r14), %rax
movd %xmm0, %ecx
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %edx
imull 0x10(%r14), %edx
leal (%rdx,%rcx,4), %ecx
movl %ebp, (%rax,%rcx)
addq $0x1c, %r13
cmpq %r13, %r15
jne 0x2011
leaq 0x14(%rbx), %r15
cmpb $0x0, 0x4006(%rip)      # 0x614a
je 0x215a
movq (%r12), %rsi
movq %r14, %rdi
movl $0x859900, %edx         # imm = 0x859900
movq %r15, %rcx
callq 0x21d7
cmpb $0x1, 0x3fea(%rip)      # 0x614b
jne 0x218b
addq $0x4, %rbx
movq %r14, %rdi
movq %rbx, %rsi
movl $0xdc322f, %edx         # imm = 0xDC322F
movq %r15, %rcx
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x23d0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq