// File: /workspace/github_c_makefile_star2_O3/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
movl %edi, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
movq %rsi, 0x50(%rsp)
movq (%rsi), %rdi
testq %rdi, %rdi
je 0x29d9
callq 0x9840
movl 0x30(%rsp), %edi
movq 0x50(%rsp), %rsi
callq 0x7a6c
testq %rax, %rax
je 0x3187
movq %rax, %r15
movq $-0x1, 0x40(%rsp)
movq $0x0, 0x18(%rsp)
leaq 0x21dec(%rip), %rbp     # 0x247f8
leaq 0xa61e(%rip), %r14      # 0xd031
leaq 0xa663(%rip), %r13      # 0xd07d
leaq 0xa65f(%rip), %r12      # 0xd080
leaq 0xbedf(%rip), %rbx      # 0xe907
movq $0x0, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x40, 0x8(%rsp)
movq 0x8(%rsp), %rax
decq %rax
movq %rax, 0x58(%rsp)
movl $0x0, 0x4c(%rsp)
cmpl $0x0, (%rbp)
je 0x2a75
movq %r15, %rdi
callq 0x7db3
addq 0x8(%rsp), %rax
jmp 0x2a83
movq 0x58(%rsp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x58(%rsp)
cmpq $0x5a, %rax
jle 0x2abe
cmpq $0x63, %rax
jle 0x2af8
cmpq $0x64, %rax
je 0x2bad
cmpq $0x69, %rax
jne 0x2bd0
cmpl $0x0, (%rbp)
jne 0x3065
movl $0x69, %edi
subq 0x8(%rsp), %rdi
callq 0x7f81
jmp 0x2a60
cmpq $0x4e, %rax
jg 0x2b24
cmpq $0x41, %rax
je 0x2b6a
cmpq $0x47, %rax
jne 0x2bd6
cmpl $0x0, (%rbp)
jne 0x2cbe
movl $0x47, %esi
subq 0x8(%rsp), %rsi
leaq 0xa55f(%rip), %rdi      # 0xd052
jmp 0x2b9e
cmpq $0x5b, %rax
je 0x2b51
cmpq $0x61, %rax
jne 0x2bd6
cmpl $0x0, (%rbp)
jne 0x308f
movl $0x61, %esi
subq 0x8(%rsp), %rsi
movq %r12, %rdi
jmp 0x2bc4
cmpq $0x4f, %rax
je 0x2b83
cmpq $0x55, %rax
jne 0x2bd6
cmpl $0x0, (%rbp)
jne 0x2c75
movl $0x55, %esi
subq 0x8(%rsp), %rsi
leaq 0xa52b(%rip), %rdi      # 0xd07a
jmp 0x2b9e
cmpl $0x0, (%rbp)
jne 0x2c13
movl $0x5b, %esi
subq 0x8(%rsp), %rsi
movq %r13, %rdi
jmp 0x2b9e
cmpl $0x0, (%rbp)
jne 0x2d3b
movl $0x41, %esi
subq 0x8(%rsp), %rsi
movq %r14, %rdi
jmp 0x2b9e
cmpl $0x0, (%rbp)
jne 0x2c44
movl $0x4f, %esi
subq 0x8(%rsp), %rsi
leaq 0xa4d9(%rip), %rdi      # 0xd077
movl $0x1, %edx
callq 0x7e70
jmp 0x2a60
cmpl $0x0, (%rbp)
jne 0x2ca6
movl $0x64, %esi
subq 0x8(%rsp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x7e70
jmp 0x2a60
cmpq $0x6c, %rax
je 0x2c07
cmpl $0x0, (%rbp)
jne 0x306e
cmpl $0x0, 0x4c(%rsp)
jne 0x2a60
movl $0x6c, %edi
subq 0x8(%rsp), %rdi
callq 0x7fc1
movl $0x1, 0x4c(%rsp)
jmp 0x2a60
movl $0x1, (%rbp)
jmp 0x2d70
leaq 0x21bd6(%rip), %rax     # 0x247f0
movq (%rax), %r14
testq %r14, %r14
je 0x312a
cmpq $0x0, 0x28(%rsp)
jne 0x3094
movq %r14, %rdi
callq 0x2390
movq %rax, 0x28(%rsp)
jmp 0x2d67
leaq 0x21ba5(%rip), %rax     # 0x247f0
movq (%rax), %r14
testq %r14, %r14
je 0x3168
cmpq $0x0, 0x38(%rsp)
jne 0x3094
movq %r14, %rdi
callq 0x2390
movq %rax, 0x38(%rsp)
jmp 0x2d67
leaq 0x21b74(%rip), %rax     # 0x247f0
movq (%rax), %r14
testq %r14, %r14
je 0x3149
cmpq $0x0, 0x18(%rsp)
jne 0x3094
movq %r14, %rdi
callq 0x2390
movq %rax, 0x18(%rsp)
jmp 0x2d67
cmpl $0x0, 0x24(%rsp)
movl $0x1, 0x24(%rsp)
je 0x2d70
jmp 0x3094
leaq 0x21b2b(%rip), %rax     # 0x247f0
movq (%rax), %r15
testq %r15, %r15
je 0x30ec
cmpq $-0x1, 0x40(%rsp)
jne 0x3094
callq 0x20a0
movq %rax, %r14
movl $0x0, (%rax)
movq %r15, %rdi
leaq 0x60(%rsp), %rsi
xorl %edx, %edx
callq 0x2310
movq %rax, 0x40(%rsp)
movq 0x60(%rsp), %rcx
movl $0x16, %eax
cmpq %r15, %rcx
je 0x2d2d
cmpb $0x0, (%rcx)
jne 0x2d2d
movq 0x40(%rsp), %rax
leaq -0x20001(%rax), %rcx
movl $0x22, %eax
cmpq $-0x1fe02, %rcx         # imm = 0xFFFE01FE
ja 0x2d30
movl %eax, (%r14)
cmpl $0x0, (%r14)
je 0x2d70
jmp 0x30d2
leaq 0x21aae(%rip), %rax     # 0x247f0
movq (%rax), %r14
testq %r14, %r14
je 0x310b
cmpq $0x0, 0x10(%rsp)
jne 0x3094
movq %r14, %rdi
callq 0x2390
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x3099
movl 0x30(%rsp), %edi
movq 0x50(%rsp), %rsi
callq 0x7a6c
movq %rax, %r15
testq %rax, %rax
leaq 0xa2a6(%rip), %r14      # 0xd031
jne 0x2a42
leaq 0x117b0(%rip), %rax     # 0x14548
movl 0x30(%rsp), %ecx
cmpl %ecx, (%rax)
movq 0x18(%rsp), %r14
jne 0x3094
testq %r14, %r14
je 0x3094
movq 0x28(%rsp), %r15
testq %r15, %r15
movq 0x10(%rsp), %rbx
je 0x3094
testq %rbx, %rbx
je 0x3094
cmpq $-0x1, 0x40(%rsp)
je 0x3094
movq %r14, %rdi
callq 0x883c
testq %rax, %rax
je 0x3199
movq %rax, %r12
cmpq $0x0, (%rax)
je 0x31b8
movq %r15, %rdi
callq 0x883c
testq %rax, %rax
je 0x31c4
movq %rax, %r13
cmpq $0x0, (%rax)
je 0x31e0
cmpq $0x0, 0x8(%r12)
je 0x2e3a
leaq 0xa2e8(%rip), %rdi      # 0xd10d
movq %r14, %rsi
xorl %eax, %eax
callq 0x99df
callq 0x20a0
movl $0x0, (%rax)
movq (%r12), %rdi
callq 0x8f92
cmpl $-0x1, %eax
je 0x305b
movl %eax, %ebp
movq %r13, %rdi
callq 0x9154
cmpl $-0x1, %eax
je 0x305b
movl %eax, %r15d
movq %r13, 0x50(%rsp)
movq %r12, 0x30(%rsp)
movl %eax, %edi
callq 0xb01c
testq %rax, %rax
je 0x3201
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3d68
testq %rax, %rax
je 0x3217
movq %rax, %r13
movq %r12, %rdi
movq %rbx, %rsi
movq 0x40(%rsp), %rdx
movq %rax, %rcx
callq 0x3998
testq %rax, %rax
je 0x322a
movq %rax, %r14
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
jne 0x2ee5
leaq 0xa2fa(%rip), %rsi      # 0xd1c0
leaq 0x38(%rsp), %rdi
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x7778
cmpl $-0x1, %eax
je 0x32c9
movq 0x38(%rsp), %rdi
callq 0x787c
testl %eax, %eax
jne 0x324c
movq %r14, %rdi
movl %ebp, %esi
callq 0x3334
movq %rax, %rbx
testq %rax, %rax
je 0x305b
callq 0x50d4
testl %eax, %eax
jne 0x302a
movq %rbx, %rdi
callq 0x356e
testl %eax, %eax
jne 0x2f08
movq %rbx, %rdi
callq 0x358c
testl %eax, %eax
jne 0x305b
cmpl $0x0, 0x24(%rsp)
je 0x2ef2
movq %r14, %rdi
callq 0x3d2a
movq %r13, %rdi
callq 0x4114
movq %r12, %rdi
callq 0xb70d
movq %r12, %rdi
callq 0xb721
movl %r15d, %edi
callq 0x21c0
testl %eax, %eax
movq 0x18(%rsp), %r14
movq 0x28(%rsp), %r15
movq 0x30(%rsp), %r12
movq 0x50(%rsp), %r13
je 0x2fa9
callq 0x20a0
cmpl $0x0, (%rax)
je 0x2f9b
movq %rax, %rbx
leaq 0xaf20(%rip), %rdi      # 0xdeac
xorl %eax, %eax
callq 0x98c0
movl $0x0, (%rbx)
jmp 0x2fa9
leaq 0xaf0a(%rip), %rdi      # 0xdeac
xorl %eax, %eax
callq 0x99df
movl %ebp, %edi
callq 0x21c0
testl %eax, %eax
je 0x2fe5
callq 0x20a0
cmpl $0x0, (%rax)
je 0x2fd7
movq %rax, %rbx
leaq 0xaee4(%rip), %rdi      # 0xdeac
xorl %eax, %eax
callq 0x98c0
movl $0x0, (%rbx)
jmp 0x2fe5
leaq 0xaece(%rip), %rdi      # 0xdeac
xorl %eax, %eax
callq 0x99df
movq %r13, %rdi
callq 0x8f4e
movq %r12, %rdi
callq 0x8f4e
movq %r14, %rdi
callq 0x2040
movq 0x38(%rsp), %rdi
callq 0x2040
movq %r15, %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 0x3044
leaq 0xa1a9(%rip), %rdi      # 0xd1e4
xorl %eax, %eax
callq 0x99df
jmp 0x305b
movq %rax, %rbx
leaq 0xa196(%rip), %rdi      # 0xd1e4
xorl %eax, %eax
callq 0x98c0
movl $0x0, (%rbx)
movl $0x1, %edi
callq 0x2350
leaq 0xa038(%rip), %rdi      # 0xd0a4
jmp 0x3075
leaq 0xa046(%rip), %rdi      # 0xd0bb
movq %r15, %rsi
xorl %eax, %eax
callq 0x99df
callq 0x20a0
movl $0x0, (%rax)
callq 0x32ee
callq 0x23f0
callq 0x32ee
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x30b9
leaq 0x9f8a(%rip), %rdi      # 0xd034
movq %r15, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x99df
jmp 0x305b
movq %rax, %rbx
leaq 0x9f71(%rip), %rdi      # 0xd034
movq %r15, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x98c0
jmp 0x3055
leaq 0x9f7c(%rip), %rdi      # 0xd055
xorl %eax, %eax
callq 0x99df
movl $0x0, (%r14)
jmp 0x305b
leaq 0x9f11(%rip), %rdi      # 0xd004
leaq 0x9f19(%rip), %rsi      # 0xd013
leaq 0x9f19(%rip), %rcx      # 0xd01a
movl $0x47, %edx
callq 0x21a0
leaq 0x9ef2(%rip), %rdi      # 0xd004
leaq 0x9efa(%rip), %rsi      # 0xd013
leaq 0x9efa(%rip), %rcx      # 0xd01a
movl $0x41, %edx
callq 0x21a0
leaq 0x9ed3(%rip), %rdi      # 0xd004
leaq 0x9edb(%rip), %rsi      # 0xd013
leaq 0x9edb(%rip), %rcx      # 0xd01a
movl $0x5b, %edx
callq 0x21a0
leaq 0x9eb4(%rip), %rdi      # 0xd004
leaq 0x9ebc(%rip), %rsi      # 0xd013
leaq 0x9ebc(%rip), %rcx      # 0xd01a
movl $0x55, %edx
callq 0x21a0
leaq 0x9e95(%rip), %rdi      # 0xd004
leaq 0x9e9d(%rip), %rsi      # 0xd013
leaq 0x9e9d(%rip), %rcx      # 0xd01a
movl $0x4f, %edx
callq 0x21a0
leaq 0x113ba(%rip), %rax     # 0x14548
movl 0x30(%rsp), %ecx
cmpl %ecx, (%rax)
callq 0x32ee
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x3262
leaq 0x9f22(%rip), %rdi      # 0xd0d0
movq 0x18(%rsp), %rsi
jmp 0x3240
leaq 0x9f34(%rip), %rdi      # 0xd0f3
movq %r14, %rsi
jmp 0x31ea
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x3273
leaq 0x9ef7(%rip), %rdi      # 0xd0d0
movq 0x28(%rsp), %rsi
jmp 0x3240
leaq 0x9f0c(%rip), %rdi      # 0xd0f3
movq %r15, %rsi
xorl %eax, %eax
callq 0x99df
callq 0x20a0
movl $0x0, (%rax)
jmp 0x305b
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x3284
leaq 0x9f31(%rip), %rdi      # 0xd143
jmp 0x303b
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x3293
leaq 0x9f3a(%rip), %rdi      # 0xd162
jmp 0x323b
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x329f
leaq 0x9f5f(%rip), %rdi      # 0xd19a
movq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x99df
jmp 0x305b
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x32ba
leaq 0x9f73(%rip), %rdi      # 0xd1d0
jmp 0x303b
movq %rax, %rbx
leaq 0x9e64(%rip), %rdi      # 0xd0d0
movq 0x18(%rsp), %rsi
jmp 0x32ae
movq %rax, %rbx
leaq 0x9e53(%rip), %rdi      # 0xd0d0
movq 0x28(%rsp), %rsi
jmp 0x32ae
movq %rax, %rbx
leaq 0x9eb5(%rip), %rdi      # 0xd143
jmp 0x304e
movq %rax, %rbx
leaq 0x9ec5(%rip), %rdi      # 0xd162
jmp 0x32a9
movq %rax, %rbx
leaq 0x9ef1(%rip), %rdi      # 0xd19a
movq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x98c0
jmp 0x3055
movq %rax, %rbx
leaq 0x9f0c(%rip), %rdi      # 0xd1d0
jmp 0x304e
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x32df
leaq 0x9eed(%rip), %rdi      # 0xd1c7
jmp 0x303b
movq %rax, %rbx
leaq 0x9ede(%rip), %rdi      # 0xd1c7
jmp 0x304e