// File: /workspace/github_c_makefile_star2_O0/tylerneylon[P]APIsWithLua/ch3/eatyguy3.c
// Function: main
int main() {

  start();

  // Create a Lua state and load the module.
  lua_State *L = luaL_newstate();
  luaL_openlibs(L);

  // Make our Lua-callable functions visible to Lua.
  lua_register(L, "set_color", set_color);
  lua_register(L, "set_pos",   set_pos);
  lua_register(L, "timestamp", timestamp);

  // Load eatyguy3 and run the init() function.
  luaL_dofile(L, "eatyguy3.lua");
  lua_setglobal(L, "eatyguy");
  lua_settop(L, 0);

  lua_getglobal(L, "eatyguy");
  lua_getfield(L, -1, "init");  // -1 means stack top.
  lua_call(L, 0, 0);            // 0, 0 = #args, #retvals

  lua_getglobal(L, "eatyguy");
  while (1) {
    int is_end_of_seq;
    int key = getkey(&is_end_of_seq);
    if (key == ESC_KEY || key == 'q' || key == 'Q') done();

    // Call eatyguy.loop(<key>).
    lua_getfield(L, -1, "loop");
    push_keypress(L, key, is_end_of_seq);
    lua_call(L, 1, 0);

    sleephires(0.016);  // Sleep for 16ms.
  }

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movl $0x0, -0x4(%rbp)
callq 0x5760
callq 0x217c0
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x21b80
movq -0x10(%rbp), %rdi
leaq -0x161(%rip), %rsi      # 0x5830
xorl %edx, %edx
callq 0x6eb0
movq -0x10(%rbp), %rdi
leaq 0x2e6fc(%rip), %rsi     # 0x3409f
callq 0x77f0
movq -0x10(%rbp), %rdi
leaq -0x103(%rip), %rsi      # 0x58b0
xorl %edx, %edx
callq 0x6eb0
movq -0x10(%rbp), %rdi
leaq 0x2e6e4(%rip), %rsi     # 0x340a9
callq 0x77f0
movq -0x10(%rbp), %rdi
leaq -0xa5(%rip), %rsi       # 0x5930
xorl %edx, %edx
callq 0x6eb0
movq -0x10(%rbp), %rdi
leaq 0x2e6ca(%rip), %rsi     # 0x340b1
callq 0x77f0
movq -0x10(%rbp), %rdi
leaq 0x2e6c4(%rip), %rsi     # 0x340bb
xorl %eax, %eax
movl %eax, %edx
callq 0x20c90
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x19(%rbp)
jne 0x5a2f
movq -0x10(%rbp), %rdi
xorl %ecx, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl %eax, %r9d
movl %ecx, %esi
movq %r9, %r8
callq 0x8220
cmpl $0x0, %eax
setne %al
movb %al, -0x19(%rbp)
movb -0x19(%rbp), %al
movq -0x10(%rbp), %rdi
leaq 0x2e68b(%rip), %rsi     # 0x340c8
callq 0x77f0
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x5dd0
movq -0x10(%rbp), %rdi
leaq 0x2e670(%rip), %rsi     # 0x340c8
callq 0x70c0
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x2e663(%rip), %rdx     # 0x340d0
callq 0x7300
movq -0x10(%rbp), %rdi
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %r8d
movl %edx, %esi
movq %r8, %rcx
callq 0x8150
movq -0x10(%rbp), %rdi
leaq 0x2e636(%rip), %rsi     # 0x340c8
callq 0x70c0
leaq -0x14(%rbp), %rdi
callq 0x5650
movl %eax, -0x18(%rbp)
cmpl $0x1b, -0x18(%rbp)
je 0x5ab5
cmpl $0x71, -0x18(%rbp)
je 0x5ab5
cmpl $0x51, -0x18(%rbp)
jne 0x5aba
callq 0x57a0
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x2f3f6(%rip), %rdx     # 0x34ec0
callq 0x7300
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %esi
movl -0x14(%rbp), %edx
callq 0x57d0
movq -0x10(%rbp), %rdi
movl $0x1, %esi
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rcx
callq 0x8150
movsd 0x2e51a(%rip), %xmm0    # 0x34018
callq 0x56f0
jmp 0x5a97
nopw %cs:(%rax,%rax)
nop