// File: roberty65[P]bServer/common/src/xbs_naddr.cpp
// Function: beyondy::str2sockaddr(char const*, int*, sockaddr*, unsigned int*, int)
int str2sockaddr(const char *str, int *ptype, struct sockaddr *naddr, socklen_t *nlen, int count)
{
	int type = SOCK_STREAM;
	if (strncmp(str, "tcp://", 6) == 0) { type = SOCK_STREAM; str += 6; }
	else if (strncmp(str, "udp://", 6) == 0) { type = SOCK_DGRAM; str += 6; }


	if (ptype != NULL) *ptype = type;
	if (*str == '@') {
		return __unix_str2sockaddr(str + 1, naddr, nlen, count);
	}

	const char *iptr;
	char host[NI_MAXHOST], serv[NI_MAXSERV];
	if ((iptr = strrchr(str, ':')) == NULL)
		return errno = EINVAL, -1;

	if (iptr - str > (int)sizeof(host) - 1)
		return errno = E2BIG, -1;
	strncpy(host, str, sizeof host);
	host[iptr - str] = 0;

	int slen = str + strlen(str) - iptr - 1;
	if (slen > (int)sizeof(serv) - 1) return errno = E2BIG, -1;
	memcpy(serv, iptr + 1, slen); serv[slen] = 0;

	return __inet_str2sockaddr(type, host, serv, naddr, nlen, count);	
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x470, %rsp            # imm = 0x470
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movl $0x1, -0x30(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x3024(%rip), %rsi      # 0x13555
movl $0x6, %edx
callq 0x3180
cmpl $0x0, %eax
jne 0x10555
movl $0x1, -0x30(%rbp)
movq -0x10(%rbp), %rax
addq $0x6, %rax
movq %rax, -0x10(%rbp)
jmp 0x10584
movq -0x10(%rbp), %rdi
leaq 0x2ffc(%rip), %rsi      # 0x1355c
movl $0x6, %edx
callq 0x3180
cmpl $0x0, %eax
jne 0x10582
movl $0x2, -0x30(%rbp)
movq -0x10(%rbp), %rax
addq $0x6, %rax
movq %rax, -0x10(%rbp)
jmp 0x10584
cmpq $0x0, -0x18(%rbp)
je 0x10594
movl -0x30(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x40, %eax
jne 0x105c0
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl -0x2c(%rbp), %ecx
callq 0x106f0
movl %eax, -0x4(%rbp)
jmp 0x106df
movq -0x10(%rbp), %rdi
movl $0x3a, %esi
callq 0x3470
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x105ef
callq 0x3060
movl $0x16, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x106df
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
cmpq $0x400, %rax            # imm = 0x400
jle 0x10619
callq 0x3060
movl $0x7, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x106df
leaq -0x440(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x401, %edx            # imm = 0x401
callq 0x31b0
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
movb $0x0, -0x440(%rbp,%rax)
movq -0x10(%rbp), %rax
movq %rax, -0x470(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3170
movq %rax, %rcx
movq -0x470(%rbp), %rax
addq %rcx, %rax
movq -0x38(%rbp), %rcx
subq %rcx, %rax
subq $0x1, %rax
movl %eax, -0x464(%rbp)
cmpl $0x1f, -0x464(%rbp)
jle 0x10690
callq 0x3060
movl $0x7, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x106df
leaq -0x460(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0x1, %rsi
movslq -0x464(%rbp), %rdx
callq 0x3320
movslq -0x464(%rbp), %rax
movb $0x0, -0x460(%rbp,%rax)
movl -0x30(%rbp), %edi
leaq -0x440(%rbp), %rsi
leaq -0x460(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
movl -0x2c(%rbp), %r9d
callq 0x10750
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x470, %rsp            # imm = 0x470
popq %rbp
retq
nopl (%rax,%rax)