// File: mbourand[P]webserv/srcs/Utils/Utils.cpp
// Function: ft::complete_ip(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
std::pair<std::string, int> ft::complete_ip(const std::string& incomplete_ip)
{
	std::pair<std::string, int> ret;

	std::string ip = "0.0.0.0";
	int port = 80;

	if (incomplete_ip.size() == 0 || incomplete_ip == ":")
		return std::make_pair(ip, port);
	if (incomplete_ip.find_first_not_of("0123456789:.") != std::string::npos || std::count(incomplete_ip.begin(), incomplete_ip.end(), ':') > 1)
		throw std::invalid_argument("Bad ip");

	if (ft::contains(incomplete_ip, ':'))
	{
		std::string part1 = incomplete_ip.substr(0, incomplete_ip.find(':'));
		std::string part2 = incomplete_ip.substr(incomplete_ip.find(':') + 1);
		if (part1.size() != 0)
		{
			if (std::count(part1.begin(), part1.end(), '.') != 3)
				throw std::invalid_argument("Bad ip");
			ip = part1;
		}
		if (part2.size() != 0)
		{
			if (part2.size() > 5)
				throw std::invalid_argument("Bad port");
			port = ft::toInt(part2);
		}
		return std::make_pair(ip, port);
	}
	if (std::count(incomplete_ip.begin(), incomplete_ip.end(), '.') == 3)
		ip = incomplete_ip;
	else if (std::count(incomplete_ip.begin(), incomplete_ip.end(), '.') == 0)
	{
		if (incomplete_ip.size() > 5)
			throw std::invalid_argument("Bad port");
		port = ft::toInt(incomplete_ip);
	}
	else
		throw std::invalid_argument("Bad ip");
	return std::make_pair(ip, port);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movq %rdi, -0x130(%rbp)
movq %rdi, %rax
movq %rax, -0x128(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x32de0
leaq -0x59(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x9b10
movq -0x120(%rbp), %rdx
leaq 0x3ba26(%rip), %rsi     # 0x6b5f1
leaq -0x58(%rbp), %rdi
callq 0xa780
jmp 0x2fbd6
leaq -0x59(%rbp), %rdi
callq 0x9700
movl $0x50, -0x70(%rbp)
movq -0x10(%rbp), %rdi
callq 0x9480
cmpq $0x0, %rax
je 0x2fc19
movq -0x10(%rbp), %rdi
leaq 0x3b9f9(%rip), %rsi     # 0x6b5f9
callq 0xdf50
movb %al, -0x131(%rbp)
jmp 0x2fc0d
movb -0x131(%rbp), %al
testb $0x1, %al
jne 0x2fc19
jmp 0x2fc66
movq -0x130(%rbp), %rdi
leaq -0x58(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0x32e10
jmp 0x2fc2f
movl $0x1, -0x74(%rbp)
jmp 0x301d3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x59(%rbp), %rdi
callq 0x9700
jmp 0x301fe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x301f5
movq -0x10(%rbp), %rdi
leaq 0x3b98a(%rip), %rsi     # 0x6b5fb
xorl %eax, %eax
movl %eax, %edx
callq 0x93f0
movq %rax, %rcx
movb $0x1, %al
cmpq $-0x1, %rcx
movb %al, -0x132(%rbp)
jne 0x2fce3
movq -0x10(%rbp), %rdi
callq 0x9640
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rdi
callq 0x9bd0
movq %rax, -0x88(%rbp)
movb $0x3a, -0x89(%rbp)
movq -0x80(%rbp), %rdi
movq -0x88(%rbp), %rsi
leaq -0x89(%rbp), %rdx
callq 0x32d60
movq %rax, -0x140(%rbp)
jmp 0x2fccf
movq -0x140(%rbp), %rax
cmpq $0x1, %rax
setg %al
movb %al, -0x132(%rbp)
movb -0x132(%rbp), %al
testb $0x1, %al
jne 0x2fcef
jmp 0x2fd50
movl $0x10, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x148(%rbp)
leaq 0x3b8fb(%rip), %rsi     # 0x6b608
callq 0x9be0
jmp 0x2fd14
movq -0x148(%rbp), %rdi
movq 0x5f2a6(%rip), %rsi     # 0x8efc8
movq 0x5f28f(%rip), %rdx     # 0x8efb8
callq 0x9aa0
jmp 0x30210
movq -0x148(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
callq 0x94c0
jmp 0x301f5
movq -0x10(%rbp), %rdi
movb $0x3a, -0x8a(%rbp)
leaq -0x8a(%rbp), %rsi
callq 0x143d0
movb %al, -0x149(%rbp)
jmp 0x2fd6f
movb -0x149(%rbp), %al
testb $0x1, %al
jne 0x2fd7e
jmp 0x30007
movq -0x10(%rbp), %rdi
movq %rdi, -0x160(%rbp)
xorl %eax, %eax
movl %eax, %edx
movq %rdx, -0x158(%rbp)
movl $0x3a, %esi
callq 0x9860
movq -0x160(%rbp), %rsi
movq -0x158(%rbp), %rdx
movq %rax, %rcx
leaq -0xb0(%rbp), %rdi
callq 0x96b0
jmp 0x2fdbd
movq -0x10(%rbp), %rdi
movq %rdi, -0x168(%rbp)
xorl %eax, %eax
movl %eax, %edx
movl $0x3a, %esi
callq 0x9860
movq -0x168(%rbp), %rsi
movq %rax, %rdx
incq %rdx
leaq -0xd0(%rbp), %rdi
movq $-0x1, %rcx
callq 0x96b0
jmp 0x2fdf8
leaq -0xb0(%rbp), %rdi
callq 0x9480
cmpq $0x0, %rax
je 0x2ff0d
leaq -0xb0(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x95b0
movq -0x178(%rbp), %rdi
movq %rax, -0xd8(%rbp)
callq 0x9400
movq %rax, -0xe0(%rbp)
movb $0x2e, -0xe1(%rbp)
movq -0xd8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
leaq -0xe1(%rbp), %rdx
callq 0x2e1f0
movq %rax, -0x170(%rbp)
jmp 0x2fe65
movq -0x170(%rbp), %rax
cmpq $0x3, %rax
je 0x2fef9
movl $0x10, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x180(%rbp)
leaq 0x3b774(%rip), %rsi     # 0x6b608
callq 0x9be0
jmp 0x2fe9b
movq -0x180(%rbp), %rdi
movq 0x5f11f(%rip), %rsi     # 0x8efc8
movq 0x5f108(%rip), %rdx     # 0x8efb8
callq 0x9aa0
jmp 0x30210
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x2fff6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x2ffea
movq -0x180(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
callq 0x94c0
jmp 0x2ffea
leaq -0x58(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x9070
jmp 0x2ff0b
jmp 0x2ff0d
leaq -0xd0(%rbp), %rdi
callq 0x9480
cmpq $0x0, %rax
je 0x2ffb0
leaq -0xd0(%rbp), %rdi
callq 0x9480
cmpq $0x5, %rax
jbe 0x2ff93
movl $0x10, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x188(%rbp)
leaq 0x3b6bc(%rip), %rsi     # 0x6b60f
callq 0x9be0
jmp 0x2ff5a
movq -0x188(%rbp), %rdi
movq 0x5f060(%rip), %rsi     # 0x8efc8
movq 0x5f049(%rip), %rdx     # 0x8efb8
callq 0x9aa0
jmp 0x30210
movq -0x188(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
callq 0x94c0
jmp 0x2ffea
leaq -0xd0(%rbp), %rdi
callq 0x2f2a0
movl %eax, -0x18c(%rbp)
jmp 0x2ffa7
movl -0x18c(%rbp), %eax
movl %eax, -0x70(%rbp)
movq -0x130(%rbp), %rdi
leaq -0x58(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0x32e10
jmp 0x2ffc6
movl $0x1, -0x74(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x9c98
leaq -0xb0(%rbp), %rdi
callq 0x9c98
jmp 0x301d3
leaq -0xd0(%rbp), %rdi
callq 0x9c98
leaq -0xb0(%rbp), %rdi
callq 0x9c98
jmp 0x301f5
movq -0x10(%rbp), %rdi
callq 0x9640
movq %rax, -0xf0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x9bd0
movq %rax, -0xf8(%rbp)
movb $0x2e, -0xf9(%rbp)
movq -0xf0(%rbp), %rdi
movq -0xf8(%rbp), %rsi
leaq -0xf9(%rbp), %rdx
callq 0x32d60
movq %rax, -0x198(%rbp)
jmp 0x30051
movq -0x198(%rbp), %rax
cmpq $0x3, %rax
jne 0x30072
movq -0x10(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x9070
jmp 0x3006d
jmp 0x301b6
movq -0x10(%rbp), %rdi
callq 0x9640
movq %rax, -0x108(%rbp)
movq -0x10(%rbp), %rdi
callq 0x9bd0
movq %rax, -0x110(%rbp)
movb $0x2e, -0x111(%rbp)
movq -0x108(%rbp), %rdi
movq -0x110(%rbp), %rsi
leaq -0x111(%rbp), %rdx
callq 0x32d60
movq %rax, -0x1a0(%rbp)
jmp 0x300bc
movq -0x1a0(%rbp), %rax
cmpq $0x0, %rax
jne 0x30159
movq -0x10(%rbp), %rdi
callq 0x9480
cmpq $0x5, %rax
jbe 0x3013d
movl $0x10, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x1a8(%rbp)
leaq 0x3b515(%rip), %rsi     # 0x6b60f
callq 0x9be0
jmp 0x30101
movq -0x1a8(%rbp), %rdi
movq 0x5eeb9(%rip), %rsi     # 0x8efc8
movq 0x5eea2(%rip), %rdx     # 0x8efb8
callq 0x9aa0
jmp 0x30210
movq -0x1a8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
callq 0x94c0
jmp 0x301f5
movq -0x10(%rbp), %rdi
callq 0x2f2a0
movl %eax, -0x1ac(%rbp)
jmp 0x3014e
movl -0x1ac(%rbp), %eax
movl %eax, -0x70(%rbp)
jmp 0x301b4
movl $0x10, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x1b8(%rbp)
leaq 0x3b491(%rip), %rsi     # 0x6b608
callq 0x9be0
jmp 0x3017e
movq -0x1b8(%rbp), %rdi
movq 0x5ee3c(%rip), %rsi     # 0x8efc8
movq 0x5ee25(%rip), %rdx     # 0x8efb8
callq 0x9aa0
jmp 0x30210
movq -0x1b8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
callq 0x94c0
jmp 0x301f5
jmp 0x301b6
movq -0x130(%rbp), %rdi
leaq -0x58(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0x32e10
jmp 0x301cc
movl $0x1, -0x74(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x9c98
leaq -0x38(%rbp), %rdi
callq 0x32e50
movq -0x128(%rbp), %rax
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
leaq -0x58(%rbp), %rdi
callq 0x9c98
leaq -0x38(%rbp), %rdi
callq 0x32e50
movq -0x68(%rbp), %rdi
callq 0x9af0