// File: /workspace/github_c_makefile_star2_O2/tylerneylon[P]APIsWithLua/ch4/eatyguy8.c
// Function: main
int main() {

  start();

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

  // Set up API functions written in C.
  lua_register(L, "timestamp", timestamp);
  luaopen_Pair(L);
  lua_setglobal(L, "Pair");

  // Set up API functions written in Lua.
  luaL_dofile(L, "util.lua");
  luaL_dofile(L, "Character.lua");
  lua_setglobal(L, "Character");

  // Load eatyguy8 and run the init() function.
  luaL_dofile(L, "eatyguy8.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(state).
    lua_getfield(L, -1, "loop");
    push_state_table(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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
callq 0x5742
callq 0x13eea
movq %rax, %rbx
movq %rax, %rdi
callq 0x14110
leaq -0x4a(%rip), %rsi       # 0x5839
movq %rbx, %rdi
xorl %edx, %edx
callq 0x630a
leaq 0x197f7(%rip), %rsi     # 0x1f08b
movq %rbx, %rdi
callq 0x6749
movq %rbx, %rdi
callq 0x1ecf8
leaq 0x197ea(%rip), %rsi     # 0x1f095
movq %rbx, %rdi
callq 0x6749
leaq 0x197e0(%rip), %rsi     # 0x1f09a
movq %rbx, %rdi
xorl %edx, %edx
callq 0x136bb
testl %eax, %eax
jne 0x58dd
pushq $-0x1
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6c18
leaq 0x197bf(%rip), %rsi     # 0x1f0a3
movq %rbx, %rdi
xorl %edx, %edx
callq 0x136bb
testl %eax, %eax
jne 0x5907
pushq $-0x1
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6c18
leaq 0x197a3(%rip), %rsi     # 0x1f0b1
movq %rbx, %rdi
callq 0x6749
leaq 0x1979e(%rip), %rsi     # 0x1f0bb
movq %rbx, %rdi
xorl %edx, %edx
callq 0x136bb
testl %eax, %eax
jne 0x5940
pushq $-0x1
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6c18
leaq 0x19781(%rip), %r15     # 0x1f0c8
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6749
movq %rbx, %rdi
xorl %esi, %esi
callq 0x5b31
movq %rbx, %rdi
movq %r15, %rsi
callq 0x63d8
leaq 0x19762(%rip), %rdx     # 0x1f0d0
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0x64f4
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x6bb0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x63d8
leaq -0x2c(%rbp), %r15
leaq 0x1a20b(%rip), %r12     # 0x1fbab
pushq $0x1
popq %r13
movq %r15, %rdi
callq 0x5661
cmpl $0x1b, %eax
je 0x59fc
movl %eax, %r14d
cmpl $0x71, %eax
je 0x59fc
cmpl $0x51, %r14d
je 0x59fc
movq %rbx, %rdi
pushq $-0x1
popq %rsi
movq %r12, %rdx
callq 0x64f4
movl -0x2c(%rbp), %edx
movq %rbx, %rdi
movl %r14d, %esi
callq 0x57d0
movq %rbx, %rdi
movl %r13d, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x6bb0
movsd 0x19623(%rip), %xmm0    # 0x1f018
callq 0x56ed
jmp 0x59a4
callq 0x5777
nopl (%rax)