// File: /workspace/github_c_makefile_star2_O1/Tarsnap[P]kivaloo/lbs/dispatch.c
// Function: dispatch_init
struct dispatch_state *
dispatch_init(struct storage_state * S, size_t blocklen, size_t nreaders)
{
	struct dispatch_state * D;
	size_t nworkers;
	size_t i;

	/* Bake a cookie. */
	if ((D = malloc(sizeof(struct dispatch_state))) == NULL)
		goto err0;
	D->nreaders = nreaders;
	D->writer_busy = D->deleter_busy = 0;
	D->blocklen = blocklen;
	D->sstate = S;

	/* All the readers will be idle when we create them. */
	if (IMALLOC(D->readers_idle, D->nreaders, size_t))
		goto err1;
	D->nreaders_idle = D->nreaders;
	for (i = 0; i < D->nreaders; i++)
		D->readers_idle[i] = i;

	/* Create a socket pair for sending work completion messages. */
	if (socketpair(AF_UNIX, SOCK_STREAM, 0, D->spair)) {
		warnp("socketpair");
		goto err2;
	}

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

	/* Read work completion messages from the socket. */
	if ((D->wakeup_cookie = network_read(D->spair[0],
	    (uint8_t *)&D->wakeupID, sizeof(size_t), sizeof(size_t),
	    workdone, D)) == NULL) {
		warnp("Error reading thread ID from socket");
		goto err3;
	}

	/* Create worker threads. */
	nworkers = D->nreaders + 2;
	if (IMALLOC(D->workers, nworkers, struct workctl *)) {
		warnp("malloc");
		goto err4;
	}
	for (i = 0; i < nworkers; i++)
		D->workers[i] = NULL;
	for (i = 0; i < nworkers; i++) {
		if ((D->workers[i] =
		    worker_create(i, S, D->spair[1])) == NULL) {
			warnp("Cannot create worker thread");
			goto err5;
		}
	}

	/* Success! */
	return (D);

err5:
	for (i = 0; i < nworkers; i++) {
		if (D->workers[i] == NULL)
			continue;
		worker_kill(D->workers[i]);
	}
	free(D->workers);
err4:
	network_read_cancel(D->wakeup_cookie);
err3:
	if (close(D->spair[1]))
		warnp("close");
	if (close(D->spair[0]))
		warnp("close");
err2:
	free(D->readers_idle);
err1:
	free(D);
err0:
	/* Failure! */
	return (NULL);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
movl $0x88, %edi
callq 0x2380
testq %rax, %rax
je 0x39ed
movq %rax, %rbx
movq %r14, 0x8(%rax)
movq $0x0, 0x10(%rax)
movq %r12, 0x28(%rax)
movq %r15, 0x30(%rax)
testq %r14, %r14
je 0x3711
movq %r14, %rax
shrq $0x3d, %rax
je 0x3715
callq 0x20a0
movl $0xc, (%rax)
xorl %eax, %eax
jmp 0x3722
leaq (,%r14,8), %rdi
callq 0x2380
testq %r14, %r14
setne %cl
movq %rax, 0x20(%rbx)
testq %rax, %rax
sete %dl
testb %dl, %cl
jne 0x39e5
movq %r14, 0x18(%rbx)
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0x3755
xorl %edx, %edx
movq %rdx, (%rax,%rdx,8)
incq %rdx
cmpq %rdx, %rcx
jne 0x3749
leaq 0x38(%rbx), %r14
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
movq %r14, %rcx
callq 0x2320
testl %eax, %eax
je 0x378a
callq 0x20a0
cmpl $0x0, (%rax)
je 0x37f1
movq %rax, %r14
leaq 0x9c4f(%rip), %rdi      # 0xd3d4
jmp 0x39be
movl (%r14), %edi
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x2120
cmpl $-0x1, %eax
je 0x37fd
movl 0x38(%rbx), %edi
movq %rbx, %rsi
addq $0x40, %rsi
leaq 0x248(%rip), %r8        # 0x39fc
movl $0x8, %edx
movl $0x8, %ecx
movq %rbx, %r9
callq 0x80f0
movq %rax, 0x48(%rbx)
testq %rax, %rax
je 0x3813
movq 0x8(%rbx), %r13
addq $0x2, %r13
je 0x37ed
movq %r13, %rax
shrq $0x3d, %rax
je 0x384a
callq 0x20a0
movl $0xc, (%rax)
xorl %eax, %eax
jmp 0x3857
leaq 0x9bdc(%rip), %rdi      # 0xd3d4
jmp 0x39d5
callq 0x20a0
cmpl $0x0, (%rax)
je 0x383e
movq %rax, %r15
leaq 0x9bce(%rip), %rdi      # 0xd3df
jmp 0x382b
callq 0x20a0
cmpl $0x0, (%rax)
je 0x38d9
movq %rax, %r15
leaq 0x9bdb(%rip), %rdi      # 0xd406
xorl %eax, %eax
callq 0xac18
movl $0x0, (%r15)
jmp 0x3960
leaq 0x9b9a(%rip), %rdi      # 0xd3df
jmp 0x38e0
leaq (,%r13,8), %rdi
callq 0x2380
movq %rax, (%rbx)
testq %rax, %rax
sete %al
testq %r13, %r13
setne %cl
testb %al, %cl
je 0x3891
callq 0x20a0
cmpl $0x0, (%rax)
je 0x38e9
movq %rax, %r15
leaq 0x9bac(%rip), %rdi      # 0xd42a
xorl %eax, %eax
callq 0xac18
movl $0x0, (%r15)
jmp 0x3957
testq %r13, %r13
je 0x39ef
xorl %eax, %eax
movq (%rbx), %rcx
movq $0x0, (%rcx,%rax,8)
incq %rax
cmpq %rax, %r13
jne 0x389c
xorl %r12d, %r12d
movl 0x3c(%rbx), %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x4550
movq (%rbx), %rcx
movq %rax, (%rcx,%r12,8)
testq %rax, %rax
je 0x38f9
incq %r12
cmpq %r12, %r13
jne 0x38b2
jmp 0x39ef
leaq 0x9b26(%rip), %rdi      # 0xd406
xorl %eax, %eax
callq 0xad37
jmp 0x3960
leaq 0x9b3a(%rip), %rdi      # 0xd42a
xorl %eax, %eax
callq 0xad37
jmp 0x3957
callq 0x20a0
cmpl $0x0, (%rax)
je 0x391d
movq %rax, %r15
leaq 0x9b24(%rip), %rdi      # 0xd431
xorl %eax, %eax
callq 0xac18
movl $0x0, (%r15)
jmp 0x392b
leaq 0x9b0d(%rip), %rdi      # 0xd431
xorl %eax, %eax
callq 0xad37
cmpq $0x1, %r13
adcq $0x0, %r13
xorl %r15d, %r15d
movq (%rbx), %rax
movq (%rax,%r15,8), %rdi
testq %rdi, %rdi
je 0x3947
callq 0x4b08
incq %r15
cmpq %r15, %r13
jne 0x3936
movq (%rbx), %rdi
callq 0x2040
movq 0x48(%rbx), %rdi
callq 0x839b
movl 0x3c(%rbx), %edi
callq 0x2240
testl %eax, %eax
je 0x399e
callq 0x20a0
cmpl $0x0, (%rax)
je 0x3990
movq %rax, %r15
leaq 0xab1c(%rip), %rdi      # 0xe49c
xorl %eax, %eax
callq 0xac18
movl $0x0, (%r15)
jmp 0x399e
leaq 0xab05(%rip), %rdi      # 0xe49c
xorl %eax, %eax
callq 0xad37
movl (%r14), %edi
callq 0x2240
testl %eax, %eax
je 0x39dc
callq 0x20a0
cmpl $0x0, (%rax)
je 0x39ce
movq %rax, %r14
leaq 0xaade(%rip), %rdi      # 0xe49c
xorl %eax, %eax
callq 0xac18
movl $0x0, (%r14)
jmp 0x39dc
leaq 0xaac7(%rip), %rdi      # 0xe49c
xorl %eax, %eax
callq 0xad37
movq 0x20(%rbx), %rdi
callq 0x2040
movq %rbx, %rdi
callq 0x2040
xorl %ebx, %ebx
movq %rbx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq