// File: /workspace/github_c_makefile_star2_O2/Tarsnap[P]kivaloo/lbs-s3/main.c
// Function: main
int
main(int argc, char * argv[])
{
	/* State variables. */
	struct wire_requestqueue * Q_S3;
	struct deleteto * deleter;
	struct s3state * S;
	struct dispatch_state * D;
	int s;
	int s_t;

	/* Command-line parameters. */
	char * opt_s = NULL;
	char * opt_t = NULL;
	size_t opt_b = (size_t)(-1);
	char * opt_B = NULL;
	char * opt_p = NULL;
	int opt_1 = 0;

	/* Working variables. */
	struct sock_addr ** sas_s;
	struct sock_addr ** sas_t;
	const char * ch;

	WARNP_INIT;

	/* Parse the command line. */
	while ((ch = GETOPT(argc, argv)) != NULL) {
		GETOPT_SWITCH(ch) {
		GETOPT_OPTARG("-B"):
			if (opt_B != NULL)
				usage();
			if ((opt_B = strdup(optarg)) == NULL)
				OPT_EPARSE(ch, optarg);
			break;
		GETOPT_OPTARG("-b"):
			if (opt_b != (size_t)(-1))
				usage();
			if (PARSENUM(&opt_b, optarg, 512, 128 * 1024)) {
				warn0("Block size must be in [2^9, 2^17]");
				exit(1);
			}
			break;
		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_OPT("--version"):
			fprintf(stderr, "kivaloo-lbs-s3 @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_B == NULL)
		usage();
	if (opt_b == (size_t)(-1))
		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 (S3 daemon) address. */
	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);
	}

	/* 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 S3 daemon. */
	if ((s_t = sock_connect(sas_t)) == -1)
		exit(1);

	/* Create a queue of requests to the S3 daemon. */
	if ((Q_S3 = wire_requestqueue_init(s_t)) == NULL) {
		warnp("Cannot create S3 request queue");
		exit(1);
	}

	/* Create a deleter state. */
	if ((deleter = deleteto_init(Q_S3, opt_B)) == NULL) {
		warnp("Error initializing garbage collection for"
		    " S3 bucket: %s", opt_B);
		exit(1);
	}

	/* Initialize the S3 state. */
	if ((S = s3state_init(Q_S3, opt_B, opt_b, deleter)) == NULL) {
		warnp("Error initializing from S3 bucket: %s", opt_B);
		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, 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 S3 state. */
	s3state_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 S3 request queue. */
	wire_requestqueue_destroy(Q_S3);
	wire_requestqueue_free(Q_S3);

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

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

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

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
andq $0x0, 0x30(%rsp)
movl %edi, 0x48(%rsp)
movq %rsi, 0x50(%rsp)
movq (%rsi), %rdi
testq %rdi, %rdi
je 0x2506
callq 0x8ab4
movq $0x0, 0x10(%rsp)
pushq $-0x1
popq %rax
movq %rax, 0x38(%rsp)
leaq 0x212da(%rip), %r13     # 0x237f8
pushq $0x1
popq %rbx
leaq 0x9b58(%rip), %r12      # 0xc080
pushq $0x69
popq %r14
movl $0x0, 0x24(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x18(%rsp)
movl 0x48(%rsp), %edi
movq 0x50(%rsp), %rsi
callq 0x6d70
testq %rax, %rax
je 0x28a5
movq %rax, %r15
movq $0x40, 0x8(%rsp)
movq 0x8(%rsp), %rax
decq %rax
movq %rax, 0x58(%rsp)
movl $0x0, 0x44(%rsp)
pushq $0x41
popq %rbp
cmpl $0x0, (%r13)
je 0x2597
movq %r15, %rdi
callq 0x7185
addq 0x8(%rsp), %rax
jmp 0x25a5
movq 0x58(%rsp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x58(%rsp)
cmpq $0x41, %rax
je 0x26f5
cmpq $0x47, %rax
je 0x2684
cmpq $0x4f, %rax
je 0x26d9
cmpq $0x55, %rax
je 0x26bd
cmpq $0x5b, %rax
je 0x2627
cmpq $0x61, %rax
je 0x2662
cmpq $0x64, %rax
je 0x2646
cmpq $0x69, %rax
je 0x26a0
cmpq $0x6c, %rax
je 0x271b
cmpl $0x0, (%r13)
jne 0x2b6b
cmpl $0x0, 0x44(%rsp)
jne 0x2581
pushq $0x6c
popq %rdi
subq 0x8(%rsp), %rdi
callq 0x74f2
movl $0x1, 0x44(%rsp)
jmp 0x2581
cmpl $0x0, (%r13)
jne 0x2728
pushq $0x5b
popq %rsi
subq 0x8(%rsp), %rsi
leaq 0x9a3c(%rip), %rdi      # 0xc07d
jmp 0x270f
cmpl $0x0, (%r13)
jne 0x2759
pushq $0x64
popq %rsi
subq 0x8(%rsp), %rsi
leaq 0xb20b(%rip), %rdi      # 0xd86b
jmp 0x2678
cmpl $0x0, (%r13)
jne 0x2b74
pushq $0x61
popq %rsi
subq 0x8(%rsp), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x729d
jmp 0x2581
cmpl $0x0, (%r13)
jne 0x27cf
pushq $0x47
popq %rsi
subq 0x8(%rsp), %rsi
leaq 0x99b4(%rip), %rdi      # 0xc052
jmp 0x270f
cmpl $0x0, (%r13)
jne 0x2b97
movq %r14, %rdi
subq 0x8(%rsp), %rdi
callq 0x7467
jmp 0x2581
cmpl $0x0, (%r13)
jne 0x279e
pushq $0x55
popq %rsi
subq 0x8(%rsp), %rsi
leaq 0x99a3(%rip), %rdi      # 0xc07a
jmp 0x270f
cmpl $0x0, (%r13)
jne 0x276d
pushq $0x4f
popq %rsi
subq 0x8(%rsp), %rsi
leaq 0x9984(%rip), %rdi      # 0xc077
jmp 0x270f
cmpl $0x0, (%r13)
jne 0x2849
movq %rbp, %rsi
subq 0x8(%rsp), %rsi
leaq 0x9922(%rip), %rdi      # 0xc031
movl %ebx, %edx
callq 0x729d
jmp 0x2581
movl $0x1, (%r13)
jmp 0x2546
leaq 0x210c1(%rip), %rax     # 0x237f0
movq (%rax), %rbp
testq %rbp, %rbp
je 0x2c14
cmpq $0x0, 0x28(%rsp)
jne 0x2bb0
movq %rbp, %rdi
callq 0x2390
movq %rax, 0x28(%rsp)
jmp 0x2875
cmpl $0x0, 0x24(%rsp)
movl %ebx, 0x24(%rsp)
je 0x2546
jmp 0x2bb0
leaq 0x2107c(%rip), %rax     # 0x237f0
movq (%rax), %rbp
testq %rbp, %rbp
je 0x2c46
cmpq $0x0, 0x30(%rsp)
jne 0x2bb0
movq %rbp, %rdi
callq 0x2390
movq %rax, 0x30(%rsp)
jmp 0x2875
leaq 0x2104b(%rip), %rax     # 0x237f0
movq (%rax), %rbp
testq %rbp, %rbp
je 0x2c2d
cmpq $0x0, 0x18(%rsp)
jne 0x2bb0
movq %rbp, %rdi
callq 0x2390
movq %rax, 0x18(%rsp)
jmp 0x2875
leaq 0x2101a(%rip), %rax     # 0x237f0
movq (%rax), %rbp
testq %rbp, %rbp
je 0x2be2
cmpq $-0x1, 0x38(%rsp)
jne 0x2bb0
callq 0x20a0
movq %rax, %r15
andl $0x0, (%rax)
movq %rbp, %rdi
leaq 0x60(%rsp), %rsi
xorl %edx, %edx
callq 0x2310
movq %rax, 0x38(%rsp)
movq 0x60(%rsp), %rcx
pushq $0x16
popq %rax
cmpq %rbp, %rcx
je 0x2837
cmpb $0x0, (%rcx)
jne 0x2837
movq 0x38(%rsp), %rax
leaq -0x20001(%rax), %rcx
pushq $0x22
popq %rax
cmpq $-0x1fe02, %rcx         # imm = 0xFFFE01FE
ja 0x283a
movl %eax, (%r15)
cmpl $0x0, (%r15)
je 0x2546
jmp 0x2bce
leaq 0x20fa0(%rip), %rax     # 0x237f0
movq (%rax), %rbp
testq %rbp, %rbp
je 0x2bfb
cmpq $0x0, 0x10(%rsp)
jne 0x2bb0
movq %rbp, %rdi
callq 0x2390
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0x2546
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2bb5
leaq 0x97a1(%rip), %rdi      # 0xc034
movq %r15, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x8cc0
jmp 0x2b63
leaq 0x10c9c(%rip), %rax     # 0x13548
movl 0x48(%rsp), %ecx
cmpl %ecx, (%rax)
jne 0x2bb0
movq 0x18(%rsp), %r15
testq %r15, %r15
movq 0x10(%rsp), %rbx
movq 0x28(%rsp), %r14
je 0x2bb0
testq %r14, %r14
je 0x2bb0
testq %rbx, %rbx
je 0x2bb0
cmpq $-0x1, 0x38(%rsp)
je 0x2bb0
movq %r15, %rdi
callq 0x7c3c
testq %rax, %rax
je 0x2c63
movq %rax, %r12
cmpq $0x0, (%rax)
je 0x2c82
movq %r14, %rdi
callq 0x7c3c
testq %rax, %rax
je 0x2c8e
movq %rax, %r13
cmpq $0x0, (%rax)
je 0x2caa
cmpq $0x0, 0x8(%r12)
je 0x294b
leaq 0x97d4(%rip), %rdi      # 0xc10d
movq %r15, %rsi
xorl %eax, %eax
callq 0x8cc0
callq 0x20a0
andl $0x0, (%rax)
movq (%r12), %rdi
callq 0x826d
cmpl $-0x1, %eax
je 0x2b63
movl %eax, %ebp
movq %r13, %rdi
callq 0x8415
cmpl $-0x1, %eax
je 0x2b63
movl %eax, %r15d
movq %r13, 0x50(%rsp)
movq %r12, 0x48(%rsp)
movl %eax, %edi
callq 0xa1d8
testq %rax, %rax
je 0x2cc8
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0x37a8
testq %rax, %rax
je 0x2cde
movq %rax, %r13
movq %r12, %rdi
movq %rbx, %rsi
movq 0x38(%rsp), %rdx
movq %rax, %rcx
callq 0x33f4
testq %rax, %rax
je 0x2cf1
movq %rax, %r14
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
jne 0x29f6
leaq 0x97e9(%rip), %rsi      # 0xc1c0
leaq 0x30(%rsp), %rdi
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x6a98
cmpl $-0x1, %eax
je 0x2d90
movq 0x30(%rsp), %rdi
callq 0x6b8c
testl %eax, %eax
jne 0x2d13
movq %r14, %rdi
movl %ebp, %esi
callq 0x2df8
movq %rax, %rbx
testq %rax, %rax
je 0x2b63
callq 0x4ad0
testl %eax, %eax
jne 0x2b35
movq %rbx, %rdi
callq 0x3018
testl %eax, %eax
jne 0x2a19
movq %rbx, %rdi
callq 0x3034
testl %eax, %eax
jne 0x2b63
cmpl $0x0, 0x24(%rsp)
je 0x2a03
movq %r14, %rdi
callq 0x376b
movq %r13, %rdi
callq 0x3b1d
movq %r12, %rdi
callq 0xa6bb
movq %r12, %rdi
callq 0xa6cf
movl %r15d, %edi
callq 0x21c0
testl %eax, %eax
movq 0x28(%rsp), %r14
movq 0x18(%rsp), %r15
movq 0x48(%rsp), %r12
movq 0x50(%rsp), %r13
je 0x2ab7
callq 0x20a0
cmpl $0x0, (%rax)
je 0x2aa9
movq %rax, %rbx
leaq 0xa40f(%rip), %rdi      # 0xceac
xorl %eax, %eax
callq 0x8b31
andl $0x0, (%rbx)
jmp 0x2ab7
leaq 0xa3fc(%rip), %rdi      # 0xceac
xorl %eax, %eax
callq 0x8cc0
movl %ebp, %edi
callq 0x21c0
testl %eax, %eax
je 0x2af0
callq 0x20a0
cmpl $0x0, (%rax)
je 0x2ae2
movq %rax, %rbx
leaq 0xa3d6(%rip), %rdi      # 0xceac
xorl %eax, %eax
callq 0x8b31
andl $0x0, (%rbx)
jmp 0x2af0
leaq 0xa3c3(%rip), %rdi      # 0xceac
xorl %eax, %eax
callq 0x8cc0
movq %r13, %rdi
callq 0x823b
movq %r12, %rdi
callq 0x823b
movq %r15, %rdi
callq 0x2040
movq 0x30(%rsp), %rdi
callq 0x2040
movq %r14, %rdi
callq 0x2040
movq 0x10(%rsp), %rdi
callq 0x2040
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2b4f
leaq 0x969e(%rip), %rdi      # 0xc1e4
xorl %eax, %eax
callq 0x8cc0
jmp 0x2b63
movq %rax, %rbx
leaq 0x968b(%rip), %rdi      # 0xc1e4
xorl %eax, %eax
callq 0x8b31
andl $0x0, (%rbx)
pushq $0x1
popq %rdi
callq 0x2350
leaq 0x9549(%rip), %rdi      # 0xc0bb
jmp 0x2b9e
movq 0x1047d(%rip), %rax     # 0x12ff8
movq (%rax), %rcx
leaq 0x9505(%rip), %rdi      # 0xc08a
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
callq 0x2370
xorl %edi, %edi
callq 0x2350
leaq 0x9506(%rip), %rdi      # 0xc0a4
movq %r15, %rsi
xorl %eax, %eax
callq 0x8cc0
callq 0x20a0
andl $0x0, (%rax)
callq 0x2db5
movq %rax, %rbx
leaq 0x9475(%rip), %rdi      # 0xc034
movq %r15, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x8b31
jmp 0x2b60
leaq 0x9480(%rip), %rdi      # 0xc055
xorl %eax, %eax
callq 0x8cc0
andl $0x0, (%r15)
jmp 0x2b63
leaq 0x941b(%rip), %rdi      # 0xc004
leaq 0x9423(%rip), %rsi      # 0xc013
leaq 0x9423(%rip), %rcx      # 0xc01a
pushq $0x47
jmp 0x2c5d
leaq 0x9402(%rip), %rdi      # 0xc004
leaq 0x940a(%rip), %rsi      # 0xc013
leaq 0x940a(%rip), %rcx      # 0xc01a
pushq $0x41
jmp 0x2c5d
leaq 0x93e9(%rip), %rdi      # 0xc004
leaq 0x93f1(%rip), %rsi      # 0xc013
leaq 0x93f1(%rip), %rcx      # 0xc01a
pushq $0x5b
jmp 0x2c5d
leaq 0x93d0(%rip), %rdi      # 0xc004
leaq 0x93d8(%rip), %rsi      # 0xc013
leaq 0x93d8(%rip), %rcx      # 0xc01a
pushq $0x55
jmp 0x2c5d
leaq 0x93b7(%rip), %rdi      # 0xc004
leaq 0x93bf(%rip), %rsi      # 0xc013
leaq 0x93bf(%rip), %rcx      # 0xc01a
pushq $0x4f
popq %rdx
callq 0x21a0
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2d29
leaq 0x9458(%rip), %rdi      # 0xc0d0
movq 0x18(%rsp), %rsi
jmp 0x2d07
leaq 0x946a(%rip), %rdi      # 0xc0f3
movq %r15, %rsi
jmp 0x2cb4
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2d3a
leaq 0x942d(%rip), %rdi      # 0xc0d0
movq 0x28(%rsp), %rsi
jmp 0x2d07
leaq 0x9442(%rip), %rdi      # 0xc0f3
movq %r14, %rsi
xorl %eax, %eax
callq 0x8cc0
callq 0x20a0
andl $0x0, (%rax)
jmp 0x2b63
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2d4b
leaq 0x946a(%rip), %rdi      # 0xc143
jmp 0x2b46
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2d5a
leaq 0x9473(%rip), %rdi      # 0xc162
jmp 0x2d02
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2d66
leaq 0x9498(%rip), %rdi      # 0xc19a
movq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x8cc0
jmp 0x2b63
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2d81
leaq 0x94ac(%rip), %rdi      # 0xc1d0
jmp 0x2b46
movq %rax, %rbx
leaq 0x939d(%rip), %rdi      # 0xc0d0
movq 0x18(%rsp), %rsi
jmp 0x2d75
movq %rax, %rbx
leaq 0x938c(%rip), %rdi      # 0xc0d0
movq 0x28(%rsp), %rsi
jmp 0x2d75
movq %rax, %rbx
leaq 0x93ee(%rip), %rdi      # 0xc143
jmp 0x2b59
movq %rax, %rbx
leaq 0x93fe(%rip), %rdi      # 0xc162
jmp 0x2d70
movq %rax, %rbx
leaq 0x942a(%rip), %rdi      # 0xc19a
movq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x8b31
jmp 0x2b60
movq %rax, %rbx
leaq 0x9445(%rip), %rdi      # 0xc1d0
jmp 0x2b59
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2da6
leaq 0x9426(%rip), %rdi      # 0xc1c7
jmp 0x2b46
movq %rax, %rbx
leaq 0x9417(%rip), %rdi      # 0xc1c7
jmp 0x2b59