// File: /workspace/github_c_makefile_star2_O3/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 0x39e8
movq %rax, %rbx
movq %r14, 0x8(%rax)
movq $0x0, 0x10(%rax)
movq %r12, 0x28(%rax)
movq %r15, 0x30(%rax)
testq %r14, %r14
je 0x3694
movq %r14, %rax
shrq $0x3d, %rax
je 0x36a9
callq 0x20a0
movl $0xc, (%rax)
jmp 0x39e0
leaq 0x20(%rbx), %r13
movq $0x0, 0x20(%rbx)
movq %r14, 0x18(%rbx)
jmp 0x3766
leaq (,%r14,8), %rdi
callq 0x2380
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x39e0
leaq 0x20(%rbx), %r13
movq %r14, 0x18(%rbx)
leaq 0x1(%r14), %rdx
movabsq $0x3ffffffffffffffe, %rcx # imm = 0x3FFFFFFFFFFFFFFE
andq %rdx, %rcx
decq %r14
movq %r14, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x9c1f(%rip), %xmm1     # 0xd310
xorl %edx, %edx
movdqa 0x9c25(%rip), %xmm2     # 0xd320
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x9c25(%rip), %xmm4     # 0xd330
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %esi
notl %esi
testb $0x1, %sil
je 0x3741
movq %rdx, (%rax,%rdx,8)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %esi
testb $0x1, %sil
je 0x3759
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%rax,%rdx,8)
addq $0x2, %rdx
paddq %xmm4, %xmm1
cmpq %rdx, %rcx
jne 0x370b
leaq 0x38(%rbx), %r14
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
movq %r14, %rcx
callq 0x2320
testl %eax, %eax
je 0x379b
callq 0x20a0
cmpl $0x0, (%rax)
je 0x3802
movq %rax, %r14
leaq 0x9bbe(%rip), %rdi      # 0xd354
jmp 0x39b9
movl (%r14), %edi
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x2120
cmpl $-0x1, %eax
je 0x380e
movl 0x38(%rbx), %edi
movq %rbx, %rsi
addq $0x40, %rsi
leaq 0x237(%rip), %r8        # 0x39fc
movl $0x8, %edx
movl $0x8, %ecx
movq %rbx, %r9
callq 0x8124
movq %rax, 0x48(%rbx)
testq %rax, %rax
je 0x3824
movq 0x8(%rbx), %rbp
addq $0x2, %rbp
je 0x384f
movq %rbp, %rax
shrq $0x3d, %rax
je 0x3864
callq 0x20a0
movl $0xc, (%rax)
xorl %eax, %eax
jmp 0x3871
leaq 0x9b4b(%rip), %rdi      # 0xd354
jmp 0x39d0
callq 0x20a0
cmpl $0x0, (%rax)
je 0x385b
movq %rax, %r15
leaq 0x9b3d(%rip), %rdi      # 0xd35f
jmp 0x383c
callq 0x20a0
cmpl $0x0, (%rax)
je 0x38dc
movq %rax, %r15
leaq 0x9b4a(%rip), %rdi      # 0xd386
xorl %eax, %eax
callq 0xacd8
movl $0x0, (%r15)
jmp 0x395b
movq $0x0, (%rbx)
jmp 0x39ea
leaq 0x9afd(%rip), %rdi      # 0xd35f
jmp 0x38e3
leaq (,%rbp,8), %rdi
callq 0x2380
movq %rax, (%rbx)
testq %rax, %rax
je 0x38b8
xorl %eax, %eax
movq (%rbx), %rcx
movq $0x0, (%rcx,%rax,8)
incq %rax
cmpq %rax, %rbp
jne 0x387b
xorl %r12d, %r12d
movl 0x3c(%rbx), %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x456c
movq (%rbx), %rcx
movq %rax, (%rcx,%r12,8)
testq %rax, %rax
je 0x38ec
incq %r12
cmpq %r12, %rbp
jne 0x3891
jmp 0x39ea
callq 0x20a0
cmpl $0x0, (%rax)
je 0x3910
movq %rax, %r15
leaq 0x9ade(%rip), %rdi      # 0xd3aa
xorl %eax, %eax
callq 0xacd8
movl $0x0, (%r15)
jmp 0x3952
leaq 0x9aa3(%rip), %rdi      # 0xd386
xorl %eax, %eax
callq 0xadf7
jmp 0x395b
callq 0x20a0
cmpl $0x0, (%rax)
je 0x3920
movq %rax, %r15
leaq 0x9ab1(%rip), %rdi      # 0xd3b1
xorl %eax, %eax
callq 0xacd8
movl $0x0, (%r15)
jmp 0x392e
leaq 0x9a93(%rip), %rdi      # 0xd3aa
xorl %eax, %eax
callq 0xadf7
jmp 0x3952
leaq 0x9a8a(%rip), %rdi      # 0xd3b1
xorl %eax, %eax
callq 0xadf7
xorl %r15d, %r15d
movq (%rbx), %rax
movq (%rax,%r15,8), %rdi
testq %rdi, %rdi
je 0x3942
callq 0x4b24
incq %r15
cmpq %r15, %rbp
jne 0x3931
movq (%rbx), %rdi
callq 0x2040
movq 0x48(%rbx), %rdi
callq 0x83dc
movl 0x3c(%rbx), %edi
callq 0x2240
testl %eax, %eax
je 0x3999
callq 0x20a0
cmpl $0x0, (%rax)
je 0x398b
movq %rax, %r15
leaq 0xaab1(%rip), %rdi      # 0xe42c
xorl %eax, %eax
callq 0xacd8
movl $0x0, (%r15)
jmp 0x3999
leaq 0xaa9a(%rip), %rdi      # 0xe42c
xorl %eax, %eax
callq 0xadf7
movl (%r14), %edi
callq 0x2240
testl %eax, %eax
je 0x39d7
callq 0x20a0
cmpl $0x0, (%rax)
je 0x39c9
movq %rax, %r14
leaq 0xaa73(%rip), %rdi      # 0xe42c
xorl %eax, %eax
callq 0xacd8
movl $0x0, (%r14)
jmp 0x39d7
leaq 0xaa5c(%rip), %rdi      # 0xe42c
xorl %eax, %eax
callq 0xadf7
movq (%r13), %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