// File: /workspace/github_c_makefile_star2_O3/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 0x10e7(%rip), %rax      # 0x666c
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 0x3f6d
leaq 0x1caf(%rip), %rdi      # 0x7278
callq 0x2925
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x592c
movq %rax, %rbx
leaq 0x1ca7(%rip), %rdi      # 0x728d
callq 0x2925
testq %rax, %rax
je 0x592c
movq %rax, %r14
movl (%rbx), %r15d
cmpl (%rax), %r15d
jne 0x5901
movl 0x4(%rbx), %r12d
cmpl 0x4(%r14), %r12d
jne 0x5901
testl %r12d, %r12d
jle 0x564d
leaq (,%r15,4), %rax
xorl %ecx, %ecx
xorl %edx, %edx
testl %r15d, %r15d
jle 0x5642
movq 0x10(%r14), %rsi
addq %rcx, %rsi
xorl %edi, %edi
movzbl 0x2(%rsi,%rdi,4), %r8d
movl %r8d, (%rsi,%rdi,4)
incq %rdi
cmpq %rdi, %r15
jne 0x5630
incq %rdx
addq %rax, %rcx
cmpq %r12, %rdx
jne 0x5622
movl %r15d, 0x90(%rsp)
movl %r12d, 0x94(%rsp)
movq %rbx, 0x98(%rsp)
movq %r14, 0xa0(%rsp)
movaps 0xa5c(%rip), %xmm0      # 0x60d0
movaps %xmm0, 0x40(%rsp)
movsd 0xa5f(%rip), %xmm0      # 0x60e0
movsd %xmm0, 0x50(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x4a46
testb %al, %al
je 0x58ce
movd %r15d, %xmm0
movd %r12d, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
paddd 0xa3e(%rip), %xmm1      # 0x60f0
movdqa %xmm1, 0xe0(%rsp)
movsd 0xa3d(%rip), %xmm0      # 0x6100
movaps %xmm0, 0x60(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x3c(%rsp)
movss 0x95f(%rip), %xmm0      # 0x6038
leaq 0xf8(%rsp), %r15
movss 0xfc(%rsp), %xmm3
movq 0x138(%rsp), %r12
cmpb $0x1, 0x52(%r12)
movaps %xmm0, 0x70(%rsp)
movaps %xmm3, 0xd0(%rsp)
jne 0x575b
movss 0x3c(%rsp), %xmm0
callq 0x21d0
movaps %xmm0, 0x80(%rsp)
movss 0x3c(%rsp), %xmm0
callq 0x2180
movaps 0xd0(%rsp), %xmm3
movaps 0x80(%rsp), %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
mulps 0x9d1(%rip), %xmm1      # 0x6110
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 0x57b7
movss 0x3c(%rsp), %xmm0
callq 0x21d0
movaps %xmm0, 0x80(%rsp)
movss 0x3c(%rsp), %xmm0
callq 0x2180
movaps 0xd0(%rsp), %xmm3
movaps 0x80(%rsp), %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
mulps 0x985(%rip), %xmm1      # 0x6120
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 0x70(%rsp), %xmm2
cmpb $0x1, 0x50(%r12)
jne 0x57da
movss 0x3c(%rsp), %xmm0
addss %xmm3, %xmm0
movss %xmm0, 0x3c(%rsp)
movss %xmm0, 0x48(%rsp)
cmpb $0x1, 0x4f(%r12)
jne 0x57f8
movss 0x3c(%rsp), %xmm0
subss %xmm3, %xmm0
movss %xmm0, 0x3c(%rsp)
movss %xmm0, 0x48(%rsp)
cmpb $0x1, 0x1b(%r12)
jne 0x5815
movaps %xmm3, %xmm0
mulss 0x831(%rip), %xmm0      # 0x603c
addss %xmm0, %xmm2
movss %xmm2, 0x4c(%rsp)
cmpb $0x1, 0x1d(%r12)
jne 0x582f
mulss 0x81b(%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
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
ucomiss %xmm2, %xmm0
jbe 0x5872
movss %xmm0, 0x4c(%rsp)
movaps %xmm0, %xmm2
movaps %xmm2, 0x70(%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 0x593e
movq %r15, %rdi
callq 0x4b6f
movq %r15, %rdi
callq 0x4a46
movaps 0x70(%rsp), %xmm0
testb %al, %al
jne 0x56e1
movq 0x10(%r14), %rdi
callq 0x2290
movq %r14, %rdi
callq 0x2290
movq 0x10(%rbx), %rdi
callq 0x2290
movq %rbx, %rdi
callq 0x2290
leaq 0xf8(%rsp), %rdi
callq 0x5042
xorl %ebp, %ebp
jmp 0x592c
leaq 0x868(%rip), %rsi       # 0x6170
leaq 0xb81(%rip), %rdx       # 0x6490
leaq 0x198d(%rip), %rcx      # 0x72a3
leaq 0x199d(%rip), %r9       # 0x72ba
xorl %edi, %edi
movl $0x27, %r8d
xorl %eax, %eax
callq 0x22e0
movl %ebp, %eax
addq $0x160, %rsp            # imm = 0x160
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq