// File: /workspace/github_c_makefile_star2_O3/Tarsnap[P]kivaloo/dynamodb-kv/capacity.c
// Function: callback_readmetadata
static int
callback_readmetadata(void * cookie, struct http_response * res)
{
	struct capacity_reader * M = cookie;
	const uint8_t * buf;
	const uint8_t * end;
	const uint8_t * capstr;
	long capr, capw;
	char * ddberr = NULL;

	/* This request is no longer in progress. */
	M->http_cookie = NULL;

	/* Don't need these any more. */
	sock_addr_free(M->addrs[0]);
	M->addrs[0] = NULL;
	free(M->ddbreq);
	M->ddbreq = NULL;

	/* If we got an HTTP 400 response, warn and bail. */
	if ((res != NULL) && (res->status == 400)) {
		if (dynamodb_request_extracterror(res, &ddberr)) {
			/* Failed to parse. */
			warn0("dynamodb_request_extracterror failed");
			goto err0;
		} else {
			/* Parsed ok; show error and bail. */
			warn0("DescribeTable failed: %s", ddberr);
			free(ddberr);
			goto err0;
		}
	}

	/* If we have a response, pull data out of it. */
	if ((res != NULL) && (res->body != NULL)) {
		/*
		 * Overwrite the last byte of the JSON response with a NUL;
		 * this allows us to use string functions, and it's safe
		 * since we own the buffer now anyway.
		 */
		if (res->bodylen == 0)
			goto doneparse;
		res->body[res->bodylen - 1] = '\0';

		/* Look for Table->BillingModeSummary->BillingMode. */
		buf = res->body;
		end = &buf[res->bodylen];
		buf = json_find(buf, end, "Table");
		buf = json_find(buf, end, "BillingModeSummary");
		buf = json_find(buf, end, "BillingMode");

		/*
		 * The table has unlimited capacity if we found the string
		 * "PAY_PER_REQUEST"; if we didn't find anything (possible if
		 * the table has always been in provisioned mode, since for
		 * backwards compatibility DynamoDB doesn't always fill in
		 * this field) or we found "PROVISIONED" then we have limited
		 * capacity.
		 */
		if ((&buf[17] <= end) &&
		    (memcmp(buf, "\"PAY_PER_REQUEST\"", 17) == 0)) {
			dynamodb_request_queue_setcapacity(M->QR, 0);
			dynamodb_request_queue_setcapacity(M->QW, 0);
			goto doneparse;
		}

		/* Get Table->ProvisionedThroughput. */
		buf = res->body;
		end = &buf[res->bodylen];
		buf = json_find(buf, end, "Table");
		buf = json_find(buf, end, "ProvisionedThroughput");

		/* Get ReadCapacityUnits and WriteCapacityUnits. */
		capstr = json_find(buf, end, "ReadCapacityUnits");
		if (PARSENUM_EX(&capr, (const char *)capstr, 0, LONG_MAX,
		    10, 1)) {
			warnp("parsenum failed on %s", capstr);
			goto doneparse;
		}
		capstr = json_find(buf, end, "WriteCapacityUnits");
		if (PARSENUM_EX(&capw, (const char *)capstr, 0, LONG_MAX,
		    10, 1)) {
			warnp("parsenum failed on %s", capstr);
			goto doneparse;
		}

		/* Set new capacities. */
		dynamodb_request_queue_setcapacity(M->QR, capr);
		dynamodb_request_queue_setcapacity(M->QW, capw);

doneparse:
		/* Free the response body. */
		free(res->body);

		/* We have fetched capacity parameters. */
		M->done = 1;
	}

	/*
	 * Perform another read 15 seconds from now -- unless we haven't
	 * succeeded yet, in which case wait only 1 second.
	 */
	if ((M->timer_cookie = events_timer_register_double(callback_timer,
	    M, M->done ? 15.0 : 1.0)) == NULL)
		goto err0;

	/* Success! */
	return (0);

err0:
	/* Failure! */
	return (-1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r15
movq %rdi, %r14
xorl %ebx, %ebx
movq %rbx, 0x8(%rsp)
movq %rbx, 0x50(%rdi)
movq 0x40(%rdi), %rdi
callq 0xafaa
movq %rbx, 0x40(%r14)
movq 0x38(%r14), %rdi
callq 0x3460
movq %rbx, 0x38(%r14)
testq %r15, %r15
je 0x4c4f
cmpl $0x190, (%r15)          # imm = 0x190
jne 0x4a24
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
callq 0xc45c
testl %eax, %eax
je 0x4bcc
leaq 0xf944(%rip), %rdi      # 0x14351
xorl %eax, %eax
callq 0xba57
callq 0x3040
movl $0x0, (%rax)
jmp 0x4c7d
movq 0x20(%r15), %rax
testq %rax, %rax
je 0x4c4f
movq 0x18(%r15), %rcx
testq %rcx, %rcx
je 0x4c3e
movb $0x0, -0x1(%rax,%rcx)
movq 0x20(%r15), %rdi
movq 0x18(%r15), %r12
addq %rdi, %r12
leaq 0xf8f6(%rip), %rdx      # 0x1434b
movq %r12, %rsi
callq 0xa0e4
leaq 0xf92b(%rip), %rdx      # 0x1438f
movq %rax, %rdi
movq %r12, %rsi
callq 0xa0e4
leaq 0xf92c(%rip), %rdx      # 0x143a2
movq %rax, %rdi
movq %r12, %rsi
callq 0xa0e4
leaq 0x11(%rax), %rcx
cmpq %r12, %rcx
ja 0x4ab9
movdqu (%rax), %xmm0
movzbl 0x10(%rax), %eax
movd %eax, %xmm1
pcmpeqb 0xf862(%rip), %xmm0     # 0x14300
pcmpeqb 0xf86a(%rip), %xmm1     # 0x14310
pand %xmm0, %xmm1
pmovmskb %xmm1, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x4c28
movq 0x20(%r15), %rdi
movq 0x18(%r15), %r13
addq %rdi, %r13
leaq 0xf880(%rip), %rdx      # 0x1434b
movq %r13, %rsi
callq 0xa0e4
leaq 0xf8e6(%rip), %rdx      # 0x143c0
movq %rax, %rdi
movq %r13, %rsi
callq 0xa0e4
leaq 0xf8ea(%rip), %rdx      # 0x143d6
movq %rax, 0x18(%rsp)
movq %rax, %rdi
movq %r13, %rsi
callq 0xa0e4
movq %rax, %rbp
callq 0x3040
movq %rax, (%rsp)
movl $0x0, (%rax)
testq %rbp, %rbp
je 0x4cb6
leaq 0x20(%rsp), %r12
movq %rbp, %rdi
movq %r12, %rsi
movl $0xa, %edx
callq 0x33d0
cmpq %rbp, (%r12)
je 0x4bf9
testq %rax, %rax
movq (%rsp), %r12
js 0x4c04
cmpl $0x0, (%r12)
jne 0x4c0d
movq %rax, 0x10(%rsp)
leaq 0xf8a4(%rip), %rdx      # 0x143fe
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0xa0e4
movl $0x0, (%r12)
testq %rax, %rax
je 0x4cb6
movq %rax, %r13
leaq 0x20(%rsp), %r12
movq %rax, %rdi
movq %r12, %rsi
movl $0xa, %edx
callq 0x33d0
cmpq %r13, (%r12)
je 0x4c93
movq %rax, %rbp
testq %rax, %rax
movq (%rsp), %r12
js 0x4c9e
cmpl $0x0, (%r12)
jne 0x4ca7
movq 0x30(%r14), %rdi
movq 0x10(%rsp), %rsi
callq 0xc889
movq 0x28(%r14), %rdi
movq %rbp, %rsi
jmp 0x4c39
movq 0x8(%rsp), %rsi
leaq 0xf79e(%rip), %rdi      # 0x14376
xorl %eax, %eax
callq 0xba57
callq 0x3040
movl $0x0, (%rax)
movq 0x8(%rsp), %rdi
callq 0x3460
jmp 0x4c7d
movl $0x16, %eax
movq (%rsp), %r12
jmp 0x4c09
movl $0x22, %eax
movl %eax, (%r12)
leaq 0xf7d4(%rip), %rdi      # 0x143e8
movq %rbp, %rsi
xorl %eax, %eax
callq 0xb938
movl $0x0, (%r12)
jmp 0x4c3e
movq 0x30(%r14), %rdi
xorl %esi, %esi
callq 0xc889
movq 0x28(%r14), %rdi
xorl %esi, %esi
callq 0xc889
movq 0x20(%r15), %rdi
callq 0x3460
movl $0x1, 0x60(%r14)
xorl %eax, %eax
cmpl $0x0, 0x60(%r14)
sete %al
leaq 0xf6c0(%rip), %rcx      # 0x14320
movsd (%rcx,%rax,8), %xmm0
leaq 0x69(%rip), %rdi        # 0x4cd5
movq %r14, %rsi
callq 0x6b05
movq %rax, 0x58(%r14)
testq %rax, %rax
jne 0x4c82
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x16, %eax
movq (%rsp), %r12
jmp 0x4ca3
movl $0x22, %eax
movl %eax, (%r12)
leaq 0xf73a(%rip), %rdi      # 0x143e8
movq %r13, %rsi
jmp 0x4c17
leaq 0x10889(%rip), %rdi     # 0x15546
leaq 0xf74d(%rip), %rsi      # 0x14411
leaq 0xf765(%rip), %rcx      # 0x14430
movl $0xa7, %edx
callq 0x3210