// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movl %esi, %r15d
movq %rdi, %r14
movzwl (%rdi), %edi
orl $0x80800, %esi          # imm = 0x80800
xorl %edx, %edx
callq 0xf300
cmpl $-0x1, %eax
je 0x2ea83
movl %eax, %ebp
movzwl (%r14), %eax
movl %eax, %ecx
orl $0x8, %ecx
cmpl $0xa, %ecx
jne 0x2eb6d
cmpl $0x2, %r15d
jne 0x2eb6d
cmpq $0x2, %rax
je 0x2ea7d
cmpl $0xa, %eax
jne 0x2ea96
leaq 0x2(%r14), %rcx
jmp 0x2ea9d
callq 0xf0a0
movl (%rax), %eax
movl %eax, (%rbx)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x2eb6f
leaq 0x19bb3(%rip), %rcx     # 0x48650
cmpw $0x0, (%rcx)
leaq 0x9d38(%rip), %r12      # 0x387e0
jne 0x2eb3b
movq (%r12,%rax,8), %rdx
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0xfa60
xorl %r13d, %r13d
xorl %r15d, %r15d
callq *0x19805(%rip)          # 0x482d0
movzwl 0x8(%rsp), %ecx
cmpl $0x2, %ecx
je 0x2eae1
leaq 0x19b74(%rip), %rdx     # 0x48650
cmpl $0xa, %ecx
jne 0x2eae6
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 (%r12,%rcx,8), %edx
movl %ebp, %edi
leaq 0x8(%rsp), %rsi
callq 0xf180
testl %eax, %eax
je 0x2eb35
cmpl $0x6, %r15d
leal 0x1(%r15), %eax
setae %r13b
movl %eax, %r15d
cmpl $0x7, %eax
jne 0x2eac5
jmp 0x2eb3b
testb $0x1, %r13b
je 0x2eb6d
movzwl (%r14), %eax
movl (%r12,%rax,8), %edx
movl %ebp, %edi
movq %r14, %rsi
callq 0xf180
testl %eax, %eax
je 0x2eb6d
callq 0xf0a0
movl (%rax), %eax
movl %eax, (%rbx)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $-0x1, %ebp
je 0x2eb6f
movl %ebp, %edi
callq 0x10a10
jmp 0x2eb6f
movl %ebp, %ebx
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq