// File: 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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x108(%rbp)
movb %dl, %al
movq %rdi, %rdx
movq %rdx, -0x100(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movl %ecx, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0x9480
cmpq $0x0, %rax
jne 0x32321
leaq -0x19(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x9b10
movq -0x108(%rbp), %rdi
movq -0x110(%rbp), %rdx
leaq 0x38a7e(%rip), %rsi     # 0x6ad70
callq 0xa780
jmp 0x322f9
leaq -0x19(%rbp), %rdi
callq 0x9700
jmp 0x327f0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x19(%rbp), %rdi
callq 0x9700
jmp 0x32800
movq -0x10(%rbp), %rax
movq %rax, -0x120(%rbp)
leaq -0x69(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0x9b10
movq -0x118(%rbp), %rdx
leaq 0x3870e(%rip), %rsi     # 0x6aa58
leaq -0x68(%rbp), %rdi
callq 0xa780
jmp 0x32355
movq -0x120(%rbp), %rsi
leaq -0x48(%rbp), %rdi
leaq -0x68(%rbp), %rdx
callq 0x2f910
jmp 0x3236b
leaq -0x68(%rbp), %rdi
callq 0x9c98
leaq -0x69(%rbp), %rdi
callq 0x9700
movq -0x108(%rbp), %rdi
movb $0x0, -0x6a(%rbp)
callq 0x98c0
testb $0x1, -0x11(%rbp)
je 0x32518
movl $0x0, -0x70(%rbp)
leaq -0x48(%rbp), %rdi
callq 0xcc30
movq %rax, -0x78(%rbp)
leaq -0x48(%rbp), %rdi
callq 0xccb0
movq %rax, -0x80(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x14110
testb $0x1, %al
jne 0x323ce
jmp 0x32516
movl -0x18(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x18(%rbp)
cmpl $0x0, %eax
jle 0x32414
jmp 0x324ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x32406
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x9c98
leaq -0x69(%rbp), %rdi
callq 0x9700
jmp 0x32800
leaq -0x78(%rbp), %rdi
callq 0xd8f0
movq %rax, %rdi
leaq 0x3b5c9(%rip), %rsi     # 0x6d9f0
callq 0xdf50
movb %al, -0x121(%rbp)
jmp 0x32434
movb -0x121(%rbp), %al
testb $0x1, %al
jne 0x32440
jmp 0x32456
jmp 0x324ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x327d9
leaq -0x78(%rbp), %rdi
callq 0xd8f0
movq %rax, %rdi
leaq 0x3b2d7(%rip), %rsi     # 0x6d740
callq 0xdf50
movb %al, -0x122(%rbp)
jmp 0x32476
movb -0x122(%rbp), %al
testb $0x1, %al
jne 0x32482
jmp 0x3248d
movl -0x70(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x32496
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
cmpl $-0x1, -0x70(%rbp)
jne 0x324fd
movl $0x10, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x130(%rbp)
leaq 0x39707(%rip), %rsi     # 0x6bbc1
callq 0x9be0
jmp 0x324c1
movq -0x130(%rbp), %rdi
movq 0x5caf9(%rip), %rsi     # 0x8efc8
movq 0x5cae2(%rip), %rdx     # 0x8efb8
callq 0x9aa0
jmp 0x32809
movq -0x130(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x94c0
jmp 0x327d9
jmp 0x324ff
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
callq 0x142e0
movq %rax, -0x88(%rbp)
jmp 0x323ab
jmp 0x32518
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x9720
movsbl (%rax), %eax
cmpl $0x2f, %eax
jne 0x32544
movq -0x108(%rbp), %rdi
leaq 0x3851d(%rip), %rsi     # 0x6aa58
callq 0x9650
jmp 0x32542
jmp 0x32544
leaq -0x48(%rbp), %rdi
callq 0xcc30
movq %rax, -0x90(%rbp)
leaq -0x48(%rbp), %rdi
callq 0xccb0
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x14110
testb $0x1, %al
jne 0x32580
jmp 0x327b8
leaq -0x90(%rbp), %rdi
callq 0xd8f0
movq %rax, %rdi
leaq 0x3b45a(%rip), %rsi     # 0x6d9f0
callq 0xdf50
movb %al, -0x131(%rbp)
jmp 0x325a3
movb -0x131(%rbp), %al
testb $0x1, %al
jne 0x325af
jmp 0x325b4
jmp 0x3279e
leaq -0x90(%rbp), %rdi
callq 0xd8f0
movq %rax, %rdi
leaq 0x3b176(%rip), %rsi     # 0x6d740
callq 0xdf50
movb %al, -0x132(%rbp)
jmp 0x325d7
movb -0x132(%rbp), %al
testb $0x1, %al
jne 0x325e6
jmp 0x326e7
movq -0x108(%rbp), %rdi
callq 0x9480
cmpq $0x0, %rax
jbe 0x326e7
leaq -0x90(%rbp), %rdi
movq %rdi, -0x140(%rbp)
xorl %esi, %esi
callq 0x32e70
movq -0x140(%rbp), %rdi
movq %rax, -0xa0(%rbp)
callq 0xd8f0
movq %rax, %rdi
leaq 0x3b112(%rip), %rsi     # 0x6d740
callq 0xdf50
movb %al, -0x133(%rbp)
jmp 0x3263b
movb -0x133(%rbp), %al
testb $0x1, %al
jne 0x32647
jmp 0x3265e
movq -0x108(%rbp), %rdi
leaq 0x37ceb(%rip), %rsi     # 0x6a340
callq 0x9650
jmp 0x3265c
jmp 0x326cd
movq -0x108(%rbp), %rdi
callq 0x9480
movq %rax, -0x148(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x32ea0
movq %rax, %rdi
callq 0x9480
movq -0x108(%rbp), %rsi
movq %rax, %rcx
movq -0x148(%rbp), %rax
notq %rcx
addq %rax, %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0xc0(%rbp), %rdi
callq 0x96b0
jmp 0x326ae
movq -0x108(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x97e0
leaq -0xc0(%rbp), %rdi
callq 0x9c98
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
callq 0x142e0
movq %rax, -0xc8(%rbp)
jmp 0x3279a
leaq -0x48(%rbp), %rdi
callq 0xccb0
movq %rax, -0xd0(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0xdf30
movq %rax, %rsi
leaq -0x90(%rbp), %rdi
callq 0x32ec0
testb $0x1, %al
jne 0x32718
jmp 0x32737
leaq -0x90(%rbp), %rdi
callq 0xd8f0
movq -0x108(%rbp), %rdi
movq %rax, %rsi
callq 0x9830
jmp 0x32735
jmp 0x32798
leaq -0x90(%rbp), %rdi
callq 0xd8f0
movq %rax, %rsi
leaq 0x3830b(%rip), %rdx     # 0x6aa58
leaq -0xf0(%rbp), %rdi
callq 0x14190
jmp 0x3275b
movq -0x108(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x9830
jmp 0x32770
leaq -0xf0(%rbp), %rdi
callq 0x9c98
jmp 0x32798
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x9c98
jmp 0x327d9
jmp 0x3279a
jmp 0x3279c
jmp 0x3279e
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
callq 0x142e0
movq %rax, -0xf8(%rbp)
jmp 0x32554
movb $0x1, -0x6a(%rbp)
testb $0x1, -0x6a(%rbp)
jne 0x327ce
movq -0x108(%rbp), %rdi
callq 0x9c98
leaq -0x48(%rbp), %rdi
callq 0xceb0
jmp 0x327f0
movq -0x108(%rbp), %rdi
callq 0x9c98
leaq -0x48(%rbp), %rdi
callq 0xceb0
jmp 0x32800
movq -0x100(%rbp), %rax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x9af0
nopl (%rax)