// File: 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
movq %rsp, %rbp
subq $0x4c0, %rsp            # imm = 0x4C0
movq %rdi, -0x470(%rbp)
movq %rdi, %rax
movq %rax, -0x468(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x460(%rbp)
movb $0x0, -0x19(%rbp)
callq 0x57b0
movq -0x460(%rbp), %rdi
subq $-0x80, %rdi
callq 0x51f0
movq %rax, %rdi
callq 0x5840
movq %rax, -0x458(%rbp)
jmp 0x36b6d
movq -0x470(%rbp), %rdi
movq -0x458(%rbp), %rax
movq %rax, -0x58(%rbp)
leaq 0xcc0f(%rip), %rsi      # 0x43795
callq 0x5520
jmp 0x36b8d
movq -0x18(%rbp), %rdx
leaq 0xd2e6(%rip), %rsi      # 0x43e7e
leaq -0x140(%rbp), %rdi
callq 0x6ad0
jmp 0x36ba6
leaq 0xcbff(%rip), %rdx      # 0x437ac
leaq -0x120(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x6a80
jmp 0x36bc2
movq -0x470(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x5710
jmp 0x36bd7
leaq -0x120(%rbp), %rdi
callq 0x5aa8
leaq -0x140(%rbp), %rdi
callq 0x5aa8
movq -0x470(%rbp), %rdi
leaq 0xcbc1(%rip), %rsi      # 0x437be
callq 0x5520
jmp 0x36c04
movq -0x18(%rbp), %rdx
leaq 0xd286(%rip), %rsi      # 0x43e95
leaq -0x180(%rbp), %rdi
callq 0x6ad0
jmp 0x36c1d
leaq 0xd27f(%rip), %rdx      # 0x43ea3
leaq -0x160(%rbp), %rdi
leaq -0x180(%rbp), %rsi
callq 0x6a80
jmp 0x36c39
movq -0x470(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x5710
jmp 0x36c4e
leaq -0x160(%rbp), %rdi
callq 0x5aa8
leaq -0x180(%rbp), %rdi
callq 0x5aa8
leaq -0x198(%rbp), %rdi
callq 0x38480
movq -0x58(%rbp), %rdi
callq 0x5860
jmp 0x36c7d
jmp 0x36c7f
movq -0x58(%rbp), %rdi
callq 0x5860
movq %rax, -0x478(%rbp)
jmp 0x36c91
movq -0x478(%rbp), %rax
movq %rax, -0xf0(%rbp)
cmpq $0x0, %rax
je 0x36fdd
leaq -0x1e8(%rbp), %rdi
callq 0x384a0
jmp 0x36cb7
movq -0xf0(%rbp), %rsi
addq $0x13, %rsi
leaq -0x1e8(%rbp), %rdi
callq 0x5780
jmp 0x36cd0
leaq -0x1e8(%rbp), %rdi
callq 0x5830
cmpq $0x32, %rax
jbe 0x36de5
leaq -0x1e8(%rbp), %rdi
movl $0x2f, %esi
movq $-0x1, %rdx
callq 0x50d0
jmp 0x36d00
leaq 0xd1ab(%rip), %rsi      # 0x43eb2
leaq -0x1e8(%rbp), %rdi
callq 0x5520
jmp 0x36d15
jmp 0x36de5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
jmp 0x37607
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
jmp 0x36d63
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x5aa8
leaq -0x140(%rbp), %rdi
callq 0x5aa8
jmp 0x37607
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
jmp 0x36da6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x5aa8
leaq -0x180(%rbp), %rdi
callq 0x5aa8
jmp 0x37607
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
jmp 0x375dd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
jmp 0x36fcc
movq -0x460(%rbp), %rsi
subq $-0x80, %rsi
leaq 0xc7ac(%rip), %rdx      # 0x435a3
leaq -0x228(%rbp), %rdi
callq 0x8f70
jmp 0x36e05
movq -0xf0(%rbp), %rdx
addq $0x13, %rdx
leaq -0x208(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0x6a80
jmp 0x36e25
leaq -0x228(%rbp), %rdi
callq 0x5aa8
leaq -0x208(%rbp), %rdi
callq 0x51f0
movq %rax, %rdi
leaq -0xe8(%rbp), %rsi
callq 0x54c0
movl -0xd0(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x36eb6
movb $0x1, -0x1c8(%rbp)
leaq 0xc737(%rip), %rsi      # 0x435a3
leaq -0x1e8(%rbp), %rdi
callq 0x5520
jmp 0x36e7a
jmp 0x36eb6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
leaq -0x228(%rbp), %rdi
callq 0x5aa8
jmp 0x36fcc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
jmp 0x36fc0
leaq -0x90(%rbp), %rdi
callq 0x5470
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rcx
leaq 0xcfe5(%rip), %rdx      # 0x43eb6
leaq -0x50(%rbp), %rdi
movq %rdi, -0x490(%rbp)
movl $0x20, %esi
callq 0x5430
movl %eax, -0x22c(%rbp)
movslq -0x22c(%rbp), %rax
movq %rax, -0x488(%rbp)
leaq -0x251(%rbp), %rdi
movq %rdi, -0x480(%rbp)
callq 0x5960
movq -0x490(%rbp), %rsi
movq -0x488(%rbp), %rdx
movq -0x480(%rbp), %rcx
leaq -0x250(%rbp), %rdi
callq 0x5630
jmp 0x36f30
leaq -0x1c0(%rbp), %rdi
leaq -0x250(%rbp), %rsi
movq %rsi, -0x498(%rbp)
callq 0x56d0
movq -0x498(%rbp), %rdi
callq 0x5aa8
leaq -0x251(%rbp), %rdi
callq 0x55e0
movq -0xb8(%rbp), %rax
movq %rax, -0x1a0(%rbp)
leaq -0x198(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x384d0
jmp 0x36f85
leaq -0x208(%rbp), %rdi
callq 0x5aa8
leaq -0x1e8(%rbp), %rdi
callq 0x38540
jmp 0x36c7f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
leaq -0x251(%rbp), %rdi
callq 0x55e0
leaq -0x208(%rbp), %rdi
callq 0x5aa8
leaq -0x1e8(%rbp), %rdi
callq 0x38540
jmp 0x375dd
leaq -0x198(%rbp), %rdi
movq %rdi, -0x4a0(%rbp)
callq 0x385c0
movq -0x4a0(%rbp), %rdi
movq %rax, -0x260(%rbp)
callq 0x385f0
movq %rax, -0x268(%rbp)
movq -0x260(%rbp), %rdi
movq -0x268(%rbp), %rsi
leaq -0x5af(%rip), %rdx      # 0x36a70
callq 0x38570
jmp 0x37026
leaq -0x198(%rbp), %rdi
callq 0x385c0
movq %rax, -0x270(%rbp)
leaq -0x198(%rbp), %rdi
callq 0x385f0
movq %rax, -0x278(%rbp)
leaq -0x270(%rbp), %rdi
leaq -0x278(%rbp), %rsi
callq 0x38620
testb $0x1, %al
jne 0x37068
jmp 0x37590
movq -0x470(%rbp), %rsi
leaq 0xce4f(%rip), %rdx      # 0x43ec5
leaq -0x318(%rbp), %rdi
callq 0x8f70
jmp 0x37084
movq -0x18(%rbp), %rsi
leaq 0xc514(%rip), %rdx      # 0x435a3
leaq -0x378(%rbp), %rdi
callq 0x8f70
jmp 0x3709d
leaq -0x270(%rbp), %rdi
callq 0x38660
movq %rax, %rdx
leaq -0x358(%rbp), %rdi
leaq -0x378(%rbp), %rsi
callq 0x6a30
jmp 0x370c1
leaq -0x338(%rbp), %rdi
leaq -0x358(%rbp), %rsi
callq 0x41780
jmp 0x370d6
leaq -0x2f8(%rbp), %rdi
leaq -0x318(%rbp), %rsi
leaq -0x338(%rbp), %rdx
callq 0x9550
jmp 0x370f2
leaq 0xcdd6(%rip), %rdx      # 0x43ecf
leaq -0x2d8(%rbp), %rdi
leaq -0x2f8(%rbp), %rsi
callq 0x6a80
jmp 0x3710e
leaq -0x270(%rbp), %rdi
callq 0x38660
movq %rax, %rdx
leaq -0x2b8(%rbp), %rdi
leaq -0x2d8(%rbp), %rsi
callq 0x6a30
jmp 0x37132
leaq 0xcd99(%rip), %rdx      # 0x43ed2
leaq -0x298(%rbp), %rdi
leaq -0x2b8(%rbp), %rsi
callq 0x6a80
jmp 0x3714e
movq -0x470(%rbp), %rdi
leaq -0x298(%rbp), %rsi
callq 0x56d0
leaq -0x298(%rbp), %rdi
callq 0x5aa8
leaq -0x2b8(%rbp), %rdi
callq 0x5aa8
leaq -0x2d8(%rbp), %rdi
callq 0x5aa8
leaq -0x2f8(%rbp), %rdi
callq 0x5aa8
leaq -0x338(%rbp), %rdi
callq 0x5aa8
leaq -0x358(%rbp), %rdi
callq 0x5aa8
leaq -0x378(%rbp), %rdi
callq 0x5aa8
leaq -0x318(%rbp), %rdi
callq 0x5aa8
leaq -0x198(%rbp), %rdi
callq 0x385c0
movq %rax, -0x380(%rbp)
leaq -0x270(%rbp), %rdi
leaq -0x380(%rbp), %rsi
callq 0x38620
testb $0x1, %al
jne 0x371f0
jmp 0x3755f
leaq -0x270(%rbp), %rdi
movq %rdi, -0x4b0(%rbp)
callq 0x38660
movq %rax, %rdi
callq 0x5830
movq -0x4b0(%rbp), %rdi
movq %rax, %rcx
movl $0x44, %eax
subq %rcx, %rax
movq %rax, -0x4a8(%rbp)
callq 0x38660
movq %rax, %rsi
addq $0x28, %rsi
leaq -0x3c0(%rbp), %rdi
callq 0x5200
jmp 0x3723e
movq -0x4a8(%rbp), %rsi
leaq -0x3a0(%rbp), %rdi
leaq -0x3c0(%rbp), %rdx
callq 0x36990
jmp 0x3725a
movq -0x470(%rbp), %rdi
leaq -0x3a0(%rbp), %rsi
callq 0x5710
jmp 0x3726f
leaq -0x3a0(%rbp), %rdi
callq 0x5aa8
leaq -0x3c0(%rbp), %rdi
callq 0x5aa8
leaq -0x270(%rbp), %rdi
callq 0x38660
testb $0x1, 0x20(%rax)
je 0x374b3
leaq -0x401(%rbp), %rdi
movq %rdi, -0x4b8(%rbp)
callq 0x5960
movq -0x4b8(%rbp), %rdx
leaq 0xcc19(%rip), %rsi      # 0x43ed7
leaq -0x400(%rbp), %rdi
callq 0x6be0
jmp 0x372cc
leaq -0x3e0(%rbp), %rdi
movl $0x14, %esi
leaq -0x400(%rbp), %rdx
callq 0x36990
jmp 0x372e6
movq -0x470(%rbp), %rdi
leaq -0x3e0(%rbp), %rsi
callq 0x5710
jmp 0x372fb
leaq -0x3e0(%rbp), %rdi
callq 0x5aa8
leaq -0x400(%rbp), %rdi
callq 0x5aa8
leaq -0x401(%rbp), %rdi
callq 0x55e0
jmp 0x3755d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
jmp 0x373fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
jmp 0x373f0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
jmp 0x373e4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
jmp 0x373d8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
jmp 0x373cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
jmp 0x373c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
leaq -0x2b8(%rbp), %rdi
callq 0x5aa8
leaq -0x2d8(%rbp), %rdi
callq 0x5aa8
leaq -0x2f8(%rbp), %rdi
callq 0x5aa8
leaq -0x338(%rbp), %rdi
callq 0x5aa8
leaq -0x358(%rbp), %rdi
callq 0x5aa8
leaq -0x378(%rbp), %rdi
callq 0x5aa8
leaq -0x318(%rbp), %rdi
callq 0x5aa8
jmp 0x375dd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
jmp 0x3743f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
leaq -0x3a0(%rbp), %rdi
callq 0x5aa8
leaq -0x3c0(%rbp), %rdi
callq 0x5aa8
jmp 0x375dd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
jmp 0x374a2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
jmp 0x37496
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
leaq -0x3e0(%rbp), %rdi
callq 0x5aa8
leaq -0x400(%rbp), %rdi
callq 0x5aa8
leaq -0x401(%rbp), %rdi
callq 0x55e0
jmp 0x375dd
leaq -0x270(%rbp), %rdi
callq 0x38660
movq 0x48(%rax), %rsi
leaq -0x448(%rbp), %rdi
callq 0x20f10
jmp 0x374d1
leaq -0x428(%rbp), %rdi
movl $0x14, %esi
leaq -0x448(%rbp), %rdx
callq 0x36990
jmp 0x374eb
movq -0x470(%rbp), %rdi
leaq -0x428(%rbp), %rsi
callq 0x5710
jmp 0x37500
leaq -0x428(%rbp), %rdi
callq 0x5aa8
leaq -0x448(%rbp), %rdi
callq 0x5aa8
jmp 0x3755d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
jmp 0x3754c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
leaq -0x428(%rbp), %rdi
callq 0x5aa8
leaq -0x448(%rbp), %rdi
callq 0x5aa8
jmp 0x375dd
jmp 0x3755f
movq -0x470(%rbp), %rdi
leaq 0xc24e(%rip), %rsi      # 0x437bb
callq 0x5520
jmp 0x37574
jmp 0x37576
leaq -0x270(%rbp), %rdi
xorl %esi, %esi
callq 0x38680
movq %rax, -0x450(%rbp)
jmp 0x37039
movq -0x470(%rbp), %rdi
leaq 0xc93b(%rip), %rsi      # 0x43ed9
callq 0x5520
jmp 0x375a5
movq -0x470(%rbp), %rdi
leaq 0xc246(%rip), %rsi      # 0x437f9
callq 0x5520
jmp 0x375ba
movq -0x58(%rbp), %rdi
callq 0x58a0
jmp 0x375c5
movb $0x1, -0x19(%rbp)
leaq -0x198(%rbp), %rdi
callq 0x386c0
testb $0x1, -0x19(%rbp)
jne 0x375f7
jmp 0x375eb
leaq -0x198(%rbp), %rdi
callq 0x386c0
jmp 0x37607
movq -0x470(%rbp), %rdi
callq 0x5aa8
movq -0x468(%rbp), %rax
addq $0x4c0, %rsp            # imm = 0x4C0
popq %rbp
retq
movq -0x470(%rbp), %rdi
callq 0x5aa8
movq -0xf8(%rbp), %rdi
callq 0x5950
nop