// File: /workspace/github_c_makefile_star2_O2/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 0x29d(%rip), %rdi       # 0x6f32
movq %r14, %rsi
callq 0x22326
movq %rax, %rbx
testl %ebp, %ebp
je 0x6cd7
movb $0x1, 0x3b9b5(%rip)     # 0x42660
leaq 0x3b9b6(%rip), %r15     # 0x42668
leaq 0x253b1(%rip), %rcx     # 0x2c06a
movl $0x80000, %edx          # imm = 0x80000
movq %r15, %rdi
movq %r14, %rsi
callq 0xa4a0
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x90a0
testq %rbx, %rbx
je 0x6f0e
leaq 0x284(%rip), %rsi       # 0x6f6b
movq %rbx, %rdi
callq 0x18081
movq %rbx, %rdi
callq 0x1d594
movq %rbx, %rdi
callq 0x788c
movq %rbx, %rdi
callq 0x8c84
movq %rbx, %rdi
callq 0x87a8
leaq 0x25381(%rip), %rsi     # 0x2c097
leaq 0x3a333(%rip), %rdx     # 0x41050
movq %rbx, %rdi
callq 0x198df
leaq 0x25379(%rip), %rsi     # 0x2c0a5
movq %rbx, %rdi
callq 0x18741
leaq 0x25370(%rip), %rdx     # 0x2c0ab
pushq $-0x2
popq %rsi
movq %rbx, %rdi
callq 0x18b4f
cmpb $0x0, 0x3b953(%rip)     # 0x426a0
jne 0x6e4d
leaq 0x2554f(%rip), %rdi     # 0x2c2a9
callq 0x65c0
testq %rax, %rax
je 0x6d85
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0xa558
leaq 0x3b923(%rip), %rdi     # 0x426a0
movq %r14, %rsi
jmp 0x6e48
callq 0x9d2b
leaq 0x20(%rsp), %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0xa558
leaq 0x424(%rsp), %r15
leaq 0x8bc(%rsp), %r12
leaq 0x6b0(%rsp), %rbp
leaq 0x8(%rsp), %r14
leaq 0x20(%rsp), %rdi
callq 0xa795
testb %al, %al
je 0x6f1c
xorl %r13d, %r13d
cmpq $0x20, %r13
je 0x6db6
movl $0x28c, %edx            # imm = 0x28C
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
callq 0x63d0
leaq 0x3a346(%rip), %rax     # 0x41130
movq (%r13,%rax), %rsi
movq %r15, %rdi
callq 0xa7b5
movl $0x28c, %edx            # imm = 0x28C
movq %r12, %rdi
movq %r15, %rsi
callq 0x63d0
movq %r12, %rdi
leaq 0x254b1(%rip), %rsi     # 0x2c2c2
callq 0xa7b5
movq %rbp, %rdi
movq %r12, %rsi
callq 0xa8ce
movq %r14, %rdi
movq %rbp, %rsi
callq 0x9ed8
testb $0x1, 0x8(%rsp)
jne 0x6e39
addq $0x10, %r13
jmp 0x6dcb
leaq 0x3b860(%rip), %rdi     # 0x426a0
leaq 0x424(%rsp), %rsi
callq 0xa8ce
leaq 0x25265(%rip), %rdi     # 0x2c0b9
callq 0x65c0
testq %rax, %rax
leaq 0x27c9f(%rip), %r8      # 0x2eb02
cmovneq %rax, %r8
leaq 0x2525b(%rip), %rdx     # 0x2c0c9
leaq 0x3b82b(%rip), %r14     # 0x426a0
leaq 0x20(%rsp), %r15
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x6780
leaq 0x25242(%rip), %rdx     # 0x2c0d5
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x18977
movq %rbx, %rdi
movq %r15, %rsi
callq 0x18741
leaq 0x25743(%rip), %rdx     # 0x2c5f5
pushq $-0x2
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x18b4f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x18741
leaq 0x2520c(%rip), %rdx     # 0x2c0dd
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x18b4f
movq %rbx, %rdi
movl %ebp, %esi
callq 0x180d8
leaq 0x251fe(%rip), %rsi     # 0x2c0ed
pushq $0x8
popq %rdi
xorl %eax, %eax
callq 0x9bed
movq %rbx, %rax
addq $0xb48, %rsp            # imm = 0xB48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x25168(%rip), %rdi     # 0x2c07d
xorl %eax, %eax
callq 0x9939
callq 0x9d2b
leaq 0x253ad(%rip), %rdi     # 0x2c2d5
movq %rax, %rsi
xorl %eax, %eax
callq 0x9939