// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl $0x88, %edi
callq 0x23a0
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
jne 0x389d
jmp 0x3caf
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x8, %esi
callq 0x3cd0
movq -0x28(%rbp), %rcx
movq %rax, 0x20(%rcx)
cmpq $0x0, %rax
jne 0x3907
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jbe 0x3907
jmp 0x3ca6
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x394f
movq -0x38(%rbp), %rdx
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x38(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x391f
movq -0x28(%rbp), %rcx
addq $0x38, %rcx
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x2340
cmpl $0x0, %eax
je 0x39a6
jmp 0x396c
callq 0x20a0
cmpl $0x0, (%rax)
je 0x3991
leaq 0xebaf(%rip), %rdi      # 0x1252c
movb $0x0, %al
callq 0xf070
callq 0x20a0
movl $0x0, (%rax)
jmp 0x399f
leaq 0xeb94(%rip), %rdi      # 0x1252c
movb $0x0, %al
callq 0xf2c0
jmp 0x39a1
jmp 0x3c99
movq -0x28(%rbp), %rax
movl 0x38(%rax), %edi
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
movb $0x0, %al
callq 0x2130
cmpl $-0x1, %eax
jne 0x39ff
jmp 0x39c5
callq 0x20a0
cmpl $0x0, (%rax)
je 0x39ea
leaq 0xeb61(%rip), %rdi      # 0x12537
movb $0x0, %al
callq 0xf070
callq 0x20a0
movl $0x0, (%rax)
jmp 0x39f8
leaq 0xeb46(%rip), %rdi      # 0x12537
movb $0x0, %al
callq 0xf2c0
jmp 0x39fa
jmp 0x3c03
movq -0x28(%rbp), %rax
movl 0x38(%rax), %edi
movq -0x28(%rbp), %rsi
addq $0x40, %rsi
movq -0x28(%rbp), %r9
movl $0x8, %ecx
leaq 0x352(%rip), %r8        # 0x3d70
movq %rcx, %rdx
callq 0xac80
movq -0x28(%rbp), %rcx
movq %rax, 0x48(%rcx)
cmpq $0x0, %rax
jne 0x3a70
jmp 0x3a36
callq 0x20a0
cmpl $0x0, (%rax)
je 0x3a5b
leaq 0xeb17(%rip), %rdi      # 0x1255e
movb $0x0, %al
callq 0xf070
callq 0x20a0
movl $0x0, (%rax)
jmp 0x3a69
leaq 0xeafc(%rip), %rdi      # 0x1255e
movb $0x0, %al
callq 0xf2c0
jmp 0x3a6b
jmp 0x3c03
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movl $0x8, %esi
callq 0x3cd0
movq -0x28(%rbp), %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0x3ade
cmpq $0x0, -0x30(%rbp)
jbe 0x3ade
jmp 0x3aa4
callq 0x20a0
cmpl $0x0, (%rax)
je 0x3ac9
leaq 0xeacd(%rip), %rdi      # 0x12582
movb $0x0, %al
callq 0xf070
callq 0x20a0
movl $0x0, (%rax)
jmp 0x3ad7
leaq 0xeab2(%rip), %rdi      # 0x12582
movb $0x0, %al
callq 0xf2c0
jmp 0x3ad9
jmp 0x3bf6
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x3b11
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x3ae6
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x3b95
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rax
movl 0x3c(%rax), %edx
callq 0x4ec0
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
movq -0x38(%rbp), %rdx
movq %rax, (%rcx,%rdx,8)
cmpq $0x0, %rax
jne 0x3b85
jmp 0x3b4e
callq 0x20a0
cmpl $0x0, (%rax)
je 0x3b73
leaq 0xea2a(%rip), %rdi      # 0x12589
movb $0x0, %al
callq 0xf070
callq 0x20a0
movl $0x0, (%rax)
jmp 0x3b81
leaq 0xea0f(%rip), %rdi      # 0x12589
movb $0x0, %al
callq 0xf2c0
jmp 0x3b83
jmp 0x3ba2
jmp 0x3b87
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x3b19
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x3cb7
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x3bea
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x3bc8
jmp 0x3bdc
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x5710
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x3baa
movq -0x28(%rbp), %rax
movq (%rax), %rdi
callq 0x2040
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0xaf50
movq -0x28(%rbp), %rax
movl 0x3c(%rax), %edi
callq 0x2260
cmpl $0x0, %eax
je 0x3c4d
jmp 0x3c16
callq 0x20a0
cmpl $0x0, (%rax)
je 0x3c3b
leaq 0xf9fd(%rip), %rdi      # 0x13624
movb $0x0, %al
callq 0xf070
callq 0x20a0
movl $0x0, (%rax)
jmp 0x3c49
leaq 0xf9e2(%rip), %rdi      # 0x13624
movb $0x0, %al
callq 0xf2c0
jmp 0x3c4b
jmp 0x3c4d
movq -0x28(%rbp), %rax
movl 0x38(%rax), %edi
callq 0x2260
cmpl $0x0, %eax
je 0x3c97
jmp 0x3c60
callq 0x20a0
cmpl $0x0, (%rax)
je 0x3c85
leaq 0xf9b3(%rip), %rdi      # 0x13624
movb $0x0, %al
callq 0xf070
callq 0x20a0
movl $0x0, (%rax)
jmp 0x3c93
leaq 0xf998(%rip), %rdi      # 0x13624
movb $0x0, %al
callq 0xf2c0
jmp 0x3c95
jmp 0x3c97
jmp 0x3c99
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x2040
movq -0x28(%rbp), %rdi
callq 0x2040
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)