// File: /workspace/github_c_makefile_star2_O2/tylerneylon[P]APIsWithLua/ch6/eatyguy10.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 eatyguy10 and run the init() function.
  luaL_dofile(L, "eatyguy10.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(NULL);

    // Call eatyguy.loop(state).
    lua_getfield(L, -1, "loop");
    push_state_table(L, key, is_end_of_seq);
    lua_call(L, 1, 1);

    // Check to see if the game is over.
    if (lua_isstring(L, -1)) {
      const char *msg = lua_tostring(L, -1);
      done(msg);
    }

    // Pop the return value of eatyguy.loop() off the stack.
    lua_pop(L, 1);

    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 0x5752
callq 0x13f3e
movq %rax, %rbx
movq %rax, %rdi
callq 0x14164
leaq -0x4a(%rip), %rsi       # 0x585b
movq %rbx, %rdi
xorl %edx, %edx
callq 0x635e
leaq 0x197d5(%rip), %rsi     # 0x1f08b
movq %rbx, %rdi
callq 0x679d
movq %rbx, %rdi
callq 0x1ed4c
leaq 0x197c8(%rip), %rsi     # 0x1f095
movq %rbx, %rdi
callq 0x679d
leaq 0x197be(%rip), %rsi     # 0x1f09a
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1370f
testl %eax, %eax
jne 0x58ff
pushq $-0x1
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6c6c
leaq 0x1979d(%rip), %rsi     # 0x1f0a3
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1370f
testl %eax, %eax
jne 0x5929
pushq $-0x1
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6c6c
leaq 0x19781(%rip), %rsi     # 0x1f0b1
movq %rbx, %rdi
callq 0x679d
leaq 0x1977c(%rip), %rsi     # 0x1f0bb
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1370f
testl %eax, %eax
jne 0x5962
pushq $-0x1
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6c6c
leaq 0x19760(%rip), %r15     # 0x1f0c9
movq %rbx, %rdi
movq %r15, %rsi
callq 0x679d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x5b85
movq %rbx, %rdi
movq %r15, %rsi
callq 0x642c
leaq 0x19741(%rip), %rdx     # 0x1f0d1
pushq $-0x1
popq %r14
movq %rbx, %rdi
movl %r14d, %esi
callq 0x6548
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x6c04
movq %rbx, %rdi
movq %r15, %rsi
callq 0x642c
pushq $0x1
popq %r13
pushq $-0x2
popq %r15
leaq -0x2c(%rbp), %rdi
callq 0x5671
cmpl $0x1b, %eax
je 0x5a3b
movl %eax, %r12d
cmpl $0x71, %eax
je 0x5a3b
cmpl $0x51, %r12d
je 0x5a3b
movq %rbx, %rdi
movl %r14d, %esi
leaq 0x1a1bf(%rip), %rdx     # 0x1fbab
callq 0x6548
movl -0x2c(%rbp), %edx
movq %rbx, %rdi
movl %r12d, %esi
callq 0x57f2
movq %rbx, %rdi
movl %r13d, %esi
movl %r13d, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x6c04
movq %rbx, %rdi
movl %r14d, %esi
callq 0x5de2
testl %eax, %eax
jne 0x5a42
movq %rbx, %rdi
movl %r15d, %esi
callq 0x5b85
movsd 0x195e4(%rip), %xmm0    # 0x1f018
callq 0x56fd
jmp 0x59c3
xorl %edi, %edi
callq 0x5787
pushq $-0x1
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x601e
movq %rax, %rdi
callq 0x5787
nop