// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdi, %rbx
pushq $0x1
popq %r14
movl %r14d, %esi
xorl %edx, %edx
callq 0x1958c
movq %rax, %r15
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x18a30
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x18a30
leaq 0x583(%rip), %rdx       # 0x7778
movq %r15, %rdi
movq %rbx, %rsi
callq 0xa012
leaq 0x27844(%rip), %rdx     # 0x2ea4b
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x18977
leaq 0x24fd3(%rip), %rdx     # 0x2c1ee
pushq $-0x1
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x18977
movq %rbx, %rdi
movl %ebp, %esi
callq 0x182f8
pushq $-0x5
popq %rsi
movq %rbx, %rdi
callq 0x182f8
movq %rbx, %rdi
movl %r14d, %esi
xorl %edx, %edx
callq 0x18d85
movq %rbx, %rdi
movl %ebp, %esi
callq 0x182f8
pushq $-0x4
popq %rsi
movq %rbx, %rdi
callq 0x182f8
movq %rbx, %rdi
movl %r14d, %esi
xorl %edx, %edx
callq 0x18d85
pushq $-0x3
popq %rsi
movq %rbx, %rdi
callq 0x180d8
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0xa2bc
pushq $-0x2
popq %r15
movq %rbx, %rdi
movl %r15d, %esi
callq 0x185ab
movq %rax, %r12
pushq $0x1
popq %r13
cmpq %r12, %r13
ja 0x72dc
movq %rbx, %rdi
movl %r15d, %esi
movl %r13d, %edx
callq 0x189fb
movq %rbx, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x1853d
movq %r14, %rdi
movq %rax, %rsi
callq 0x7862
movq %r14, %rdi
callq 0xa3c7
movq %rbx, %rdi
movl %r15d, %esi
callq 0x180d8
incq %r13
jmp 0x729a
pushq $-0x1
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x185ab
movq %rax, %r14
pushq $0x1
popq %r15
leaq 0x10(%rsp), %r12
pushq $-0x2
popq %r13
cmpq %r14, %r15
ja 0x733a
movq %rbx, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x189fb
movq %rbx, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x1853d
movq %r12, %rdi
movq %rax, %rsi
callq 0x7862
movq %r12, %rdi
callq 0xa3c7
movq %rbx, %rdi
movl %r13d, %esi
callq 0x180d8
incq %r15
jmp 0x72f9
leaq 0x10(%rsp), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0xa2d0
pushq $0x10
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x186ea
pushq $0x3
popq %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq