// File: /workspace/github_c_makefile_star2_O1/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 0x2999
callq 0x88c0
movl $0x8, %esi
xorl %edi, %edi
callq 0x3e08
testq %rax, %rax
je 0x318e
movq %rax, %rbx
movl 0xc(%rsp), %edi
movq 0x40(%rsp), %rsi
callq 0x6c3c
testq %rax, %rax
je 0x2d39
movq %rax, %r15
movq $0x0, 0x28(%rsp)
leaq 0x5edcd(%rip), %r13     # 0x617a8
leaq 0x865d(%rip), %r14      # 0xb03f
leaq 0x86cb(%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 0x2a28
movq %r15, %rdi
callq 0x6f67
addq 0x10(%rsp), %rax
jmp 0x2a36
movq 0x48(%rsp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x48(%rsp)
cmpq $0x6f, %rax
jle 0x2a77
cmpq $0x78, %rax
jg 0x2aaf
cmpq $0x70, %rax
je 0x2ad8
cmpq $0x76, %rax
jne 0x2b41
cmpl $0x0, (%r13)
jne 0x2fd7
movl $0x76, %esi
subq 0x10(%rsp), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x7024
jmp 0x2a12
cmpq $0x49, %rax
je 0x2af6
cmpq $0x51, %rax
je 0x2b10
cmpq $0x57, %rax
jne 0x2b41
cmpl $0x0, (%r13)
jne 0x2bbd
movl $0x57, %esi
subq 0x10(%rsp), %rsi
leaq 0x85cc(%rip), %rdi      # 0xb079
jmp 0x2b2c
cmpq $0x79, %rax
jne 0x2b3b
cmpl $0x0, (%r13)
jne 0x2fce
movl $0x79, %edi
subq 0x10(%rsp), %rdi
callq 0x713b
jmp 0x2a12
cmpl $0x0, (%r13)
jne 0x2b80
movl $0x70, %esi
subq 0x10(%rsp), %rsi
leaq 0x85bd(%rip), %rdi      # 0xb0b1
jmp 0x2b2c
cmpl $0x0, (%r13)
jne 0x2c55
movl $0x49, %esi
subq 0x10(%rsp), %rsi
movq %r14, %rdi
jmp 0x2b2c
cmpl $0x0, (%r13)
jne 0x2cd9
movl $0x51, %esi
subq 0x10(%rsp), %rsi
leaq 0x852c(%rip), %rdi      # 0xb058
movl $0x1, %edx
callq 0x7024
jmp 0x2a12
cmpq $0x7c, %rax
je 0x2b73
cmpl $0x0, (%r13)
jne 0x2fdc
cmpl $0x0, 0x34(%rsp)
jne 0x2a12
movl $0x7c, %edi
subq 0x10(%rsp), %rdi
callq 0x717b
movl $0x1, 0x34(%rsp)
jmp 0x2a12
movl $0x1, (%r13)
jmp 0x2d16
leaq 0x5ec19(%rip), %rax     # 0x617a0
cmpq $0x0, (%rax)
je 0x3139
testq %rbp, %rbp
jne 0x3054
leaq 0x5ebff(%rip), %rax     # 0x617a0
movq (%rax), %r14
movq %r14, %rdi
callq 0x2390
testq %rax, %rax
je 0x3059
movq %rax, %rbp
jmp 0x2d16
leaq 0x5ebdc(%rip), %rax     # 0x617a0
movq (%rax), %r14
testq %r14, %r14
je 0x3099
movq %rbp, 0x38(%rsp)
cmpq $0x0, 0x20(%rsp)
jne 0x2bf3
movq %r14, %rdi
callq 0x2390
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x3059
movq %r14, %rdi
callq 0x7898
testq %rax, %rax
je 0x316b
movq %rax, %rbp
cmpq $0x0, (%rax)
je 0x3158
cmpq $0x0, (%rbp)
movq %rbp, %r14
je 0x2c43
movl $0x1, %edx
movl $0x8, %ecx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3f80
testl %eax, %eax
jne 0x2ffd
cmpq $0x0, 0x8(%r14)
leaq 0x8(%r14), %r14
jne 0x2c1b
movq %rbp, %rdi
callq 0x2040
movq 0x38(%rsp), %rbp
jmp 0x2d16
leaq 0x5eb44(%rip), %rax     # 0x617a0
cmpq $0x0, (%rax)
je 0x311a
cmpq $0x0, 0x28(%rsp)
jne 0x3054
callq 0x20a0
movq %rax, %r15
movl $0x0, (%rax)
leaq 0x5eb19(%rip), %rax     # 0x617a0
movq (%rax), %r14
testq %r14, %r14
je 0x30fb
movq %r14, %rdi
leaq 0x50(%rsp), %rsi
xorl %edx, %edx
callq 0x2310
movq %rax, 0x28(%rsp)
movq 0x50(%rsp), %rcx
movl $0x16, %eax
cmpq %r14, %rcx
je 0x2ccb
cmpb $0x0, (%rcx)
jne 0x2ccb
movl $0x22, %eax
cmpq $0x10000, 0x28(%rsp)    # imm = 0x10000
jb 0x2cce
movl %eax, (%r15)
cmpl $0x0, (%r15)
je 0x2d16
jmp 0x30d7
leaq 0x5eac0(%rip), %rax     # 0x617a0
cmpq $0x0, (%rax)
je 0x30b8
cmpq $0x0, 0x18(%rsp)
jne 0x3054
leaq 0x5eaa3(%rip), %rax     # 0x617a0
movq (%rax), %r14
movq %r14, %rdi
callq 0x2390
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x3059
movl 0xc(%rsp), %edi
movq 0x40(%rsp), %rsi
callq 0x6c3c
movq %rax, %r15
testq %rax, %rax
leaq 0x830e(%rip), %r14      # 0xb03f
jne 0x29f4
jmp 0x2d4d
xorl %ebp, %ebp
movq $0x0, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
leaq 0xe7f4(%rip), %rax      # 0x11548
movl 0xc(%rsp), %ecx
cmpl %ecx, (%rax)
jne 0x3054
movl $0x8, %esi
movq %rbx, %rdi
callq 0x3f77
testq %rax, %rax
je 0x3054
testq %rbp, %rbp
je 0x3054
movq %rax, %r14
movq %rbp, %rdi
callq 0x7898
testq %rax, %rax
je 0x31a8
movq %rax, %r15
cmpq $0x0, (%rax)
je 0x31cc
movq %r15, %rdi
callq 0x81b5
cmpl $-0x1, %eax
je 0x308f
movl %eax, %r12d
movq %r15, %rdi
callq 0x7faf
movl %r12d, 0xc(%rsp)
movl %r12d, %edi
callq 0x90bc
testq %rax, %rax
je 0x31ed
movq %rax, %r12
leaq (,%r14,4), %rdi
callq 0x2270
testq %rax, %rax
je 0x3203
movq %rax, %r15
xorl %r13d, %r13d
movl $0x8, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x40b9
movq (%rax), %rdi
callq 0x7ff3
movl %eax, (%r15,%r13,4)
cmpl $-0x1, %eax
je 0x308f
incq %r13
cmpq %r13, %r14
jne 0x2df1
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 0x3219
movq %rax, %r13
cmpq $0x0, 0x18(%rsp)
jne 0x2e72
leaq 0x830f(%rip), %rsi      # 0xb167
leaq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x6948
cmpl $-0x1, %eax
je 0x32a9
movq 0x18(%rsp), %rdi
callq 0x6a4c
testl %eax, %eax
jne 0x322f
movq %rbp, 0x38(%rsp)
callq 0x42f0
testl %eax, %eax
jne 0x3027
movq %r13, %rdi
callq 0x3462
testl %eax, %eax
jne 0x2e89
movq %r13, %rdi
callq 0x3479
movq %r12, %rdi
callq 0x9781
movq %r12, %rdi
callq 0x9795
cmpq $0x1, %r14
adcq $0x0, %r14
leaq 0x8dcb(%rip), %r12      # 0xbc94
xorl %ebp, %ebp
movl (%r15,%rbp,4), %edi
callq 0x21c0
testl %eax, %eax
je 0x2f03
callq 0x20a0
cmpl $0x0, (%rax)
je 0x2ef9
movq %rax, %r13
movq %r12, %rdi
xorl %eax, %eax
callq 0x8940
movl $0x0, (%r13)
jmp 0x2f03
movq %r12, %rdi
xorl %eax, %eax
callq 0x8a5f
incq %rbp
cmpq %rbp, %r14
jne 0x2ecb
movq %r15, %rdi
callq 0x2040
movl 0xc(%rsp), %edi
callq 0x21c0
testl %eax, %eax
movq 0x38(%rsp), %r15
je 0x2f57
callq 0x20a0
cmpl $0x0, (%rax)
je 0x2f49
movq %rax, %r14
leaq 0x8d5b(%rip), %rdi      # 0xbc94
xorl %eax, %eax
callq 0x8940
movl $0x0, (%r14)
jmp 0x2f57
leaq 0x8d44(%rip), %rdi      # 0xbc94
xorl %eax, %eax
callq 0x8a5f
movl $0x8, %esi
movq %rbx, %rdi
callq 0x3f77
testq %rax, %rax
je 0x2f99
xorl %r14d, %r14d
movl $0x8, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x40b9
movq (%rax), %rdi
callq 0x7f93
incq %r14
movl $0x8, %esi
movq %rbx, %rdi
callq 0x3f77
cmpq %rax, %r14
jb 0x2f6c
movq %rbx, %rdi
callq 0x3eb9
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
leaq 0x8100(%rip), %rdi      # 0xb0d5
jmp 0x2fe3
callq 0x23f0
leaq 0x8109(%rip), %rdi      # 0xb0ec
movq %r15, %rsi
xorl %eax, %eax
callq 0x8a5f
callq 0x20a0
movl $0x0, (%rax)
callq 0x32ce
callq 0x20a0
movq %rax, %rbx
cmpl $0x0, (%rax)
leaq 0x5e791(%rip), %rax     # 0x617a0
movq (%rax), %rdx
leaq 0x8042(%rip), %rdi      # 0xb05b
movq %r15, %rsi
jne 0x3082
xorl %eax, %eax
callq 0x8a5f
jmp 0x308f
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x3041
leaq 0x8153(%rip), %rdi      # 0xb18b
xorl %eax, %eax
callq 0x8a5f
jmp 0x308f
movq %rax, %rbx
leaq 0x8140(%rip), %rdi      # 0xb18b
xorl %eax, %eax
callq 0x8940
jmp 0x3089
callq 0x32ce
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x3072
leaq 0x7ff1(%rip), %rdi      # 0xb05b
movq %r15, %rsi
movq %r14, %rdx
jmp 0x301e
movq %rax, %rbx
leaq 0x7fdf(%rip), %rdi      # 0xb05b
movq %r15, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x8940
movl $0x0, (%rbx)
movl $0x1, %edi
callq 0x2350
leaq 0x7f72(%rip), %rdi      # 0xb012
leaq 0x7f7a(%rip), %rsi      # 0xb021
leaq 0x7f7a(%rip), %rcx      # 0xb028
movl $0x57, %edx
callq 0x21a0
leaq 0x7f53(%rip), %rdi      # 0xb012
leaq 0x7f5b(%rip), %rsi      # 0xb021
leaq 0x7f5b(%rip), %rcx      # 0xb028
movl $0x51, %edx
callq 0x21a0
leaq 0x5e6c2(%rip), %rax     # 0x617a0
movq (%rax), %rsi
leaq 0x7f5a(%rip), %rdi      # 0xb042
xorl %eax, %eax
callq 0x8a5f
movl $0x0, (%r15)
callq 0x32ce
leaq 0x8d3e(%rip), %rdi      # 0xbe40
leaq 0x8133(%rip), %rsi      # 0xb23c
leaq 0x814b(%rip), %rcx      # 0xb25b
movl $0xbb, %edx
callq 0x21a0
leaq 0x7ef1(%rip), %rdi      # 0xb012
leaq 0x7ef9(%rip), %rsi      # 0xb021
leaq 0x7ef9(%rip), %rcx      # 0xb028
movl $0x49, %edx
callq 0x21a0
leaq 0x7ed2(%rip), %rdi      # 0xb012
leaq 0x7eda(%rip), %rsi      # 0xb021
leaq 0x7eda(%rip), %rcx      # 0xb028
movl $0x70, %edx
callq 0x21a0
leaq 0x5e641(%rip), %rax     # 0x617a0
movq (%rax), %rsi
leaq 0x7f2e(%rip), %rdi      # 0xb097
jmp 0x31d6
callq 0x20a0
movq %rax, %rbx
cmpl $0x0, (%rax)
leaq 0x5e623(%rip), %rax     # 0x617a0
movq (%rax), %rsi
leaq 0x7ef5(%rip), %rdi      # 0xb07c
je 0x31c0
jmp 0x3261
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x3245
leaq 0x7e61(%rip), %rdi      # 0xb004
jmp 0x3038
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x3254
leaq 0x7f44(%rip), %rdi      # 0xb101
movq %rbp, %rsi
xorl %eax, %eax
callq 0x8a5f
jmp 0x308f
leaq 0x7ec4(%rip), %rdi      # 0xb097
movq %rbp, %rsi
xorl %eax, %eax
callq 0x8a5f
callq 0x20a0
movl $0x0, (%rax)
jmp 0x308f
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x326d
leaq 0x7f26(%rip), %rdi      # 0xb124
jmp 0x3038
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x327c
leaq 0x7f2c(%rip), %rdi      # 0xb140
jmp 0x3038
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x328b
leaq 0x7f1d(%rip), %rdi      # 0xb147
jmp 0x3038
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x329a
leaq 0x7f37(%rip), %rdi      # 0xb177
jmp 0x3038
movq %rax, %rbx
leaq 0x7db5(%rip), %rdi      # 0xb004
jmp 0x304b
movq %rax, %rbx
leaq 0x7ea3(%rip), %rdi      # 0xb101
movq %rbp, %rsi
xorl %eax, %eax
callq 0x8940
jmp 0x3089
movq %rax, %rbx
leaq 0x7ead(%rip), %rdi      # 0xb124
jmp 0x304b
movq %rax, %rbx
leaq 0x7eba(%rip), %rdi      # 0xb140
jmp 0x304b
movq %rax, %rbx
leaq 0x7eb2(%rip), %rdi      # 0xb147
jmp 0x304b
movq %rax, %rbx
leaq 0x7ed3(%rip), %rdi      # 0xb177
jmp 0x304b
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x32bf
leaq 0x7eb4(%rip), %rdi      # 0xb16e
jmp 0x3038
movq %rax, %rbx
leaq 0x7ea5(%rip), %rdi      # 0xb16e
jmp 0x304b