// File: /workspace/github_cpp_makefile_star2_O2/solaldunckel[P]webserv/srcs/Utils/File.cpp
// Function: File::autoIndex(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
std::string File::autoIndex(std::string &target) {
  std::string body;
  struct tm	*tm;
  char buf[32];
  DIR *dir;
  struct stat statbuf;
  struct dirent *ent;

  dir = opendir(path_.c_str());
  body += "<html>\r\n";
  body += "<head><title>Index of " + target + "</title></head>\r\n";
  body += "<body>\r\n";
  body += "<h1>Index of " + target + "</h1><hr><pre>";
  std::vector<auto_listing> listing;

  readdir(dir);
  while ((ent = readdir(dir))) {
    auto_listing li;

    li.name_ = ent->d_name;
    if (li.name_.length() > 50) {
      li.name_.erase(47);
      li.name_ += "..>";
    }
    std::string path(path_ + "/" + ent->d_name);
    stat(path.c_str(), &statbuf);

    if (S_ISDIR(statbuf.st_mode)) {
      li.is_dir_ = true;
      li.name_ += "/";
    }

    tm = gmtime(&statbuf.st_mtime);
    int ret = strftime(buf, 32, "%d-%b-%Y %H:%M", tm);
    li.date_ = std::string(buf, ret);
    li.size_ = statbuf.st_size;
    listing.push_back(li);
  }

  std::sort(listing.begin(), listing.end(), sort_auto_listing);

  for (std::vector<auto_listing>::iterator it = listing.begin(); it != listing.end(); it++) {
    body = body + "<a href=\"" + ft::unique_char(target + + "/" + it->name_) + "\">" + it->name_ + "</a>";
    if (it != listing.begin()) {
      body += set_width(68 - it->name_.length(), it->date_);
      if (it->is_dir_)
        body += set_width(20, "-");
      else
        body += set_width(20, ft::to_string(it->size_));
    }
    body += "\r\n";
  }

  body += "</pre><hr></body>\r\n";
  body += "</html>\r\n";
  closedir(dir);
  return body;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x288, %rsp            # imm = 0x288
movq %rdi, %rbx
leaq 0x10(%rdi), %rax
movq %rax, (%rdi)
andq $0x0, 0x8(%rdi)
movq %rdx, %r14
movb $0x0, 0x10(%rdi)
movq %rsi, 0x50(%rsp)
movq 0x80(%rsi), %rdi
callq 0x4660
movq %rax, 0x8(%rsp)
leaq 0x568a(%rip), %rsi      # 0x1c758
movq %rbx, %rdi
callq 0x4820
leaq 0x5d71(%rip), %rsi      # 0x1ce4e
leaq 0x30(%rsp), %rdi
movq %r14, %rdx
callq 0x52b1
leaq 0x567e(%rip), %rdx      # 0x1c76f
leaq 0x58(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movq %rbx, (%rsp)
callq 0x5292
leaq 0x58(%rsp), %rsi
movq %rbx, %rdi
callq 0x4840
leaq 0x58(%rsp), %rdi
callq 0x4898
leaq 0x30(%rsp), %rdi
callq 0x4898
leaq 0x5655(%rip), %rsi      # 0x1c781
movq %rbx, %rdi
callq 0x4820
leaq 0x5d2a(%rip), %rsi      # 0x1ce65
leaq 0x30(%rsp), %rdi
movq %r14, %rdx
callq 0x52b1
leaq 0x5d24(%rip), %rdx      # 0x1ce73
leaq 0x58(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x5292
leaq 0x58(%rsp), %rsi
movq %rbx, %rdi
callq 0x4840
movq %r14, 0xa8(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x4898
leaq 0x30(%rsp), %rdi
callq 0x4898
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movq 0x8(%rsp), %rdi
callq 0x4670
subq $-0x80, 0x50(%rsp)
xorl %ebp, %ebp
leaq 0x58(%rsp), %r15
leaq 0xb0(%rsp), %r13
leaq 0x30(%rsp), %rbx
leaq 0x190(%rsp), %r14
movq 0x8(%rsp), %rdi
callq 0x4670
movq %rax, %r12
testq %rax, %rax
je 0x1733f
leaq 0x68(%rsp), %rax
movq %rax, 0x58(%rsp)
andq $0x0, 0x60(%rsp)
movb %bpl, 0x68(%rsp)
movb %bpl, 0x78(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, 0x80(%rsp)
andq $0x0, 0x88(%rsp)
movb %bpl, 0x90(%rsp)
addq $0x13, %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x4830
cmpq $0x33, 0x60(%rsp)
jb 0x17246
movq %r15, %rdi
pushq $0x2f
popq %rsi
pushq $-0x1
popq %rdx
callq 0x40a0
movq %r15, %rdi
leaq 0x5c41(%rip), %rsi      # 0x1ce82
callq 0x4820
movq %r13, %rdi
movq 0x50(%rsp), %rsi
leaq 0x5329(%rip), %rdx      # 0x1c57e
callq 0x60d3
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x5292
movq %r13, %rdi
callq 0x4898
movq 0x30(%rsp), %rdi
leaq 0x1f8(%rsp), %rsi
callq 0x43f0
movl 0x210(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x172ab
movb $0x1, 0x78(%rsp)
movq %r15, %rdi
leaq 0x52d8(%rip), %rsi      # 0x1c57e
callq 0x4820
leaq 0x250(%rsp), %rdi
callq 0x43a0
movq %r14, %rdi
pushq $0x20
popq %rsi
leaq 0x5bc1(%rip), %rdx      # 0x1ce86
movq %rax, %rcx
callq 0x4380
cltq
leaq 0xc0(%rsp), %rcx
movq %rcx, 0xb0(%rsp)
leaq (%rsp,%rax), %rdx
addq $0x190, %rdx            # imm = 0x190
movq %r13, %rdi
movq %r14, %rsi
callq 0x53a8
leaq 0x80(%rsp), %rdi
movq %r13, %rsi
callq 0x4560
movq %r13, %rdi
callq 0x4898
movq 0x228(%rsp), %rax
movq %rax, 0xa0(%rsp)
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x17e98
movq %rbx, %rdi
callq 0x4898
movq %r15, %rdi
callq 0x17ec4
jmp 0x171c1
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
leaq -0x2e7(%rip), %rdx      # 0x17069
movq (%rsp), %rbx
callq 0x181d3
movq 0x10(%rsp), %r12
addq $0x28, %r12
leaq 0x30(%rsp), %r13
leaq 0x58(%rsp), %r15
leaq -0x28(%r12), %r14
cmpq 0x18(%rsp), %r14
je 0x17591
leaq 0xd0(%rsp), %rdi
movq (%rsp), %rsi
leaq 0x5b06(%rip), %rdx      # 0x1ce95
callq 0x60d3
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
movq 0xa8(%rsp), %rsi
leaq 0x51d0(%rip), %rdx      # 0x1c57e
callq 0x60d3
leaq 0x130(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x5273
leaq 0x150(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1b4d0
leaq 0x170(%rsp), %rbp
movq %rbp, %rdi
leaq 0xd0(%rsp), %rsi
movq %rbx, %rdx
callq 0x61d5
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x5a93(%rip), %rdx      # 0x1ce9f
callq 0x5292
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x5273
movq %r15, %rdi
movq %r13, %rsi
leaq 0x5a76(%rip), %rdx      # 0x1cea2
callq 0x5292
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x4560
movq %r15, %rdi
callq 0x4898
movq %r13, %rdi
callq 0x4898
leaq 0xb0(%rsp), %rdi
callq 0x4898
leaq 0x170(%rsp), %rdi
callq 0x4898
leaq 0x150(%rsp), %rdi
callq 0x4898
leaq 0x130(%rsp), %rdi
callq 0x4898
leaq 0x110(%rsp), %rdi
callq 0x4898
leaq 0xd0(%rsp), %rdi
callq 0x4898
cmpq 0x10(%rsp), %r14
je 0x17578
movq -0x20(%r12), %r14
leaq 0x1d8(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x45e0
pushq $0x44
popq %rsi
subq %r14, %rsi
movq %r15, %rdi
movq %rbx, %rdx
callq 0x16ff1
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x4840
leaq 0x1b8(%rsp), %r14
movq %r15, %rdi
callq 0x4898
movq %rbx, %rdi
callq 0x4898
cmpb $0x1, -0x8(%r12)
jne 0x17534
movq %r14, %rdi
leaq 0x59a2(%rip), %rsi      # 0x1cea7
movq %r13, %rdx
callq 0x5328
movq %r15, %rdi
pushq $0x14
popq %rsi
movq %r14, %rdx
callq 0x16ff1
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x4840
movq %r15, %rdi
callq 0x4898
movq %r14, %rdi
jmp 0x17573
movq 0x20(%r12), %rsi
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
callq 0xddbc
movq %r15, %rdi
pushq $0x14
popq %rsi
movq %rbx, %rdx
callq 0x16ff1
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x4840
movq %r15, %rdi
callq 0x4898
leaq 0xf0(%rsp), %rdi
callq 0x4898
movq (%rsp), %rdi
leaq 0x51fb(%rip), %rsi      # 0x1c77e
callq 0x4820
addq $0x50, %r12
jmp 0x1736c
leaq 0x5911(%rip), %rsi      # 0x1cea9
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x4820
leaq 0x5211(%rip), %rsi      # 0x1c7bc
movq %rbx, %rdi
callq 0x4820
movq 0x8(%rsp), %rdi
callq 0x46c0
leaq 0x10(%rsp), %rdi
callq 0x17eda
movq %rbx, %rax
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x175e2
jmp 0x175f1
jmp 0x17609
movq %rax, %r14
leaq 0x58(%rsp), %rdi
callq 0x4898
jmp 0x175f4
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x4898
movq (%rsp), %rbx
jmp 0x17758
jmp 0x17609
movq %rax, %r14
jmp 0x17758
movq %rax, %r14
jmp 0x1774e
movq %rax, %r14
jmp 0x17651
jmp 0x17734
movq %rax, %r14
leaq 0x58(%rsp), %rdi
callq 0x4898
jmp 0x17635
movq %rax, %r14
leaq 0x1b8(%rsp), %rdi
jmp 0x1767f
jmp 0x17734
movq %rax, %r14
leaq 0x58(%rsp), %rdi
callq 0x4898
leaq 0xf0(%rsp), %rdi
jmp 0x1767f
jmp 0x17723
jmp 0x17734
movq %rax, %r14
jmp 0x17677
movq %rax, %r14
leaq 0x58(%rsp), %rdi
callq 0x4898
leaq 0x1d8(%rsp), %rdi
callq 0x4898
jmp 0x17737
movq %rax, %r14
jmp 0x176ad
movq %rax, %r14
jmp 0x176a0
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x4898
leaq 0xb0(%rsp), %rdi
callq 0x4898
leaq 0x170(%rsp), %rdi
callq 0x4898
jmp 0x176da
jmp 0x17734
jmp 0x17734
movq %rax, %r14
movq (%rsp), %rbx
jmp 0x17705
movq %rax, %r14
movq (%rsp), %rbx
jmp 0x176f8
movq %rax, %r14
jmp 0x176e7
movq %rax, %r14
leaq 0x150(%rsp), %rdi
callq 0x4898
movq (%rsp), %rbx
leaq 0x130(%rsp), %rdi
callq 0x4898
leaq 0x110(%rsp), %rdi
callq 0x4898
leaq 0xd0(%rsp), %rdi
callq 0x4898
jmp 0x1774e
movq %rax, %r14
leaq 0xb0(%rsp), %rdi
jmp 0x1772b
jmp 0x17723
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x4898
jmp 0x17740
jmp 0x1773d
movq %rax, %r14
movq (%rsp), %rbx
jmp 0x1774e
movq %rax, %r14
movq (%rsp), %rbx
leaq 0x58(%rsp), %rdi
callq 0x17ec4
leaq 0x10(%rsp), %rdi
callq 0x17eda
movq %rbx, %rdi
callq 0x4898
movq %r14, %rdi
callq 0x4750