// File: /workspace/github_c_makefile_star2_O3/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, %rbp
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r13
movq 0x15a0c(%rip), %rax     # 0x17fe0
movq (%rax), %rdi
leaq 0xdc98(%rip), %rsi      # 0x10276
movq %r8, %rdx
xorl %eax, %eax
callq 0x2490
leaq 0xdc9a(%rip), %rsi      # 0x10289
leaq 0x8(%rsp), %rdi
movq %rbx, 0x10(%rsp)
movq %rbx, %rdx
xorl %eax, %eax
callq 0x525c
cmpl $-0x1, %eax
je 0x2742
movq 0x8(%rsp), %r9
leaq 0xdd1c(%rip), %r8       # 0x10334
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x3964
testq %rax, %rax
je 0x2765
movq %rax, %rdi
callq 0x23c0
leaq 0x8(%rsp), %r14
movq (%r14), %rdi
callq 0x23c0
leaq 0xdd09(%rip), %rsi      # 0x10357
movq %r14, %rdi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x525c
cmpl $-0x1, %eax
je 0x2742
movq 0x8(%rsp), %r9
movq %r13, %rbx
movq %r13, %rdi
movq %r12, %r13
movq %r12, %rsi
movq %r15, %r12
movq %r15, %rdx
movq %rbp, %r15
movq %rbp, %rcx
leaq 0xdce0(%rip), %r8       # 0x1036a
callq 0x3964
testq %rax, %rax
je 0x2799
movq %rax, %rbp
movq %rax, %rdi
callq 0x2140
movq %rbp, %rsi
addq %rax, %rsi
movq %rbp, %rdi
leaq 0xdcbf(%rip), %rdx      # 0x10372
callq 0x5cd8
movq %rax, %r14
movq %rax, %rdi
callq 0x2140
leaq (%rax,%r14), %rsi
movq %r14, %rdi
leaq 0xdcc0(%rip), %rdx      # 0x10391
callq 0x5cd8
movq %rax, %r14
movq %rax, %rdi
leaq 0xdcba(%rip), %rsi      # 0x1039d
pushq $0xa
popq %rdx
callq 0x2150
testl %eax, %eax
jne 0x27be
movq 0x158e6(%rip), %rax     # 0x17fe0
movq (%rax), %rsi
pushq $0x2e
popq %rdi
callq 0x23f0
pushq $0x1
popq %rdi
callq 0x2210
movq %rbp, %rdi
callq 0x23c0
leaq 0x8(%rsp), %rdi
leaq 0xdc36(%rip), %rsi      # 0x10357
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x525c
cmpl $-0x1, %eax
movq %r15, %rbp
movq %r12, %r15
movq %r13, %r12
movq %rbx, %r13
jne 0x2666
callq 0x2040
cmpl $0x0, (%rax)
je 0x2780
movq %rax, %rbx
leaq 0xd9b3(%rip), %rdi      # 0x10109
xorl %eax, %eax
callq 0x746c
andl $0x0, (%rbx)
jmp 0x280f
callq 0x2040
cmpl $0x0, (%rax)
movq 0x8(%rsp), %rsi
je 0x2790
movq %rax, %rbx
leaq 0xdbc2(%rip), %rdi      # 0x10340
jmp 0x27b2
leaq 0xd982(%rip), %rdi      # 0x10109
xorl %eax, %eax
callq 0x758b
jmp 0x280f
leaq 0xdba9(%rip), %rdi      # 0x10340
jmp 0x27fe
callq 0x2040
cmpl $0x0, (%rax)
movq 0x8(%rsp), %rsi
je 0x27f7
movq %rax, %rbx
leaq 0xdbc6(%rip), %rdi      # 0x10378
xorl %eax, %eax
callq 0x746c
andl $0x0, (%rbx)
jmp 0x2805
leaq 0xdbe3(%rip), %rsi      # 0x103a8
pushq $0x8
popq %rdx
movq %r14, %rdi
callq 0x2150
testl %eax, %eax
je 0x2821
leaq 0xdbd6(%rip), %rdi      # 0x103b1
movq %rbp, %rsi
xorl %eax, %eax
callq 0x758b
callq 0x2040
andl $0x0, (%rax)
movq %rbp, %rdi
callq 0x23c0
jmp 0x2805
leaq 0xdb7a(%rip), %rdi      # 0x10378
xorl %eax, %eax
callq 0x758b
movq 0x8(%rsp), %rdi
callq 0x23c0
pushq $-0x1
popq %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, %rdi
callq 0x23c0
movq 0x157b0(%rip), %rax     # 0x17fe0
movq (%rax), %rsi
pushq $0xa
popq %rdi
callq 0x23f0
xorl %eax, %eax
jmp 0x2812