// File: /workspace/github_cpp_makefile_star2_O2/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 0x17d3d
movl %ecx, %ebp
movl %edx, %r12d
movq %rsi, %r15
leaq 0x14eec(%rip), %rsi     # 0x2ca48
leaq 0x28(%rsp), %rdi
leaq 0xb(%rsp), %rdx
callq 0x8032
leaq 0x10(%rsp), %rdi
leaq 0x28(%rsp), %rdx
movq %r15, %rsi
callq 0x16f10
leaq 0x28(%rsp), %rdi
callq 0x79b8
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
andq $0x0, 0x8(%rbx)
movb $0x0, 0x10(%rbx)
testb %r12b, %r12b
je 0x17c2f
movl $0x0, 0xc(%rsp)
leaq 0x10(%rsp), %r12
movq %r12, %r14
movq (%r14), %r14
cmpq %r12, %r14
je 0x17c2f
testl %ebp, %ebp
jg 0x17bd3
leaq 0x10(%r14), %r13
movq %r13, %rdi
leaq 0x179f6(%rip), %rsi     # 0x2f5c0
callq 0x91b7
testb %al, %al
je 0x17bd7
decl %ebp
jmp 0x17bb0
movq %r13, %rdi
leaq 0x1772f(%rip), %rsi     # 0x2f310
callq 0x91b7
movzbl %al, %eax
negl %eax
orl $0x1, %eax
movl 0xc(%rsp), %ecx
addl %eax, %ecx
decl %ebp
movl %ecx, 0xc(%rsp)
cmpl $-0x1, %ecx
jne 0x17bb0
pushq $0x10
popq %rdi
callq 0x7290
movq %rax, %r15
leaq 0x15b88(%rip), %rsi     # 0x2d799
movq %rax, %rdi
callq 0x7910
movq 0x2b3a8(%rip), %rsi     # 0x42fc8
movq 0x2b389(%rip), %rdx     # 0x42fb0
movq %r15, %rdi
callq 0x7830
movq (%r15), %rax
cmpb $0x2f, (%rax)
jne 0x17c46
leaq 0x14e0a(%rip), %rsi     # 0x2ca48
movq %rbx, %rdi
callq 0x7920
leaq 0x17973(%rip), %r15     # 0x2f5c0
leaq 0x176bc(%rip), %r12     # 0x2f310
leaq 0x28(%rsp), %rbp
leaq 0x10(%rsp), %r14
movq (%r14), %r14
leaq 0x10(%rsp), %rax
cmpq %rax, %r14
je 0x17d31
leaq 0x10(%r14), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x91b7
testb %al, %al
jne 0x17c5e
movq %r13, %rdi
movq %r12, %rsi
callq 0x91b7
testb %al, %al
je 0x17cbd
cmpq $0x0, 0x8(%rbx)
je 0x17cbd
movq 0x8(%r14), %r14
leaq 0x10(%r14), %rdi
movq %r12, %rsi
callq 0x91b7
testb %al, %al
je 0x17cee
movq %rbx, %rdi
leaq 0x1467a(%rip), %rsi     # 0x2c330
callq 0x7920
jmp 0x17d19
cmpq 0x18(%rsp), %r14
je 0x17d21
movq %rbp, %rdi
movq %r13, %rsi
leaq 0x14d77(%rip), %rdx     # 0x2ca48
callq 0xc1e9
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x7950
movq %rbp, %rdi
callq 0x79b8
jmp 0x17c5e
movq 0x18(%r14), %rcx
notq %rcx
addq 0x8(%rbx), %rcx
movq %rbp, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x7540
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x75f0
movq %rbp, %rdi
callq 0x79b8
movq (%r14), %r14
jmp 0x17c5e
movq %rbx, %rdi
movq %r13, %rsi
callq 0x7950
jmp 0x17c5e
leaq 0x10(%rsp), %rdi
callq 0x8f64
jmp 0x17d51
leaq 0x1500b(%rip), %rsi     # 0x2cd4f
leaq 0x28(%rsp), %rdx
movq %rbx, %rdi
callq 0x8032
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x17d9b
movq %rax, %r14
movq %r15, %rdi
callq 0x73d0
jmp 0x17d9e
jmp 0x17d9b
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0x79b8
jmp 0x17db0
movq %rax, %r14
jmp 0x17db0
jmp 0x17d9b
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0x79b8
jmp 0x17d9e
jmp 0x17d9b
movq %rax, %r14
movq %rbx, %rdi
callq 0x79b8
leaq 0x10(%rsp), %rdi
callq 0x8f64
movq %r14, %rdi
callq 0x7860