// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
andq $0x0, 0x8(%rsp)
andq $0x0, 0x50(%rdi)
movq 0x40(%rdi), %rdi
callq 0x9fff
andq $0x0, 0x40(%rbx)
movq 0x38(%rbx), %rdi
callq 0x3470
andq $0x0, 0x38(%rbx)
testq %r14, %r14
je 0x466c
cmpl $0x190, (%r14)          # imm = 0x190
jne 0x44d6
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0xb4d0
testl %eax, %eax
je 0x45bf
leaq 0xee67(%rip), %rdi      # 0x13329
xorl %eax, %eax
callq 0xaaa0
callq 0x3040
andl $0x0, (%rax)
jmp 0x469e
movq 0x20(%r14), %rax
testq %rax, %rax
je 0x466c
movq 0x18(%r14), %rcx
testq %rcx, %rcx
je 0x465c
movb $0x0, -0x1(%rax,%rcx)
movq 0x20(%r14), %rdi
movq 0x18(%r14), %r15
addq %rdi, %r15
leaq 0xee1c(%rip), %rdx      # 0x13323
movq %r15, %rsi
callq 0x93d8
leaq 0xee51(%rip), %rdx      # 0x13367
movq %rax, %rdi
movq %r15, %rsi
callq 0x93d8
leaq 0xee52(%rip), %rdx      # 0x1337a
movq %rax, %rdi
movq %r15, %rsi
callq 0x93d8
leaq 0x11(%rax), %rcx
cmpq %r15, %rcx
ja 0x4556
leaq 0xee43(%rip), %rsi      # 0x13386
pushq $0x11
popq %rdx
movq %rax, %rdi
callq 0x33b0
testl %eax, %eax
je 0x462c
movq 0x20(%r14), %rdi
movq 0x18(%r14), %r12
addq %rdi, %r12
leaq 0xedbb(%rip), %rdx      # 0x13323
movq %r12, %rsi
callq 0x93d8
leaq 0xee21(%rip), %rdx      # 0x13398
movq %rax, %rdi
movq %r12, %rsi
callq 0x93d8
movq %rax, %rbp
leaq 0xee22(%rip), %rdx      # 0x133ae
movq %rax, %rdi
movq %r12, %rsi
callq 0x93d8
movq %rax, %r13
callq 0x3040
movq %rax, %r15
andl $0x0, (%rax)
movq %r13, %rdi
callq 0x46b0
cmpl $0x0, (%r15)
je 0x45e9
leaq 0xee06(%rip), %rdi      # 0x133c0
movq %r13, %rsi
jmp 0x461f
movq 0x8(%rsp), %rsi
leaq 0xed83(%rip), %rdi      # 0x1334e
xorl %eax, %eax
callq 0xaaa0
callq 0x3040
andl $0x0, (%rax)
movq 0x8(%rsp), %rdi
callq 0x3470
jmp 0x469e
movq %rax, 0x10(%rsp)
leaq 0xede1(%rip), %rdx      # 0x133d6
movq %rbp, %rdi
movq %r12, %rsi
callq 0x93d8
movq %rax, %r12
andl $0x0, (%r15)
movq %rax, %rdi
callq 0x46b0
cmpl $0x0, (%r15)
je 0x463f
leaq 0xeda4(%rip), %rdi      # 0x133c0
movq %r12, %rsi
xorl %eax, %eax
callq 0xa911
andl $0x0, (%r15)
jmp 0x465c
movq 0x30(%rbx), %rdi
xorl %esi, %esi
callq 0xb8ee
movq 0x28(%rbx), %rdi
xorl %esi, %esi
jmp 0x4657
movq %rax, %r13
movq 0x30(%rbx), %rdi
movq 0x10(%rsp), %rsi
callq 0xb8ee
movq 0x28(%rbx), %rdi
movq %r13, %rsi
callq 0xb8ee
movq 0x20(%r14), %rdi
callq 0x3470
movl $0x1, 0x60(%rbx)
xorl %eax, %eax
cmpl $0x0, 0x60(%rbx)
sete %al
leaq 0xec7c(%rip), %rcx      # 0x132f8
movsd (%rcx,%rax,8), %xmm0
leaq 0x90(%rip), %rdi        # 0x4718
movq %rbx, %rsi
callq 0x621a
movq %rax, %rcx
movq %rax, 0x58(%rbx)
xorl %eax, %eax
testq %rcx, %rcx
jne 0x46a1
pushq $-0x1
popq %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq