// File: /workspace/github_c_makefile_star2_O3/wahern[P]cqueues/src/lib/dns.c
// Function: dns_socket
static int dns_socket(struct sockaddr *local, int type, int *error_) {
	int fd = -1, flags, error;
#if defined FIONBIO
	unsigned long opt;
#endif

	flags = 0;
#if HAVE_SOCK_CLOEXEC
	flags |= SOCK_CLOEXEC;
#endif
#if HAVE_SOCK_NONBLOCK
	flags |= SOCK_NONBLOCK;
#endif
	if (-1 == (fd = socket(local->sa_family, type|flags, 0)))
		goto soerr;

#if defined F_SETFD && !HAVE_SOCK_CLOEXEC
	if (-1 == fcntl(fd, F_SETFD, 1))
		goto syerr;
#endif

#if defined O_NONBLOCK && !HAVE_SOCK_NONBLOCK
	if (-1 == (flags = fcntl(fd, F_GETFL)))
		goto syerr;
	if (-1 == fcntl(fd, F_SETFL, flags | O_NONBLOCK))
		goto syerr;
#elif defined FIONBIO && HAVE_IOCTLSOCKET
	opt = 1;
	if (0 != ioctlsocket(fd, FIONBIO, &opt))
		goto soerr;
#endif

#if defined SO_NOSIGPIPE
	if (type != SOCK_DGRAM) {
		if (0 != setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(int){ 1 }, sizeof (int)))
			goto soerr;
	}
#endif

	if (local->sa_family != AF_INET && local->sa_family != AF_INET6)
		return fd;

	if (type != SOCK_DGRAM)
		return fd;

	/*
	 * FreeBSD, Linux, OpenBSD, OS X, and Solaris use random ports by
	 * default. Though the ephemeral range is quite small on OS X
	 * (49152-65535 on 10.10) and Linux (32768-60999 on 4.4.0, Ubuntu
	 * Xenial). See also RFC 6056.
	 *
	 * TODO: Optionally rely on the kernel to select a random port.
	 */
	if (*dns_sa_port(local->sa_family, local) == 0) {
		struct sockaddr_storage tmp;
		unsigned i, port;

		memcpy(&tmp, local, dns_sa_len(local));

		for (i = 0; i < DNS_SO_MAXTRY; i++) {
			port = 1025 + (dns_random() % 64510);

			*dns_sa_port(tmp.ss_family, &tmp) = htons(port);

			if (0 == bind(fd, (struct sockaddr *)&tmp, dns_sa_len(&tmp)))
				return fd;
		}

		/* NB: continue to next bind statement */
	}

	if (0 == bind(fd, local, dns_sa_len(local)))
		return fd;

	/* FALL THROUGH */
soerr:
	error = dns_soerr();

	goto error;
#if (defined F_SETFD && !HAVE_SOCK_CLOEXEC) || (defined O_NONBLOCK && !HAVE_SOCK_NONBLOCK)
syerr:
	error = dns_syerr();

	goto error;
#endif
error:
	*error_ = error;

	dns_socketclose(&fd, NULL);

	return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %r15
movzwl (%rdi), %edi
orl $0x80800, %esi          # imm = 0x80800
xorl %edx, %edx
callq 0xf300
cmpl $-0x1, %eax
je 0x2e65f
movl %eax, %ebx
movzwl (%r15), %eax
movzwl %ax, %ecx
movl %ecx, %edx
orl $0x8, %edx
cmpl $0xa, %edx
jne 0x2e729
cmpl $0x2, %ebp
jne 0x2e729
cmpl $0x2, %ecx
je 0x2e659
cmpl $0xa, %ecx
jne 0x2e66e
leaq 0x2(%r15), %rdx
jmp 0x2e675
callq 0xf0a0
movl (%rax), %eax
movl %eax, (%r14)
jmp 0x2e724
leaq 0x19fdb(%rip), %rdx     # 0x48650
cmpw $0x0, (%rdx)
leaq 0xa110(%rip), %r13      # 0x38790
jne 0x2e6fd
movq (%r13,%rcx,8), %rdx
leaq 0x8(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0xfa60
movl $0x7, %ebp
callq *0x19c2e(%rip)          # 0x482d0
movzwl 0x8(%rsp), %ecx
cmpl $0x2, %ecx
je 0x2e6b8
leaq 0x19f9d(%rip), %rdx     # 0x48650
cmpl $0xa, %ecx
jne 0x2e6bd
leaq 0xa(%rsp), %rdx
movl %eax, %esi
shrl %esi
imulq $0x4104945b, %rsi, %rsi # imm = 0x4104945B
shrq $0x2d, %rsi
imull $0xfbfe, %esi, %esi     # imm = 0xFBFE
negl %esi
addl %esi, %eax
addl $0x401, %eax            # imm = 0x401
rolw $0x8, %ax
movw %ax, (%rdx)
movl (%r13,%rcx,8), %edx
movl %ebx, %edi
movq %r12, %rsi
callq 0xf180
testl %eax, %eax
je 0x2e729
decl %ebp
jne 0x2e69c
movzwl (%r15), %eax
movzwl %ax, %eax
movl (%r13,%rax,8), %edx
movl %ebx, %edi
movq %r15, %rsi
callq 0xf180
testl %eax, %eax
je 0x2e729
callq 0xf0a0
movl (%rax), %eax
movl %eax, (%r14)
movl %ebx, %edi
callq 0x10a10
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq