// File: /workspace/github_c_makefile_star2_O1/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 0xaed7
movq %rbx, 0x40(%r14)
movq 0x38(%r14), %rdi
callq 0x3460
movq %rbx, 0x38(%r14)
testq %r15, %r15
je 0x4c3d
cmpl $0x190, (%r15)          # imm = 0x190
jne 0x4a0e
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
callq 0xc520
testl %eax, %eax
je 0x4b2e
leaq 0xf95a(%rip), %rdi      # 0x14351
xorl %eax, %eax
callq 0xb9a3
callq 0x3040
movl $0x0, (%rax)
jmp 0x4c6b
movq 0x20(%r15), %rax
testq %rax, %rax
je 0x4c3d
movq 0x18(%r15), %rcx
testq %rcx, %rcx
je 0x4c2c
movb $0x0, -0x1(%rax,%rcx)
movq 0x20(%r15), %rdi
movq 0x18(%r15), %r12
addq %rdi, %r12
leaq 0xf90c(%rip), %rdx      # 0x1434b
movq %r12, %rsi
callq 0xa00c
leaq 0xf941(%rip), %rdx      # 0x1438f
movq %rax, %rdi
movq %r12, %rsi
callq 0xa00c
leaq 0xf942(%rip), %rdx      # 0x143a2
movq %rax, %rdi
movq %r12, %rsi
callq 0xa00c
leaq 0x11(%rax), %rcx
cmpq %r12, %rcx
ja 0x4aa3
movdqu (%rax), %xmm0
movzbl 0x10(%rax), %eax
movd %eax, %xmm1
pcmpeqb 0xf878(%rip), %xmm0     # 0x14300
pcmpeqb 0xf880(%rip), %xmm1     # 0x14310
pand %xmm0, %xmm1
pmovmskb %xmm1, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x4bd0
movq 0x20(%r15), %rdi
movq 0x18(%r15), %rbp
addq %rdi, %rbp
leaq 0xf896(%rip), %rdx      # 0x1434b
movq %rbp, %rsi
callq 0xa00c
leaq 0xf8fc(%rip), %rdx      # 0x143c0
movq %rax, %rdi
movq %rbp, %rsi
callq 0xa00c
leaq 0xf900(%rip), %rdx      # 0x143d6
movq %rax, 0x18(%rsp)
movq %rax, %rdi
movq %rbp, %rsi
callq 0xa00c
movq %rax, %r13
callq 0x3040
movq %rax, (%rsp)
movl $0x0, (%rax)
testq %r13, %r13
je 0x4c81
leaq 0x20(%rsp), %r12
movq %r13, %rdi
movq %r12, %rsi
movl $0xa, %edx
callq 0x33d0
cmpq %r13, (%r12)
je 0x4b5b
testq %rax, %rax
movq (%rsp), %r12
jns 0x4b68
movl $0x22, %ecx
xorl %eax, %eax
jmp 0x4b64
movq 0x8(%rsp), %rsi
leaq 0xf83c(%rip), %rdi      # 0x14376
xorl %eax, %eax
callq 0xb9a3
callq 0x3040
movl $0x0, (%rax)
movq 0x8(%rsp), %rdi
callq 0x3460
jmp 0x4c6b
movl $0x16, %ecx
movq (%rsp), %r12
movl %ecx, (%r12)
cmpl $0x0, (%r12)
jne 0x4bf7
movq %rax, 0x10(%rsp)
leaq 0xf87f(%rip), %rdx      # 0x143fe
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0xa00c
movl $0x0, (%r12)
testq %rax, %rax
je 0x4c81
movq %rax, %r13
leaq 0x20(%rsp), %r12
movq %rax, %rdi
movq %r12, %rsi
movl $0xa, %edx
callq 0x33d0
movq %rax, %rbp
cmpq %r13, (%r12)
je 0x4be3
testq %rbp, %rbp
movq (%rsp), %r12
jns 0x4bf0
movl $0x22, %eax
xorl %ebp, %ebp
jmp 0x4bec
movq 0x30(%r14), %rdi
xorl %esi, %esi
callq 0xc96f
movq 0x28(%r14), %rdi
xorl %esi, %esi
jmp 0x4c27
movl $0x16, %eax
movq (%rsp), %r12
movl %eax, (%r12)
cmpl $0x0, (%r12)
je 0x4c12
leaq 0xf7ea(%rip), %rdi      # 0x143e8
movq %r13, %rsi
xorl %eax, %eax
callq 0xb884
movl $0x0, (%r12)
jmp 0x4c2c
movq 0x30(%r14), %rdi
movq 0x10(%rsp), %rsi
callq 0xc96f
movq 0x28(%r14), %rdi
movq %rbp, %rsi
callq 0xc96f
movq 0x20(%r15), %rdi
callq 0x3460
movl $0x1, 0x60(%r14)
xorl %eax, %eax
cmpl $0x0, 0x60(%r14)
sete %al
leaq 0xf6d2(%rip), %rcx      # 0x14320
movsd (%rcx,%rax,8), %xmm0
leaq 0x46(%rip), %rdi        # 0x4ca0
movq %r14, %rsi
callq 0x6ab0
movq %rax, 0x58(%r14)
testq %rax, %rax
jne 0x4c70
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10871(%rip), %rdi     # 0x154f9
leaq 0xf782(%rip), %rsi      # 0x14411
leaq 0xf79a(%rip), %rcx      # 0x14430
movl $0xa7, %edx
callq 0x3210