// File: /workspace/github_c_makefile_star2_O3/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 0x38b9
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), %rcx
shrq $0x2, %rcx
movq %rcx, 0x58(%rbx)
movq %rax, 0x60(%rbx)
movq %rax, 0x70(%rbx)
movq %rcx, 0x78(%rbx)
movl %eax, 0x80(%rbx)
movsd (%rsp), %xmm1
cvttsd2si %xmm1, %rdx
movupd %xmm0, 0x88(%rbx)
movl %eax, 0x98(%rbx)
movq %rdx, 0xa0(%rbx)
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
subsd %xmm0, %xmm1
mulsd 0x10be6(%rip), %xmm1    # 0x143c8
cvttsd2si %xmm1, %rax
movq %rax, 0xa8(%rbx)
movq %r14, 0xb0(%rbx)
movq (%r15), %rsi
movl $0xffffffef, %eax       # imm = 0xFFFFFFEF
xorl %edx, %edx
divq %rsi
cmpq %rax, %rcx
setbe %dil
cmovaeq %rax, %rcx
movq $-0x25, %rax
xorl %edx, %edx
divq %rsi
cmpq %rax, %rcx
setbe %dl
testb %dl, %dil
jne 0x382f
cmpq %rax, %rcx
cmovbq %rcx, %rax
movq %rax, 0x78(%rbx)
movl $0x0, 0xc0(%rbx)
leaq 0x8d(%rip), %rdi        # 0x38cd
leaq 0x10b99(%rip), %rdx     # 0x143e0
movq %rbx, %rsi
callq 0xc618
movq %rax, 0xb8(%rbx)
testq %rax, %rax
je 0x387f
leaq 0xc7(%rip), %rsi        # 0x3929
movl %ebp, %edi
movq %rbx, %rdx
callq 0xd184
testq %rax, %rax
jne 0x38bb
movq 0xb8(%rbx), %rdi
callq 0xc792
jmp 0x38b1
callq 0x20a0
cmpl $0x0, (%rax)
je 0x38a3
movq %rax, %r14
leaq 0x10b5d(%rip), %rdi     # 0x143f0
xorl %eax, %eax
callq 0xffd0
movl $0x0, (%r14)
jmp 0x38b1
leaq 0x10b46(%rip), %rdi     # 0x143f0
xorl %eax, %eax
callq 0x100ef
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