// File: /workspace/github_c_makefile_star2_O1/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
subq $0x18, %rsp
callq 0x5910
callq 0x15442
movq %rax, %rbx
movq %rax, %rdi
callq 0x15644
leaq -0x76(%rip), %rsi       # 0x5a3c
movq %rbx, %rdi
xorl %edx, %edx
callq 0x660d
leaq 0x1b5c0(%rip), %rsi     # 0x21083
movq %rbx, %rdi
callq 0x6a43
movq %rbx, %rdi
callq 0x20d80
leaq 0x1b5b3(%rip), %rsi     # 0x2108d
movq %rbx, %rdi
callq 0x6a43
leaq 0x1b5a9(%rip), %rsi     # 0x21092
movq %rbx, %rdi
xorl %edx, %edx
callq 0x14bb5
testl %eax, %eax
jne 0x5b0e
movq %rbx, %rdi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6f06
leaq 0x1b586(%rip), %rsi     # 0x2109b
movq %rbx, %rdi
xorl %edx, %edx
callq 0x14bb5
testl %eax, %eax
jne 0x5b3a
movq %rbx, %rdi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6f06
leaq 0x1b568(%rip), %rsi     # 0x210a9
movq %rbx, %rdi
callq 0x6a43
leaq 0x1b563(%rip), %rsi     # 0x210b3
movq %rbx, %rdi
xorl %edx, %edx
callq 0x14bb5
testl %eax, %eax
jne 0x5b75
movq %rbx, %rdi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6f06
leaq 0x1b545(%rip), %r14     # 0x210c1
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6a43
movq %rbx, %rdi
xorl %esi, %esi
callq 0x5dcd
movq %rbx, %rdi
movq %r14, %rsi
callq 0x66dc
leaq 0x1b526(%rip), %rdx     # 0x210c9
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x67f3
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x6e9e
movq %rbx, %rdi
movq %r14, %rsi
callq 0x66dc
leaq -0x2c(%rbp), %r14
leaq 0x1bf3d(%rip), %r15     # 0x21b14
leaq -0x40(%rbp), %r12
movq %r14, %rdi
callq 0x5821
cmpl $0x1b, %eax
je 0x5c6c
movl %eax, %r13d
cmpl $0x71, %eax
je 0x5c6c
cmpl $0x51, %r13d
je 0x5c6c
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %r15, %rdx
callq 0x67f3
movl -0x2c(%rbp), %edx
movq %rbx, %rdi
movl %r13d, %esi
callq 0x59b0
movq %rbx, %rdi
movl $0x1, %esi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x6e9e
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x6084
testl %eax, %eax
jne 0x5c8b
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x5dcd
movq $0x0, -0x40(%rbp)
movq $0xf42400, -0x38(%rbp)  # imm = 0xF42400
movq %r12, %rdi
xorl %esi, %esi
callq 0x51b0
jmp 0x5bdb
leaq 0x1b3d7(%rip), %rdi     # 0x2104a
callq 0x5170
leaq 0x1b3dc(%rip), %rdi     # 0x2105b
callq 0x5170
xorl %edi, %edi
callq 0x54a0
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x62c5
movq %rax, %rdi
callq 0x5945
nop