// File: /workspace/github_c_makefile_star2_O1/adelciotto[P]scg/examples/voxel_space.c
// Function: draw
static void draw(scg_image_t *draw_target, terrain_t terrain, camera_t camera) {
    scg_image_clear(draw_target, SCG_COLOR_WHITE);

    int w = draw_target->width;
    int h = draw_target->height;
    int map_w_period = terrain.map_w - 1;
    int map_h_period = terrain.map_h - 1;

    float32_t s = sinf(camera.angle);
    float32_t c = cosf(camera.angle);

    float32_t ybuffer[w];
    for (int x = 0; x < w; x++) {
        ybuffer[x] = h;
    }

    float32_t dt = 1.0f;
    float32_t inv_max_distance = 1.0f / camera.max_distance;

    for (float32_t z = 1; z < camera.max_distance; z += dt) {
        float32_t pleft_x = -c * z - s * z;
        float32_t pleft_y = s * z - c * z;
        float32_t pright_x = c * z - s * z;
        float32_t pright_y = -s * z - c * z;

        float32_t dx = (pright_x - pleft_x) / (float32_t)w;
        float32_t dy = (pright_y - pleft_y) / (float32_t)w;
        pleft_x += camera.x;
        pleft_y += camera.y;
        float32_t invz = 1.0f / z * 240.0f;

        float32_t fog = fog_factor(z * inv_max_distance, 2.2f);

        for (int x = 0; x < w; x++) {
            int i = (((int)pleft_y & map_w_period) << 10) +
                    ((int)pleft_x & map_h_period);

            scg_pixel_t color_map_pixel =
                scg_pixel_new_uint32(terrain.color_map->pixels[i]);
            scg_pixel_t height_map_pixel =
                scg_pixel_new_uint32(terrain.height_map->pixels[i]);

            color_map_pixel = shade_pixel(color_map_pixel, fog);

            float32_t height_on_screen =
                (camera.height - height_map_pixel.packed) * invz +
                camera.horizon;
            draw_vertical_line(draw_target, x, height_on_screen, ybuffer[x],
                               color_map_pixel);

            if (height_on_screen < ybuffer[x]) {
                ybuffer[x] = height_on_screen;
            }

            pleft_x += dx;
            pleft_y += dy;
        }

        dt += 0.01f;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
leaq 0x28(%rbp), %rcx
leaq 0x10(%rbp), %r12
movl 0x4(%rdi), %eax
imull (%rdi), %eax
testl %eax, %eax
movq %rdi, -0x58(%rbp)
jle 0x593d
movq 0x10(%rdi), %rdi
movl %eax, %edx
shlq $0x2, %rdx
movl $0xff, %esi
movq %rcx, %rbx
callq 0x2100
movq %rbx, %rcx
movq -0x58(%rbp), %rdi
movl (%rdi), %r14d
movl 0x4(%rdi), %eax
movl %eax, -0x2c(%rbp)
movl $0x3fffff, %r15d        # imm = 0x3FFFFF
addl (%r12), %r15d
movl 0x4(%r12), %r12d
movq %rcx, %rbx
movss 0x8(%rcx), %xmm0
movss %xmm0, -0x30(%rbp)
callq 0x21d0
movaps %xmm0, -0x90(%rbp)
movss -0x30(%rbp), %xmm0
callq 0x2180
movaps %xmm0, -0x80(%rbp)
movq %rsp, %r13
leaq 0xf(,%r14,4), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
testl %r14d, %r14d
jle 0x59af
xorps %xmm0, %xmm0
cvtsi2ssl -0x2c(%rbp), %xmm0
xorl %eax, %eax
movss %xmm0, (%r13,%rax,4)
incq %rax
cmpq %rax, %r14
jne 0x59a0
movss 0x14(%rbx), %xmm0
movss %xmm0, -0x34(%rbp)
ucomiss 0x648(%rip), %xmm0      # 0x6008
jbe 0x5c99
decl %r12d
movss 0x637(%rip), %xmm0      # 0x6008
movaps %xmm0, %xmm1
divss -0x34(%rbp), %xmm1
movss %xmm1, -0x4c(%rbp)
movaps 0x69b(%rip), %xmm1      # 0x6080
movaps -0x80(%rbp), %xmm2
xorps %xmm1, %xmm2
movaps %xmm2, -0xa0(%rbp)
xorps %xmm2, %xmm2
cvtsi2ss %r14d, %xmm2
movss %xmm2, -0x40(%rbp)
xorps -0x90(%rbp), %xmm1
movaps %xmm1, -0xb0(%rbp)
movss (%rbx), %xmm1
movss %xmm1, -0x3c(%rbp)
movss 0x4(%rbx), %xmm1
movss %xmm1, -0x38(%rbp)
leaq 0x10(%rbp), %rcx
movq 0x8(%rcx), %rax
movq %rax, -0x68(%rbp)
movq 0x10(%rcx), %rax
movq %rax, -0x60(%rbp)
movss 0xc(%rbx), %xmm12
movss 0x10(%rbx), %xmm13
movl $0xff, %ebx
movaps %xmm0, %xmm1
movaps %xmm0, %xmm15
movss %xmm12, -0x48(%rbp)
movss %xmm13, -0x44(%rbp)
movss %xmm15, -0x2c(%rbp)
movss %xmm1, -0x30(%rbp)
movss -0x4c(%rbp), %xmm0
mulss %xmm15, %xmm0
mulss 0x5ce(%rip), %xmm0      # 0x6044
mulss %xmm0, %xmm0
mulss 0x5c6(%rip), %xmm0      # 0x6048
callq 0x23e0
movss -0x2c(%rbp), %xmm15
movss 0x576(%rip), %xmm14     # 0x600c
movss -0x44(%rbp), %xmm13
movss -0x48(%rbp), %xmm12
movss 0x55e(%rip), %xmm1      # 0x6008
divss %xmm15, %xmm1
mulss 0x595(%rip), %xmm1      # 0x604c
testl %r14d, %r14d
movq -0x58(%rbp), %r11
jle 0x5c78
movaps %xmm15, %xmm5
mulss -0xb0(%rbp), %xmm5
movaps -0xa0(%rbp), %xmm2
mulss %xmm15, %xmm2
movaps -0x90(%rbp), %xmm3
mulss %xmm15, %xmm3
addss %xmm2, %xmm3
addss %xmm5, %xmm2
movaps -0x80(%rbp), %xmm4
mulss %xmm15, %xmm4
addss %xmm5, %xmm4
subss %xmm2, %xmm4
movss -0x40(%rbp), %xmm6
divss %xmm6, %xmm4
movaps %xmm2, %xmm5
subss %xmm3, %xmm5
divss %xmm6, %xmm5
xorps %xmm6, %xmm6
maxss %xmm0, %xmm6
movss 0x4e4(%rip), %xmm0      # 0x6008
movaps %xmm0, %xmm7
minss %xmm6, %xmm7
subss %xmm7, %xmm0
addss -0x38(%rbp), %xmm3
addss -0x3c(%rbp), %xmm2
movq -0x68(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x60(%rbp), %rcx
movq 0x10(%rcx), %rcx
xorl %edx, %edx
cvttss2si %xmm3, %esi
andl %r15d, %esi
cvttss2si %xmm2, %edi
shll $0xa, %esi
andl %r12d, %edi
addl %esi, %edi
movslq %edi, %r8
movl (%rcx,%r8,4), %esi
xorps %xmm7, %xmm7
cvtsi2ss %rsi, %xmm7
movaps %xmm12, %xmm6
subss %xmm7, %xmm6
mulss %xmm1, %xmm6
addss %xmm13, %xmm6
cvttss2si %xmm6, %edi
movss (%r13,%rdx,4), %xmm7
cvttss2si %xmm7, %esi
testl %edi, %edi
movl $0x0, %r9d
cmovlel %r9d, %edi
subl %edi, %esi
jle 0x5c58
movl (%rax,%r8,4), %r8d
movl %r8d, %r9d
shrl $0x8, %r9d
movl %r8d, %r10d
shrl $0x10, %r10d
andl %ebx, %r10d
xorps %xmm8, %xmm8
cvtsi2ss %r10d, %xmm8
andl %ebx, %r9d
xorps %xmm9, %xmm9
cvtsi2ss %r9d, %xmm9
andl %ebx, %r8d
cvtsi2ss %r8d, %xmm10
movaps %xmm14, %xmm11
subss %xmm8, %xmm11
mulss %xmm0, %xmm11
addss %xmm8, %xmm11
movaps %xmm14, %xmm8
subss %xmm9, %xmm8
mulss %xmm0, %xmm8
addss %xmm9, %xmm8
movaps %xmm14, %xmm9
subss %xmm10, %xmm9
mulss %xmm0, %xmm9
addss %xmm10, %xmm9
cvttss2si %xmm9, %r8d
cvttss2si %xmm8, %r9d
cvttss2si %xmm11, %r10d
shll $0x10, %r10d
andl %ebx, %r9d
shll $0x8, %r9d
orl %r10d, %r9d
andl %ebx, %r8d
orl %r9d, %r8d
orl $0xff000000, %r8d       # imm = 0xFF000000
movslq (%r11), %r9
imull %r9d, %edi
addl %edx, %edi
movslq %edi, %rdi
shlq $0x2, %rdi
addq 0x10(%r11), %rdi
shlq $0x2, %r9
movl %r8d, (%rdi)
addq %r9, %rdi
decl %esi
jne 0x5c4e
ucomiss %xmm6, %xmm7
jbe 0x5c64
movss %xmm6, (%r13,%rdx,4)
addss %xmm4, %xmm2
addss %xmm5, %xmm3
incq %rdx
cmpq %r14, %rdx
jne 0x5b4b
movss -0x30(%rbp), %xmm1
addss 0x3cb(%rip), %xmm1      # 0x6050
addss %xmm1, %xmm15
movss -0x34(%rbp), %xmm0
ucomiss %xmm15, %xmm0
ja 0x5a59
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq