// File: /workspace/github_c_makefile_star2_O0/tylerneylon[P]APIsWithLua/ch4/eatyguy6.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);

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

  // Load eatyguy6 and run the init() function.
  luaL_dofile(L, "eatyguy6.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
subq $0x20, %rsp
movl $0x0, -0x4(%rbp)
callq 0x5760
callq 0x21790
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x21b50
movq -0x10(%rbp), %rdi
leaq -0x61(%rip), %rsi       # 0x58b0
xorl %edx, %edx
callq 0x6e80
movq -0x10(%rbp), %rdi
leaq 0x2e768(%rip), %rsi     # 0x3408b
callq 0x77c0
movq -0x10(%rbp), %rdi
leaq 0x2e762(%rip), %rsi     # 0x34095
xorl %eax, %eax
movl %eax, %edx
callq 0x20c60
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x19(%rbp)
jne 0x596b
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 0x81f0
cmpl $0x0, %eax
setne %al
movb %al, -0x19(%rbp)
movb -0x19(%rbp), %al
movq -0x10(%rbp), %rdi
leaq 0x2e725(%rip), %rsi     # 0x3409e
xorl %eax, %eax
movl %eax, %edx
callq 0x20c60
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x1a(%rbp)
jne 0x59b1
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 0x81f0
cmpl $0x0, %eax
setne %al
movb %al, -0x1a(%rbp)
movb -0x1a(%rbp), %al
movq -0x10(%rbp), %rdi
leaq 0x2e6ed(%rip), %rsi     # 0x340ac
callq 0x77c0
movq -0x10(%rbp), %rdi
leaq 0x2e6e7(%rip), %rsi     # 0x340b6
xorl %eax, %eax
movl %eax, %edx
callq 0x20c60
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x1b(%rbp)
jne 0x5a07
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 0x81f0
cmpl $0x0, %eax
setne %al
movb %al, -0x1b(%rbp)
movb -0x1b(%rbp), %al
movq -0x10(%rbp), %rdi
leaq 0x2e6ae(%rip), %rsi     # 0x340c3
callq 0x77c0
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x5da0
movq -0x10(%rbp), %rdi
leaq 0x2e693(%rip), %rsi     # 0x340c3
callq 0x7090
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x2e686(%rip), %rdx     # 0x340cb
callq 0x72d0
movq -0x10(%rbp), %rdi
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %r8d
movl %edx, %esi
movq %r8, %rcx
callq 0x8120
movq -0x10(%rbp), %rdi
leaq 0x2e659(%rip), %rsi     # 0x340c3
callq 0x7090
leaq -0x14(%rbp), %rdi
callq 0x5650
movl %eax, -0x18(%rbp)
cmpl $0x1b, -0x18(%rbp)
je 0x5a8d
cmpl $0x71, -0x18(%rbp)
je 0x5a8d
cmpl $0x51, -0x18(%rbp)
jne 0x5a92
callq 0x57a0
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x2f40e(%rip), %rdx     # 0x34eb0
callq 0x72d0
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %esi
movl -0x14(%rbp), %edx
callq 0x5830
movq -0x10(%rbp), %rdi
movl $0x1, %esi
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rcx
callq 0x8120
movsd 0x2e542(%rip), %xmm0    # 0x34018
callq 0x56f0
jmp 0x5a6f
nopl (%rax)