// File: /workspace/github_c_makefile_star2_O0/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
subq $0x30, %rsp
movl $0x0, -0x4(%rbp)
callq 0x5770
callq 0x21820
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x21be0
movq -0x10(%rbp), %rdi
leaq -0x61(%rip), %rsi       # 0x58e0
xorl %edx, %edx
callq 0x6f10
movq -0x10(%rbp), %rdi
leaq 0x2e73c(%rip), %rsi     # 0x3408f
callq 0x7850
movq -0x10(%rbp), %rdi
callq 0x336f0
movq -0x10(%rbp), %rdi
leaq 0x2e72d(%rip), %rsi     # 0x34099
callq 0x7850
movq -0x10(%rbp), %rdi
leaq 0x2e722(%rip), %rsi     # 0x3409e
xorl %eax, %eax
movl %eax, %edx
callq 0x20cf0
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x21(%rbp)
jne 0x59b4
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 0x8280
cmpl $0x0, %eax
setne %al
movb %al, -0x21(%rbp)
movb -0x21(%rbp), %al
movq -0x10(%rbp), %rdi
leaq 0x2e6e5(%rip), %rsi     # 0x340a7
xorl %eax, %eax
movl %eax, %edx
callq 0x20cf0
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x22(%rbp)
jne 0x59fa
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 0x8280
cmpl $0x0, %eax
setne %al
movb %al, -0x22(%rbp)
movb -0x22(%rbp), %al
movq -0x10(%rbp), %rdi
leaq 0x2e6ad(%rip), %rsi     # 0x340b5
callq 0x7850
movq -0x10(%rbp), %rdi
leaq 0x2e6a7(%rip), %rsi     # 0x340bf
xorl %eax, %eax
movl %eax, %edx
callq 0x20cf0
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x23(%rbp)
jne 0x5a50
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 0x8280
cmpl $0x0, %eax
setne %al
movb %al, -0x23(%rbp)
movb -0x23(%rbp), %al
movq -0x10(%rbp), %rdi
leaq 0x2e66f(%rip), %rsi     # 0x340cd
callq 0x7850
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x5e30
movq -0x10(%rbp), %rdi
leaq 0x2e654(%rip), %rsi     # 0x340cd
callq 0x7120
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x2e647(%rip), %rdx     # 0x340d5
callq 0x7360
movq -0x10(%rbp), %rdi
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %r8d
movl %edx, %esi
movq %r8, %rcx
callq 0x81b0
movq -0x10(%rbp), %rdi
leaq 0x2e61a(%rip), %rsi     # 0x340cd
callq 0x7120
leaq -0x14(%rbp), %rdi
callq 0x5660
movl %eax, -0x18(%rbp)
cmpl $0x1b, -0x18(%rbp)
je 0x5ad6
cmpl $0x71, -0x18(%rbp)
je 0x5ad6
cmpl $0x51, -0x18(%rbp)
jne 0x5adf
xorl %eax, %eax
movl %eax, %edi
callq 0x57b0
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x2f3d1(%rip), %rdx     # 0x34ec0
callq 0x7360
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %esi
movl -0x14(%rbp), %edx
callq 0x5860
movq -0x10(%rbp), %rdi
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %r8d
movl %edx, %esi
movq %r8, %rcx
callq 0x81b0
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x63d0
cmpl $0x0, %eax
je 0x5b4d
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl %eax, %edx
callq 0x6840
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x57b0
movq -0x10(%rbp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x5e30
movsd 0x2e4b5(%rip), %xmm0    # 0x34018
callq 0x5700
jmp 0x5ab8
nopl (%rax)