// File: /workspace/github_c_makefile_star2_O2/Tarsnap[P]kivaloo/liball/../lib/serverpool/serverpool.c
// Function: serverpool_create
struct serverpool *
serverpool_create(const char * target, unsigned int freq, time_t ttl)
{
	struct serverpool * P;
	struct sock_addr ** sas;
	int fd[2];
	size_t i;

	/* Allocate server pool structure and (empty) array. */
	if ((P = malloc(sizeof(struct serverpool))) == NULL)
		goto err0;
	if ((P->A = serverpool_addrs_init(0)) == NULL)
		goto err1;

	/* Record TTL for future reference. */
	P->ttl = ttl;

	/* The first addresses will be generation #0. */
	P->generation = 0;

	/* Perform a first lookup. */
	if ((sas = sock_resolve(target)) == NULL)
		goto err2;

	/* Stuff the results into the server pool address list. */
	for (i = 0; sas[i] != NULL; i++) {
		/* Add this address to the pool. */
		if (addaddr(P, sas[i]))
			goto err3;
	}

	/* Next addresses will be generation #1. */
	P->generation = 1;

	/* Create a socket for the child to send us addresses. */
	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
		warnp("socketpair");
		goto err3;
	}

	/* Mark the read end of the socket pair as non-blocking. */
	if (fcntl(fd[0], F_SETFL, O_NONBLOCK) == -1) {
		warnp("Cannot make dns socket non-blocking");
		goto err4;
	}

	/* Fork off the process to perform DNS lookups. */
	if ((P->pid = forkdns(target, fd[0], fd[1], freq)) == -1)
		goto err4;

	/* Start reading from the child. */
	P->s = fd[0];
	P->addr = NULL;
	if ((P->read_cookie = network_read(P->s, (uint8_t *)&P->addrlen,
	    sizeof(size_t), sizeof(size_t), callback_read_len, P)) == NULL) {
		warnp("Error reading address via socket");
		goto err5;
	}

	/* Close write socket; only the child needs it. */
	if (close(fd[1]))
		warnp("close");

	/* Free the initial list of addresses. */
	sock_addr_freelist(sas);

	/* Success! */
	return (P);

err5:
	kill(P->pid, SIGTERM);
err4:
	if (close(fd[1]))
		warnp("close");
	if (close(fd[0]))
		warnp("close");
err3:
	sock_addr_freelist(sas);
	for (i = serverpool_addrs_getsize(P->A); i > 0; i--)
		sock_addr_free(serverpool_addrs_get(P->A, i - 1)->sa);
err2:
	serverpool_addrs_free(P->A);
err1:
	free(P);
err0:
	return (NULL);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %r15
