// File: /workspace/github_c_makefile_star2_O2/adelciotto[P]scg/examples/voxel_space.c
// Function: main
int main(int arcg, char *argv[]) {
    scg_config_t config = scg_config_new_default();
    config.video.title = "SCG Example: Voxel Space";

    scg_app_t app;
    scg_app_init(&app, config);

    terrain_t terrain;
    camera_t camera;
    bool success = init(&terrain, &camera);
    if (!success) {
        return -1;
    }

    while (scg_app_process_events(&app)) {
        float32_t delta_time = app.delta_time;

        if (scg_keyboard_is_key_down(app.keyboard, SCG_KEY_UP)) {
            camera.x += -120.0f * sinf(camera.angle) * delta_time;
            camera.y += -120.0f * cosf(camera.angle) * delta_time;
        }
        if (scg_keyboard_is_key_down(app.keyboard, SCG_KEY_DOWN)) {
            camera.x += 120.0f * sinf(camera.angle) * delta_time;
            camera.y += 120.0f * cosf(camera.angle) * delta_time;
        }
        if (scg_keyboard_is_key_down(app.keyboard, SCG_KEY_LEFT)) {
            camera.angle += 1.0f * delta_time;
        }
        if (scg_keyboard_is_key_down(app.keyboard, SCG_KEY_RIGHT)) {
            camera.angle += -1.0f * delta_time;
        }
        if (scg_keyboard_is_key_down(app.keyboard, SCG_KEY_X)) {
            camera.height += -120.0f * delta_time;
        }
        if (scg_keyboard_is_key_down(app.keyboard, SCG_KEY_Z)) {
            camera.height += 120.0f * delta_time;
        }

        int map_w_period = terrain.map_w - 1;
        int map_h_period = terrain.map_h - 1;
        int map_offset = (((int)camera.y & map_w_period) << 10) +
                         ((int)camera.x & map_h_period);
        int map_height =
            scg_pixel_new_uint32(terrain.height_map->pixels[map_offset])
                .packed +
            10;
        if (map_height > camera.height) {
            camera.height = map_height;
        }

        draw(app.draw_target, terrain, camera);

        scg_app_present(&app);
    }

    scg_image_free(terrain.height_map);
    scg_image_free(terrain.color_map);
    scg_app_free(&app);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x160, %rsp            # imm = 0x160
movabsq $0x1e000000280, %rax    # imm = 0x1E000000280
movq %rax, 0xa8(%rsp)
movl $0x1, 0xb8(%rsp)
xorl %eax, %eax
movb %al, 0xbc(%rsp)
movl $0x1010101, 0xbd(%rsp) # imm = 0x1010101
movb %al, 0xc4(%rsp)
movl $0x40, 0xc8(%rsp)
leaq 0x1509(%rip), %rax      # 0x662c
movq %rax, 0xb0(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x20(%rsp)
movups 0xa8(%rsp), %xmm0
movdqu 0xb8(%rsp), %xmm1
movdqu %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x3dc3
leaq 0x20d1(%rip), %rdi      # 0x7238
callq 0x2911
pushq $-0x1
popq %rbp
testq %rax, %rax
je 0x54a1
movq %rax, %rbx
leaq 0x20cb(%rip), %rdi      # 0x724d
callq 0x2911
testq %rax, %rax
je 0x54a1
movq %rax, %r14
movl (%rbx), %eax
cmpl (%r14), %eax
jne 0x51f2
movl 0x4(%rbx), %ecx
cmpl 0x4(%r14), %ecx
jne 0x51f2
movslq %eax, %rdx
xorl %esi, %esi
testl %eax, %eax
movl $0x0, %edi
cmovgl %eax, %edi
testl %ecx, %ecx
movl $0x0, %r8d
cmovgl %ecx, %r8d
shlq $0x2, %rdx
xorl %r9d, %r9d
cmpq %r8, %r9
je 0x5220
movq %rdi, %r10
movq %rsi, %r11
subq $0x1, %r10
jb 0x51ea
movq 0x10(%r14), %r15
movzbl 0x2(%r15,%r11), %ebp
movl %ebp, (%r15,%r11)
addq $0x4, %r11
jmp 0x51d0
incq %r9
addq %rdx, %rsi
jmp 0x51c5
leaq 0xf37(%rip), %rsi       # 0x6130
leaq 0x1250(%rip), %rdx      # 0x6450
leaq 0x205c(%rip), %rcx      # 0x7263
leaq 0x206c(%rip), %r9       # 0x727a
pushq $0x27
popq %r8
xorl %edi, %edi
xorl %eax, %eax
callq 0x22e0
jmp 0x54a1
movl %eax, 0x90(%rsp)
movl %ecx, 0x94(%rsp)
movq %rbx, 0x98(%rsp)
movq %r14, 0xa0(%rsp)
movaps 0xe5b(%rip), %xmm0      # 0x60a0
movaps %xmm0, 0x40(%rsp)
movsd 0xe5e(%rip), %xmm0      # 0x60b0
movsd %xmm0, 0x50(%rsp)
movd %eax, %xmm0
movd %ecx, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
paddd 0xe54(%rip), %xmm1      # 0x60c0
movdqa %xmm1, 0xe0(%rsp)
movsd 0xe53(%rip), %xmm0      # 0x60d0
movaps %xmm0, 0x60(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x3c(%rsp)
movss 0xda5(%rip), %xmm0      # 0x6038
movaps %xmm0, 0x80(%rsp)
leaq 0xf8(%rsp), %r15
movq %r15, %rdi
callq 0x4858
testb %al, %al
je 0x5482
movss 0xfc(%rsp), %xmm3
movq 0x138(%rsp), %r12
cmpb $0x1, 0x52(%r12)
movaps %xmm3, 0xd0(%rsp)
jne 0x5322
movss 0x3c(%rsp), %xmm0
callq 0x21d0
movaps %xmm0, 0x70(%rsp)
movss 0x3c(%rsp), %xmm0
callq 0x2180
movaps 0xd0(%rsp), %xmm3
movaps 0x70(%rsp), %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
mulps 0xdda(%rip), %xmm1      # 0x60e0
movaps %xmm3, %xmm0
shufps $0x0, %xmm3, %xmm0      # xmm0 = xmm0[0,0],xmm3[0,0]
mulps %xmm1, %xmm0
movaps 0x60(%rsp), %xmm1
addps %xmm0, %xmm1
movaps %xmm1, 0x60(%rsp)
movlps %xmm1, 0x40(%rsp)
cmpb $0x1, 0x51(%r12)
jne 0x5378
movss 0x3c(%rsp), %xmm0
callq 0x21d0
movaps %xmm0, 0x70(%rsp)
movss 0x3c(%rsp), %xmm0
callq 0x2180
movaps 0xd0(%rsp), %xmm3
movaps 0x70(%rsp), %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
mulps 0xd94(%rip), %xmm1      # 0x60f0
movaps %xmm3, %xmm0
shufps $0x0, %xmm3, %xmm0      # xmm0 = xmm0[0,0],xmm3[0,0]
mulps %xmm1, %xmm0
movaps 0x60(%rsp), %xmm1
addps %xmm0, %xmm1
movaps %xmm1, 0x60(%rsp)
movlps %xmm1, 0x40(%rsp)
movaps 0x80(%rsp), %xmm2
cmpb $0x1, 0x50(%r12)
jne 0x539e
movss 0x3c(%rsp), %xmm0
addss %xmm3, %xmm0
movss %xmm0, 0x3c(%rsp)
movss %xmm0, 0x48(%rsp)
cmpb $0x1, 0x4f(%r12)
jne 0x53bc
movss 0x3c(%rsp), %xmm0
subss %xmm3, %xmm0
movss %xmm0, 0x3c(%rsp)
movss %xmm0, 0x48(%rsp)
cmpb $0x1, 0x1b(%r12)
jne 0x53d9
movaps %xmm3, %xmm0
mulss 0xc6d(%rip), %xmm0      # 0x603c
addss %xmm0, %xmm2
movss %xmm2, 0x4c(%rsp)
cmpb $0x1, 0x1d(%r12)
jne 0x53f3
mulss 0xc57(%rip), %xmm3      # 0x6040
addss %xmm3, %xmm2
movss %xmm2, 0x4c(%rsp)
cvttps2dq 0x60(%rsp), %xmm0
pand 0xe0(%rsp), %xmm0
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
movd %xmm1, %eax
shll $0xa, %eax
movd %xmm0, %ecx
addl %eax, %ecx
movq 0x10(%r14), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
addl $0xa, %eax
cvtsi2ss %eax, %xmm0
ucomiss %xmm2, %xmm0
jbe 0x5433
movss %xmm0, 0x4c(%rsp)
movaps %xmm0, %xmm2
movaps %xmm2, 0x80(%rsp)
movq 0x130(%rsp), %rdi
movq 0x50(%rsp), %rax
movq %rax, 0x28(%rsp)
movaps 0x40(%rsp), %xmm0
movups %xmm0, 0x18(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x90(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x54b3
movq %r15, %rdi
callq 0x491f
jmp 0x52a3
movq %r14, %rdi
callq 0x3bb8
movq %rbx, %rdi
callq 0x3bb8
leaq 0xf8(%rsp), %rdi
callq 0x4bf7
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x160, %rsp            # imm = 0x160
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq