// File: tylerkaraszewski[P]Argyle/FilenameResolver.cpp
// Function: FilenameResolver::findNewestInDir(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, Config const&)
string FilenameResolver::findNewestInDir(const std::string& webPath, const Config& config)
{
  string basePath = config.getString(Config::S_KEY_BASE_PATH);
  string fullPath = basePath + webPath;
  char * name = 0;
  DIR * cwd = opendir(fullPath.c_str());
  struct dirent * entry;
  time_t timestamp = 0;

  struct tm time;
  time.tm_sec = 0;
  time.tm_min = 0;
  time.tm_hour = 12;
  time.tm_wday = 0;
  time.tm_yday = 0;
  time.tm_isdst = 0;

  while((entry = readdir(cwd)))
  {
    // Skip files that are too short to count.
    if(strlen(entry->d_name) < 10)
    {
      continue;
    }

    int day = atoi(entry->d_name + 8);
    int mon = atoi(entry->d_name + 5);
    int year = atoi(entry->d_name);

    // Sanity check.
    if ((year < 1900) || (year > 2500) || (mon < 1) || (mon > 12))
    {
      continue;
    }

    // Because of how these are stored in a struct tm.
    mon--;
    year -= 1900;

    // Parse our date.
    time.tm_mday = day;
    time.tm_mon = mon;
    time.tm_year = year;

    time_t filestamp = mktime(&time);
    if(filestamp > timestamp)
    {
        timestamp = filestamp;
        name = entry->d_name;
    }
  }

  closedir(cwd);
  if (name != 0)
  {
    return fullPath + "/" + name;
  }
  return "";
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x20(%rsp)
movq %rdi, %rax
movq %rax, 0x28(%rsp)
movq %rdi, 0x130(%rsp)
movq %rsi, 0x128(%rsp)
movq %rdx, 0x120(%rsp)
movq 0x120(%rsp), %rsi
leaq 0xb0fd(%rip), %rdx      # 0x194f8
leaq 0x100(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x11c60
movq 0x30(%rsp), %rsi
movq 0x128(%rsp), %rdx
leaq 0xe0(%rsp), %rdi
callq 0xeb30
jmp 0xe429
movq $0x0, 0xc8(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x4190
movq %rax, %rdi
callq 0x45b0
movq %rax, 0x18(%rsp)
jmp 0xe451
movq 0x18(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq $0x0, 0xb0(%rsp)
movl $0x0, 0x78(%rsp)
movl $0x0, 0x7c(%rsp)
movl $0xc, 0x80(%rsp)
movl $0x0, 0x90(%rsp)
movl $0x0, 0x94(%rsp)
movl $0x0, 0x98(%rsp)
movq 0xc0(%rsp), %rdi
callq 0x45d0
movq %rax, 0x10(%rsp)
jmp 0xe4ba
movq 0x10(%rsp), %rax
movq %rax, 0xb8(%rsp)
cmpq $0x0, %rax
je 0xe607
movq 0xb8(%rsp), %rdi
addq $0x13, %rdi
callq 0x41c0
cmpq $0xa, %rax
jae 0xe51c
jmp 0xe4a6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
jmp 0xe71b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
jmp 0xe70e
movq 0xb8(%rsp), %rdi
addq $0x13, %rdi
addq $0x8, %rdi
callq 0x45f0
movl %eax, 0x74(%rsp)
movq 0xb8(%rsp), %rdi
addq $0x13, %rdi
addq $0x5, %rdi
callq 0x45f0
movl %eax, 0x70(%rsp)
movq 0xb8(%rsp), %rdi
addq $0x13, %rdi
callq 0x45f0
movl %eax, 0x6c(%rsp)
cmpl $0x76c, 0x6c(%rsp)      # imm = 0x76C
jl 0xe585
cmpl $0x9c4, 0x6c(%rsp)      # imm = 0x9C4
jg 0xe585
cmpl $0x1, 0x70(%rsp)
jl 0xe585
cmpl $0xc, 0x70(%rsp)
jle 0xe58a
jmp 0xe4a6
movl 0x70(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x70(%rsp)
movl 0x6c(%rsp), %eax
subl $0x76c, %eax            # imm = 0x76C
movl %eax, 0x6c(%rsp)
movl 0x74(%rsp), %eax
movl %eax, 0x84(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0x88(%rsp)
movl 0x6c(%rsp), %eax
movl %eax, 0x8c(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x4260
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rax
cmpq 0xb0(%rsp), %rax
jle 0xe602
movq 0x60(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0xb8(%rsp), %rax
addq $0x13, %rax
movq %rax, 0xc8(%rsp)
jmp 0xe4a6
movq 0xc0(%rsp), %rdi
callq 0x4630
jmp 0xe616
cmpq $0x0, 0xc8(%rsp)
je 0xe68c
leaq 0x4c98(%rip), %rdx      # 0x132c0
leaq 0x40(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0xebc0
jmp 0xe63c
movq 0x20(%rsp), %rdi
movq 0xc8(%rsp), %rdx
leaq 0x40(%rsp), %rsi
callq 0x61e0
jmp 0xe655
leaq 0x40(%rsp), %rdi
callq 0x4788
movl $0x1, 0x3c(%rsp)
jmp 0xe6e7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x4788
jmp 0xe70e
leaq 0x3b(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x46c0
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rdx
leaq 0x4a94(%rip), %rsi      # 0x13140
callq 0x6450
jmp 0xe6b3
leaq 0x3b(%rsp), %rdi
callq 0x4400
movl $0x1, 0x3c(%rsp)
jmp 0xe6e7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
leaq 0x3b(%rsp), %rdi
callq 0x4400
jmp 0xe70e
leaq 0xe0(%rsp), %rdi
callq 0x4788
leaq 0x100(%rsp), %rdi
callq 0x4788
movq 0x28(%rsp), %rax
addq $0x138, %rsp            # imm = 0x138
retq
leaq 0xe0(%rsp), %rdi
callq 0x4788
leaq 0x100(%rsp), %rdi
callq 0x4788
movq 0xd8(%rsp), %rdi
callq 0x46b0
nopw %cs:(%rax,%rax)