// File: /workspace/github_c_makefile_star2_O3/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 $0xa8, %rsp
movq %rdi, %rbx
leaq 0x28(%rbp), %rcx
leaq 0x10(%rbp), %r15
movl (%rdi), %r12d
movl 0x4(%rdi), %r13d
movl %r13d, %eax
imull %r12d, %eax
testl %eax, %eax
jle 0x5990
movq 0x10(%rbx), %rdi
movl %eax, %edx
shlq $0x2, %rdx
movl $0xff, %esi
movq %rcx, %r14
callq 0x2100
movq %r14, %rcx
movl (%rbx), %r12d
movl 0x4(%rbx), %r13d
movsd (%r15), %xmm0
movaps %xmm0, -0x60(%rbp)
movss 0x8(%rcx), %xmm0
movss %xmm0, -0x38(%rbp)
callq 0x21d0
movaps %xmm0, -0x70(%rbp)
movss -0x38(%rbp), %xmm0
callq 0x2180
movl %r12d, %r14d
movq %rsp, %r15
leaq 0xf(,%r14,4), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
testl %r12d, %r12d
jle 0x5ae4
cvtsi2ss %r13d, %xmm1
leal 0x3(%r14), %eax
leaq -0x1(%r14), %rcx
movq %rcx, %xmm2
pshufd $0x44, %xmm2, %xmm2     # xmm2 = xmm2[0,1,0,1]
shrl $0x2, %eax
shlq $0x4, %rax
movdqa 0x683(%rip), %xmm3      # 0x6080
movdqa 0x68b(%rip), %xmm4      # 0x6090
xorl %ecx, %ecx
movdqa 0x691(%rip), %xmm5      # 0x60a0
pxor %xmm5, %xmm2
pcmpeqd %xmm6, %xmm6
movdqa 0x691(%rip), %xmm7      # 0x60b0
movdqa %xmm4, %xmm8
pxor %xmm5, %xmm8
movdqa %xmm8, %xmm9
pcmpgtd %xmm2, %xmm9
pcmpeqd %xmm2, %xmm8
pshufd $0xf5, %xmm8, %xmm10    # xmm10 = xmm8[1,1,3,3]
pand %xmm9, %xmm10
pshufd $0xf5, %xmm9, %xmm8     # xmm8 = xmm9[1,1,3,3]
por %xmm10, %xmm8
movd %xmm8, %edx
notl %edx
testb $0x1, %dl
je 0x5a60
movss %xmm1, (%r15,%rcx)
pxor %xmm6, %xmm8
pextrw $0x4, %xmm8, %edx
testb $0x1, %dl
je 0x5a77
movss %xmm1, 0x4(%r15,%rcx)
movdqa %xmm3, %xmm8
pxor %xmm5, %xmm8
movdqa %xmm8, %xmm9
pcmpgtd %xmm2, %xmm9
pcmpeqd %xmm2, %xmm8
pshufd $0xf5, %xmm8, %xmm10    # xmm10 = xmm8[1,1,3,3]
pand %xmm9, %xmm10
pshufd $0xf5, %xmm9, %xmm8     # xmm8 = xmm9[1,1,3,3]
por %xmm10, %xmm8
pxor %xmm6, %xmm8
pextrw $0x0, %xmm8, %edx
testb $0x1, %dl
je 0x5abd
movss %xmm1, 0x8(%r15,%rcx)
pextrw $0x4, %xmm8, %edx
testb $0x1, %dl
je 0x5acf
movss %xmm1, 0xc(%r15,%rcx)
paddq %xmm7, %xmm4
paddq %xmm7, %xmm3
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x5a1f
movq %rbx, -0x38(%rbp)
leaq 0x28(%rbp), %rcx
movss 0x14(%rcx), %xmm1
movss %xmm1, -0x2c(%rbp)
ucomiss 0x50b(%rip), %xmm1      # 0x6008
jbe 0x5ee3
pshufd $0xe1, -0x60(%rbp), %xmm12 # xmm12 = mem[1,0,2,3]
paddd 0x5dd(%rip), %xmm12     # 0x60f0
movss 0x4ed(%rip), %xmm3      # 0x6008
movaps %xmm3, %xmm1
divss -0x2c(%rbp), %xmm1
movss %xmm1, -0x44(%rbp)
movaps -0x70(%rbp), %xmm2
movaps %xmm2, %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movaps 0x567(%rip), %xmm4      # 0x60a0
xorps %xmm1, %xmm4
xorps %xmm5, %xmm5
cvtsi2ss %r14d, %xmm5
movsd (%rcx), %xmm6
movaps %xmm6, -0xa0(%rbp)
leaq 0x10(%rbp), %rdx
movq 0x8(%rdx), %rax
movq %rax, -0x80(%rbp)
movq 0x10(%rdx), %rax
movq %rax, -0x78(%rbp)
movss 0xc(%rcx), %xmm13
movss 0x10(%rcx), %xmm14
movaps %xmm4, %xmm6
movlhps %xmm0, %xmm6            # xmm6 = xmm6[0],xmm0[0]
shufps $0xe2, %xmm1, %xmm6     # xmm6 = xmm6[2,0],xmm1[2,3]
movaps %xmm6, -0x90(%rbp)
shufps $0x0, %xmm5, %xmm5      # xmm5 = xmm5[0,0,0,0]
movaps %xmm5, -0xb0(%rbp)
shufps $0x10, %xmm4, %xmm2     # xmm2 = xmm2[0,0],xmm4[1,0]
movaps %xmm4, -0xc0(%rbp)
shufps $0xe2, %xmm4, %xmm2     # xmm2 = xmm2[2,0],xmm4[2,3]
movaps %xmm2, -0x70(%rbp)
movl $0xff, %r13d
movaps %xmm3, %xmm1
movdqa %xmm12, -0xd0(%rbp)
movss %xmm13, -0x40(%rbp)
movss %xmm14, -0x3c(%rbp)
movaps %xmm3, -0x60(%rbp)
movss %xmm1, -0x48(%rbp)
movss -0x44(%rbp), %xmm0
mulss %xmm3, %xmm0
mulss 0x46e(%rip), %xmm0      # 0x6044
mulss %xmm0, %xmm0
mulss 0x466(%rip), %xmm0      # 0x6048
callq 0x23e0
movss 0x41c(%rip), %xmm15     # 0x600c
movss -0x3c(%rbp), %xmm14
movss -0x40(%rbp), %xmm13
movdqa -0xd0(%rbp), %xmm12
movss 0x3fb(%rip), %xmm1      # 0x6008
divss -0x60(%rbp), %xmm1
mulss 0x432(%rip), %xmm1      # 0x604c
testl %r14d, %r14d
jle 0x5ec0
movaps -0x60(%rbp), %xmm2
shufps $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0]
movaps %xmm2, %xmm4
mulps -0xc0(%rbp), %xmm4
movaps -0x70(%rbp), %xmm3
mulps %xmm2, %xmm3
addps %xmm4, %xmm3
mulps -0x90(%rbp), %xmm2
addps %xmm4, %xmm2
subps %xmm3, %xmm2
divps -0xb0(%rbp), %xmm2
xorps %xmm4, %xmm4
maxss %xmm0, %xmm4
movss 0x3a6(%rip), %xmm0      # 0x6008
movaps %xmm0, %xmm5
minss %xmm4, %xmm5
subss %xmm5, %xmm0
addps -0xa0(%rbp), %xmm3
movq -0x80(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x78(%rbp), %rcx
movq 0x10(%rcx), %rcx
xorl %edx, %edx
cvttps2dq %xmm3, %xmm4
pand %xmm12, %xmm4
pshufd $0x55, %xmm4, %xmm5     # xmm5 = xmm4[1,1,1,1]
movd %xmm5, %esi
shll $0xa, %esi
movd %xmm4, %edi
addl %esi, %edi
movslq %edi, %rsi
movl (%rcx,%rsi,4), %edi
xorps %xmm5, %xmm5
cvtsi2ss %rdi, %xmm5
movaps %xmm13, %xmm4
subss %xmm5, %xmm4
mulss %xmm1, %xmm4
addss %xmm14, %xmm4
cvttss2si %xmm4, %r9d
movss (%r15,%rdx,4), %xmm5
cvttss2si %xmm5, %r8d
testl %r9d, %r9d
movl $0x0, %edi
cmovlel %edi, %r9d
cmpl %r8d, %r9d
setg %dil
setae %r10b
orb %dil, %r10b
jne 0x5ea6
movl (%rax,%rsi,4), %esi
movl %esi, %edi
shrl $0x8, %edi
movl %esi, %r10d
shrl $0x10, %r10d
andl %r13d, %r10d
cvtsi2ss %r10d, %xmm6
andl %r13d, %edi
xorps %xmm7, %xmm7
cvtsi2ss %edi, %xmm7
andl %r13d, %esi
xorps %xmm8, %xmm8
cvtsi2ss %esi, %xmm8
movaps %xmm15, %xmm9
subss %xmm6, %xmm9
mulss %xmm0, %xmm9
addss %xmm6, %xmm9
movaps %xmm15, %xmm6
subss %xmm7, %xmm6
mulss %xmm0, %xmm6
addss %xmm7, %xmm6
movaps %xmm15, %xmm7
subss %xmm8, %xmm7
mulss %xmm0, %xmm7
addss %xmm8, %xmm7
cvttss2si %xmm7, %esi
cvttss2si %xmm6, %edi
cvttss2si %xmm9, %r10d
shll $0x10, %r10d
andl %r13d, %edi
shll $0x8, %edi
orl %r10d, %edi
andl %r13d, %esi
orl %edi, %esi
orl $0xff000000, %esi       # imm = 0xFF000000
movq -0x38(%rbp), %rbx
movslq (%rbx), %rdi
movl %edi, %r10d
imull %r9d, %r10d
notl %r9d
addl %r8d, %r9d
leaq 0x4(%r9), %r8
andq $-0x4, %r8
movq %r9, %xmm6
pshufd $0x44, %xmm6, %xmm6     # xmm6 = xmm6[0,1,0,1]
leaq (,%rdi,4), %r11
addl %edx, %r10d
movslq %r10d, %r9
shlq $0x2, %r9
addq 0x10(%rbx), %r9
leaq (%r11,%r11,2), %r10
movq %rdi, %r11
shlq $0x4, %r11
xorl %r12d, %r12d
movq %r12, %xmm7
pshufd $0x44, %xmm7, %xmm7     # xmm7 = xmm7[0,1,0,1]
movdqa %xmm7, %xmm9
por 0x2b7(%rip), %xmm9      # 0x6090
movdqa %xmm6, %xmm8
movdqa 0x2b9(%rip), %xmm10     # 0x60a0
pxor %xmm10, %xmm8
pxor %xmm10, %xmm9
movdqa %xmm9, %xmm10
pcmpgtd %xmm8, %xmm10
pcmpeqd %xmm8, %xmm9
pshufd $0xf5, %xmm9, %xmm11    # xmm11 = xmm9[1,1,3,3]
pand %xmm10, %xmm11
pshufd $0xf5, %xmm10, %xmm9    # xmm9 = xmm10[1,1,3,3]
por %xmm11, %xmm9
movd %xmm9, %ebx
notl %ebx
testb $0x1, %bl
je 0x5e25
movl %esi, (%r9)
pxor 0x302(%rip), %xmm9      # 0x6130
pextrw $0x4, %xmm9, %ebx
testb $0x1, %bl
je 0x5e3d
movl %esi, (%r9,%rdi,4)
por 0x23b(%rip), %xmm7      # 0x6080
pxor 0x253(%rip), %xmm7      # 0x60a0
movdqa %xmm7, %xmm9
pcmpgtd %xmm8, %xmm9
pcmpeqd %xmm8, %xmm7
pshufd $0xf5, %xmm7, %xmm8     # xmm8 = xmm7[1,1,3,3]
pand %xmm9, %xmm8
pshufd $0xf5, %xmm9, %xmm7     # xmm7 = xmm9[1,1,3,3]
por %xmm8, %xmm7
pxor 0x2b6(%rip), %xmm7      # 0x6130
pextrw $0x0, %xmm7, %ebx
testb $0x1, %bl
je 0x5e88
movl %esi, (%r9,%rdi,8)
pextrw $0x4, %xmm7, %ebx
testb $0x1, %bl
je 0x5e96
movl %esi, (%r9,%r10)
addq $0x4, %r12
addq %r11, %r9
cmpq %r12, %r8
jne 0x5dc1
ucomiss %xmm4, %xmm5
jbe 0x5eb1
movss %xmm4, (%r15,%rdx,4)
addps %xmm2, %xmm3
incq %rdx
cmpq %r14, %rdx
jne 0x5c86
movss -0x48(%rbp), %xmm1
addss 0x183(%rip), %xmm1      # 0x6050
movaps -0x60(%rbp), %xmm3
addss %xmm1, %xmm3
movss -0x2c(%rbp), %xmm0
ucomiss %xmm3, %xmm0
ja 0x5bbc
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq