// File: /workspace/github_c_makefile_star2_O1/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 0x1bcb3
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1b144
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1b144
leaq 0x5ed(%rip), %rdx       # 0x7c52
movq %r14, %rdi
movq %rbx, %rsi
callq 0xa837
leaq 0x2b374(%rip), %rdx     # 0x329eb
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x1b08b
leaq 0x28b63(%rip), %rdx     # 0x301ee
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1b08b
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a9d8
movq %rbx, %rdi
movl $0xfffffffb, %esi       # imm = 0xFFFFFFFB
callq 0x1a9d8
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1b49d
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a9d8
movq %rbx, %rdi
movl $0xfffffffc, %esi       # imm = 0xFFFFFFFC
callq 0x1a9d8
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1b49d
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x1a7aa
leaq 0x10(%rsp), %rdi
callq 0xab22
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1aca3
testq %rax, %rax
je 0x7776
movq %rax, %r14
movl $0x1, %r15d
leaq 0x10(%rsp), %r12
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
movl %r15d, %edx
callq 0x1b10f
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x1ac32
movq %rax, %r13
movq %rax, %rdi
callq 0x6240
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0xaa0c
movq %r12, %rdi
callq 0xac4b
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1a7aa
incq %r15
cmpq %r14, %r15
jbe 0x7721
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1aca3
testq %rax, %rax
je 0x77eb
movq %rax, %r14
movl $0x1, %r15d
leaq 0x10(%rsp), %r12
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl %r15d, %edx
callq 0x1b10f
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x1ac32
movq %rax, %r13
movq %rax, %rdi
callq 0x6240
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0xaa0c
movq %r12, %rdi
callq 0xac4b
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1a7aa
incq %r15
cmpq %r14, %r15
jbe 0x7796
leaq 0x10(%rsp), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0xab39
movl $0x10, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1adfd
movl $0x3, %eax
addq $0xb0, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq