// File: solaldunckel[P]webserv/srcs/HTTP/Response.cpp
// Function: Response::accept_charset(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>&)
std::string Response::accept_charset(std::vector<std::string> &matches){
  std::string path = file_.getPath();
  std::string all = config_.getHeader("Accept-Charset");
  int q = 10;
  int max = 0;
  std::string ret;
  std::vector<std::string> new_matches;
  std::vector<std::string> select_matches;

  ret = "";
  while (1) {
    std::string str = all.substr(0, all.find_first_of(" ,;\0"));

    if (str.find("*") == std::string::npos){
      for(std::vector<std::string>::iterator it = matches.begin() ; it != matches.end() ; it++)
        if (it->find("." + str) != std::string::npos)
          new_matches.push_back(*it);
    }
    else
      new_matches = matches;;
    if (!new_matches.empty() && (q > max)) {
        select_matches = new_matches;
      if(str[0] != '*')
         ret = str;
      max = q;
    }
    if (all.find(".") != std::string::npos)
      q = ft::stoi(all.substr(all.find_first_of(".") + 1, 1));
    if (all.find(",") == std::string::npos){
        if (!select_matches.empty())
          matches = select_matches;
      break ;
    }
    all = all.substr(all.find_first_of(" ,;"));
    all = all.substr(all.find_first_of("abcdefghijklmnoprstuvwxyz*"));
  }
  return (ret);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x1b0(%rbp)
movq %rdi, %rax
movq %rax, -0x1a8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
addq $0x8, %rdi
callq 0x38460
movq %rax, %rsi
leaq -0x38(%rbp), %rdi
callq 0x5200
movq -0x1a0(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x198(%rbp)
leaq -0x79(%rbp), %rdi
movq %rdi, -0x190(%rbp)
callq 0x5960
movq -0x190(%rbp), %rdx
leaq 0x1ab14(%rip), %rsi     # 0x4387b
leaq -0x78(%rbp), %rdi
callq 0x6be0
jmp 0x28d72
movq -0x198(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x13050
movq %rax, -0x1b8(%rbp)
jmp 0x28d8b
movq -0x1b8(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x5200
jmp 0x28d9d
leaq -0x78(%rbp), %rdi
callq 0x5aa8
leaq -0x79(%rbp), %rdi
callq 0x55e0
movq -0x1b0(%rbp), %rdi
movl $0xa, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movb $0x0, -0x95(%rbp)
callq 0x57b0
leaq -0xb0(%rbp), %rdi
callq 0x8b40
leaq -0xc8(%rbp), %rdi
callq 0x8b40
movq -0x1b0(%rbp), %rdi
leaq 0x1ac58(%rip), %rsi     # 0x43a54
callq 0x5780
jmp 0x28e03
jmp 0x28e05
jmp 0x28e07
leaq 0x1ab97(%rip), %rsi     # 0x439a5
xorl %eax, %eax
movl %eax, %edx
movq %rdx, -0x1c0(%rbp)
leaq -0x58(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
callq 0x5270
movq -0x1c8(%rbp), %rsi
movq -0x1c0(%rbp), %rdx
movq %rax, %rcx
leaq -0xe8(%rbp), %rdi
callq 0x55a0
jmp 0x28e48
leaq -0xe8(%rbp), %rdi
leaq 0x1ab2c(%rip), %rsi     # 0x43982
xorl %eax, %eax
movl %eax, %edx
callq 0x5a70
cmpq $-0x1, %rax
jne 0x28fb7
movq -0x18(%rbp), %rdi
callq 0x9210
movq %rax, -0xf0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x9280
movq %rax, -0xf8(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x9240
testb $0x1, %al
jne 0x28ea5
jmp 0x28fb5
leaq -0xf0(%rbp), %rdi
callq 0x18da0
movq %rax, -0x1d0(%rbp)
leaq 0x1aae4(%rip), %rsi     # 0x439a3
leaq -0x118(%rbp), %rdi
leaq -0xe8(%rbp), %rdx
callq 0x6ad0
jmp 0x28ed4
movq -0x1d0(%rbp), %rdi
leaq -0x118(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x5a90
movq %rax, -0x1d8(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x5aa8
movq -0x1d8(%rbp), %rax
cmpq $-0x1, %rax
je 0x28f99
leaq -0xf0(%rbp), %rdi
callq 0x92b0
movq %rax, %rsi
leaq -0xb0(%rbp), %rdi
callq 0x9130
jmp 0x28f2c
jmp 0x28f99
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x28f5d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x5aa8
leaq -0x79(%rbp), %rdi
callq 0x55e0
jmp 0x292c7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x2926a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x29228
jmp 0x28f9b
leaq -0xf0(%rbp), %rdi
xorl %esi, %esi
callq 0x13ae0
movq %rax, -0x120(%rbp)
jmp 0x28e79
jmp 0x28fcb
movq -0x18(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x18870
jmp 0x28fc9
jmp 0x28fcb
leaq -0xb0(%rbp), %rdi
callq 0x13c40
testb $0x1, %al
jne 0x29049
movl -0x90(%rbp), %eax
cmpl -0x94(%rbp), %eax
jle 0x29049
leaq -0xc8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x18870
jmp 0x28ffe
xorl %eax, %eax
movl %eax, %esi
leaq -0xe8(%rbp), %rdi
callq 0x5a80
movq %rax, -0x1e0(%rbp)
jmp 0x29017
movq -0x1e0(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2a, %eax
je 0x2903d
movq -0x1b0(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x5050
jmp 0x2903b
jmp 0x2903d
movl -0x90(%rbp), %eax
movl %eax, -0x94(%rbp)
leaq -0x58(%rbp), %rdi
leaq 0x1a94f(%rip), %rsi     # 0x439a3
xorl %eax, %eax
movl %eax, %edx
callq 0x5a70
cmpq $-0x1, %rax
je 0x290fc
leaq 0x1a935(%rip), %rsi     # 0x439a3
xorl %eax, %eax
movl %eax, %edx
leaq -0x58(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x5270
movq -0x1e8(%rbp), %rsi
movq %rax, %rdx
incq %rdx
leaq -0x140(%rbp), %rdi
movl $0x1, %ecx
callq 0x55a0
jmp 0x290a2
xorl %eax, %eax
movl %eax, %esi
leaq -0x140(%rbp), %rdi
movl $0xa, %edx
callq 0x41890
movl %eax, -0x1ec(%rbp)
jmp 0x290bf
movl -0x1ec(%rbp), %eax
movl %eax, -0x90(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x5aa8
jmp 0x290fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x5aa8
jmp 0x29228
leaq -0x58(%rbp), %rdi
leaq 0x1a85c(%rip), %rsi     # 0x43963
xorl %eax, %eax
movl %eax, %edx
callq 0x5a70
cmpq $-0x1, %rax
jne 0x29149
leaq -0xc8(%rbp), %rdi
callq 0x13c40
testb $0x1, %al
jne 0x2913a
movq -0x18(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x18870
jmp 0x29138
jmp 0x2913a
movl $0x3, -0x144(%rbp)
jmp 0x29209
leaq 0x1a815(%rip), %rsi     # 0x43965
xorl %eax, %eax
movl %eax, %edx
leaq -0x58(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x5270
movq -0x1f8(%rbp), %rsi
movq %rax, %rdx
leaq -0x168(%rbp), %rdi
movq $-0x1, %rcx
callq 0x55a0
jmp 0x29183
leaq -0x58(%rbp), %rdi
movq %rdi, -0x200(%rbp)
leaq -0x168(%rbp), %rsi
movq %rsi, -0x208(%rbp)
callq 0x56d0
movq -0x208(%rbp), %rdi
callq 0x5aa8
movq -0x200(%rbp), %rdi
leaq 0x1a7ae(%rip), %rsi     # 0x43969
xorl %eax, %eax
movl %eax, %edx
callq 0x5270
movq -0x200(%rbp), %rsi
movq %rax, %rdx
leaq -0x188(%rbp), %rdi
movq $-0x1, %rcx
callq 0x55a0
jmp 0x291e3
leaq -0x58(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x56d0
leaq -0x188(%rbp), %rdi
callq 0x5aa8
movl $0x0, -0x144(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x5aa8
movl -0x144(%rbp), %eax
testl %eax, %eax
je 0x29223
jmp 0x29221
jmp 0x29236
jmp 0x28e05
leaq -0xe8(%rbp), %rdi
callq 0x5aa8
jmp 0x2926a
movb $0x1, -0x95(%rbp)
movl $0x1, -0x144(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x8ec0
leaq -0xb0(%rbp), %rdi
callq 0x8ec0
testb $0x1, -0x95(%rbp)
jne 0x292a5
jmp 0x29299
leaq -0xc8(%rbp), %rdi
callq 0x8ec0
leaq -0xb0(%rbp), %rdi
callq 0x8ec0
movq -0x1b0(%rbp), %rdi
callq 0x5aa8
leaq -0x58(%rbp), %rdi
callq 0x5aa8
jmp 0x292c7
movq -0x1b0(%rbp), %rdi
callq 0x5aa8
leaq -0x58(%rbp), %rdi
callq 0x5aa8
leaq -0x38(%rbp), %rdi
callq 0x5aa8
movq -0x1a8(%rbp), %rax
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x5aa8
movq -0x88(%rbp), %rdi
callq 0x5950
nopl (%rax)