pushq $0x38
popq %rdi
callq 0x2220
testq %rax, %rax
je 0x4ed7
movq %rax, %rbx
pushq $0x20
popq %rsi
xorl %edi, %edi
callq 0x5488
movq %rax, (%rbx)
testq %rax, %rax
je 0x4ecf
movq %rax, %r12
movq %r14, 0x28(%rbx)
andq $0x0, 0x30(%rbx)
movq %r15, %rdi
callq 0x3a24
testq %rax, %rax
je 0x4ec7
movq %rax, %r14
movq %rax, %r12
movq (%r12), %rsi
testq %rsi, %rsi
je 0x4cad
movq %rbx, %rdi
callq 0x4fd8
addq $0x8, %r12
testl %eax, %eax
je 0x4c8f
jmp 0x4e93
movq $0x1, 0x30(%rbx)
pushq $0x1
popq %rdi
leaq 0x8(%rsp), %rcx
movl %edi, %esi
xorl %edx, %edx
callq 0x21f0
testl %eax, %eax
je 0x4ce7
callq 0x2090
cmpl $0x0, (%rax)
je 0x4d89
movq %rax, %r15
leaq 0x2a15(%rip), %rdi      # 0x76f7
jmp 0x4e78
movl 0x8(%rsp), %edi
pushq $0x4
popq %rsi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x20f0
cmpl $-0x1, %eax
je 0x4d95
movl 0x8(%rsp), %r13d
movl 0xc(%rsp), %r12d
callq 0x2330
testl %eax, %eax
je 0x4eeb
movl %eax, 0xc(%rbx)
cmpl $-0x1, %eax
je 0x4e1b
movl 0x8(%rsp), %edi
movl %edi, 0x8(%rbx)
andq $0x0, 0x18(%rbx)
movq %rbx, %rsi
addq $0x10, %rsi
leaq 0x362(%rip), %r8        # 0x50a2
pushq $0x8
popq %rcx
movq %rcx, %rdx
movq %rbx, %r9
callq 0x5948
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x4db6
movl 0xc(%rsp), %edi
callq 0x2180
testl %eax, %eax
je 0x4df5
callq 0x2090
cmpl $0x0, (%rax)
je 0x4de7
movq %rax, %r15
leaq 0x27af(%rip), %rdi      # 0x752b
xorl %eax, %eax
callq 0x4919
andl $0x0, (%r15)
jmp 0x4df5
leaq 0x2967(%rip), %rdi      # 0x76f7
jmp 0x4e8c
callq 0x2090
cmpl $0x0, (%rax)
je 0x4dd7
movq %rax, %r15
leaq 0x2959(%rip), %rdi      # 0x7702
xorl %eax, %eax
callq 0x4919
andl $0x0, (%r15)
jmp 0x4e1b
callq 0x2090
cmpl $0x0, (%rax)
je 0x4e02
movq %rax, %r15
leaq 0x295c(%rip), %rdi      # 0x7726
xorl %eax, %eax
callq 0x4919
andl $0x0, (%r15)
jmp 0x4e10
leaq 0x2924(%rip), %rdi      # 0x7702
xorl %eax, %eax
callq 0x4aa8
jmp 0x4e1b
leaq 0x273d(%rip), %rdi      # 0x752b
xorl %eax, %eax
callq 0x4aa8
movq %r14, %rdi
callq 0x4023
jmp 0x4ed9
leaq 0x291d(%rip), %rdi      # 0x7726
xorl %eax, %eax
callq 0x4aa8
movl 0xc(%rbx), %edi
pushq $0xf
popq %rsi
callq 0x2210
movl 0xc(%rsp), %edi
callq 0x2180
testl %eax, %eax
je 0x4e57
callq 0x2090
cmpl $0x0, (%rax)
je 0x4e49
movq %rax, %r15
leaq 0x26ef(%rip), %rdi      # 0x752b
xorl %eax, %eax
callq 0x4919
andl $0x0, (%r15)
jmp 0x4e57
leaq 0x26db(%rip), %rdi      # 0x752b
xorl %eax, %eax
callq 0x4aa8
movl 0x8(%rsp), %edi
callq 0x2180
testl %eax, %eax
je 0x4e93
callq 0x2090
cmpl $0x0, (%rax)
je 0x4e85
movq %rax, %r15
leaq 0x26b3(%rip), %rdi      # 0x752b
xorl %eax, %eax
callq 0x4919
andl $0x0, (%r15)
jmp 0x4e93
leaq 0x269f(%rip), %rdi      # 0x752b
xorl %eax, %eax
callq 0x4aa8
movq %r14, %rdi
callq 0x4023
movq (%rbx), %rdi
callq 0x51b0
movq %rax, %r14
decq %r14
movq (%rbx), %r12
cmpq $-0x1, %r14
je 0x4ec7
movq %r12, %rdi
movq %r14, %rsi
callq 0x51b8
movq (%rax), %rdi
callq 0x4007
jmp 0x4ea6
movq %r12, %rdi
callq 0x551f
movq %rbx, %rdi
callq 0x2040
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r13d, %edi
callq 0x2180
testl %eax, %eax
je 0x4f25
callq 0x2090
cmpl $0x0, (%rax)
jne 0x4f11
leaq 0x2623(%rip), %rdi      # 0x752b
xorl %eax, %eax
callq 0x4aa8
jmp 0x4f25
movq %rax, %rbx
leaq 0x2610(%rip), %rdi      # 0x752b
xorl %eax, %eax
callq 0x4919
andl $0x0, (%rbx)
callq 0x21a0
leaq 0x18(%rsp), %rbx
pushq $0x8
popq %r14
movq %r15, %rdi
callq 0x3a24
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x4fc4
movq 0x10(%rsp), %r13
movq (%r13), %rdi
testq %rdi, %rdi
je 0x4fa0
leaq 0x20(%rsp), %rsi
movq %rbx, %rdx
callq 0x45e9
testl %eax, %eax
jne 0x4fd0
movl %r12d, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x5d94
cmpq $-0x1, %rax
je 0x4fd0
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
movl %r12d, %edi
callq 0x5d94
cmpq $-0x1, %rax
je 0x4fd0
movq 0x20(%rsp), %rdi
callq 0x2040
addq $0x8, %r13
jmp 0x4f4a
movq 0x10(%rsp), %rdi
callq 0x4023
andq $0x0, 0x18(%rsp)
movl %r12d, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x5d94
cmpq $-0x1, %rax
je 0x4fd0
movl %ebp, %edi
callq 0x2320
jmp 0x4f33
pushq $0x1
popq %rdi
callq 0x20a0