// File: /workspace/github_c_makefile_star2_O3/Tarsnap[P]kivaloo/mux/main.c
// Function: main
int
main(int argc, char * argv[])
{
	/* State variables. */
	int * socks_s;
	int sock_t;
	struct wire_requestqueue * Q_t;
	struct dispatch_state * dstate;

	/* Command-line parameters. */
	size_t opt_n = 0;
	char * opt_p = NULL;
	char * opt_t = NULL;
	ADDRLIST opt_s;
	char * opt_s_1 = NULL;

	/* Working variables. */
	size_t opt_s_size;
	struct sock_addr ** sas;
	size_t i;
	const char * ch;

	WARNP_INIT;

	/* We have no addresses to listen on yet. */
	if ((opt_s = addrlist_init(0)) == NULL) {
		warnp("addrlist_init");
		exit(1);
	}

	/* Parse the command line. */
	while ((ch = GETOPT(argc, argv)) != NULL) {
		GETOPT_SWITCH(ch) {
		GETOPT_OPTARG("-n"):
			if (opt_n != 0)
				usage();
			if (PARSENUM(&opt_n, optarg, 0, 65535)) {
				warn0("Invalid option: -n %s", optarg);
				usage();
			}
			break;
		GETOPT_OPTARG("-p"):
			if (opt_p != NULL)
				usage();
			if ((opt_p = strdup(optarg)) == NULL)
				OPT_EPARSE(ch, optarg);
			break;
		GETOPT_OPTARG("-s"):
			/* Keep a copy of the path for pidfile generation. */
			if ((opt_s_1 == NULL) &&
			    ((opt_s_1 = strdup(optarg)) == NULL))
				OPT_EPARSE(ch, optarg);

			/* Attempt to resolve to a list of addresses. */
			if ((sas = sock_resolve(optarg)) == NULL) {
				warnp("Cannot resolve address: %s", optarg);
				exit(1);
			}
			if (sas[0] == NULL) {
				warn0("No addresses found for %s", optarg);
				exit(1);
			}

			/* Push pointers to addresses onto the list. */
			for (i = 0; sas[i] != NULL; i++) {
				if (addrlist_append(opt_s, &sas[i], 1))
					OPT_EPARSE(ch, optarg);
			}

			/* Free the array (but keep the addresses). */
			free(sas);
			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-mux @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_s_size = addrlist_getsize(opt_s)) == 0)
		usage();
	if (opt_t == NULL)
		usage();

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

	/* Connect to the target. */
	if ((sock_t = sock_connect(sas)) == -1)
		exit(1);

	/* Free the target address(es). */
	sock_addr_freelist(sas);

	/* Create a queue of requests to the target. */
	if ((Q_t = wire_requestqueue_init(sock_t)) == NULL) {
		warnp("Cannot create request queue");
		exit(1);
	}

	/* Allocate array of source sockets. */
	if ((socks_s = malloc(opt_s_size * sizeof(int))) == NULL) {
		warnp("malloc");
		exit(1);
	}

	/* Create listening sockets. */
	for (i = 0; i < opt_s_size; i++) {
		if ((socks_s[i] =
		    sock_listener(*addrlist_get(opt_s, i))) == -1)
			exit(1);
	}

	/* Initialize the dispatcher. */
	if ((dstate = dispatch_init(socks_s, opt_s_size,
	    Q_t, opt_n ? opt_n : SIZE_MAX)) == NULL) {
		warnp("Failed to initialize dispatcher");
		exit(1);
	}

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

	/* Loop until the dispatcher is finished. */
	do {
		if (events_run()) {
			warnp("Error running event loop");
			exit(1);
		}
	} while (dispatch_alive(dstate));

	/* Clean up the dispatcher. */
	dispatch_done(dstate);

	/* Shut down the request queue. */
	wire_requestqueue_destroy(Q_t);
	wire_requestqueue_free(Q_t);

	/* Close sockets. */
	for (i = 0; i < opt_s_size; i++) {
		if (close(socks_s[i]))
			warnp("close");
	}
	free(socks_s);
	if (close(sock_t))
		warnp("close");

	/* Free source socket addresses. */
	for (i = 0; i < addrlist_getsize(opt_s); i++)
		sock_addr_free(*addrlist_get(opt_s, i));
	addrlist_free(opt_s);

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

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edi, 0xc(%rsp)
movq $0x0, 0x18(%rsp)
movq %rsi, 0x40(%rsp)
movq (%rsi), %rdi
testq %rdi, %rdi
je 0x29d9
callq 0x8980
movl $0x8, %esi
xorl %edi, %edi
callq 0x3e38
testq %rax, %rax
je 0x318f
movq %rax, %rbx
movl 0xc(%rsp), %edi
movq 0x40(%rsp), %rsi
callq 0x6cb4
testq %rax, %rax
je 0x2d51
movq %rax, %r15
movq $0x0, 0x28(%rsp)
leaq 0x5ed8d(%rip), %r13     # 0x617a8
leaq 0x861d(%rip), %r14      # 0xb03f
leaq 0x868b(%rip), %r12      # 0xb0b4
movq $0x0, 0x20(%rsp)
xorl %ebp, %ebp
movq $0x48, 0x10(%rsp)
movq 0x10(%rsp), %rax
decq %rax
movq %rax, 0x48(%rsp)
movl $0x0, 0x34(%rsp)
cmpl $0x0, (%r13)
je 0x2a68
movq %r15, %rdi
callq 0x6ffb
addq 0x10(%rsp), %rax
jmp 0x2a76
movq 0x48(%rsp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x48(%rsp)
cmpq $0x6f, %rax
jle 0x2ab7
cmpq $0x78, %rax
jg 0x2aef
cmpq $0x70, %rax
je 0x2b18
cmpq $0x76, %rax
jne 0x2b81
cmpl $0x0, (%r13)
jne 0x301a
movl $0x76, %esi
subq 0x10(%rsp), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x70b8
jmp 0x2a52
cmpq $0x49, %rax
je 0x2b36
cmpq $0x51, %rax
je 0x2b50
cmpq $0x57, %rax
jne 0x2b81
cmpl $0x0, (%r13)
jne 0x2bf5
movl $0x57, %esi
subq 0x10(%rsp), %rsi
leaq 0x858c(%rip), %rdi      # 0xb079
jmp 0x2b6c
cmpq $0x79, %rax
jne 0x2b7b
cmpl $0x0, (%r13)
jne 0x3011
movl $0x79, %edi
subq 0x10(%rsp), %rdi
callq 0x71c9
jmp 0x2a52
cmpl $0x0, (%r13)
jne 0x2bc0
movl $0x70, %esi
subq 0x10(%rsp), %rsi
leaq 0x857d(%rip), %rdi      # 0xb0b1
jmp 0x2b6c
cmpl $0x0, (%r13)
jne 0x2c86
movl $0x49, %esi
subq 0x10(%rsp), %rsi
movq %r14, %rdi
jmp 0x2b6c
cmpl $0x0, (%r13)
jne 0x2cf9
movl $0x51, %esi
subq 0x10(%rsp), %rsi
leaq 0x84ec(%rip), %rdi      # 0xb058
movl $0x1, %edx
callq 0x70b8
jmp 0x2a52
cmpq $0x7c, %rax
je 0x2bb3
cmpl $0x0, (%r13)
jne 0x301f
cmpl $0x0, 0x34(%rsp)
jne 0x2a52
movl $0x7c, %edi
subq 0x10(%rsp), %rdi
callq 0x7209
movl $0x1, 0x34(%rsp)
jmp 0x2a52
movl $0x1, (%r13)
jmp 0x2d2e
leaq 0x5ebd9(%rip), %rax     # 0x617a0
movq (%rax), %r14
testq %r14, %r14
je 0x315d
testq %rbp, %rbp
jne 0x306d
movq %r14, %rdi
callq 0x2390
testq %rax, %rax
je 0x3072
movq %rax, %rbp
jmp 0x2d2e
leaq 0x5eba4(%rip), %rax     # 0x617a0
movq (%rax), %r14
testq %r14, %r14
je 0x30dc
movq %rbp, 0x38(%rsp)
cmpq $0x0, 0x20(%rsp)
jne 0x2c2b
movq %r14, %rdi
callq 0x2390
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x3072
movq %r14, %rdi
callq 0x797c
testq %rax, %rax
je 0x30b5
movq %rax, %rbp
cmpq $0x0, (%rax)
movq %rax, %r14
je 0x317c
movl $0x1, %edx
movl $0x8, %ecx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3fa7
testl %eax, %eax
jne 0x2fe0
cmpq $0x0, 0x8(%r14)
leaq 0x8(%r14), %r14
jne 0x2c4c
movq %rbp, %rdi
callq 0x2040
movq 0x38(%rsp), %rbp
jmp 0x2d2e
leaq 0x5eb13(%rip), %rax     # 0x617a0
movq (%rax), %r15
testq %r15, %r15
je 0x313e
cmpq $0x0, 0x28(%rsp)
jne 0x306d
callq 0x20a0
movq %rax, %r14
movl $0x0, (%rax)
movq %r15, %rdi
leaq 0x50(%rsp), %rsi
xorl %edx, %edx
callq 0x2310
movq %rax, 0x28(%rsp)
movq 0x50(%rsp), %rcx
movl $0x16, %eax
cmpq %r15, %rcx
je 0x2ceb
cmpb $0x0, (%rcx)
jne 0x2ceb
movl $0x22, %eax
cmpq $0x10000, 0x28(%rsp)    # imm = 0x10000
jb 0x2cee
movl %eax, (%r14)
cmpl $0x0, (%r14)
je 0x2d2e
jmp 0x311a
leaq 0x5eaa0(%rip), %rax     # 0x617a0
movq (%rax), %r14
testq %r14, %r14
je 0x30fb
cmpq $0x0, 0x18(%rsp)
jne 0x306d
movq %r14, %rdi
callq 0x2390
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x3072
movl 0xc(%rsp), %edi
movq 0x40(%rsp), %rsi
callq 0x6cb4
movq %rax, %r15
testq %rax, %rax
leaq 0x82f6(%rip), %r14      # 0xb03f
jne 0x2a34
jmp 0x2d65
xorl %ebp, %ebp
movq $0x0, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
leaq 0xe7dc(%rip), %rax      # 0x11548
movl 0xc(%rsp), %ecx
cmpl %ecx, (%rax)
jne 0x306d
movl $0x8, %esi
movq %rbx, %rdi
callq 0x3f9e
testq %rax, %rax
je 0x306d
testq %rbp, %rbp
je 0x306d
movq %rax, %r14
movq %rbp, %rdi
callq 0x797c
testq %rax, %rax
je 0x31a9
movq %rax, %r15
cmpq $0x0, (%rax)
je 0x31cd
movq %r15, %rdi
callq 0x8294
cmpl $-0x1, %eax
je 0x30ab
movl %eax, %r12d
movq %r15, %rdi
callq 0x808e
movl %r12d, 0xc(%rsp)
movl %r12d, %edi
callq 0x9194
testq %rax, %rax
je 0x31ee
movq %rax, %r12
leaq (,%r14,4), %rdi
callq 0x2270
testq %rax, %rax
je 0x3204
movq %rax, %r15
xorl %r13d, %r13d
movl $0x8, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x40e0
movq (%rax), %rdi
callq 0x80d2
movl %eax, (%r15,%r13,4)
cmpl $-0x1, %eax
je 0x30ab
incq %r13
cmpq %r13, %r14
jne 0x2e09
xorl %ecx, %ecx
movq 0x28(%rsp), %rax
cmpq $0x1, %rax
sbbq %rcx, %rcx
orq %rax, %rcx
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x3314
testq %rax, %rax
je 0x321a
movq %rax, %r13
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
jne 0x2e91
leaq 0x82f5(%rip), %rsi      # 0xb167
leaq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x69c0
cmpl $-0x1, %eax
je 0x32aa
movq 0x18(%rsp), %rdi
callq 0x6ac4
testl %eax, %eax
jne 0x3230
movq %rbp, 0x38(%rsp)
callq 0x431c
testl %eax, %eax
jne 0x3040
movq %r13, %rdi
callq 0x345e
testl %eax, %eax
jne 0x2ea3
movq %r13, %rdi
callq 0x3475
movq %r12, %rdi
callq 0x9885
movq %r12, %rdi
callq 0x9899
leaq 0x8d61(%rip), %r12      # 0xbc3c
xorl %ebp, %ebp
movl (%r15,%rbp,4), %edi
callq 0x21c0
testl %eax, %eax
je 0x2f15
callq 0x20a0
cmpl $0x0, (%rax)
je 0x2f0b
movq %rax, %r13
movq %r12, %rdi
xorl %eax, %eax
callq 0x8a00
movl $0x0, (%r13)
jmp 0x2f15
movq %r12, %rdi
xorl %eax, %eax
callq 0x8b1f
incq %rbp
cmpq %rbp, %r14
jne 0x2edd
movq %r15, %rdi
callq 0x2040
movl 0xc(%rsp), %edi
callq 0x21c0
testl %eax, %eax
movq 0x38(%rsp), %r15
je 0x2f69
callq 0x20a0
cmpl $0x0, (%rax)
je 0x2f5b
movq %rax, %r14
leaq 0x8cf1(%rip), %rdi      # 0xbc3c
xorl %eax, %eax
callq 0x8a00
movl $0x0, (%r14)
jmp 0x2f69
leaq 0x8cda(%rip), %rdi      # 0xbc3c
xorl %eax, %eax
callq 0x8b1f
movl $0x8, %esi
movq %rbx, %rdi
callq 0x3f9e
testq %rax, %rax
je 0x2fab
xorl %r14d, %r14d
movl $0x8, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x40e0
movq (%rax), %rdi
callq 0x8072
incq %r14
movl $0x8, %esi
movq %rbx, %rdi
callq 0x3f9e
cmpq %rax, %r14
jb 0x2f7e
movq %rbx, %rdi
callq 0x3ee0
movq 0x18(%rsp), %rdi
callq 0x2040
movq 0x20(%rsp), %rdi
callq 0x2040
movq %r15, %rdi
callq 0x2040
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x20a0
movq %rax, %rbx
cmpl $0x0, (%rax)
leaq 0x5e7ae(%rip), %rax     # 0x617a0
movq (%rax), %rdx
leaq 0x805f(%rip), %rdi      # 0xb05b
movq %r15, %rsi
jne 0x309e
xorl %eax, %eax
callq 0x8b1f
jmp 0x30ab
leaq 0x80bd(%rip), %rdi      # 0xb0d5
jmp 0x3026
callq 0x23f0
leaq 0x80c6(%rip), %rdi      # 0xb0ec
movq %r15, %rsi
xorl %eax, %eax
callq 0x8b1f
callq 0x20a0
movl $0x0, (%rax)
callq 0x32cf
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x305a
leaq 0x813a(%rip), %rdi      # 0xb18b
xorl %eax, %eax
callq 0x8b1f
jmp 0x30ab
movq %rax, %rbx
leaq 0x8127(%rip), %rdi      # 0xb18b
xorl %eax, %eax
callq 0x8a00
jmp 0x30a5
callq 0x32cf
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x308e
leaq 0x7fd8(%rip), %rdi      # 0xb05b
movq %r15, %rsi
movq %r14, %rdx
jmp 0x3005
movq %rax, %rbx
leaq 0x7fc3(%rip), %rdi      # 0xb05b
movq %r15, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x8a00
movl $0x0, (%rbx)
movl $0x1, %edi
callq 0x2350
callq 0x20a0
movq %rax, %rbx
cmpl $0x0, (%rax)
leaq 0x5e6d9(%rip), %rax     # 0x617a0
movq (%rax), %rsi
leaq 0x7fab(%rip), %rdi      # 0xb07c
je 0x31c1
jmp 0x3262
leaq 0x7f2f(%rip), %rdi      # 0xb012
leaq 0x7f37(%rip), %rsi      # 0xb021
leaq 0x7f37(%rip), %rcx      # 0xb028
movl $0x57, %edx
callq 0x21a0
leaq 0x7f10(%rip), %rdi      # 0xb012
leaq 0x7f18(%rip), %rsi      # 0xb021
leaq 0x7f18(%rip), %rcx      # 0xb028
movl $0x51, %edx
callq 0x21a0
leaq 0x5e67f(%rip), %rax     # 0x617a0
movq (%rax), %rsi
leaq 0x7f17(%rip), %rdi      # 0xb042
xorl %eax, %eax
callq 0x8b1f
movl $0x0, (%r14)
callq 0x32cf
leaq 0x7ecd(%rip), %rdi      # 0xb012
leaq 0x7ed5(%rip), %rsi      # 0xb021
leaq 0x7ed5(%rip), %rcx      # 0xb028
movl $0x49, %edx
callq 0x21a0
leaq 0x7eae(%rip), %rdi      # 0xb012
leaq 0x7eb6(%rip), %rsi      # 0xb021
leaq 0x7eb6(%rip), %rcx      # 0xb028
movl $0x70, %edx
callq 0x21a0
leaq 0x5e61d(%rip), %rax     # 0x617a0
movq (%rax), %rsi
leaq 0x7f0a(%rip), %rdi      # 0xb097
jmp 0x31d7
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x3246
leaq 0x7e60(%rip), %rdi      # 0xb004
jmp 0x3051
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x3255
leaq 0x7f43(%rip), %rdi      # 0xb101
movq %rbp, %rsi
xorl %eax, %eax
callq 0x8b1f
jmp 0x30ab
leaq 0x7ec3(%rip), %rdi      # 0xb097
movq %rbp, %rsi
xorl %eax, %eax
callq 0x8b1f
callq 0x20a0
movl $0x0, (%rax)
jmp 0x30ab
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x326e
leaq 0x7f25(%rip), %rdi      # 0xb124
jmp 0x3051
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x327d
leaq 0x7f2b(%rip), %rdi      # 0xb140
jmp 0x3051
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x328c
leaq 0x7f1c(%rip), %rdi      # 0xb147
jmp 0x3051
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x329b
leaq 0x7f36(%rip), %rdi      # 0xb177
jmp 0x3051
movq %rax, %rbx
leaq 0x7db4(%rip), %rdi      # 0xb004
jmp 0x3064
movq %rax, %rbx
leaq 0x7ea2(%rip), %rdi      # 0xb101
movq %rbp, %rsi
xorl %eax, %eax
callq 0x8a00
jmp 0x30a5
movq %rax, %rbx
leaq 0x7eac(%rip), %rdi      # 0xb124
jmp 0x3064
movq %rax, %rbx
leaq 0x7eb9(%rip), %rdi      # 0xb140
jmp 0x3064
movq %rax, %rbx
leaq 0x7eb1(%rip), %rdi      # 0xb147
jmp 0x3064
movq %rax, %rbx
leaq 0x7ed2(%rip), %rdi      # 0xb177
jmp 0x3064
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x32c0
leaq 0x7eb3(%rip), %rdi      # 0xb16e
jmp 0x3051
movq %rax, %rbx
leaq 0x7ea4(%rip), %rdi      # 0xb16e
jmp 0x3064