// File: /workspace/github_c_makefile_star2_O3/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
subq $0x18, %rsp
callq 0x58f0
callq 0x15eb1
movq %rax, %rbx
movq %rax, %rdi
callq 0x160b4
leaq -0x76(%rip), %rsi       # 0x5a0a
movq %rbx, %rdi
xorl %edx, %edx
callq 0x66a8
leaq 0x1c5f2(%rip), %rsi     # 0x22083
movq %rbx, %rdi
callq 0x6aec
movq %rbx, %rdi
callq 0x21ad8
leaq 0x1c5e5(%rip), %rsi     # 0x2208d
movq %rbx, %rdi
callq 0x6aec
leaq 0x1c5db(%rip), %rsi     # 0x22092
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1560a
testl %eax, %eax
jne 0x5adc
movq %rbx, %rdi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6fcf
leaq 0x1c5b8(%rip), %rsi     # 0x2209b
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1560a
testl %eax, %eax
jne 0x5b08
movq %rbx, %rdi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6fcf
leaq 0x1c59a(%rip), %rsi     # 0x220a9
movq %rbx, %rdi
callq 0x6aec
leaq 0x1c595(%rip), %rsi     # 0x220b3
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1560a
testl %eax, %eax
jne 0x5b43
movq %rbx, %rdi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6fcf
leaq 0x1c576(%rip), %r14     # 0x220c0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6aec
movq %rbx, %rdi
xorl %esi, %esi
callq 0x5d4d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6777
leaq 0x1c557(%rip), %rdx     # 0x220c8
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x6895
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x6f67
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6777
leaq -0x2c(%rbp), %r14
leaq 0x1cfdf(%rip), %r15     # 0x22b84
leaq -0x40(%rbp), %r12
movq %r14, %rdi
callq 0x5801
cmpl $0x1b, %eax
je 0x5c12
movl %eax, %r13d
cmpl $0x71, %eax
je 0x5c12
cmpl $0x51, %r13d
je 0x5c12
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %r15, %rdx
callq 0x6895
movl -0x2c(%rbp), %edx
movq %rbx, %rdi
movl %r13d, %esi
callq 0x597e
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x6f67
movq $0x0, -0x40(%rbp)
movq $0xf42400, -0x38(%rbp)  # imm = 0xF42400
movq %r12, %rdi
xorl %esi, %esi
callq 0x5190
jmp 0x5ba9
callq 0x5925
nop