// File: /workspace/github_c_makefile_star2_O3/deplinenoise[P]tundra/src/LuaInterface.cpp
// Function: t2::LuaListDirectory(lua_State*)
static int LuaListDirectory(lua_State* L)
{
  const char* dir = luaL_checkstring(L, 1);

  // Push dir result table on stack
  lua_newtable(L);
  // Push file result table on stack
  lua_newtable(L);

  ListDirectory(dir, L, OnFileIter);

  // Sort both tables
  lua_getglobal(L, "table");
  lua_getfield(L, -1, "sort");

  // Sort dirs
  // stack here: dirs, files, table package, table.sort
  lua_pushvalue(L, -1); // table.sort
  lua_pushvalue(L, -5);
  lua_call(L, 1, 0);

  // Sort files
  // stack here: dirs, files, table package, table.sort
  lua_pushvalue(L, -1); // table.sort
  lua_pushvalue(L, -4);
  lua_call(L, 1, 0);

  lua_pop(L, 2);

  // stack here: dirs, files

  // Compute directory listing hash digest for signature purposes
  HashState h;
  HashInit(&h);

  // Hash directories
  for (size_t i = 1, count = lua_objlen(L, -2); i <= count; ++i)
  {
    lua_rawgeti(L, -2, (int) i);
    HashAddString(&h, lua_tostring(L, -1));
    HashAddSeparator(&h);
    lua_pop(L, 1);
  }

  // Hash files
  for (size_t i = 1, count = lua_objlen(L, -1); i <= count; ++i)
  {
    lua_rawgeti(L, -1, (int) i);
    HashAddString(&h, lua_tostring(L, -1));
    HashAddSeparator(&h);
    lua_pop(L, 1);
  }

  HashDigest digest;
  HashFinalize(&h, &digest);

  lua_pushlstring(L, (char*)digest.m_Data, sizeof digest.m_Data);

  return 3;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb0, %rsp
movq %rdi, %rbx
movl $0x1, %esi
xorl %edx, %edx
callq 0x1b664
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1aaed
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1aaed
leaq 0x5ed(%rip), %rdx       # 0x7c3b
movq %r14, %rdi
movq %rbx, %rsi
callq 0xa747
leaq 0x2b3fb(%rip), %rdx     # 0x32a5b
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x1aa34
leaq 0x28b7a(%rip), %rdx     # 0x301ee
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1aa34
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a37a
movq %rbx, %rdi
movl $0xfffffffb, %esi       # imm = 0xFFFFFFFB
callq 0x1a37a
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1ae4a
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a37a
movq %rbx, %rdi
movl $0xfffffffc, %esi       # imm = 0xFFFFFFFC
callq 0x1a37a
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1ae4a
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x1a039
leaq 0x10(%rsp), %rdi
callq 0xaa36
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1a645
testq %rax, %rax
je 0x775f
movq %rax, %r14
movl $0x1, %r15d
leaq 0x10(%rsp), %r12
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
movl %r15d, %edx
callq 0x1aab8
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x1a5d4
movq %rax, %r13
movq %rax, %rdi
callq 0x6240
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0xa920
movq %r12, %rdi
callq 0xabb7
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1a039
incq %r15
cmpq %r14, %r15
jbe 0x770a
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a645
testq %rax, %rax
je 0x77d4
movq %rax, %r14
movl $0x1, %r15d
leaq 0x10(%rsp), %r12
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl %r15d, %edx
callq 0x1aab8
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x1a5d4
movq %rax, %r13
movq %rax, %rdi
callq 0x6240
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0xa920
movq %r12, %rdi
callq 0xabb7
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1a039
incq %r15
cmpq %r14, %r15
jbe 0x777f
leaq 0x10(%rsp), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0xaa4d
movl $0x10, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1a79f
movl $0x3, %eax
addq $0xb0, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq