// File: /workspace/github_c_makefile_star2_O3/deplinenoise[P]tundra/src/LuaInterface.cpp
// Function: t2::CreateLuaState(t2::MemAllocHeap*, bool)
lua_State* CreateLuaState(MemAllocHeap* lua_heap, bool profile)
{
  lua_State* L = lua_newstate(LuaAllocFunc, lua_heap);

  if (profile)
  {
    s_IsProfiling = true;
    LinearAllocInit(&s_ProfilerAllocator, lua_heap, 512 * 1024, "Profiler Allocator");
    LuaProfilerInit(lua_heap, &s_ProfilerAllocator, L); 
  }

  if (!L)
    Croak("couldn't create Lua state");

  // Install panic function.
  lua_atpanic(L, OnLuaPanic);

  // Expose standard Lua libraries.
  luaL_openlibs(L);

  // Expose interpolation module
  LuaEnvNativeOpen(L);

  // Expose path module
  LuaPathNativeOpen(L);

  // Expose JSON writer module
  LuaJsonNativeOpen(L);

  luaL_register(L, "tundra.native", s_LuaFunctions);
  lua_pushstring(L, TUNDRA_PLATFORM_STRING);
  lua_setfield(L, -2, "host_platform");

  // setup package.path
  {
    const char* homedir = FindScriptDirectory();
    const char* luapath = getenv("TUNDRA_LUA_PATH");
    char ppath[1024];
    snprintf(ppath, sizeof(ppath), "%s" TD_PATHSEP_STR "?.lua;%s", homedir, (luapath ? luapath : ""));
    lua_getglobal(L, "package");
    CHECK(LUA_TTABLE == lua_type(L, -1));

    lua_pushstring(L, ppath);
    lua_setfield(L, -2, "path");

    lua_pushstring(L, homedir);
    lua_setglobal(L, "TundraScriptDir");
  }

  /* native table on the top of the stack */
  lua_pop(L, 1);

  Log(kDebug, "Lua initialized successfully");
  return L;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb48, %rsp            # imm = 0xB48
movl %esi, %ebp
movq %rdi, %r14
leaq 0x2c6(%rip), %rdi       # 0x73a7
movq %r14, %rsi
callq 0x25039
movq %rax, %rbx
testl %ebp, %ebp
je 0x7123
movb $0x1, 0x3b569(%rip)     # 0x42660
leaq 0x3b56a(%rip), %r15     # 0x42668
leaq 0x28f65(%rip), %rcx     # 0x3006a
movl $0x80000, %edx          # imm = 0x80000
movq %r15, %rdi
movq %r14, %rsi
callq 0xaca4
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x97ec
testq %rbx, %rbx
je 0x7399
leaq 0x2ad(%rip), %rsi       # 0x73e0
movq %rbx, %rdi
callq 0x19fe2
movq %rbx, %rdi
callq 0x1fcc8
movq %rbx, %rdi
callq 0x7d3c
movq %rbx, %rdi
callq 0x93a4
movq %rbx, %rdi
callq 0x8dac
leaq 0x28f35(%rip), %rsi     # 0x30097
leaq 0x39ee7(%rip), %rdx     # 0x41050
movq %rbx, %rdi
callq 0x1b9e5
leaq 0x28f2d(%rip), %rsi     # 0x300a5
movq %rbx, %rdi
callq 0x1a7f6
leaq 0x28f24(%rip), %rdx     # 0x300ab
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1ac10
cmpb $0x0, 0x3b505(%rip)     # 0x426a0
jne 0x72bd
leaq 0x29101(%rip), %rdi     # 0x302a9
callq 0x65c0
testq %rax, %rax
je 0x71d3
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0xad64
leaq 0x3b4d5(%rip), %rdi     # 0x426a0
movq %r14, %rsi
jmp 0x72b8
callq 0xa46c
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0xad64
movq %r14, %rdi
callq 0xafa1
testb %al, %al
je 0x7383
movb $0x1, %r13b
xorl %r15d, %r15d
leaq 0x424(%rsp), %r14
leaq 0x8bc(%rsp), %r12
leaq 0x6b0(%rsp), %rbp
movl $0x28c, %edx            # imm = 0x28C
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
callq 0x63d0
shlq $0x4, %r15
leaq 0x39efb(%rip), %rax     # 0x41130
movq (%r15,%rax), %rsi
movq %r14, %rdi
callq 0xafc1
movl $0x28c, %edx            # imm = 0x28C
movq %r12, %rdi
movq %r14, %rsi
callq 0x63d0
movq %r12, %rdi
leaq 0x29067(%rip), %rsi     # 0x302c2
callq 0xafc1
movq %rbp, %rdi
movq %r12, %rsi
callq 0xb0e6
leaq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0xa61c
testb $0x1, 0x8(%rsp)
jne 0x72a9
movl $0x1, %r15d
testb $0x1, %r13b
jne 0x72a0
leaq 0x20(%rsp), %rdi
callq 0xafa1
testb %al, %al
je 0x7383
xorl %r15d, %r15d
xorb $0x1, %r13b
jmp 0x7218
leaq 0x3b3f0(%rip), %rdi     # 0x426a0
leaq 0x424(%rsp), %rsi
callq 0xb0e6
leaq 0x28df5(%rip), %rdi     # 0x300b9
callq 0x65c0
testq %rax, %rax
leaq 0x2b83f(%rip), %r8      # 0x32b12
cmovneq %rax, %r8
leaq 0x28deb(%rip), %rdx     # 0x300c9
leaq 0x3b3bb(%rip), %r14     # 0x426a0
leaq 0x20(%rsp), %r15
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x6780
leaq 0x28dd2(%rip), %rdx     # 0x300d5
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x1aa34
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1a7f6
leaq 0x292d0(%rip), %rdx     # 0x305f2
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1ac10
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1a7f6
leaq 0x28d9c(%rip), %rdx     # 0x300dd
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x1ac10
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1a039
leaq 0x28d8b(%rip), %rsi     # 0x300ed
movl $0x8, %edi
xorl %eax, %eax
callq 0xa32c
movq %rbx, %rax
addq $0xb48, %rsp            # imm = 0xB48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0xa46c
leaq 0x28f46(%rip), %rdi     # 0x302d5
movq %rax, %rsi
xorl %eax, %eax
callq 0xa019
leaq 0x28cdd(%rip), %rdi     # 0x3007d
xorl %eax, %eax
callq 0xa019