// File: /workspace/github_c_makefile_star2_O2/deplinenoise[P]tundra/src/LuaInterface.cpp
// Function: t2::RunBuildScript(lua_State*, char const**, int)
bool RunBuildScript(lua_State *L, const char** args, int argc_count)
{
  // Push our error handler on the stack now (before the chunk to run)
  lua_pushcclosure(L, LuaGetTraceback, 0);
  int error_index = lua_gettop(L);

  switch (luaL_loadbuffer(L, s_BootSnippet, sizeof(s_BootSnippet)-1, "boot_snippet"))
  {
  case LUA_ERRMEM    : Croak("out of memory");
  case LUA_ERRSYNTAX : Croak("syntax error\n%s\n", lua_tostring(L, -1));
  }

  // Expose the path to tundra2 -- we're not that executable, so we'll need to find it.
  {
    PathBuffer exe_path;
    PathInit(&exe_path, GetExePath());
    PathStripLast(&exe_path);
    PathConcat(&exe_path, "tundra2" TUNDRA_EXE_SUFFIX);

    char tundra2_exe[kMaxPathLength];
    PathFormat(tundra2_exe, &exe_path);

    lua_pushstring(L, tundra2_exe);
    lua_setglobal(L, "TundraExePath");
  }

  // Push args from the command line
  for (int i = 0; i < argc_count; ++i)
  {
    lua_pushstring(L, args[i]);
  }

  int res = lua_pcall(L, /*narg:*/argc_count, /*nres:*/1, /*errorfunc:*/ error_index);

  if (0 == res)
  {
    return true;
  }
  else
  {
    // pcall failed
    if (lua_isstring(L, -1))
    {
      fprintf(stderr, "\nLua error\n%s\n", lua_tostring(L, -1));
    }
    return false;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4a8, %rsp            # imm = 0x4A8
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x15d(%rip), %rsi       # 0x70f7
xorl %edx, %edx
callq 0x1886c
movq %rbx, %rdi
callq 0x180cb
movl %eax, %r15d
leaq 0x2539d(%rip), %rsi     # 0x2c350
leaq 0x25150(%rip), %rcx     # 0x2c10a
pushq $0x2e
popq %rdx
movq %rbx, %rdi
callq 0x1a179
cmpl $0x3, %eax
je 0x70cb
movl %r15d, 0xc(%rsp)
cmpl $0x4, %eax
je 0x70e9
callq 0x9d2b
leaq 0x21c(%rsp), %r12
xorl %r15d, %r15d
movq %r12, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0xa558
movq %r12, %rdi
callq 0xa795
leaq 0x2512e(%rip), %rsi     # 0x2c136
movq %r12, %rdi
callq 0xa7b5
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0xa8ce
movq %rbx, %rdi
movq %r13, %rsi
callq 0x18741
leaq 0x2510c(%rip), %rdx     # 0x2c13e
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x18b4f
testl %ebp, %ebp
movl $0x0, %r12d
cmovgl %ebp, %r12d
cmpq %r15, %r12
je 0x7061
movq (%r14,%r15,8), %rsi
movq %rbx, %rdi
callq 0x18741
incq %r15
jmp 0x704b
pushq $0x1
popq %rdx
movq %rbx, %rdi
movl %ebp, %esi
movl 0xc(%rsp), %ecx
callq 0x18dbd
movl %eax, %ebp
testl %eax, %eax
je 0x7087
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0x183a0
testl %eax, %eax
jne 0x709e
testl %ebp, %ebp
sete %al
addq $0x4a8, %rsp            # imm = 0x4A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3af33(%rip), %rax     # 0x41fd8
movq (%rax), %r14
pushq $-0x1
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1853d
leaq 0x25090(%rip), %rsi     # 0x2c14c
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x6670
jmp 0x7087
pushq $-0x1
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1853d
leaq 0x25046(%rip), %rdi     # 0x2c125
movq %rax, %rsi
xorl %eax, %eax
callq 0x9939
leaq 0x25027(%rip), %rdi     # 0x2c117
xorl %eax, %eax
callq 0x9939