// File: /workspace/github_c_makefile_star2_O0/tylerneylon[P]APIsWithLua/ch3/eatyguy4.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 eatyguy4 and run the init() function.
  luaL_dofile(L, "eatyguy4.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(<key>).
    lua_getfield(L, -1, "loop");
    push_keypress(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 0x216c0
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x21a80
movq -0x10(%rbp), %rdi
leaq -0x61(%rip), %rsi       # 0x5830
xorl %edx, %edx
callq 0x6db0
movq -0x10(%rbp), %rdi
leaq 0x2e7de(%rip), %rsi     # 0x34081
callq 0x76f0
movq -0x10(%rbp), %rdi
leaq 0x2e7d8(%rip), %rsi     # 0x3408b
xorl %eax, %eax
movl %eax, %edx
callq 0x20b90
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x19(%rbp)
jne 0x58eb
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 0x8120
cmpl $0x0, %eax
setne %al
movb %al, -0x19(%rbp)
movb -0x19(%rbp), %al
movq -0x10(%rbp), %rdi
leaq 0x2e79b(%rip), %rsi     # 0x34094
xorl %eax, %eax
movl %eax, %edx
callq 0x20b90
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x1a(%rbp)
jne 0x5931
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 0x8120
cmpl $0x0, %eax
setne %al
movb %al, -0x1a(%rbp)
movb -0x1a(%rbp), %al
movq -0x10(%rbp), %rdi
leaq 0x2e762(%rip), %rsi     # 0x340a1
callq 0x76f0
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x5cd0
movq -0x10(%rbp), %rdi
leaq 0x2e747(%rip), %rsi     # 0x340a1
callq 0x6fc0
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x2e73a(%rip), %rdx     # 0x340a9
callq 0x7200
movq -0x10(%rbp), %rdi
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %r8d
movl %edx, %esi
movq %r8, %rcx
callq 0x8050
movq -0x10(%rbp), %rdi
leaq 0x2e70d(%rip), %rsi     # 0x340a1
callq 0x6fc0
leaq -0x14(%rbp), %rdi
callq 0x5650
movl %eax, -0x18(%rbp)
cmpl $0x1b, -0x18(%rbp)
je 0x59b7
cmpl $0x71, -0x18(%rbp)
je 0x59b7
cmpl $0x51, -0x18(%rbp)
jne 0x59bc
callq 0x57a0
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x2f4c4(%rip), %rdx     # 0x34e90
callq 0x7200
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %esi
movl -0x14(%rbp), %edx
callq 0x57d0
movq -0x10(%rbp), %rdi
movl $0x1, %esi
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rcx
callq 0x8050
movsd 0x2e618(%rip), %xmm0    # 0x34018
callq 0x56f0
jmp 0x5999
nopw (%rax,%rax)