// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
movl $0x88, %edi
callq 0x2380
testq %rax, %rax
je 0x33c8
movq %rax, %rbx
movq %r14, 0x8(%rax)
andq $0x0, 0x10(%rax)
movq %r12, 0x28(%rax)
movq %r15, 0x30(%rax)
movq %r14, %rdi
callq 0x33dc
movq %rax, 0x20(%rbx)
testq %rax, %rax
setne %dl
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
sete %sil
orb %dl, %sil
cmpb $0x1, %sil
jne 0x33c0
movq %rcx, 0x18(%rbx)
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x3175
movq %rdx, (%rax,%rdx,8)
incq %rdx
jmp 0x3167
leaq 0x38(%rbx), %r14
pushq $0x1
popq %rdi
movl %edi, %esi
xorl %edx, %edx
movq %r14, %rcx
callq 0x2320
testl %eax, %eax
je 0x31a9
callq 0x20a0
cmpl $0x0, (%rax)
je 0x3232
movq %rax, %r14
leaq 0x91ec(%rip), %rdi      # 0xc390
jmp 0x339c
movl (%r14), %edi
pushq $0x4
popq %rsi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x2120
cmpl $-0x1, %eax
je 0x323e
movl 0x38(%rbx), %edi
movq %rbx, %rsi
addq $0x40, %rsi
leaq 0x235(%rip), %r8        # 0x3406
pushq $0x8
popq %rcx
movq %rcx, %rdx
movq %rbx, %r9
callq 0x75e8
movq %rax, 0x48(%rbx)
testq %rax, %rax
je 0x3258
movq 0x8(%rbx), %r12
addq $0x2, %r12
setne %bpl
movq %r12, %rdi
callq 0x33dc
movq %rax, (%rbx)
testq %rax, %rax
sete %al
testb %bpl, %al
je 0x3280
callq 0x20a0
cmpl $0x0, (%rax)
je 0x32f7
movq %rax, %r15
leaq 0x91c4(%rip), %rdi      # 0xc3e6
xorl %eax, %eax
callq 0x9d39
andl $0x0, (%r15)
jmp 0x3338
leaq 0x9157(%rip), %rdi      # 0xc390
jmp 0x33b0
callq 0x20a0
cmpl $0x0, (%rax)
je 0x32de
movq %rax, %r15
leaq 0x9145(%rip), %rdi      # 0xc39b
jmp 0x3270
callq 0x20a0
cmpl $0x0, (%rax)
je 0x32e7
movq %rax, %r15
leaq 0x9152(%rip), %rdi      # 0xc3c2
xorl %eax, %eax
callq 0x9d39
andl $0x0, (%r15)
jmp 0x3341
xorl %eax, %eax
cmpq %rax, %r12
je 0x3294
movq (%rbx), %rcx
andq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0x3282
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x33ca
movl 0x3c(%rbx), %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0x3f00
movq (%rbx), %rcx
movq %rax, (%rcx,%r13,8)
incq %r13
testq %rax, %rax
jne 0x3297
callq 0x20a0
cmpl $0x0, (%rax)
je 0x3307
movq %rax, %r15
leaq 0x911c(%rip), %rdi      # 0xc3ed
xorl %eax, %eax
callq 0x9d39
andl $0x0, (%r15)
jmp 0x3315
leaq 0x90b6(%rip), %rdi      # 0xc39b
jmp 0x32ee
leaq 0x90d4(%rip), %rdi      # 0xc3c2
xorl %eax, %eax
callq 0x9ec8
jmp 0x3341
leaq 0x90e8(%rip), %rdi      # 0xc3e6
xorl %eax, %eax
callq 0x9ec8
jmp 0x3338
leaq 0x90df(%rip), %rdi      # 0xc3ed
xorl %eax, %eax
callq 0x9ec8
xorl %r15d, %r15d
movq (%rbx), %rdi
cmpq %r15, %r12
je 0x3333
movq (%rdi,%r15,8), %rdi
testq %rdi, %rdi
je 0x332e
callq 0x4489
incq %r15
jmp 0x3318
callq 0x2040
movq 0x48(%rbx), %rdi
callq 0x78bb
movl 0x3c(%rbx), %edi
callq 0x2240
testl %eax, %eax
je 0x337c
callq 0x20a0
cmpl $0x0, (%rax)
je 0x336e
movq %rax, %r15
leaq 0xa10b(%rip), %rdi      # 0xd46c
xorl %eax, %eax
callq 0x9d39
andl $0x0, (%r15)
jmp 0x337c
leaq 0xa0f7(%rip), %rdi      # 0xd46c
xorl %eax, %eax
callq 0x9ec8
movl (%r14), %edi
callq 0x2240
testl %eax, %eax
je 0x33b7
callq 0x20a0
cmpl $0x0, (%rax)
je 0x33a9
movq %rax, %r14
leaq 0xa0d0(%rip), %rdi      # 0xd46c
xorl %eax, %eax
callq 0x9d39
andl $0x0, (%r14)
jmp 0x33b7
leaq 0xa0bc(%rip), %rdi      # 0xd46c
xorl %eax, %eax
callq 0x9ec8
movq 0x20(%rbx), %rdi
callq 0x2040
movq %rbx, %rdi
callq 0x2040
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq