// File: /workspace/github_cpp_makefile_star2_O3/mbourand[P]webserv/srcs/Utils/Utils.cpp
// Function: ft::simplify_path(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool, int)
std::string ft::simplify_path(const std::string& input, bool safe, int base_depth)
{
	if (input.size() == 0)
		return "";

	std::list<std::string> splitted = ft::split(input, "/");
	std::string ret;

	if (safe)
	{
		int depth = 0;
		for (std::list<std::string>::iterator it = splitted.begin(); it != splitted.end(); it++)
		{
			if (base_depth-- > 0)
				continue;
			if (*it == ".")
				continue;

			if (*it == "..")
				depth--;
			else
				depth++;
			if (depth == -1)
				throw std::invalid_argument("Path go out of current directory");
		}
	}

	if (input[0] == '/')
		ret += "/";

	for (std::list<std::string>::iterator it = splitted.begin(); it != splitted.end(); it++)
	{
		if (*it == ".")
			continue;
		else if (*it == ".." && ret.size() > 0)
		{
			it--;
			if (*it == "..")
				ret += "../";
			else
				ret = ret.substr(0, ret.size() - it->size() - 1);
			it++;
		}
		else
		{
			if (it == --splitted.end())
				ret += *it;
			else
				ret += *it + "/";
		}
	}
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
cmpq $0x0, 0x8(%rsi)
je 0x1d4c4
movl %ecx, %ebp
movl %edx, %r12d
movq %rsi, %r15
leaq 0x10(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x1d7f5(%rip), %rsi     # 0x3aa94
leaq 0x1d7ef(%rip), %rdx     # 0x3aa95
movq %rsp, %rdi
callq 0x80f4
leaq 0x30(%rsp), %r14
movq %rsp, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1bf30
movq %r15, 0x28(%rsp)
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1d2e1
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x10(%rbx), %rax
movq %rax, 0x20(%rsp)
movq %rax, (%rbx)
movq $0x0, 0x8(%rbx)
movb $0x0, 0x10(%rbx)
movq 0x30(%rsp), %r15
cmpq %r14, %r15
setne %al
andb %al, %r12b
cmpb $0x1, %r12b
jne 0x1d35e
xorl %r12d, %r12d
testl %ebp, %ebp
jg 0x1d354
leaq 0x10(%r15), %r13
movq %r13, %rdi
leaq 0x2030e(%rip), %rsi     # 0x3d630
callq 0x7180
testl %eax, %eax
je 0x1d354
movq %r13, %rdi
leaq 0x2004b(%rip), %rsi     # 0x3d380
callq 0x7180
cmpl $0x1, %eax
movl $0x0, %eax
sbbl %eax, %eax
orl $0x1, %eax
addl %eax, %r12d
cmpl $-0x1, %r12d
je 0x1d4ef
movq (%r15), %r15
decl %ebp
cmpq %r14, %r15
jne 0x1d310
movq 0x28(%rsp), %rax
movq (%rax), %rax
cmpb $0x2f, (%rax)
jne 0x1d37a
leaq 0x1d722(%rip), %rsi     # 0x3aa94
movq %rbx, %rdi
callq 0x78d0
movq 0x30(%rsp), %r13
cmpq %r14, %r13
je 0x1d4b8
leaq 0x202a1(%rip), %r15     # 0x3d630
movq %rsp, %r12
leaq 0x10(%r13), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x7180
testl %eax, %eax
je 0x1d4ab
movq %rbp, %rdi
leaq 0x1ffcd(%rip), %rsi     # 0x3d380
callq 0x7180
testl %eax, %eax
jne 0x1d426
cmpq $0x0, 0x8(%rbx)
je 0x1d426
movq 0x8(%r13), %r13
leaq 0x10(%r13), %rdi
leaq 0x1ffae(%rip), %rsi     # 0x3d380
callq 0x7180
testl %eax, %eax
je 0x1d498
movq 0x18(%r13), %rcx
notq %rcx
addq 0x8(%rbx), %rcx
movq %r12, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x74d0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x7590
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1d4a7
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x1d4a7
cmpq 0x38(%rsp), %r13
je 0x1d486
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x8a92
movq %r12, %rdi
leaq 0x1d641(%rip), %rsi     # 0x3aa94
callq 0x78d0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x7190
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1d4ab
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x1d4ab
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdx
movq %rbx, %rdi
callq 0x7190
jmp 0x1d4ab
movq %rbx, %rdi
leaq 0x1ceda(%rip), %rsi     # 0x3a37c
callq 0x78d0
movq (%r13), %r13
movq (%r13), %r13
cmpq %r14, %r13
jne 0x1d392
leaq 0x30(%rsp), %rdi
callq 0x96b6
jmp 0x1d4dd
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
leaq 0x1d8c9(%rip), %rdx     # 0x3ad9b
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x80f4
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x7260
movq %rax, %r15
leaq 0x1e2e2(%rip), %rsi     # 0x3b7e5
movq %rax, %rdi
callq 0x78c0
movq 0x30ab6(%rip), %rsi     # 0x4dfc8
movq 0x30a97(%rip), %rdx     # 0x4dfb0
movq %r15, %rdi
callq 0x77d0
jmp 0x1d57d
movq %rax, %r14
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1d5a4
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x1d5a4
movq %rax, %r14
jmp 0x1d5a4
jmp 0x1d57d
movq %rax, %r14
movq %r15, %rdi
callq 0x7390
jmp 0x1d580
jmp 0x1d559
movq %rax, %r14
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1d580
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x1d580
jmp 0x1d57d
jmp 0x1d57d
movq %rax, %r14
movq (%rbx), %rdi
cmpq 0x20(%rsp), %rdi
je 0x1d59a
movq 0x20(%rsp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x74b0
leaq 0x30(%rsp), %rdi
callq 0x96b6
movq %r14, %rdi
callq 0x7810