// File: /workspace/github_c_makefile_star2_O1/Tarsnap[P]kivaloo/kvlds/dispatch.c
// Function: dispatch_accept
struct dispatch_state *
dispatch_accept(int s, struct btree * T,
    size_t kmax, size_t vmax, double w, size_t g)
{
	struct dispatch_state * D;

	/* Allocate space for dispatcher state. */
	if ((D = malloc(sizeof(struct dispatch_state))) == NULL)
		goto err0;

	/* Initialize dispatcher. */
	D->dying = 0;
	D->readq = NULL;
	D->read_cookie = NULL;
	D->T = T;
	D->kmax = kmax;
	D->vmax = vmax;
	D->nrequests = 0;
	D->nmr_head = NULL;
	D->nmr_ip = 0;
	D->nmr_concurrency = T->poolsz / 4;
	D->mr_head = NULL;
	D->mr_reqs = 0;
	D->mr_concurrency = T->poolsz / 4;
	D->mr_inprogress = 0;
	D->mr_qlen = 0;
	D->mr_timer = NULL;
	D->mr_timer_expired = 0;
	D->mr_timeout.tv_sec = (time_t)w;
	D->mr_timeout.tv_usec = (suseconds_t)((w - (double)D->mr_timeout.tv_sec)
	    * 1000000);
	D->mr_min_batch = g;

	/**
	 * Adjust maximum # of pages touched by MRs (if necessary).  In
	 * addition to the limit of T->poolsz / 4 (as calculated above), we
	 * have 2 more limits which arise from the wire and lbs protocols.
	 * - The record of a wire packet must fit into 4 bytes.  The
	 *   packet length is defined as:
	 *       len = 16 + nblks * blklen
	 *   and we must have:
	 *       len <= UINT32_MAX
	 * - The length of the wire packet (including header and trailer) must
	 *   fit into size_t.  Using the same definition of len, we must have:
	 *       len <= SIZE_MAX - 20
	 */
	if (D->mr_concurrency > ((UINT32_MAX - 16) / D->T->pagelen))
		D->mr_concurrency = (UINT32_MAX - 16) / D->T->pagelen;
	if (D->mr_concurrency > ((SIZE_MAX - 16 - 20) / D->T->pagelen))
		D->mr_concurrency = (SIZE_MAX - 16 - 20) / D->T->pagelen;

	/* Start the periodic cleaning timer. */
	D->docleans = 0;
	if ((D->mrc_timer = events_timer_register(callback_mrc_timer, D,
	    &fivesec)) == NULL) {
		warnp("events_timer_register");
		goto err1;
	}

	/* Accept a connection. */
	if (network_accept(s, callback_accept, D) == NULL)
		goto err2;

	/* Success! */
	return (D);

err2:
	events_timer_cancel(D->mrc_timer);
err1:
	free(D);
err0:
	/* Failure! */
	return (NULL);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %r8, %r14
movsd %xmm0, (%rsp)
movq %rcx, %r12
movq %rdx, %r13
movq %rsi, %r15
movl %edi, %ebp
movl $0xc8, %edi
callq 0x22a0
movq %rax, %rbx
xorl %eax, %eax
testq %rbx, %rbx
je 0x390e
movl %eax, (%rbx)
movq %rax, 0x8(%rbx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x18(%rbx)
movq %r15, 0x28(%rbx)
movq %r13, 0x30(%rbx)
movq %r12, 0x38(%rbx)
movq %rax, 0x40(%rbx)
movq %rax, 0x50(%rbx)
movq 0x8(%r15), %rsi
shrq $0x2, %rsi
movq %rsi, 0x58(%rbx)
movq %rax, 0x60(%rbx)
movq %rax, 0x70(%rbx)
movq %rsi, 0x78(%rbx)
movl %eax, 0x80(%rbx)
movsd (%rsp), %xmm1
cvttsd2si %xmm1, %rcx
movupd %xmm0, 0x88(%rbx)
movl %eax, 0x98(%rbx)
movq %rcx, 0xa0(%rbx)
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
subsd %xmm0, %xmm1
mulsd 0x10bde(%rip), %xmm1    # 0x14420
cvttsd2si %xmm1, %rax
movq %rax, 0xa8(%rbx)
movq %r14, 0xb0(%rbx)
movq (%r15), %rcx
movl $0xffffffef, %eax       # imm = 0xFFFFFFEF
xorl %edx, %edx
divq %rcx
cmpq %rax, %rsi
jbe 0x386b
movq %rax, 0x78(%rbx)
movq 0x78(%rbx), %rsi
movq $-0x25, %rax
xorl %edx, %edx
divq %rcx
cmpq %rax, %rsi
jbe 0x3884
movq %rax, 0x78(%rbx)
movl $0x0, 0xc0(%rbx)
leaq 0x8d(%rip), %rdi        # 0x3922
leaq 0x10ba4(%rip), %rdx     # 0x14440
movq %rbx, %rsi
callq 0xc738
movq %rax, 0xb8(%rbx)
testq %rax, %rax
je 0x38d4
leaq 0xc7(%rip), %rsi        # 0x397e
movl %ebp, %edi
movq %rbx, %rdx
callq 0xd2b8
testq %rax, %rax
jne 0x3910
movq 0xb8(%rbx), %rdi
callq 0xc8b1
jmp 0x3906
callq 0x20a0
cmpl $0x0, (%rax)
je 0x38f8
movq %rax, %r14
leaq 0x10b68(%rip), %rdi     # 0x14450
xorl %eax, %eax
callq 0x10048
movl $0x0, (%r14)
jmp 0x3906
leaq 0x10b51(%rip), %rdi     # 0x14450
xorl %eax, %eax
callq 0x10167
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