// File: /workspace/github_c_makefile_star2_O2/wahern[P]cqueues/src/lib/socket.c
// Function: sa_egress
void *sa_egress(void *lcl, size_t lim, sockaddr_arg_t rmt, int *_error) {
	static struct { sa_family_t pf; int fd;} udp4 = { PF_INET, -1 }, udp6 = { PF_INET6, -1 }, *udp;
	struct sockaddr_storage ss;
	int error;

	switch (*sa_family(rmt)) {
	case AF_INET:
		udp = &udp4;

		break;
	case AF_INET6:
		udp = &udp6;

		break;
	default:
		error = EINVAL;

		goto error;
	}

	if (udp->fd == -1) {
#if defined SOCK_CLOEXEC
		if (-1 == (udp->fd = socket(udp->pf, SOCK_DGRAM|SOCK_CLOEXEC, 0)))
			goto syerr;
#else
		if (-1 == (udp->fd = socket(udp->pf, SOCK_DGRAM, 0)))
			goto syerr;
#endif

		if ((error = so_cloexec(udp->fd, 1))) {
			so_closesocket(&udp->fd, NULL);

			goto error;
		}
	}

	assert(sizeof ss >= sa_len(rmt));
	memcpy(&ss, sockaddr_ref(rmt).sa, sa_len(rmt));

	if (!*sa_port(&ss, SA_PORT_NONE, NULL))
		*sa_port(&ss, SA_PORT_NONE, NULL) = htons(6970);

	if (0 != connect(udp->fd, (struct sockaddr *)&ss, sa_len(&ss)))
		goto syerr;

	memset(&ss, 0, sizeof ss);

	if (0 != getsockname(udp->fd, (struct sockaddr *)&ss, &(socklen_t){ sizeof ss }))
		goto syerr;

	if (lim < sa_len(&ss)) {
		error = ENOSPC;

		goto error;
	}

	memcpy(lcl, &ss, sa_len(&ss));

	return lcl;
syerr:
	error = so_syerr();
error:
	if (_error)
		*_error = error;

	return memset(lcl, 0, lim);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
movzwl (%rdx), %eax
cmpl $0x2, %eax
je 0x204e4
cmpl $0xa, %eax
jne 0x2055f
leaq 0x25e4e(%rip), %rax     # 0x46330
jmp 0x204eb
leaq 0x25e3d(%rip), %rax     # 0x46328
movq %rax, 0x261ae(%rip)     # 0x466a0
cmpl $-0x1, 0x4(%rax)
je 0x20567
movq %r12, %rdi
callq 0x2048c
movl %eax, %edx
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0xfad0
movzwl (%r13), %eax
leaq 0xa(%rsp), %rcx
movl %eax, %edx
orl $0x8, %edx
cmpl $0xa, %edx
jne 0x2053d
cmpw $0x0, 0x12(%rsp)
jne 0x20542
cmpl $0xa, %eax
je 0x20538
cmpl $0x2, %eax
jne 0x2053d
leaq 0x12(%rsp), %rcx
movw $0x3a1b, (%rcx)         # imm = 0x3A1B
movq 0x26157(%rip), %rcx     # 0x466a0
movl 0x4(%rcx), %edi
cmpl $0x1, %eax
je 0x205b5
cmpl $0x2, %eax
je 0x205b1
cmpl $0xa, %eax
jne 0x205ba
pushq $0x1c
jmp 0x205b7
pushq $0x16
popq %rbp
jmp 0x20616
movzwl (%rax), %edi
movl $0x80002, %esi          # imm = 0x80002
xorl %edx, %edx
callq 0xf310
movq 0x26123(%rip), %rcx     # 0x466a0
movl %eax, 0x4(%rcx)
cmpl $-0x1, %eax
je 0x2060f
pushq $0x1
popq %rsi
movl %eax, %edi
callq 0xf450
testl %eax, %eax
je 0x204f8
movl %eax, %ebp
movq 0x260fc(%rip), %rdi     # 0x466a0
addq $0x4, %rdi
xorl %esi, %esi
callq 0xf2c0
jmp 0x20616
pushq $0x10
jmp 0x205b7
pushq $0x6e
popq %rdx
jmp 0x205bc
xorl %edx, %edx
leaq 0x10(%rsp), %rsi
callq 0xfb10
testl %eax, %eax
jne 0x2060f
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
movaps %xmm0, 0x70(%rsi)
movaps %xmm0, 0x60(%rsi)
movaps %xmm0, 0x50(%rsi)
movaps %xmm0, 0x40(%rsi)
movaps %xmm0, 0x30(%rsi)
movaps %xmm0, 0x20(%rsi)
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
movq 0x260a8(%rip), %rax     # 0x466a0
movl 0x4(%rax), %edi
leaq 0xc(%rsp), %rdx
movl $0x80, (%rdx)
callq 0xfcb0
testl %eax, %eax
je 0x20640
callq 0xf0a0
movl (%rax), %ebp
testq %r15, %r15
je 0x2061e
movl %ebp, (%r15)
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0xf790
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzwl 0x10(%rsp), %eax
cmpl $0x1, %eax
je 0x2065c
cmpl $0x2, %eax
je 0x20658
cmpl $0xa, %eax
jne 0x2067e
pushq $0x1c
jmp 0x2065e
pushq $0x10
jmp 0x2065e
pushq $0x6e
popq %rcx
cmpq %r14, %rcx
jbe 0x2066b
pushq $0x1c
jmp 0x20561
cmpl $0x1, %eax
je 0x20686
cmpl $0x2, %eax
je 0x20682
cmpl $0xa, %eax
jne 0x2067e
pushq $0x1c
jmp 0x20688
xorl %edx, %edx
jmp 0x20689
pushq $0x10
jmp 0x20688
pushq $0x6e
popq %rdx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0xfad0
jmp 0x2062b