// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r14
pushq %rbx
subq $0x90, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x78(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
andl $0x0, 0x10(%rax)
leaq 0x16102(%rip), %rsi     # 0x2d1c9
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x8032
cmpq $0x0, 0x8(%r14)
je 0x170f0
leaq 0x160ed(%rip), %rsi     # 0x2d1d1
movq %r14, %rdi
callq 0x91b7
testb %al, %al
je 0x17127
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x7690
movl $0x50, 0x20(%rbx)
leaq 0x28(%rsp), %rdi
callq 0x79b8
leaq 0x68(%rsp), %rdi
callq 0x79b8
movq %rbx, %rax
addq $0x90, %rsp
popq %rbx
popq %r14
popq %rbp
retq
leaq 0x160a5(%rip), %rsi     # 0x2d1d3
movq %r14, %rdi
xorl %edx, %edx
callq 0x7330
cmpq $-0x1, %rax
jne 0x172be
movq (%r14), %rdi
movq 0x8(%r14), %rsi
addq %rdi, %rsi
leaq 0x8(%rsp), %rdx
movb $0x3a, (%rdx)
callq 0x17fba
cmpq $0x1, %rax
jg 0x172be
leaq 0x8(%rsp), %rsi
movb $0x3a, (%rsi)
movq %r14, %rdi
callq 0xc288
testb %al, %al
je 0x17219
pushq $0x3a
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x7650
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x7540
pushq $0x3a
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x7650
leaq 0x1(%rax), %rdx
leaq 0x48(%rsp), %rdi
pushq $-0x1
popq %rcx
movq %r14, %rsi
callq 0x7540
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x171f3
movq 0x8(%rsp), %rdi
addq %rdi, %rsi
leaq 0x7(%rsp), %rdx
movb $0x2e, (%rdx)
callq 0x1619c
cmpq $0x3, %rax
jne 0x172ee
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x7300
movq 0x50(%rsp), %rax
testq %rax, %rax
je 0x17292
cmpq $0x6, %rax
jae 0x1730a
leaq 0x48(%rsp), %rdi
callq 0x16b90
movl %eax, %ebp
jmp 0x17295
movq (%r14), %rdi
movq 0x8(%r14), %rsi
addq %rdi, %rsi
leaq 0x8(%rsp), %rdx
movb $0x2e, (%rdx)
callq 0x17fba
cmpq $0x3, %rax
jne 0x17248
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x7300
pushq $0x50
popq %rbp
jmp 0x1727d
movq (%r14), %rdi
movq 0x8(%r14), %rsi
addq %rdi, %rsi
leaq 0x8(%rsp), %rdx
movb $0x2e, (%rdx)
callq 0x17fba
testq %rax, %rax
jne 0x1733a
cmpq $0x6, 0x8(%r14)
jae 0x17356
movq %r14, %rdi
callq 0x16b90
movl %eax, %ebp
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x7690
movl %ebp, 0x20(%rbx)
jmp 0x17104
pushq $0x50
popq %rbp
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x7690
movl %ebp, 0x20(%rbx)
leaq 0x48(%rsp), %rdi
callq 0x79b8
leaq 0x8(%rsp), %rdi
callq 0x79b8
jmp 0x17104
pushq $0x10
popq %rdi
callq 0x7290
movq %rax, %r14
leaq 0x15f10(%rip), %rsi     # 0x2d1e0
movq %rax, %rdi
callq 0x7910
movq 0x2bce9(%rip), %rsi     # 0x42fc8
movq 0x2bcca(%rip), %rdx     # 0x42fb0
movq %r14, %rdi
callq 0x7830
pushq $0x10
popq %rdi
callq 0x7290
movq %rax, %r14
leaq 0x15ee0(%rip), %rsi     # 0x2d1e0
movq %rax, %rdi
callq 0x7910
jmp 0x17324
pushq $0x10
popq %rdi
callq 0x7290
movq %rax, %r14
leaq 0x15ecb(%rip), %rsi     # 0x2d1e7
movq %rax, %rdi
callq 0x7910
movq 0x2bc9d(%rip), %rsi     # 0x42fc8
movq 0x2bc7e(%rip), %rdx     # 0x42fb0
movq %r14, %rdi
callq 0x7830
pushq $0x10
popq %rdi
callq 0x7290
movq %rax, %r14
leaq 0x15e94(%rip), %rsi     # 0x2d1e0
movq %rax, %rdi
callq 0x7910
jmp 0x172d8
pushq $0x10
popq %rdi
callq 0x7290
movq %rax, %r14
leaq 0x15e7f(%rip), %rsi     # 0x2d1e7
movq %rax, %rdi
callq 0x7910
jmp 0x172d8
jmp 0x17397
jmp 0x17397
jmp 0x1737b
movq %rax, %rbx
movq %r14, %rdi
callq 0x73d0
jmp 0x173a9
jmp 0x173c4
jmp 0x173a6
movq %rax, %rbx
jmp 0x173b3
jmp 0x173c4
jmp 0x173c4
jmp 0x173c4
movq %rax, %rbx
movq %r14, %rdi
callq 0x73d0
jmp 0x173c7
jmp 0x173c4
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x79b8
leaq 0x8(%rsp), %rdi
callq 0x79b8
jmp 0x173c7
movq %rax, %rbx
jmp 0x173d1
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x79b8
leaq 0x68(%rsp), %rdi
callq 0x79b8
movq %rbx, %rdi
callq 0x7860