// File: /workspace/github_c_makefile_star2_O3/Tarsnap[P]kivaloo/liball/../lib/dynamodb/dynamodb_request_queue.c
// Function: callback_reqdone
static int
callback_reqdone(void * cookie, struct http_response * res)
{
	struct request * R = cookie;
	struct dynamodb_request_queue * Q = R->Q;
	struct timeval t_end;
	double treq;
	int rc = 0;
	double capacity = 0.0;
	char * ddberr = NULL;

	/*
	 * If we have a response body, extract the number of capacity units
	 * used, and update our rolling average and current capacity.
	 */
	if ((res != NULL) && (res->bodylen > 0)) {
		if (extractcapacity(res, &capacity))
			rc = -1;
		if (capacity != 0.0) {
			Q->mu_capperreq +=
			    (capacity - Q->mu_capperreq) * 0.01;
			Q->bucket_cap -= capacity;
			if (Q->bucket_cap < 0.0)
				Q->bucket_cap = 0.0;
		}
	}

	/* If we got an HTTP 400 response, extract the DynamoDB error string. */
	if ((res != NULL) && (res->status == 400)) {
		if (dynamodb_request_extracterror(res, &ddberr))
			rc = -1;
	}

	/* Figure out how long this request took. */
	if (monoclock_get(&t_end)) {
		warnp("monoclock_get");
		t_end = R->t_start;
		rc = -1;
	}

	/* Optionally log this request. */
	if (Q->logfile) {
		if (logreq(Q->logfile, R, res, capacity, t_end, ddberr))
			rc = -1;
	}

	/*
	 * This HTTP request has completed; we call done_http here rather
	 * than earlier because it frees the target address, which is printed
	 * to the request log.
	 */
	if (done_http(R, 0))
		rc = -1;

	/* What should we do with this response? */
	if ((ddberr != NULL) &&
	    (strcmp(ddberr, "ProvisionedThroughputExceededException") == 0)) {
		/*
		 * We hit the throughput limits.  Zero out our estimate of
		 * the number of tokens in the bucket; we won't send any
		 * more requests until timer ticks add more tokens to the
		 * modelled bucket.
		 */
		Q->bucket_cap = 0.0;
	} else if ((res != NULL) && (res->status < 500)) {
		/*
		 * Anything which isn't an internal DynamoDB error or a
		 * rate limiting response is something we should pass back
		 * to the upstream code.
		 */

		/* Dequeue the request. */
		ptrheap_delete(Q->reqs, R->rc);

		/* Cancel the request timeout. */
		events_timer_cancel(R->timeout_cookie);

		/*
		 * Update request timeout statistics.  Following the strategy
		 * used in TCP, we compute exponential rolling averages for
		 * the mean and mean deviation; unlike TCP, we update our
		 * statistics even on retries, since we know which attempt
		 * succeeded.
		 */
		treq = timeval_diff(R->t_start, t_end);
		Q->tmu += (treq - Q->tmu) * 0.125;
		if (treq > Q->tmu)
			Q->tmud += ((treq - Q->tmu) - Q->tmud) * 0.25;
		else
			Q->tmud += ((Q->tmu - treq) - Q->tmud) * 0.25;

		/* Invoke the upstream callback. */
		if (rc) {
			(void)(R->callback)(R->cookie, res, ddberr);
		} else {
			rc = (R->callback)(R->cookie, res, ddberr);
		}

		/* Free the request; we're done with it now. */
		free(R->logstr);
		free(R);
	}

	/*
	 * If we didn't send the response upstream, the request is still on
	 * our queue with a timeout callback pending.  We're going to leave
	 * it that way -- we don't want to retry the failed request until
	 * the callback fires.
	 */

	/*
	 * Poke the queue.  If the request failed, it may be possible to
	 * re-issue it; if the request succeeded, we may have ceased to be
	 * at our in-flight limit and might be able to issue a new request;
	 * if we just hit our first congestion, we need to start a timer to
	 * add more tokens to our modelled bucket.
	 */
	if (poke(Q))
		rc = -1;

	/* Free the DynamoDB error string (if any). */
	free(ddberr);

	/* Return status from callback, or our own success/failure. */
	return (rc);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r15
movq %rdi, %r14
movq (%rdi), %r13
movq $0x0, 0x18(%rsp)
testq %rsi, %rsi
je 0x6b00
movq 0x18(%r15), %r12
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebp, %ebp
testq %r12, %r12
je 0x6b9e
movq %r13, 0x10(%rsp)
movq 0x20(%r15), %rdi
addq %rdi, %r12
leaq 0xad7e(%rip), %rdx      # 0x11810
movq %r12, %rsi
callq 0xb2e4
leaq 0xad80(%rip), %rdx      # 0x11821
movq %rax, %rdi
movq %r12, %rsi
callq 0xb2e4
cmpq %r12, %rax
je 0x6b99
movq %rax, %r13
jae 0x6b11
movq %r12, %rax
subq %r13, %rax
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
leaq 0xad61(%rip), %rbx      # 0x1182f
movzbl (%r13,%rbp), %esi
movl $0x10, %edx
movq %rbx, %rdi
callq 0x3400
testq %rax, %rax
je 0x6b13
leaq 0x1(,%rbp), %rax
addq %r13, %rax
incq %rbp
cmpq %r12, %rax
jb 0x6ace
movq 0x30(%rsp), %rbp
jmp 0x6b13
xorl %ebp, %ebp
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
jmp 0x6bbc
xorl %ebp, %ebp
leaq 0x1(%rbp), %rdi
callq 0x34c0
testq %rax, %rax
je 0x6b94
movq %rax, %r12
movq %rax, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x3280
movb $0x0, (%r12,%rbp)
callq 0x3040
movq %rax, %r13
movl $0x0, (%rax)
leaq 0x20(%rsp), %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3260
movq (%rbx), %rcx
movl $0x16, %eax
cmpq %r12, %rcx
je 0x6b6b
cmpb $0x0, (%rcx)
je 0x6db4
movl %eax, (%r13)
leaq 0xacc9(%rip), %rdi      # 0x1183f
xorl %ebp, %ebp
movq %r12, %rsi
xorl %eax, %eax
callq 0x58d7
movl $0x0, (%r13)
movq %r12, %rdi
callq 0x3450
jmp 0x6b99
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq 0x10(%rsp), %r13
cmpl $0x190, (%r15)          # imm = 0x190
jne 0x6bbc
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0xc87c
xorl %ecx, %ecx
negl %eax
sbbl %ecx, %ecx
orl %ecx, %ebp
leaq 0x20(%rsp), %rdi
callq 0x54fc
testl %eax, %eax
je 0x6c0c
callq 0x3040
cmpl $0x0, (%rax)
je 0x6bed
movq %rax, %rbx
leaq 0xabe2(%rip), %rdi      # 0x117c0
xorl %eax, %eax
callq 0x57b8
movl $0x0, (%rbx)
jmp 0x6bfb
leaq 0xabcc(%rip), %rdi      # 0x117c0
xorl %eax, %eax
callq 0x58d7
movupd 0x68(%r14), %xmm0
movapd %xmm0, 0x20(%rsp)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq 0x80(%r13), %rdi
testq %rdi, %rdi
je 0x6c40
movq 0x18(%rsp), %r9
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %r8
movq %r14, %rsi
movq %r15, %rdx
movsd 0x8(%rsp), %xmm0
callq 0x6f31
xorl %ecx, %ecx
negl %eax
sbbl %ecx, %ecx
orl %ecx, %ebp
movq (%r14), %rax
xorl %ebx, %ebx
movq %rbx, 0x50(%r14)
decq 0x68(%rax)
movq 0x40(%r14), %rdi
callq 0xbf6e
movq %rbx, 0x40(%r14)
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x6c78
leaq 0xab7e(%rip), %rsi      # 0x117e9
callq 0x3500
testl %eax, %eax
je 0x6d6b
testq %r15, %r15
je 0x6d73
cmpl $0x1f3, (%r15)          # imm = 0x1F3
jg 0x6d73
movq 0x70(%r13), %rdi
movq 0x88(%r14), %rsi
callq 0x86d3
movq 0x58(%r14), %rdi
callq 0x536e
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq 0x68(%r14), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
subq 0x70(%r14), %rcx
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
mulsd 0xa7f7(%rip), %xmm1     # 0x114c8
addsd %xmm0, %xmm1
movsd 0x88(%r13), %xmm0
movsd 0x90(%r13), %xmm2
movapd %xmm1, %xmm3
subsd %xmm0, %xmm3
mulsd 0xaab9(%rip), %xmm3     # 0x117b0
addsd %xmm0, %xmm3
movsd %xmm3, 0x88(%r13)
movapd %xmm3, %xmm0
subsd %xmm1, %xmm0
movapd %xmm1, %xmm4
subsd %xmm3, %xmm4
cmpltsd %xmm1, %xmm3
andpd %xmm3, %xmm4
andnpd %xmm0, %xmm3
orpd %xmm4, %xmm3
subsd %xmm2, %xmm3
mulsd 0xaa87(%rip), %xmm3     # 0x117b8
addsd %xmm2, %xmm3
movsd %xmm3, 0x90(%r13)
movq 0x38(%r14), %rdi
movq 0x18(%rsp), %rdx
movq %r15, %rsi
callq *0x30(%r14)
xorl %ebx, %ebx
negl %ebp
sbbl %ebx, %ebx
orl %eax, %ebx
movq 0x28(%r14), %rdi
callq 0x3450
movq %r14, %rdi
callq 0x3450
movl %ebx, %ebp
jmp 0x6d73
movq $0x0, 0x38(%r13)
cmpq $0x0, 0x60(%r13)
jne 0x6d99
leaq -0x69f(%rip), %rdi      # 0x66e2
movq %r13, %rsi
xorl %edx, %edx
callq 0x44ec
movq %rax, 0x60(%r13)
testq %rax, %rax
jne 0x6d99
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rdi
callq 0x3450
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
movl $0x22, %eax
ja 0x6b6b
ucomisd 0xa9d1(%rip), %xmm0     # 0x117a0
ja 0x6b6b
cmpl $0x0, (%r13)
jne 0x6b6f
movq %r12, %rdi
movsd %xmm0, 0x8(%rsp)
callq 0x3450
movsd 0x8(%rsp), %xmm4
xorl %ebp, %ebp
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm4
jne 0x6e02
jnp 0x6e50
movq 0x10(%rsp), %r13
movsd 0x20(%r13), %xmm1
movsd 0x38(%r13), %xmm2
movapd %xmm4, %xmm3
subsd %xmm1, %xmm3
mulsd 0xa985(%rip), %xmm3     # 0x117a8
addsd %xmm1, %xmm3
movsd %xmm3, 0x20(%r13)
subsd %xmm4, %xmm2
movsd %xmm2, 0x38(%r13)
ucomisd %xmm2, %xmm0
jbe 0x6e45
movq $0x0, 0x38(%r13)
movsd %xmm4, 0x8(%rsp)
jmp 0x6b9e
movsd %xmm4, 0x8(%rsp)
jmp 0x6b99