// File: /workspace/github_c_makefile_star2_O2/Tarsnap[P]kivaloo/lbs-dynamodb/main.c
// Function: main
int
main(int argc, char * argv[])
{
	/* State variables. */
	struct wire_requestqueue * Q_DDBKV;
	struct wire_requestqueue * Q_DDBKV_M;
	struct metadata * M;
	struct deleteto * deleter;
	struct state * S;
	struct dispatch_state * D;
	int s;
	int s_t;
	int s_m;

	/* Command-line parameters. */
	char * opt_s = NULL;
	char * opt_t = NULL;
	char * opt_m = NULL;
	char * opt_p = NULL;
	int opt_1 = 0;

	/* Working variables. */
	struct sock_addr ** sas_s;
	struct sock_addr ** sas_t;
	struct sock_addr ** sas_m;
	uint64_t itemsz_64;
	size_t itemsz;
	uint8_t tableid[32];
	const char * ch;

	WARNP_INIT;

	/* Parse the command line. */
	while ((ch = GETOPT(argc, argv)) != NULL) {
		GETOPT_SWITCH(ch) {
		GETOPT_OPTARG("-p"):
			if (opt_p != NULL)
				usage();
			if ((opt_p = strdup(optarg)) == NULL)
				OPT_EPARSE(ch, optarg);
			break;
		GETOPT_OPTARG("-s"):
			if (opt_s != NULL)
				usage();
			if ((opt_s = 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_OPTARG("-m"):
			if (opt_m != NULL)
				usage();
			if ((opt_m = strdup(optarg)) == NULL)
				OPT_EPARSE(ch, optarg);
			break;
		GETOPT_OPT("--version"):
			fprintf(stderr, "kivaloo-lbs-dynamodb @VERSION@\n");
			exit(0);
		GETOPT_OPT("-1"):
			if (opt_1 != 0)
				usage();
			opt_1 = 1;
			break;
		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_s == NULL)
		usage();
	if (opt_t == NULL)
		usage();
	if (opt_m == NULL)
		usage();

	/* Resolve the listening address. */
	if ((sas_s = sock_resolve(opt_s)) == NULL) {
		warnp("Error resolving socket address: %s", opt_s);
		exit(1);
	}
	if (sas_s[0] == NULL) {
		warn0("No addresses found for %s", opt_s);
		exit(1);
	}

	/* Resolve the target (dynamodb-kv daemon) addresses. */
	if ((sas_t = sock_resolve(opt_t)) == NULL) {
		warnp("Error resolving socket address: %s", opt_t);
		exit(1);
	}
	if (sas_t[0] == NULL) {
		warn0("No addresses found for %s", opt_t);
		exit(1);
	}
	if ((sas_m = sock_resolve(opt_m)) == NULL) {
		warnp("Error resolving socket address: %s", opt_m);
		exit(1);
	}
	if (sas_m[0] == NULL) {
		warn0("No addresses found for %s", opt_m);
		exit(1);
	}

	/* Create and bind a socket, and mark it as listening. */
	if (sas_s[1] != NULL)
		warn0("Listening on first of multiple addresses found for %s",
		    opt_s);
	if ((s = sock_listener(sas_s[0])) == -1)
		exit(1);

	/* Connect to the dynamodb-kv daemons. */
	if ((s_t = sock_connect(sas_t)) == -1)
		exit(1);
	if ((s_m = sock_connect(sas_m)) == -1)
		exit(1);

	/* Create queues of requests to the dynamodb-kv daemons. */
	if ((Q_DDBKV = wire_requestqueue_init(s_t)) == NULL) {
		warnp("Cannot create DynamoDB-KV request queue");
		exit(1);
	}
	if ((Q_DDBKV_M = wire_requestqueue_init(s_m)) == NULL) {
		warnp("Cannot create DynamoDB-KV request queue");
		exit(1);
	}

	/*
	 * Create a metadata handler; this also atomically takes ownership of
	 * the metadata with respect to other lbs-dynamodb processes.
	 */
	if ((M = metadata_init(Q_DDBKV_M, &itemsz_64, tableid)) == NULL) {
		warnp("Error initializing state metadata handler");
		exit(1);
	}

	/* Sanity-check the item size recorded in the metadata. */
	if ((itemsz_64 < 512) || (itemsz_64 > 8192)) {
		warn0("Invalid lbs-dynamodb item size: %" PRIu64, itemsz_64);
		exit(1);
	}
	itemsz = (size_t)itemsz_64;

	/* Create a deleter. */
	if ((deleter = deleteto_init(Q_DDBKV, M)) == NULL) {
		warnp("Error initializing garbage collection");
		exit(1);
	}

	/* Initialize the internal state. */
	if ((S = state_init(Q_DDBKV, itemsz, tableid, M)) == NULL) {
		warnp("Error initializing state from DynamoDB");
		exit(1);
	}

	/* Daemonize and write pid. */
	if (opt_p == NULL) {
		if (asprintf(&opt_p, "%s.pid", opt_s) == -1) {
			warnp("asprintf");
			exit(1);
		}
	}
	if (daemonize(opt_p)) {
		warnp("Failed to daemonize");
		exit(1);
	}

	/* Handle connections, one at once. */
	do {
		/* Accept a connection. */
		if ((D = dispatch_accept(S, deleter, s)) == NULL)
			exit(1);

		/* Loop until the connection dies. */
		do {
			if (events_run()) {
				warnp("Error running event loop");
				exit(1);
			}
		} while (dispatch_alive(D));

		/* Close and free the connection. */
		if (dispatch_done(D))
			exit(1);
	} while (opt_1 == 0);

	/* Clean up the internal state. */
	state_free(S);

	/*
	 * Shut down deleting (cleanly if possible, but we don't care if we
	 * encounter an error at this point).
	 */
	deleteto_stop(deleter);

	/* Shut down the metadata handler. */
	metadata_free(M);

	/* Shut down the dynamodb-kv request queues. */
	wire_requestqueue_destroy(Q_DDBKV_M);
	wire_requestqueue_free(Q_DDBKV_M);
	wire_requestqueue_destroy(Q_DDBKV);
	wire_requestqueue_free(Q_DDBKV);

	/* Close sockets. */
	if (close(s_m))
		warnp("close");
	if (close(s_t))
		warnp("close");
	if (close(s))
		warnp("close");

	/* Free socket addresses. */
	sock_addr_freelist(sas_m);
	sock_addr_freelist(sas_t);
	sock_addr_freelist(sas_s);

	/* Free option strings. */
	free(opt_s);
	free(opt_p);
	free(opt_m);
	free(opt_t);

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
andq $0x0, 0x28(%rsp)
movl %edi, 0x20(%rsp)
movq %rsi, 0x48(%rsp)
movq (%rsi), %rdi
testq %rdi, %rdi
je 0x2509
callq 0x86dc
movq $0x0, 0x60(%rsp)
movb $0x1, %al
movl %eax, 0xc(%rsp)
leaq 0x21289(%rip), %r13     # 0x237a8
pushq $0x1
popq %r14
leaq 0x9b31(%rip), %rbx      # 0xc05b
pushq $0x69
popq %r15
movq $0x0, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x18(%rsp)
pushq $0x49
popq %rbp
movl 0x20(%rsp), %edi
movq 0x48(%rsp), %rsi
callq 0x69ac
testq %rax, %rax
je 0x2850
movq %rax, %r12
movq $0x48, 0x10(%rsp)
movq 0x10(%rsp), %rax
decq %rax
movq %rax, 0x70(%rsp)
movl $0x0, 0x34(%rsp)
cmpl $0x0, (%r13)
je 0x259a
movq %r12, %rdi
callq 0x6dc1
addq 0x10(%rsp), %rax
jmp 0x25a8
movq 0x70(%rsp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x70(%rsp)
cmpq $0x49, %rax
je 0x2679
cmpq $0x4f, %rax
je 0x26ca
cmpq $0x55, %rax
je 0x26b2
cmpq $0x5b, %rax
je 0x261c
cmpq $0x61, %rax
je 0x2657
cmpq $0x64, %rax
je 0x263b
cmpq $0x69, %rax
je 0x2695
cmpq $0x6c, %rax
je 0x26f1
cmpl $0x0, (%r13)
jne 0x2829
cmpl $0x0, 0x34(%rsp)
jne 0x2584
pushq $0x6c
popq %rdi
subq 0x10(%rsp), %rdi
callq 0x712e
movl $0x1, 0x34(%rsp)
jmp 0x2584
cmpl $0x0, (%r13)
jne 0x27aa
pushq $0x5b
popq %rsi
subq 0x10(%rsp), %rsi
leaq 0x9a22(%rip), %rdi      # 0xc058
jmp 0x26e4
cmpl $0x0, (%r13)
jne 0x2764
pushq $0x64
popq %rsi
subq 0x10(%rsp), %rsi
leaq 0xb93b(%rip), %rdi      # 0xdf90
jmp 0x266d
cmpl $0x0, (%r13)
jne 0x2806
pushq $0x61
popq %rsi
subq 0x10(%rsp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x6ed9
jmp 0x2584
cmpl $0x0, (%r13)
jne 0x272f
movq %rbp, %rsi
subq 0x10(%rsp), %rsi
leaq 0x999e(%rip), %rdi      # 0xc031
jmp 0x26e4
cmpl $0x0, (%r13)
jne 0x2832
movq %r15, %rdi
subq 0x10(%rsp), %rdi
callq 0x70a3
jmp 0x2584
cmpl $0x0, (%r13)
jne 0x26fe
pushq $0x55
popq %rsi
subq 0x10(%rsp), %rsi
leaq 0x998d(%rip), %rdi      # 0xc055
jmp 0x26e4
cmpl $0x0, (%r13)
jne 0x277c
pushq $0x4f
popq %rsi
subq 0x10(%rsp), %rsi
leaq 0x996e(%rip), %rdi      # 0xc052
movl %r14d, %edx
callq 0x6ed9
jmp 0x2584
movl $0x1, (%r13)
jmp 0x254c
leaq 0x2109b(%rip), %rax     # 0x237a0
movq (%rax), %rbp
testq %rbp, %rbp
je 0x28a8
cmpq $0x0, 0x40(%rsp)
jne 0x2881
movq %rbp, %rdi
callq 0x2390
movq %rax, 0x40(%rsp)
jmp 0x27d6
leaq 0x2106a(%rip), %rax     # 0x237a0
movq (%rax), %rbp
testq %rbp, %rbp
je 0x28c1
testb $0x1, 0xc(%rsp)
je 0x2881
movq %rbp, %rdi
callq 0x2390
movq %rax, 0x28(%rsp)
movl $0x0, 0xc(%rsp)
jmp 0x27d6
cmpl $0x0, 0x60(%rsp)
movl %r14d, %eax
movq %rax, 0x60(%rsp)
je 0x2549
jmp 0x2881
leaq 0x2101d(%rip), %rax     # 0x237a0
movq (%rax), %rbp
testq %rbp, %rbp
je 0x28da
cmpq $0x0, 0x18(%rsp)
jne 0x2881
movq %rbp, %rdi
callq 0x2390
movq %rax, 0x18(%rsp)
jmp 0x27d6
leaq 0x20fef(%rip), %rax     # 0x237a0
movq (%rax), %rbp
testq %rbp, %rbp
je 0x28f3
cmpq $0x0, 0x38(%rsp)
jne 0x2881
movq %rbp, %rdi
callq 0x2390
movq %rax, 0x38(%rsp)
testq %rax, %rax
jne 0x2549
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2886
leaq 0x9840(%rip), %rdi      # 0xc034
movq %r12, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x88e8
jmp 0x28a0
movq 0x107eb(%rip), %rax     # 0x12ff8
movq (%rax), %rcx
leaq 0x984e(%rip), %rdi      # 0xc065
pushq $0x1f
popq %rsi
pushq $0x1
popq %rdx
callq 0x2370
xorl %edi, %edi
callq 0x2350
leaq 0x986c(%rip), %rdi      # 0xc09c
jmp 0x2839
leaq 0x984c(%rip), %rdi      # 0xc085
movq %r12, %rsi
xorl %eax, %eax
callq 0x88e8
callq 0x20a0
andl $0x0, (%rax)
callq 0x2de7
leaq 0x10cb1(%rip), %rax     # 0x13508
movl 0x20(%rsp), %ecx
cmpl %ecx, (%rax)
jne 0x2881
movq 0x18(%rsp), %r13
testq %r13, %r13
je 0x2881
movq 0x40(%rsp), %r14
testq %r14, %r14
je 0x2881
movq 0x38(%rsp), %r15
testq %r15, %r15
jne 0x2910
callq 0x2de7
movq %rax, %rbx
leaq 0x97a4(%rip), %rdi      # 0xc034
movq %r12, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x8759
andl $0x0, (%rbx)
pushq $0x1
popq %rdi
callq 0x2350
leaq 0x9755(%rip), %rdi      # 0xc004
leaq 0x975d(%rip), %rsi      # 0xc013
leaq 0x975d(%rip), %rcx      # 0xc01a
pushq $0x55
jmp 0x290a
leaq 0x973c(%rip), %rdi      # 0xc004
leaq 0x9744(%rip), %rsi      # 0xc013
leaq 0x9744(%rip), %rcx      # 0xc01a
pushq $0x49
jmp 0x290a
leaq 0x9723(%rip), %rdi      # 0xc004
leaq 0x972b(%rip), %rsi      # 0xc013
leaq 0x972b(%rip), %rcx      # 0xc01a
pushq $0x4f
jmp 0x290a
leaq 0x970a(%rip), %rdi      # 0xc004
leaq 0x9712(%rip), %rsi      # 0xc013
leaq 0x9712(%rip), %rcx      # 0xc01a
pushq $0x5b
popq %rdx
callq 0x21a0
movq %r13, %rdi
callq 0x7864
testq %rax, %rax
jne 0x293d
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2952
leaq 0x9783(%rip), %rdi      # 0xc0b1
movq %r13, %rsi
xorl %eax, %eax
callq 0x88e8
jmp 0x28a0
movq %rax, %r12
cmpq $0x0, (%rax)
jne 0x296b
leaq 0x9787(%rip), %rdi      # 0xc0d4
movq %r13, %rsi
jmp 0x29a1
movq %rax, %rbx
leaq 0x9755(%rip), %rdi      # 0xc0b1
movq %r13, %rsi
xorl %eax, %eax
callq 0x8759
jmp 0x289d
movq %r14, %rdi
callq 0x7864
testq %rax, %rax
jne 0x298e
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x29b5
leaq 0x9728(%rip), %rdi      # 0xc0b1
movq %r14, %rsi
jmp 0x2931
movq %rax, %r13
cmpq $0x0, (%rax)
jne 0x29c4
leaq 0x9736(%rip), %rdi      # 0xc0d4
movq %r14, %rsi
xorl %eax, %eax
callq 0x88e8
callq 0x20a0
andl $0x0, (%rax)
jmp 0x28a0
movq %rax, %rbx
leaq 0x96f2(%rip), %rdi      # 0xc0b1
movq %r14, %rsi
jmp 0x295f
movq %r15, %rdi
callq 0x7864
testq %rax, %rax
jne 0x29ea
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2a01
leaq 0x96cf(%rip), %rdi      # 0xc0b1
movq %r15, %rsi
jmp 0x2931
movq %rax, 0x20(%rsp)
cmpq $0x0, (%rax)
jne 0x2a13
leaq 0x96d8(%rip), %rdi      # 0xc0d4
movq %r15, %rsi
jmp 0x29a1
movq %rax, %rbx
leaq 0x96a6(%rip), %rdi      # 0xc0b1
movq %r15, %rsi
jmp 0x295f
cmpq $0x0, 0x8(%r12)
je 0x2a36
leaq 0x96cc(%rip), %rdi      # 0xc0ee
movq 0x18(%rsp), %rsi
xorl %eax, %eax
callq 0x88e8
callq 0x20a0
andl $0x0, (%rax)
movq (%r12), %rdi
callq 0x7e95
cmpl $-0x1, %eax
je 0x28a0
movl %eax, %ebx
movq %r13, %rdi
callq 0x803d
movl %eax, 0x48(%rsp)
cmpl $-0x1, %eax
je 0x28a0
movq 0x20(%rsp), %rdi
callq 0x803d
movl %eax, 0xc(%rsp)
cmpl $-0x1, %eax
je 0x28a0
movl 0x48(%rsp), %edi
callq 0x9be4
movq %rax, 0x58(%rsp)
testq %rax, %rax
je 0x2a9c
movl 0xc(%rsp), %edi
callq 0x9be4
movq %rax, 0x68(%rsp)
testq %rax, %rax
jne 0x2acf
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2ab9
leaq 0x9677(%rip), %rdi      # 0xc124
xorl %eax, %eax
callq 0x88e8
jmp 0x28a0
movq %rax, %rbx
leaq 0x9661(%rip), %rdi      # 0xc124
xorl %eax, %eax
callq 0x8759
jmp 0x289d
leaq 0x78(%rsp), %rsi
leaq 0x80(%rsp), %rdx
movq 0x68(%rsp), %rdi
callq 0x3c54
movq %rax, 0x50(%rsp)
testq %rax, %rax
jne 0x2b03
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2b23
leaq 0x964b(%rip), %rdi      # 0xc14c
jmp 0x2aad
movq 0x78(%rsp), %r14
leaq -0x2001(%r14), %rax
cmpq $-0x1e02, %rax          # imm = 0xE1FE
ja 0x2b2f
leaq 0x9658(%rip), %rdi      # 0xc176
jmp 0x299e
movq %rax, %rbx
leaq 0x961f(%rip), %rdi      # 0xc14c
jmp 0x2ac3
movq 0x58(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x398c
testq %rax, %rax
jne 0x2b59
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2b91
leaq 0x9646(%rip), %rdi      # 0xc19a
jmp 0x2aad
movq %rax, %r15
leaq 0x80(%rsp), %rdx
movq 0x58(%rsp), %rdi
movq %r14, %rsi
movq 0x50(%rsp), %rcx
callq 0x348c
testq %rax, %rax
jne 0x2ba0
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2be4
leaq 0x9634(%rip), %rdi      # 0xc1c0
jmp 0x2aad
movq %rax, %rbx
leaq 0x95ff(%rip), %rdi      # 0xc19a
jmp 0x2ac3
movq %rax, %r14
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
jne 0x2bf8
leaq 0x9633(%rip), %rsi      # 0xc1e7
leaq 0x28(%rsp), %rdi
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x6440
cmpl $-0x1, %eax
jne 0x2bf3
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2d07
leaq 0x960f(%rip), %rdi      # 0xc1ee
jmp 0x2aad
movq %rax, %rbx
leaq 0x95d2(%rip), %rdi      # 0xc1c0
jmp 0x2ac3
movq 0x28(%rsp), %rdi
callq 0x6534
testl %eax, %eax
jne 0x2cbd
movq %r14, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x2e28
movq %rax, %rbp
testq %rax, %rax
je 0x28a0
callq 0x4478
testl %eax, %eax
jne 0x2cd3
movq %rbp, %rdi
callq 0x3058
testl %eax, %eax
jne 0x2c1e
movq %rbp, %rdi
callq 0x3074
testl %eax, %eax
jne 0x28a0
cmpl $0x0, 0x60(%rsp)
je 0x2c05
movq %r14, %rdi
callq 0x3960
movq %r15, %rdi
callq 0x3b76
movq 0x50(%rsp), %rdi
callq 0x412f
movq 0x68(%rsp), %r14
movq %r14, %rdi
callq 0xa0c7
movq %r14, %rdi
callq 0xa0db
movq 0x58(%rsp), %r14
movq %r14, %rdi
callq 0xa0c7
movq %r14, %rdi
callq 0xa0db
movl 0xc(%rsp), %edi
callq 0x21c0
testl %eax, %eax
je 0x2d2b
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2d16
leaq 0xa4e8(%rip), %rdi      # 0xd19c
xorl %eax, %eax
callq 0x88e8
jmp 0x2d2b
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2ce9
leaq 0x9529(%rip), %rdi      # 0xc1f7
jmp 0x2aad
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2cf8
leaq 0x9527(%rip), %rdi      # 0xc20b
jmp 0x2aad
movq %rax, %rbx
leaq 0x9504(%rip), %rdi      # 0xc1f7
jmp 0x2ac3
movq %rax, %rbx
leaq 0x9509(%rip), %rdi      # 0xc20b
jmp 0x2ac3
movq %rax, %rbx
leaq 0x94dd(%rip), %rdi      # 0xc1ee
jmp 0x2ac3
movq %rax, %r14
leaq 0xa47c(%rip), %rdi      # 0xd19c
xorl %eax, %eax
callq 0x8759
andl $0x0, (%r14)
movl 0x48(%rsp), %edi
callq 0x21c0
testl %eax, %eax
je 0x2d67
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2d52
leaq 0xa453(%rip), %rdi      # 0xd19c
xorl %eax, %eax
callq 0x88e8
jmp 0x2d67
movq %rax, %r14
leaq 0xa440(%rip), %rdi      # 0xd19c
xorl %eax, %eax
callq 0x8759
andl $0x0, (%r14)
movl %ebx, %edi
callq 0x21c0
testl %eax, %eax
je 0x2da0
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2d8c
leaq 0xa419(%rip), %rdi      # 0xd19c
xorl %eax, %eax
callq 0x88e8
jmp 0x2da0
movq %rax, %rbx
leaq 0xa406(%rip), %rdi      # 0xd19c
xorl %eax, %eax
callq 0x8759
andl $0x0, (%rbx)
movq 0x20(%rsp), %rdi
callq 0x7e63
movq %r13, %rdi
callq 0x7e63
movq %r12, %rdi
callq 0x7e63
movq 0x18(%rsp), %rdi
callq 0x2040
movq 0x28(%rsp), %rdi
callq 0x2040
movq 0x38(%rsp), %rdi
callq 0x2040
movq 0x40(%rsp), %rdi
callq 0x2040
jmp 0x2822