// File: /workspace/github_c_makefile_star2_O3/Tarsnap[P]kivaloo/lbs-dynamodb-init/main.c
// Function: main
int
main(int argc, char * argv[])
{

	/* Command-line parameters. */
	size_t opt_b = 0;
	char * opt_k = NULL;
	char * opt_m = NULL;
	char * opt_r = NULL;
	char * opt_t = NULL;

	/* Working variables. */
	char * dynamodb_host;
	char * key_id;
	char * key_secret;
	struct sock_addr ** sas_ddb;
	const char * ch;
	uint8_t tableid[32];

	WARNP_INIT;

	/* Parse the command line. */
	while ((ch = GETOPT(argc, argv)) != NULL) {
		GETOPT_SWITCH(ch) {
		GETOPT_OPTARG("-b"):
			if (opt_b != 0)
				usage();
			if (PARSENUM(&opt_b, optarg, 512, 8192))
				OPT_EPARSE(ch, optarg);
			break;
		GETOPT_OPTARG("-k"):
			if (opt_k != NULL)
				usage();
			if ((opt_k = strdup(optarg)) == NULL)
				OPT_EPARSE(ch, optarg);
			break;
		GETOPT_OPTARG("-m"):
			if (opt_m != NULL)
				usage();
			if ((opt_m = strdup(optarg)) == NULL)
				OPT_EPARSE(ch, optarg);
			break;
		GETOPT_OPTARG("-r"):
			if (opt_r != NULL)
				usage();
			if ((opt_r = strdup(optarg)) == NULL)
				OPT_EPARSE(ch, optarg);
			break;
		GETOPT_OPTARG("-t"):
			if (opt_t != NULL)
				usage();
			if ((opt_t = strdup(optarg)) == NULL)
				OPT_EPARSE(ch, optarg);
			break;
		GETOPT_OPT("--version"):
			fprintf(stderr, "kivaloo-lbs-dynamodb-init @VERSION@\n");
			exit(0);
		GETOPT_MISSING_ARG:
			warn0("Missing argument to %s", ch);
			usage();
		GETOPT_DEFAULT:
			warn0("illegal option -- %s", ch);
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	/* We should have processed all the arguments. */
	if (argc != 0)
		usage();
	(void)argv; /* argv is not used beyond this point. */

	/* Sanity-check options. */
	if (opt_b == 0)
		usage();
	if (opt_k == NULL)
		usage();
	if (opt_m == NULL)
		usage();
	if (opt_r == NULL)
		usage();
	if (opt_t == NULL)
		usage();

	/* Warn about poor choices of block sizes. */
	if (opt_b % 1024) {
		warn0("DynamoDB item size is unlikely to be optimal: %zu",
		    opt_b);
	}

	/* Construct the DynamoDB endpoint host name. */
	if (asprintf(&dynamodb_host, "dynamodb.%s.amazonaws.com:443",
	    opt_r) == -1) {
		warnp("asprintf");
		exit(1);
	}

	/* Resolve the DynamoDB endpoint. */
	if ((sas_ddb = sock_resolve(dynamodb_host)) == NULL) {
		warnp("Error resolving DynamoDB host: %s", dynamodb_host);
		exit(1);
	}

	/* Read the key file. */
	if (aws_readkeys(opt_k, &key_id, &key_secret)) {
		warnp("Error reading AWS keys from %s", opt_k);
		exit(1);
	}

	/* Create the data and metadata tables. */
	if (createtable(key_id, key_secret, opt_r, sas_ddb, opt_t)) {
		warnp("Failed to create DynamoDB table: %s", opt_t);
		exit(1);
	}
	if (createtable(key_id, key_secret, opt_r, sas_ddb, opt_m)) {
		warnp("Failed to create DynamoDB table: %s", opt_m);
		exit(1);
	}

	/* Generate a random table ID. */
	if (entropy_read(tableid, 32)) {
		warnp("Error generating table ID");
		exit(1);
	}

	/* Record the table ID in the data table. */
	if (storetableid(key_id, key_secret, opt_r, sas_ddb, opt_t, tableid)) {
		warnp("Failed to store table ID");
		exit(1);
	}

	/* Store a metadata blob in the metadata table. */
	if (createmetadata(key_id, key_secret, opt_r, sas_ddb, opt_m,
	    opt_b, tableid)) {
		warnp("Failed to store metadata");
		exit(1);
	}

	/* Free the address structures. */
	sock_addr_freelist(sas_ddb);

	/* Free string allocated by asprintf. */
	free(dynamodb_host);

	/* Free key strings. */
	free(key_id);
	insecure_memzero(key_secret, strlen(key_secret));
	free(key_secret);

	/* Free option strings. */
	free(opt_k);
	free(opt_m);
	free(opt_r);
	free(opt_t);

	/* Success! */
	exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r14
movl %edi, %ebx
movq (%rsi), %rdi
testq %rdi, %rdi
je 0x30af
callq 0x73ec
movl %ebx, %edi
movq %r14, %rsi
callq 0x562c
testq %rax, %rax
je 0x37c1
movq %rax, %rbp
movq $0x0, 0x18(%rsp)
leaq 0x2566b(%rip), %r13     # 0x28740
leaq 0xcf55(%rip), %r15      # 0x10031
xorl %r12d, %r12d
movq $0x0, 0x48(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movq %r14, 0x60(%rsp)
movq $0x14d, 0x10(%rsp)      # imm = 0x14D
movq 0x10(%rsp), %rax
decq %rax
movq %rax, 0x58(%rsp)
movl $0x0, 0x44(%rsp)
cmpl $0x0, (%r13)
je 0x3133
movq %rbp, %rdi
callq 0x5973
addq 0x10(%rsp), %rax
jmp 0x3141
movq 0x58(%rsp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x58(%rsp)
cmpq $0x165, %rax            # imm = 0x165
jg 0x318e
cmpq $0x159, %rax            # imm = 0x159
jg 0x31d6
cmpq $0x14e, %rax            # imm = 0x14E
je 0x3255
cmpq $0x154, %rax            # imm = 0x154
jne 0x32a2
cmpl $0x0, (%r13)
jne 0x32e1
movl $0x154, %esi            # imm = 0x154
subq 0x10(%rsp), %rsi
leaq 0xcec9(%rip), %rdi      # 0x10052
jmp 0x328b
cmpq $0x16e, %rax            # imm = 0x16E
jg 0x320c
cmpq $0x166, %rax            # imm = 0x166
je 0x3237
cmpq $0x16c, %rax            # imm = 0x16C
jne 0x32a2
cmpl $0x0, (%r13)
jne 0x3680
movl $0x16c, %esi            # imm = 0x16C
subq 0x10(%rsp), %rsi
leaq 0xce94(%rip), %rdi      # 0x1005e
xorl %edx, %edx
callq 0x5a30
jmp 0x311d
cmpq $0x15a, %rax            # imm = 0x15A
je 0x326f
cmpq $0x160, %rax            # imm = 0x160
jne 0x32a2
cmpl $0x0, (%r13)
jne 0x33c9
movl $0x160, %esi            # imm = 0x160
subq 0x10(%rsp), %rsi
leaq 0xce4e(%rip), %rdi      # 0x10058
jmp 0x328b
cmpq $0x16f, %rax            # imm = 0x16F
jne 0x329a
cmpl $0x0, (%r13)
jne 0x3677
movl $0x16f, %edi            # imm = 0x16F
subq 0x10(%rsp), %rdi
callq 0x5b41
jmp 0x311d
cmpl $0x0, (%r13)
jne 0x33ff
movl $0x166, %esi            # imm = 0x166
subq 0x10(%rsp), %rsi
leaq 0xce08(%rip), %rdi      # 0x1005b
jmp 0x328b
cmpl $0x0, (%r13)
jne 0x3312
movl $0x14e, %esi            # imm = 0x14E
subq 0x10(%rsp), %rsi
movq %r15, %rdi
jmp 0x328b
cmpl $0x0, (%r13)
jne 0x339b
movl $0x15a, %esi            # imm = 0x15A
subq 0x10(%rsp), %rsi
leaq 0xcdca(%rip), %rdi      # 0x10055
movl $0x1, %edx
callq 0x5a30
jmp 0x311d
cmpq $0x172, %rax            # imm = 0x172
je 0x32d4
cmpl $0x0, (%r13)
jne 0x36a2
cmpl $0x0, 0x44(%rsp)
jne 0x311d
movl $0x172, %edi            # imm = 0x172
subq 0x10(%rsp), %rdi
callq 0x5b81
movl $0x1, 0x44(%rsp)
jmp 0x311d
movl $0x1, (%r13)
jmp 0x3450
leaq 0x25450(%rip), %rax     # 0x28738
movq (%rax), %r15
testq %r15, %r15
je 0x37a2
cmpq $0x0, 0x28(%rsp)
jne 0x37ca
movq %r15, %rdi
callq 0x2520
movq %rax, 0x28(%rsp)
jmp 0x33f5
movl %ebx, %r14d
movq %r12, %rbx
leaq 0x25419(%rip), %rax     # 0x28738
movq (%rax), %r12
testq %r12, %r12
je 0x3783
cmpq $0x0, 0x18(%rsp)
jne 0x37ca
callq 0x2040
movq %rax, %r15
movl $0x0, (%rax)
movq %r12, %rdi
leaq 0x68(%rsp), %rsi
xorl %edx, %edx
callq 0x22d0
movq %rax, 0x18(%rsp)
movq 0x68(%rsp), %rcx
movl $0x16, %eax
cmpq %r12, %rcx
je 0x3431
cmpb $0x0, (%rcx)
movq %rbx, %r12
jne 0x3434
movq 0x18(%rsp), %rax
leaq -0x2001(%rax), %rcx
movl $0x22, %eax
cmpq $-0x1e02, %rcx          # imm = 0xE1FE
jbe 0x3434
jmp 0x3437
leaq 0x25396(%rip), %rax     # 0x28738
movq (%rax), %r15
testq %r15, %r15
je 0x3764
cmpq $0x0, 0x30(%rsp)
jne 0x37ca
movq %r15, %rdi
callq 0x2520
movq %rax, 0x30(%rsp)
jmp 0x33f5
leaq 0x25368(%rip), %rax     # 0x28738
movq (%rax), %r15
testq %r15, %r15
je 0x3745
cmpq $0x0, 0x48(%rsp)
jne 0x37ca
movq %r15, %rdi
callq 0x2520
movq %rax, 0x48(%rsp)
testq %rax, %rax
jne 0x3449
jmp 0x36c3
leaq 0x25332(%rip), %rax     # 0x28738
movq (%rax), %r15
testq %r15, %r15
je 0x36ff
testq %r12, %r12
jne 0x37ca
movq %r15, %rdi
callq 0x2520
testq %rax, %rax
je 0x36c3
movq %rax, %r12
jmp 0x3449
movq %rbx, %r12
movl %eax, (%r15)
cmpl $0x0, (%r15)
jne 0x371e
movl %r14d, %ebx
movq 0x60(%rsp), %r14
leaq 0xcbe1(%rip), %r15      # 0x10031
movl %ebx, %edi
movq %r14, %rsi
callq 0x562c
movq %rax, %rbp
testq %rax, %rax
jne 0x30ff
leaq 0x150fb(%rip), %rax     # 0x18568
cmpl %ebx, (%rax)
jne 0x37ca
cmpq $0x0, 0x18(%rsp)
je 0x37ca
movq 0x28(%rsp), %r13
testq %r13, %r13
movq 0x30(%rsp), %r15
movq 0x48(%rsp), %rbp
je 0x37ca
testq %r15, %r15
je 0x37ca
testq %rbp, %rbp
je 0x37ca
testq %r12, %r12
je 0x37ca
movq 0x18(%rsp), %rax
shlq $0x36, %rax
je 0x34dd
leaq 0xcbf3(%rip), %rdi      # 0x100b9
movq 0x18(%rsp), %rsi
xorl %eax, %eax
callq 0x758b
callq 0x2040
movl $0x0, (%rax)
leaq 0xcc07(%rip), %rsi      # 0x100eb
leaq 0x50(%rsp), %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x525c
cmpl $-0x1, %eax
je 0x37cf
movq 0x50(%rsp), %rdi
callq 0x63e8
testq %rax, %rax
je 0x37e9
movq %rax, %rbx
leaq 0x38(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movq %r13, %rdi
callq 0x3b20
testl %eax, %eax
jne 0x3805
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rbp, %rdx
movq %rbx, %rcx
movq %r12, %r8
callq 0x25b0
testl %eax, %eax
jne 0x3821
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rbp, %rdx
movq %rbx, %rcx
movq %r15, %r8
callq 0x25b0
testl %eax, %eax
jne 0x383b
leaq 0x70(%rsp), %rdi
movl $0x20, %esi
callq 0x5577
testl %eax, %eax
jne 0x385d
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %rsi
leaq 0x70(%rsp), %r14
movq %rbp, %rdx
movq %rbx, %rcx
movq %r12, %r8
movq %r14, %r9
callq 0x283f
testl %eax, %eax
jne 0x3873
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %r14, (%rsp)
movq %rbp, %rdx
movq %rbx, %rcx
movq %r15, %r8
movq 0x18(%rsp), %r9
callq 0x295a
testl %eax, %eax
jne 0x363c
movq %rbx, %rdi
callq 0x6afa
movq 0x50(%rsp), %rdi
callq 0x23c0
movq 0x38(%rsp), %rdi
callq 0x23c0
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x2140
leaq 0x14f80(%rip), %rcx     # 0x18580
movq (%rcx), %rcx
movq %rbx, %rdi
movq %rax, %rsi
callq *%rcx
movq 0x20(%rsp), %rdi
callq 0x23c0
movq %r13, %rdi
callq 0x23c0
movq %r15, %rdi
callq 0x23c0
movq %rbp, %rdi
callq 0x23c0
movq %r12, %rdi
callq 0x23c0
xorl %edi, %edi
callq 0x23e0
callq 0x2040
cmpl $0x0, (%rax)
jne 0x3656
leaq 0xcb5d(%rip), %rdi      # 0x101aa
xorl %eax, %eax
callq 0x758b
jmp 0x366d
movq %rax, %rbx
leaq 0xcb4a(%rip), %rdi      # 0x101aa
xorl %eax, %eax
callq 0x746c
movl $0x0, (%rbx)
movl $0x1, %edi
callq 0x23e0
leaq 0xca0f(%rip), %rdi      # 0x1008d
jmp 0x36a9
movq 0x14959(%rip), %rax     # 0x17fe0
movq (%rax), %rcx
leaq 0xc9d7(%rip), %rdi      # 0x10068
movl $0x24, %esi
movl $0x1, %edx
callq 0x2560
jmp 0x3635
leaq 0xc9fb(%rip), %rdi      # 0x100a4
movq %rbp, %rsi
xorl %eax, %eax
callq 0x758b
callq 0x2040
movl $0x0, (%rax)
callq 0x38fd
callq 0x2040
cmpl $0x0, (%rax)
jne 0x36e3
leaq 0xc960(%rip), %rdi      # 0x10034
movq %rbp, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x758b
jmp 0x366d
movq %rax, %rbx
leaq 0xc947(%rip), %rdi      # 0x10034
movq %rbp, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x746c
jmp 0x3667
leaq 0xc8fe(%rip), %rdi      # 0x10004
leaq 0xc906(%rip), %rsi      # 0x10013
leaq 0xc906(%rip), %rcx      # 0x1001a
movl $0x166, %edx            # imm = 0x166
callq 0x21d0
leaq 0x25013(%rip), %rax     # 0x28738
movq (%rax), %rdx
leaq 0xc905(%rip), %rdi      # 0x10034
movq %rbp, %rsi
xorl %eax, %eax
callq 0x746c
movl $0x0, (%r15)
jmp 0x366d
leaq 0xc8b8(%rip), %rdi      # 0x10004
leaq 0xc8c0(%rip), %rsi      # 0x10013
leaq 0xc8c0(%rip), %rcx      # 0x1001a
movl $0x160, %edx            # imm = 0x160
callq 0x21d0
leaq 0xc899(%rip), %rdi      # 0x10004
leaq 0xc8a1(%rip), %rsi      # 0x10013
leaq 0xc8a1(%rip), %rcx      # 0x1001a
movl $0x15a, %edx            # imm = 0x15A
callq 0x21d0
leaq 0xc87a(%rip), %rdi      # 0x10004
leaq 0xc882(%rip), %rsi      # 0x10013
leaq 0xc882(%rip), %rcx      # 0x1001a
movl $0x14e, %edx            # imm = 0x14E
callq 0x21d0
leaq 0xc85b(%rip), %rdi      # 0x10004
leaq 0xc863(%rip), %rsi      # 0x10013
leaq 0xc863(%rip), %rcx      # 0x1001a
movl $0x154, %edx            # imm = 0x154
callq 0x21d0
leaq 0x14da0(%rip), %rax     # 0x18568
cmpl %ebx, (%rax)
callq 0x38fd
callq 0x2040
cmpl $0x0, (%rax)
jne 0x3889
leaq 0xc925(%rip), %rdi      # 0x10109
jmp 0x364d
callq 0x2040
cmpl $0x0, (%rax)
movq 0x50(%rsp), %rsi
jne 0x3898
leaq 0xc90f(%rip), %rdi      # 0x10112
jmp 0x3851
callq 0x2040
cmpl $0x0, (%rax)
jne 0x38a4
leaq 0xc91a(%rip), %rdi      # 0x10134
movq 0x28(%rsp), %rsi
jmp 0x3851
callq 0x2040
cmpl $0x0, (%rax)
jne 0x38b5
leaq 0xc91d(%rip), %rdi      # 0x10153
movq %r12, %rsi
jmp 0x3851
callq 0x2040
cmpl $0x0, (%rax)
jne 0x38c4
leaq 0xc907(%rip), %rdi      # 0x10153
movq 0x30(%rsp), %rsi
xorl %eax, %eax
callq 0x758b
jmp 0x366d
callq 0x2040
cmpl $0x0, (%rax)
jne 0x38df
leaq 0xc909(%rip), %rdi      # 0x10177
jmp 0x364d
callq 0x2040
cmpl $0x0, (%rax)
jne 0x38ee
leaq 0xc90d(%rip), %rdi      # 0x10191
jmp 0x364d
movq %rax, %rbx
leaq 0xc876(%rip), %rdi      # 0x10109
jmp 0x3660
movq %rax, %rbx
leaq 0xc870(%rip), %rdi      # 0x10112
jmp 0x38d3
movq %rax, %rbx
leaq 0xc886(%rip), %rdi      # 0x10134
movq 0x28(%rsp), %rsi
jmp 0x38d3
movq %rax, %rbx
leaq 0xc894(%rip), %rdi      # 0x10153
movq %r12, %rsi
jmp 0x38d3
movq %rax, %rbx
leaq 0xc885(%rip), %rdi      # 0x10153
movq 0x30(%rsp), %rsi
xorl %eax, %eax
callq 0x746c
jmp 0x3667
movq %rax, %rbx
leaq 0xc88e(%rip), %rdi      # 0x10177
jmp 0x3660
movq %rax, %rbx
leaq 0xc899(%rip), %rdi      # 0x10191
jmp 0x3660