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

  // Load eatyguy5 and run the init() function.
  luaL_dofile(L, "eatyguy5.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");
    // This replaces the previous call to push_keypress().
    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 0x21740
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x21b00
movq -0x10(%rbp), %rdi
leaq -0x61(%rip), %rsi       # 0x58b0
xorl %edx, %edx
callq 0x6e30
movq -0x10(%rbp), %rdi
leaq 0x2e768(%rip), %rsi     # 0x3408b
callq 0x7770
movq -0x10(%rbp), %rdi
leaq 0x2e762(%rip), %rsi     # 0x34095
xorl %eax, %eax
movl %eax, %edx
callq 0x20c10
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 0x81a0
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 0x20c10
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 0x81a0
cmpl $0x0, %eax
setne %al
movb %al, -0x1a(%rbp)
movb -0x1a(%rbp), %al
movq -0x10(%rbp), %rdi
leaq 0x2e6ec(%rip), %rsi     # 0x340ab
callq 0x7770
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x5d50
movq -0x10(%rbp), %rdi
leaq 0x2e6d1(%rip), %rsi     # 0x340ab
callq 0x7040
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x2e6c4(%rip), %rdx     # 0x340b3
callq 0x7280
movq -0x10(%rbp), %rdi
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %r8d
movl %edx, %esi
movq %r8, %rcx
callq 0x80d0
movq -0x10(%rbp), %rdi
leaq 0x2e697(%rip), %rsi     # 0x340ab
callq 0x7040
leaq -0x14(%rbp), %rdi
callq 0x5650
movl %eax, -0x18(%rbp)
cmpl $0x1b, -0x18(%rbp)
je 0x5a37
cmpl $0x71, -0x18(%rbp)
je 0x5a37
cmpl $0x51, -0x18(%rbp)
jne 0x5a3c
callq 0x57a0
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x2f454(%rip), %rdx     # 0x34ea0
callq 0x7280
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 0x80d0
movsd 0x2e598(%rip), %xmm0    # 0x34018
callq 0x56f0
jmp 0x5a19
nopw (%rax,%rax)