// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, 0x20(%rsp)
movl %edi, 0x14(%rsp)
movq %rsi, 0x48(%rsp)
movq (%rsi), %rdi
testq %rdi, %rdi
je 0x2506
callq 0x7a80
pushq $0x8
popq %rsi
xorl %edi, %edi
callq 0x36d0
testq %rax, %rax
je 0x2c3e
movq %rax, %r14
movq $0x0, 0x18(%rsp)
leaq 0x5e27c(%rip), %r15     # 0x607a8
pushq $0x1
popq %r12
movq $0x0, 0x30(%rsp)
xorl %r13d, %r13d
movl 0x14(%rsp), %edi
movq 0x48(%rsp), %rsi
callq 0x5e30
testq %rax, %rax
je 0x282b
movq %rax, %rbx
movq $0x48, 0x8(%rsp)
movq 0x8(%rsp), %rax
decq %rax
movq %rax, 0x38(%rsp)
movl $0x0, 0x2c(%rsp)
pushq $0x49
popq %rbp
cmpl $0x0, (%r15)
je 0x258c
movq %rbx, %rdi
callq 0x6245
addq 0x8(%rsp), %rax
jmp 0x259a
movq 0x38(%rsp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x38(%rsp)
cmpq $0x49, %rax
je 0x2698
cmpq $0x51, %rax
je 0x267d
cmpq $0x57, %rax
je 0x2603
cmpq $0x70, %rax
je 0x2646
cmpq $0x76, %rax
je 0x2621
cmpq $0x79, %rax
je 0x2661
cmpq $0x7c, %rax
je 0x26be
cmpl $0x0, (%r15)
jne 0x2aa4
cmpl $0x0, 0x2c(%rsp)
jne 0x2577
pushq $0x7c
popq %rdi
subq 0x8(%rsp), %rdi
callq 0x65b2
movl $0x1, 0x2c(%rsp)
jmp 0x2577
cmpl $0x0, (%r15)
jne 0x26ca
pushq $0x57
popq %rsi
subq 0x8(%rsp), %rsi
leaq 0x7a5d(%rip), %rdi      # 0xa079
jmp 0x26b1
cmpl $0x0, (%r15)
jne 0x2a81
pushq $0x76
popq %rsi
subq 0x8(%rsp), %rsi
leaq 0x7a7a(%rip), %rdi      # 0xa0b4
xorl %edx, %edx
callq 0x635d
jmp 0x2577
cmpl $0x0, (%r15)
jne 0x274a
pushq $0x70
popq %rsi
subq 0x8(%rsp), %rsi
leaq 0x7a52(%rip), %rdi      # 0xa0b1
jmp 0x26b1
cmpl $0x0, (%r15)
jne 0x2a78
pushq $0x79
popq %rdi
subq 0x8(%rsp), %rdi
callq 0x6527
jmp 0x2577
cmpl $0x0, (%r15)
jne 0x2778
pushq $0x51
popq %rsi
subq 0x8(%rsp), %rsi
leaq 0x79c2(%rip), %rdi      # 0xa058
jmp 0x26b1
cmpl $0x0, (%r15)
jne 0x27b2
movq %rbp, %rsi
subq 0x8(%rsp), %rsi
leaq 0x798e(%rip), %rdi      # 0xa03f
movl %r12d, %edx
callq 0x635d
jmp 0x2577
movl $0x1, (%r15)
jmp 0x253c
leaq 0x5e0cf(%rip), %rax     # 0x607a0
movq (%rax), %rbp
testq %rbp, %rbp
je 0x2c21
movq %r13, 0x40(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0x2700
movq %rbp, %rdi
callq 0x2390
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x2b21
movq %rbp, %rdi
callq 0x6c08
testq %rax, %rax
je 0x2bf3
movq %rax, %r13
cmpq $0x0, (%rax)
movq %rax, %rbp
je 0x2bce
cmpq $0x0, (%rbp)
je 0x2819
movq %r14, %rdi
movq %rbp, %rsi
pushq $0x1
popq %rdx
pushq $0x8
popq %rcx
callq 0x382a
addq $0x8, %rbp
testl %eax, %eax
je 0x2721
jmp 0x2ac2
leaq 0x5e04f(%rip), %rax     # 0x607a0
movq (%rax), %rbp
testq %rbp, %rbp
je 0x2bb5
cmpq $0x0, 0x18(%rsp)
jne 0x2b06
movq %rbp, %rdi
callq 0x2390
movq %rax, 0x18(%rsp)
jmp 0x27a4
leaq 0x5e021(%rip), %rax     # 0x607a0
movq (%rax), %rbp
testq %rbp, %rbp
je 0x2b5d
cmpq $0x0, 0x20(%rsp)
jne 0x2b06
movq %rbp, %rdi
callq 0x2390
movq %rax, 0x20(%rsp)
testq %rax, %rax
jne 0x253c
jmp 0x2b21
leaq 0x5dfe7(%rip), %rax     # 0x607a0
movq (%rax), %rbp
testq %rbp, %rbp
je 0x2b99
testq %r13, %r13
jne 0x2b06
callq 0x20a0
movq %rax, %rbx
andl $0x0, (%rax)
movq %rbp, %rdi
leaq 0x50(%rsp), %rsi
xorl %edx, %edx
callq 0x2310
movq %rax, %r13
movq 0x50(%rsp), %rcx
pushq $0x16
popq %rax
cmpq %rbp, %rcx
je 0x2809
cmpb $0x0, (%rcx)
jne 0x2809
pushq $0x22
popq %rax
cmpq $0x10000, %r13          # imm = 0x10000
jb 0x280b
movl %eax, (%rbx)
cmpl $0x0, (%rbx)
je 0x253c
jmp 0x2b79
movq %r13, %rdi
callq 0x2040
movq 0x40(%rsp), %r13
jmp 0x253c
leaq 0xdd16(%rip), %rax      # 0x10548
movl 0x14(%rsp), %ecx
cmpl %ecx, (%rax)
jne 0x2b06
movq %r14, %rdi
callq 0x2da6
testq %rax, %rax
movq 0x18(%rsp), %r12
je 0x2b06
testq %r12, %r12
je 0x2b06
movq %rax, %rbx
movq %r12, %rdi
callq 0x6c08
testq %rax, %rax
je 0x2c58
movq %rax, %r15
cmpq $0x0, (%rax)
je 0x2c74
movq %r15, %rdi
callq 0x73e1
cmpl $-0x1, %eax
je 0x2b55
movl %eax, %ebp
movq %r15, %rdi
callq 0x7207
movl %ebp, 0x14(%rsp)
movl %ebp, %edi
callq 0x825c
testq %rax, %rax
je 0x2c83
movq %rax, %r12
movq %r13, %rbp
leaq (,%rbx,4), %rdi
callq 0x2270
testq %rax, %rax
je 0x2c99
movq %rax, %r15
xorl %r13d, %r13d
cmpq %r13, %rbx
je 0x28f8
movq %r14, %rdi
movq %r13, %rsi
callq 0x2dae
movq (%rax), %rdi
callq 0x7239
movl %eax, (%r15,%r13,4)
incq %r13
cmpl $-0x1, %eax
jne 0x28cf
jmp 0x2b55
xorl %ecx, %ecx
cmpq $0x1, %rbp
sbbq %rcx, %rcx
orq %rbp, %rcx
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2db8
testq %rax, %rax
je 0x2caf
movq %rax, %r13
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
jne 0x294e
leaq 0x7838(%rip), %rsi      # 0xa167
leaq 0x20(%rsp), %rdi
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x5b58
cmpl $-0x1, %eax
je 0x2d41
movq 0x20(%rsp), %rdi
callq 0x5c4c
testl %eax, %eax
jne 0x2cc5
callq 0x3b90
testl %eax, %eax
jne 0x2aec
movq %r13, %rdi
callq 0x2ef0
testl %eax, %eax
jne 0x295b
movq %r13, %rdi
callq 0x2f05
movq %r12, %rdi
callq 0x873f
movq %r12, %rdi
callq 0x8753
leaq 0x82a9(%rip), %r12      # 0xac3c
xorl %ebp, %ebp
cmpq %rbp, %rbx
je 0x29d4
movl (%r15,%rbp,4), %edi
callq 0x21c0
testl %eax, %eax
je 0x29cf
callq 0x20a0
cmpl $0x0, (%rax)
je 0x29c5
movq %rax, %r13
movq %r12, %rdi
xorl %eax, %eax
callq 0x7afd
andl $0x0, (%r13)
jmp 0x29cf
movq %r12, %rdi
xorl %eax, %eax
callq 0x7c8c
incq %rbp
jmp 0x2995
movq %r15, %rdi
callq 0x2040
movl 0x14(%rsp), %edi
callq 0x21c0
testl %eax, %eax
movq 0x18(%rsp), %r15
je 0x2a1c
callq 0x20a0
cmpl $0x0, (%rax)
je 0x2a0e
movq %rax, %rbx
leaq 0x823a(%rip), %rdi      # 0xac3c
xorl %eax, %eax
callq 0x7afd
andl $0x0, (%rbx)
jmp 0x2a1c
leaq 0x8227(%rip), %rdi      # 0xac3c
xorl %eax, %eax
callq 0x7c8c
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x2da6
cmpq %rax, %rbx
jae 0x2a43
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2dae
movq (%rax), %rdi
callq 0x71eb
incq %rbx
jmp 0x2a1e
movq %r14, %rdi
callq 0x3767
movq 0x20(%rsp), %rdi
callq 0x2040
movq 0x30(%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
leaq 0x7656(%rip), %rdi      # 0xa0d5
jmp 0x2aab
movq 0xd570(%rip), %rax      # 0xfff8
movq (%rax), %rcx
leaq 0x762c(%rip), %rdi      # 0xa0be
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x2370
xorl %edi, %edi
callq 0x2350
leaq 0x7641(%rip), %rdi      # 0xa0ec
movq %rbx, %rsi
xorl %eax, %eax
callq 0x7c8c
callq 0x20a0
andl $0x0, (%rax)
callq 0x2d66
callq 0x20a0
movq %rax, %r14
cmpl $0x0, (%rax)
leaq 0x5dccc(%rip), %rax     # 0x607a0
movq (%rax), %rdx
leaq 0x757d(%rip), %rdi      # 0xa05b
movq %rbx, %rsi
jne 0x2b4a
xorl %eax, %eax
callq 0x7c8c
jmp 0x2b55
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2b0b
leaq 0x768e(%rip), %rdi      # 0xa18b
xorl %eax, %eax
callq 0x7c8c
jmp 0x2b55
callq 0x2d66
movq %rax, %rbx
leaq 0x7676(%rip), %rdi      # 0xa18b
xorl %eax, %eax
callq 0x7afd
andl $0x0, (%rbx)
jmp 0x2b55
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2b3a
leaq 0x7529(%rip), %rdi      # 0xa05b
movq %rbx, %rsi
movq %rbp, %rdx
jmp 0x2ae3
movq %rax, %r14
leaq 0x7517(%rip), %rdi      # 0xa05b
movq %rbx, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x7afd
andl $0x0, (%r14)
pushq $0x1
popq %rdi
callq 0x2350
leaq 0x74ae(%rip), %rdi      # 0xa012
leaq 0x74b6(%rip), %rsi      # 0xa021
leaq 0x74b6(%rip), %rcx      # 0xa028
pushq $0x51
jmp 0x2c38
leaq 0x5dc20(%rip), %rax     # 0x607a0
movq (%rax), %rsi
leaq 0x74b8(%rip), %rdi      # 0xa042
xorl %eax, %eax
callq 0x7c8c
andl $0x0, (%rbx)
callq 0x2d66
leaq 0x7472(%rip), %rdi      # 0xa012
leaq 0x747a(%rip), %rsi      # 0xa021
leaq 0x747a(%rip), %rcx      # 0xa028
pushq $0x49
jmp 0x2c38
leaq 0x7456(%rip), %rdi      # 0xa012
leaq 0x745e(%rip), %rsi      # 0xa021
leaq 0x745e(%rip), %rcx      # 0xa028
pushq $0x70
jmp 0x2c38
leaq 0x5dbcb(%rip), %rax     # 0x607a0
movq (%rax), %rsi
leaq 0x74b8(%rip), %rdi      # 0xa097
xorl %eax, %eax
callq 0x7c8c
callq 0x20a0
andl $0x0, (%rax)
jmp 0x2b55
callq 0x20a0
movq %rax, %rbx
cmpl $0x0, (%rax)
leaq 0x5db9b(%rip), %rax     # 0x607a0
movq (%rax), %rsi
leaq 0x746d(%rip), %rdi      # 0xa07c
jne 0x2cf9
xorl %eax, %eax
callq 0x7c8c
jmp 0x2b55
leaq 0x73ea(%rip), %rdi      # 0xa012
leaq 0x73f2(%rip), %rsi      # 0xa021
leaq 0x73f2(%rip), %rcx      # 0xa028
pushq $0x57
popq %rdx
callq 0x21a0
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2cdb
leaq 0x73b1(%rip), %rdi      # 0xa004
jmp 0x2afd
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2cea
leaq 0x7494(%rip), %rdi      # 0xa101
movq 0x18(%rsp), %rsi
jmp 0x2c15
leaq 0x741c(%rip), %rdi      # 0xa097
movq %r12, %rsi
jmp 0x2bdf
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2d05
leaq 0x7490(%rip), %rdi      # 0xa124
jmp 0x2afd
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2d14
leaq 0x7496(%rip), %rdi      # 0xa140
jmp 0x2afd
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2d23
leaq 0x7487(%rip), %rdi      # 0xa147
jmp 0x2afd
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2d32
leaq 0x74a1(%rip), %rdi      # 0xa177
jmp 0x2afd
movq %rax, %rbx
leaq 0x731f(%rip), %rdi      # 0xa004
jmp 0x2b15
movq %rax, %rbx
leaq 0x740d(%rip), %rdi      # 0xa101
movq 0x18(%rsp), %rsi
xorl %eax, %eax
callq 0x7afd
jmp 0x2b1c
movq %rax, %rbx
leaq 0x7415(%rip), %rdi      # 0xa124
jmp 0x2b15
movq %rax, %rbx
leaq 0x7422(%rip), %rdi      # 0xa140
jmp 0x2b15
movq %rax, %rbx
leaq 0x741a(%rip), %rdi      # 0xa147
jmp 0x2b15
movq %rax, %rbx
leaq 0x743b(%rip), %rdi      # 0xa177
jmp 0x2b15
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x2d57
leaq 0x741c(%rip), %rdi      # 0xa16e
jmp 0x2afd
movq %rax, %rbx
leaq 0x740d(%rip), %rdi      # 0xa16e
jmp 0x2b15