// File: /workspace/github_c_makefile_star2_O2/mesbahamin[P]star-sim/src/sim.c
// Function: sim_grid_render
void sim_grid_render(struct OffscreenBuffer *buffer, struct QuadTreeNode *node, uint32_t color, struct SimView *view)
{
    if (!buffer)
    {
        // TODO: handle invalid pointer error
        return;
    }

    if (node && node->ne && node->nw && node->sw && node->se)
    {
        for (int x = (node->cell->center_x - node->cell->distance_x);
            x <= (node->cell->center_x + node->cell->distance_x);
            ++x)
        {
            sim_set_pixel(
                    buffer,
                    sim_calc_render_offset(view->zoom, view->dx, x, buffer->width/2),
                    sim_calc_render_offset(view->zoom, view->dy, node->cell->center_y, buffer->height/2),
                    color);
        }

        for (int y = (node->cell->center_y - node->cell->distance_y);
            y <= (node->cell->center_y + node->cell->distance_y);
            ++y)
        {
            sim_set_pixel(
                    buffer,
                    sim_calc_render_offset(view->zoom, view->dx, node->cell->center_x, buffer->width/2),
                    sim_calc_render_offset(view->zoom, view->dy, y, buffer->height/2),
                    color);
        }

        sim_grid_render(buffer, node->ne, color, view);
        sim_grid_render(buffer, node->nw, color, view);
        sim_grid_render(buffer, node->sw, color, view);
        sim_grid_render(buffer, node->se, color, view);
    }
}

// Assembly:
testq %rdi, %rdi
je 0x227f
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rcx, %rbx
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %r15
testq %r14, %r14
je 0x2277
cmpq $0x0, 0x8(%r14)
je 0x2277
cmpq $0x0, 0x10(%r14)
je 0x2277
cmpq $0x0, 0x18(%r14)
je 0x2277
cmpq $0x0, 0x20(%r14)
je 0x2277
movq (%r14), %rax
movss (%rax), %xmm0
movss 0x8(%rax), %xmm1
movaps %xmm0, %xmm2
subss %xmm1, %xmm2
cvttss2si %xmm2, %r12d
cvtsi2ss %r12d, %xmm2
addss %xmm0, %xmm1
ucomiss %xmm2, %xmm1
jb 0x21a7
movss 0x8(%rbx), %xmm0
movl 0x8(%r15), %ecx
movl 0xc(%r15), %edx
shrl %ecx
cvtsi2ss %ecx, %xmm1
movss 0xec2(%rip), %xmm3      # 0x3014
subss %xmm0, %xmm3
addss (%rbx), %xmm2
mulss %xmm0, %xmm2
mulss %xmm3, %xmm1
addss %xmm2, %xmm1
cvttss2si %xmm1, %rsi
shrl %edx
cvtsi2ss %edx, %xmm1
movss 0x4(%rbx), %xmm2
addss 0x4(%rax), %xmm2
mulss %xmm0, %xmm2
mulss %xmm3, %xmm1
addss %xmm2, %xmm1
cvttss2si %xmm1, %rdx
movq %r15, %rdi
movl %ebp, %ecx
callq 0x2086
incl %r12d
movq (%r14), %rax
movss (%rax), %xmm0
movss 0x8(%rax), %xmm1
jmp 0x2129
movss 0x4(%rax), %xmm1
movss 0xc(%rax), %xmm2
movaps %xmm1, %xmm0
subss %xmm2, %xmm0
cvttss2si %xmm0, %r12d
cvtsi2ss %r12d, %xmm0
addss %xmm1, %xmm2
ucomiss %xmm0, %xmm2
jb 0x223b
movss (%rbx), %xmm1
movss 0x8(%rbx), %xmm2
movl 0x8(%r15), %ecx
movl 0xc(%r15), %edx
shrl %ecx
cvtsi2ss %ecx, %xmm3
movss 0xe2a(%rip), %xmm4      # 0x3014
subss %xmm2, %xmm4
addss (%rax), %xmm1
mulss %xmm2, %xmm1
mulss %xmm4, %xmm3
addss %xmm1, %xmm3
cvttss2si %xmm3, %rsi
shrl %edx
cvtsi2ss %edx, %xmm1
addss 0x4(%rbx), %xmm0
mulss %xmm2, %xmm0
mulss %xmm4, %xmm1
addss %xmm0, %xmm1
cvttss2si %xmm1, %rdx
movq %r15, %rdi
movl %ebp, %ecx
callq 0x2086
incl %r12d
movq (%r14), %rax
movss 0x4(%rax), %xmm1
movss 0xc(%rax), %xmm2
jmp 0x21bd
movq 0x8(%r14), %rsi
movq %r15, %rdi
movl %ebp, %edx
movq %rbx, %rcx
callq 0x20c0
movq 0x10(%r14), %rsi
movq %r15, %rdi
movl %ebp, %edx
movq %rbx, %rcx
callq 0x20c0
movq 0x18(%r14), %rsi
movq %r15, %rdi
movl %ebp, %edx
movq %rbx, %rcx
callq 0x20c0
movq 0x20(%r14), %r14
jmp 0x20dc
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq