// File: /workspace/github_cpp_makefile_star2_O1/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 0x1d7db
movl %ecx, %ebp
movl %edx, %r12d
movq %rsi, %r15
leaq 0x10(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x1d4de(%rip), %rsi     # 0x3aa94
leaq 0x1d4d8(%rip), %rdx     # 0x3aa95
movq %rsp, %rdi
callq 0x80f4
leaq 0x30(%rsp), %r14
movq %rsp, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1c232
movq %r15, 0x28(%rsp)
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1d5f8
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 0x1d675
xorl %r12d, %r12d
testl %ebp, %ebp
jg 0x1d66b
leaq 0x10(%r15), %r13
movq %r13, %rdi
leaq 0x1fff7(%rip), %rsi     # 0x3d630
callq 0x7180
testl %eax, %eax
je 0x1d66b
movq %r13, %rdi
leaq 0x1fd34(%rip), %rsi     # 0x3d380
callq 0x7180
cmpl $0x1, %eax
movl $0x0, %eax
sbbl %eax, %eax
orl $0x1, %eax
addl %eax, %r12d
cmpl $-0x1, %r12d
je 0x1d806
movq (%r15), %r15
decl %ebp
cmpq %r14, %r15
jne 0x1d627
movq 0x28(%rsp), %rax
movq (%rax), %rax
cmpb $0x2f, (%rax)
jne 0x1d691
leaq 0x1d40b(%rip), %rsi     # 0x3aa94
movq %rbx, %rdi
callq 0x78d0
movq 0x30(%rsp), %r13
cmpq %r14, %r13
je 0x1d7cf
leaq 0x1ff8a(%rip), %r15     # 0x3d630
movq %rsp, %r12
leaq 0x10(%r13), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x7180
testl %eax, %eax
je 0x1d7c2
movq %rbp, %rdi
leaq 0x1fcb6(%rip), %rsi     # 0x3d380
callq 0x7180
testl %eax, %eax
jne 0x1d73d
cmpq $0x0, 0x8(%rbx)
je 0x1d73d
movq 0x8(%r13), %r13
leaq 0x10(%r13), %rdi
leaq 0x1fc97(%rip), %rsi     # 0x3d380
callq 0x7180
testl %eax, %eax
je 0x1d7af
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 0x1d7be
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x1d7be
cmpq 0x38(%rsp), %r13
je 0x1d79d
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x8a8a
movq %r12, %rdi
leaq 0x1d32a(%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 0x1d7c2
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x1d7c2
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdx
movq %rbx, %rdi
callq 0x7190
jmp 0x1d7c2
movq %rbx, %rdi
leaq 0x1cbc3(%rip), %rsi     # 0x3a37c
callq 0x78d0
movq (%r13), %r13
movq (%r13), %r13
cmpq %r14, %r13
jne 0x1d6a9
leaq 0x30(%rsp), %rdi
callq 0x96d8
jmp 0x1d7f4
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
leaq 0x1d5b2(%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 0x1dfcb(%rip), %rsi     # 0x3b7e5
movq %rax, %rdi
callq 0x78c0
movq 0x3079f(%rip), %rsi     # 0x4dfc8
movq 0x30780(%rip), %rdx     # 0x4dfb0
movq %r15, %rdi
callq 0x77d0
jmp 0x1d894
movq %rax, %r14
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1d8bb
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x1d8bb
movq %rax, %r14
jmp 0x1d8bb
jmp 0x1d894
movq %rax, %r14
movq %r15, %rdi
callq 0x7390
jmp 0x1d897
jmp 0x1d870
movq %rax, %r14
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1d897
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x1d897
jmp 0x1d894
jmp 0x1d894
movq %rax, %r14
movq (%rbx), %rdi
cmpq 0x20(%rsp), %rdi
je 0x1d8b1
movq 0x20(%rsp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x74b0
leaq 0x30(%rsp), %rdi
callq 0x96d8
movq %r14, %rdi
callq 0x7810