// File: /workspace/github_c_makefile_star2_O2/Tarsnap[P]kivaloo/lbs-dynamodb-init/main.c
// Function: createtable
static int
createtable(const char * key_id, const char * key_secret, const char * region,
    struct sock_addr * const * sas_ddb, const char * tablename)
{
	char * ddbreq;
	char * body;
	const uint8_t * desc;
	const char * tablestatus;

	/* Tell the user what we're doing. */
	fprintf(stderr, "Creating table %s ", tablename);

	/* Construct a CreateTable DynamoDB request. */
	if (asprintf(&ddbreq,
	    "{"
	    "\"TableName\":\"%s\","
	    "\"AttributeDefinitions\":[{"
	        "\"AttributeName\":\"K\","
	        "\"AttributeType\":\"S\"}],"
	    "\"KeySchema\":[{"
	        "\"AttributeName\":\"K\","
	        "\"KeyType\":\"HASH\"}],"
	    "\"BillingMode\":\"PAY_PER_REQUEST\"}", tablename) == -1) {
		warnp("asprintf");
		goto err0;
	}

	/* Send the request. */
	if ((body = request(key_id, key_secret, region, sas_ddb,
	    "CreateTable", ddbreq)) == NULL) {
		warnp("CreateTable failed: %s", ddbreq);
		goto err1;
	}

	/* Free request and response. */
	free(body);
	free(ddbreq);

	/* Wait until the table creation completes. */
	do {
		/* Construct a DescribeTable request. */
		if (asprintf(&ddbreq, "{\"TableName\":\"%s\"}",
		    tablename) == -1) {
			warnp("asprintf");
			goto err0;
		}

		/* Send the request. */
		if ((body = request(key_id, key_secret, region, sas_ddb,
		    "DescribeTable", ddbreq)) == NULL) {
			warnp("DescribeTable failed: %s", ddbreq);
			goto err1;
		}

		/* Find a the Table->TableStatus field. */
		desc = json_find((uint8_t *)body,
		    (uint8_t *)body + strlen(body), "Table");
		tablestatus = (const char *)json_find(desc,
		    desc + strlen((const char *)desc), "TableStatus");

		/* It should be "CREATING" or "ACTIVE". */
		if (strncmp(tablestatus, "\"CREATING\"", 10) == 0) {
			/* Wait a second and try again. */
			fprintf(stderr, ".");
			sleep(1);
			free(body);
		} else if (strncmp(tablestatus, "\"ACTIVE\"", 8) == 0) {
			/* We're done. */
			free(body);
			break;
		} else {
			/* Invalid status. */
			warn0("Unexpected DescribeTable response: %s", body);
			free(body);
			goto err1;
		}
	} while (1);

	/* Print delayed EOL. */
	fprintf(stderr, "\n");

	/* Success! */
	return (0);

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, %rbx
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r13
movq 0x12ec8(%rip), %rax     # 0x15fe0
movq (%rax), %rdi
leaq 0xb154(%rip), %rsi      # 0xe276
movq %r8, %rdx
xorl %eax, %eax
callq 0x24a0
leaq 0xb156(%rip), %rsi      # 0xe289
leaq 0x8(%rsp), %rdi
movq %rbx, 0x10(%rsp)
movq %rbx, %rdx
xorl %eax, %eax
callq 0x49e0
cmpl $-0x1, %eax
je 0x326c
movq 0x8(%rsp), %r9
leaq 0xb1d8(%rip), %r8       # 0xe334
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x3369
testq %rax, %rax
je 0x328f
movq %rax, %rdi
callq 0x23d0
leaq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0x23d0
leaq 0x8(%rsp), %rdi
leaq 0xb1c0(%rip), %rsi      # 0xe357
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x49e0
cmpl $-0x1, %eax
je 0x326c
movq 0x8(%rsp), %r9
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
leaq 0xb1a6(%rip), %r8       # 0xe36a
callq 0x3369
testq %rax, %rax
je 0x32ba
movq %rax, %rbp
movq %r13, %rbx
movq %r12, %r13
movq %r15, %r12
movq %r14, %r15
movq %rax, %rdi
callq 0x2140
movq %rbp, %rsi
addq %rax, %rsi
movq %rbp, %rdi
leaq 0xb179(%rip), %rdx      # 0xe372
callq 0x5628
movq %rax, %r14
movq %rax, %rdi
callq 0x2140
leaq (%rax,%r14), %rsi
movq %r14, %rdi
leaq 0xb17a(%rip), %rdx      # 0xe391
callq 0x5628
movq %rax, %r14
movq %rax, %rdi
leaq 0xb174(%rip), %rsi      # 0xe39d
pushq $0xa
popq %rdx
callq 0x2160
testl %eax, %eax
jne 0x32df
movq 0x12da0(%rip), %rax     # 0x15fe0
movq (%rax), %rsi
pushq $0x2e
popq %rdi
callq 0x2400
pushq $0x1
popq %rdi
callq 0x2220
movq %rbp, %rdi
callq 0x23d0
movq %r15, %r14
movq %r12, %r15
movq %r13, %r12
movq %rbx, %r13
jmp 0x318b
callq 0x2040
cmpl $0x0, (%rax)
je 0x32aa
movq %rax, %rbx
leaq 0xae89(%rip), %rdi      # 0xe109
xorl %eax, %eax
callq 0x6ab1
andl $0x0, (%rbx)
jmp 0x3339
callq 0x2040
cmpl $0x0, (%rax)
movq 0x8(%rsp), %rsi
je 0x3318
movq %rax, %rbx
leaq 0xb098(%rip), %rdi      # 0xe340
jmp 0x32d3
leaq 0xae58(%rip), %rdi      # 0xe109
xorl %eax, %eax
callq 0x6c40
jmp 0x3339
callq 0x2040
cmpl $0x0, (%rax)
movq 0x8(%rsp), %rsi
je 0x3321
movq %rax, %rbx
leaq 0xb0a5(%rip), %rdi      # 0xe378
xorl %eax, %eax
callq 0x6ab1
andl $0x0, (%rbx)
jmp 0x332f
leaq 0xb0c2(%rip), %rsi      # 0xe3a8
pushq $0x8
popq %rdx
movq %r14, %rdi
callq 0x2160
testl %eax, %eax
je 0x334b
leaq 0xb0b5(%rip), %rdi      # 0xe3b1
movq %rbp, %rsi
xorl %eax, %eax
callq 0x6c40
callq 0x2040
andl $0x0, (%rax)
movq %rbp, %rdi
callq 0x23d0
jmp 0x332f
leaq 0xb021(%rip), %rdi      # 0xe340
jmp 0x3328
leaq 0xb050(%rip), %rdi      # 0xe378
xorl %eax, %eax
callq 0x6c40
movq 0x8(%rsp), %rdi
callq 0x23d0
pushq $-0x1
popq %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, %rdi
callq 0x23d0
movq 0x12c86(%rip), %rax     # 0x15fe0
movq (%rax), %rsi
pushq $0xa
popq %rdi
callq 0x2400
xorl %eax, %eax
jmp 0x333c