// File: /workspace/github_c_makefile_star2_O1/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 0x2dd(%rip), %rdi       # 0x73be
movq %r14, %rsi
callq 0x25215
movq %rax, %r12
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 0xad30
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x988c
testq %r12, %r12
je 0x739a
leaq 0x2c4(%rip), %rsi       # 0x73f7
movq %r12, %rdi
callq 0x1a753
movq %r12, %rdi
callq 0x1ffa8
movq %r12, %rdi
callq 0x7d54
movq %r12, %rdi
callq 0x9444
movq %r12, %rdi
callq 0x8e60
leaq 0x28f35(%rip), %rsi     # 0x30097
leaq 0x39ee7(%rip), %rdx     # 0x41050
movq %r12, %rdi
callq 0x1c034
leaq 0x28f2d(%rip), %rsi     # 0x300a5
movq %r12, %rdi
callq 0x1ae54
leaq 0x28f24(%rip), %rdx     # 0x300ab
movq %r12, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1b267
cmpb $0x0, 0x3b505(%rip)     # 0x426a0
jne 0x72d4
leaq 0x29101(%rip), %rdi     # 0x302a9
callq 0x65c0
testq %rax, %rax
je 0x71d8
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0xadf0
leaq 0x3b4d5(%rip), %rdi     # 0x426a0
movq %r14, %rsi
callq 0xb16e
jmp 0x72d4
movq %r12, 0x10(%rsp)
callq 0xa554
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0xadf0
movq %r14, %rdi
callq 0xb030
movb %al, 0xf(%rsp)
testb %al, %al
je 0x72c4
leaq 0x8bc(%rsp), %r14
leaq 0x630(%rsp), %r12
leaq 0x430(%rsp), %r13
xorl %r15d, %r15d
xorl %ebp, %ebp
movl $0x28c, %edx            # imm = 0x28C
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
callq 0x63d0
leaq 0x39ef2(%rip), %rax     # 0x41130
movq (%r15,%rax), %rsi
movq 0x8(%r15,%rax), %rbx
movq %r14, %rdi
callq 0xb050
movl $0x28c, %edx            # imm = 0x28C
movq %r12, %rdi
movq %r14, %rsi
callq 0x63d0
movq %r12, %rdi
movq %rbx, %rsi
callq 0xb050
movq %r13, %rdi
movq %r12, %rsi
callq 0xb16e
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0xa704
testb $0x1, 0x18(%rsp)
jne 0x7299
addq $0x10, %r15
cmpq $0x10, %r15
setne %bpl
je 0x7225
jmp 0x72ae
leaq 0x3b400(%rip), %rdi     # 0x426a0
movq %r14, %rsi
callq 0xb16e
testb $0x1, %bpl
je 0x72c4
leaq 0x30(%rsp), %rdi
callq 0xb030
movb %al, 0xf(%rsp)
testb %al, %al
jne 0x7220
testb $0x1, 0xf(%rsp)
movq 0x10(%rsp), %r12
je 0x73a8
leaq 0x28dde(%rip), %rdi     # 0x300b9
callq 0x65c0
testq %rax, %rax
leaq 0x2b7b8(%rip), %r8      # 0x32aa2
cmovneq %rax, %r8
leaq 0x28dd4(%rip), %rdx     # 0x300c9
leaq 0x3b3a4(%rip), %r14     # 0x426a0
leaq 0x30(%rsp), %rbx
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x6780
leaq 0x28dbb(%rip), %rdx     # 0x300d5
movq %r12, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x1b08b
movq %r12, %rdi
movq %rbx, %rsi
callq 0x1ae54
leaq 0x292b9(%rip), %rdx     # 0x305f2
movq %r12, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1b267
movq %r12, %rdi
movq %r14, %rsi
callq 0x1ae54
leaq 0x28d85(%rip), %rdx     # 0x300dd
movq %r12, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x1b267
movq %r12, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1a7aa
leaq 0x28d74(%rip), %rsi     # 0x300ed
movl $0x8, %edi
xorl %eax, %eax
callq 0xa414
movq %r12, %rax
addq $0xb48, %rsp            # imm = 0xB48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x28cdc(%rip), %rdi     # 0x3007d
xorl %eax, %eax
callq 0xa101
callq 0xa554
leaq 0x28f21(%rip), %rdi     # 0x302d5
movq %rax, %rsi
xorl %eax, %eax
callq 0xa101