// File: /workspace/github_c_makefile_star2_O3/MartinDrab[P]netpipe/netpipe.c
// Function: NetPipeMain
int NetPipeMain(int argc, char *argv[])
{
	int ret = 0;
	char *mode = NULL;

	if (argc < 2) {
		usage();
		return -1;
	}

	mode = argv[1];
	if (strcmp(mode, "ac") == 0) {
		_sourceMode = cetAccept;
		_targetMode = cetConnect;
	} else if (strcmp(mode, "cc") == 0) {
		_sourceMode = cetConnect;
		_targetMode = cetConnect;
	} else if (strcmp(mode, "aa") == 0) {
		_sourceMode = cetAccept;
		_targetMode = cetAccept;
	} else if (strcmp(mode, "ca") == 0) {
		_sourceMode = cetConnect;
		_targetMode = cetAccept;
	} else {
		fprintf(stderr, "Unknown operating mode \"%s\"\n", mode);
		return -2;
	}

	char **arg = argv + 2;
	argc -= 2;
	while (ret == 0 && argc > 0) {
		int found = 0;
		PCOMMAND_LINE_OPTION cmdOption = _cmdOptions;

		for (size_t i = 0; i < sizeof(_cmdOptions) / sizeof(_cmdOptions[0]) - 1; ++i) {
			for (size_t j = 0; j < cmdOption->NameCount; ++j) {
				found = (strcmp(*arg, cmdOption->Names[j]) == 0);
				if (found) {
					++cmdOption->Specified;
					if (argc < cmdOption->ArgumentCount) {
						ret = -1;
						LogError("Not enough arguments for the %s option", *arg);
						break;
					}

					if (cmdOption->Specified > 1)
						LogWarning("The %s has been specified for %uth time, the last specification is used", *arg);;
				
					arg_advance(argc, arg);
					break;
				}
			}

			if (found)
				break;

			++cmdOption;
		}

		switch (cmdOption->Type) {
			case otUnknown:
				ret = -1;
				LogError("Unknown option %s", *arg);
				break;
			case otSourceHost:
				_sourceAddress = *arg;
				break;
			case otSourcePort:
				_sourceService = *arg;
				break;
			case otTargetHost:
				_targetAddress = *arg;
				break;
			case otTargetPort:
				_targetService = *arg;
				break;
			case otIPv4Only:
				_sourceAddressFamily = AF_INET;
				_destAddressFamily = AF_INET;
				break;
			case otIPv6Only:
				_sourceAddressFamily = AF_INET6;
				_destAddressFamily = AF_INET6;
				break;
			case otLogError:
				_loggingFlags |= LOG_FLAG_ERROR;
				break;
			case otLogWarning:
				_loggingFlags |= LOG_FLAG_WARNING;
				break;
			case otLogInfo:
				_loggingFlags |= LOG_FLAG_INFO;
				break;
			case otLogPacket:
				_loggingFlags |= LOG_FLAG_PACKET;
				break;
			case otLogPacketData:
				_loggingFlags |= LOG_FLAG_PACKET_DATA;
				break;
			case otOneConnection:
				_oneConnection = 1;
				break;
			case otHelp:
				_help = 1;
				break;
			case otVersion:
				_version = 1;
				break;
			case otKeepAlive:
				_keepAlive = 1;
				break;
			case otAuthSource:
				_sourcePassword = *arg;
				break;
			case otAuthTarget:
				_targetPassword = *arg;
				break;
			case otLogFile:
				_logFile = *arg;
				break;
			case otReceiveDomain:
				_sourceReceiveDomain = 1;
				break;
			case otSendDomain:
				_targetSendDomain = *arg;
				break;
#ifndef _WIN32
			case otUnixSource:
				_sourceAddressFamily = AF_UNIX;
				break;
			case otUnixDest:
				_destAddressFamily = AF_UNIX;
				break;
#endif
		}

		if (ret == 0 && cmdOption->ArgumentCount > 0) {
			for (int i = 0; i < cmdOption->ArgumentCount; ++i)
				arg_advance(argc, arg);
		}
	}

	switch (ret) {
		case -3:
			fprintf(stderr, "Missing argument for command-line option \"%s\"\n", *(arg - 1));
			return ret;
			break;
		case -4:
			fprintf(stderr, "Unknown command-line option \"%s\"\n", *(arg - 1));
			return ret;
			break;
		default:
			break;
	}

	if (_help) {
		usage();
		return 0;
	}

	if (_version) {
		fprintf(stderr, "NetPipe v1.0\n");
		return 0;
	}

	if (_logFile != NULL) {
		ret = LogSetFile(_logFile);
		if (ret != 0) {
			fprintf(stderr, "Failed to change log file: %u\n", ret);
			return ret;
		}
	}

	if (_sourceAddress == NULL) {
		fprintf(stderr, "Source host not specified\n");
		return -1;
	}

	if (_sourceAddressFamily != AF_UNIX && _sourceService == NULL) {
		fprintf(stderr, "Source port not specified\n");
		return -1;
	}

	if (_targetAddress == NULL) {
		fprintf(stderr, "Target host not specified\n");
		return -1;
	}

	if (_destAddressFamily != AF_UNIX && _targetService == NULL) {
		fprintf(stderr, "Target port not specified\n");
		return -1;
	}

#ifdef _WIN32
	WSADATA wsaData;

	ret = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (ret != NO_ERROR) {
		fprintf(stderr, "WSAStartup: %i\n", ret);
		return ret;
	}

#endif
	CHANNEL_END source;
	CHANNEL_END dest;

	memset(&source, 0, sizeof(source));
	source.ListenSocket = INVALID_SOCKET;
	memset(&dest, 0, sizeof(dest));
	dest.ListenSocket = INVALID_SOCKET;
	while (!_terminated) {
		source.Type = _sourceMode;
		source.AddressFamily = _sourceAddressFamily;
		source.Address = _sourceAddress;
		source.Service = _sourceService;
		source.EndSocket = INVALID_SOCKET;
		source.Password = _sourcePassword;
		ret = _PrepareChannelEnd(&source, !_oneConnection, _sourceReceiveDomain, 0);		
		if (ret == 0) {
			dest.Type = _targetMode;
			dest.AddressFamily = _destAddressFamily;
			dest.Address = _targetAddress;
			dest.Service = _targetService;
			dest.EndSocket = INVALID_SOCKET;
			dest.Password = _targetPassword;
			ret = _PrepareChannelEnd(&dest, 0, 0, _targetSendDomain != NULL);
			if (ret == 0) {
				PCHANNEL_DATA d = NULL;

				d = (PCHANNEL_DATA)malloc(sizeof(CHANNEL_DATA));
				if (d != NULL) {
					d->Timeout.tv_sec = 5;
					d->Timeout.tv_usec = 0;
					d->SourceAddress = source.AcceptAddress;
					d->DestAddress = dest.AcceptAddress;
					d->SourceSocket = source.EndSocket;
					d->DestSocket = dest.EndSocket;
#ifdef _WIN32
					DWORD threadId = 0;
					HANDLE threadHandle = NULL;

					threadHandle = CreateThread(NULL, 0, _ChannelThreadWrapper, d, 0, &threadId);
					if (threadHandle != NULL) {
						if (_oneConnection)
							WaitForSingleObject(threadHandle, INFINITE);

						CloseHandle(threadHandle);
					} else ret = GetLastError();
#else
					ret = fork();
					if (ret > 0) {
						closesocket(d->DestSocket);
						closesocket(d->SourceSocket);
						free(d);
						if (_oneConnection)
							waitpid(ret, &ret, 0);
						
						ret = 0;
					} else if (ret == 0) {
						_ProcessChannel(d);
						return 0;
					}
#endif
					if (ret != 0)
						free(d);
				} else ret = ENOMEM;

				if (dest.ListenSocket != INVALID_SOCKET) {
					closesocket(dest.ListenSocket);
					dest.ListenSocket = INVALID_SOCKET;
				}

				if (ret != 0)
					closesocket(dest.EndSocket);
			} else LogError("Failed to prepare the target channel: %u", ret);

			if (ret != 0)
				closesocket(source.EndSocket);
		} else LogError("Failed to prepare the source channel: %u", ret);

		sleep(_timeout);
	}


#ifdef _WIN32
	WSACleanup();
#endif

	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl $0x0, 0x4(%rsp)
cmpl $0x1, %edi
jle 0x5ae7
movq %rsi, %rbx
movl %edi, %ebp
movq 0x8(%rsi), %rdx
movzbl (%rdx), %eax
cmpl $0x63, %eax
je 0x554f
cmpl $0x61, %eax
jne 0x556b
cmpb $0x63, 0x1(%rdx)
jne 0x553d
cmpb $0x0, 0x2(%rdx)
je 0x558d
cmpb $0x61, 0x1(%rdx)
jne 0x556b
cmpb $0x0, 0x2(%rdx)
jne 0x556b
movb $0x1, %cl
xorl %eax, %eax
jmp 0x5595
cmpb $0x63, 0x1(%rdx)
jne 0x555b
cmpb $0x0, 0x2(%rdx)
je 0x5591
cmpb $0x61, 0x1(%rdx)
jne 0x556b
movb $0x1, %al
cmpb $0x0, 0x2(%rdx)
movb $0x1, %cl
je 0x5595
movq 0xaa86(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x8484(%rip), %rsi      # 0xda00
xorl %eax, %eax
callq 0x2160
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x5b1d
xorl %eax, %eax
jmp 0x5593
movb $0x1, %al
xorl %ecx, %ecx
movb %al, 0xb529(%rip)       # 0x10ac4
movb %cl, 0xb527(%rip)       # 0x10ac8
movl $0x0, 0x8(%rsp)
cmpl $0x2, %ebp
je 0x57e1
addl $-0x2, %ebp
addq $0x10, %rbx
movl $0x0, 0x8(%rsp)
movl %ebp, 0xc(%rsp)
xorl %r14d, %r14d
leaq 0xab91(%rip), %r15      # 0x10160
movq 0x10(%r15), %r12
testq %r12, %r12
je 0x55f6
movq (%rbx), %r13
xorl %ebp, %ebp
movq 0x18(%r15,%rbp,8), %rsi
movq %r13, %rdi
callq 0x2150
testl %eax, %eax
je 0x5613
incq %rbp
cmpq %rbp, %r12
jne 0x55dd
addq $0x38, %r15
incq %r14
cmpq $0x16, %r14
jne 0x55cf
movb $0x1, %r13b
leaq 0xb023(%rip), %r15      # 0x10630
movl 0xc(%rsp), %ebp
jmp 0x5684
movl 0x4(%r15), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x4(%r15)
movl 0xc(%rsp), %ebp
cmpl 0x8(%r15), %ebp
jge 0x565a
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
callq 0x2090
movq (%rbx), %rcx
xorl %r13d, %r13d
movl $0x1, %edi
leaq 0x83d6(%rip), %rsi      # 0xda1d
movl %eax, %edx
xorl %eax, %eax
callq 0x52e4
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
jmp 0x5684
testl %eax, %eax
jle 0x567b
callq 0x2090
movq (%rbx), %rcx
movl $0x2, %edi
leaq 0x83d9(%rip), %rsi      # 0xda4b
movl %eax, %edx
xorl %eax, %eax
callq 0x52e4
decl %ebp
addq $0x8, %rbx
movb $0x1, %r13b
movl (%r15), %eax
cmpq $0x16, %rax
ja 0x57b8
leaq 0x7bb8(%rip), %rcx      # 0xd250
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%rbx), %rax
movq %rax, 0xb425(%rip)      # 0x10ad0
jmp 0x57b8
movb $0x1, 0xb469(%rip)      # 0x10b20
jmp 0x57b8
movb $0x1, 0xb43d(%rip)      # 0x10b00
jmp 0x57b8
movb $0x1, 0xb429(%rip)      # 0x10af8
jmp 0x57b8
leaq 0xaa75(%rip), %rax      # 0x10150
orb $0x8, (%rax)
jmp 0x57b8
movl $0x1, 0xb403(%rip)      # 0x10af0
jmp 0x57b8
movq (%rbx), %rax
movq %rax, 0xb41c(%rip)      # 0x10b18
jmp 0x57b8
leaq 0xaa48(%rip), %rax      # 0x10150
orb $0x2, (%rax)
jmp 0x57b8
movq (%rbx), %rax
movq %rax, 0xb3c6(%rip)      # 0x10ae0
jmp 0x57b8
movq (%rbx), %rax
movq %rax, 0xb3af(%rip)      # 0x10ad8
jmp 0x57b8
movl $0xa, %eax
jmp 0x5767
leaq 0xaa14(%rip), %rax      # 0x10150
orb $0x1, (%rax)
jmp 0x57b8
movb $0x1, 0xb3b4(%rip)      # 0x10afc
jmp 0x57b8
leaq 0xa9ff(%rip), %rax      # 0x10150
orb $0x4, (%rax)
jmp 0x57b8
movq (%rbx), %rax
movq %rax, 0xb3c8(%rip)      # 0x10b28
jmp 0x57b8
movl $0x2, %eax
movl %eax, 0xb383(%rip)      # 0x10af0
movl %eax, 0xb381(%rip)      # 0x10af4
jmp 0x57b8
movq (%rbx), %rax
movq %rax, 0xb369(%rip)      # 0x10ae8
jmp 0x57b8
movb $0x1, 0xb37c(%rip)      # 0x10b04
jmp 0x57b8
movq (%rbx), %rax
movq %rax, 0xb374(%rip)      # 0x10b08
jmp 0x57b8
leaq 0xa9b3(%rip), %rax      # 0x10150
orb $0x10, (%rax)
jmp 0x57b8
movq (%rbx), %rax
movq %rax, 0xb364(%rip)      # 0x10b10
jmp 0x57b8
movl $0x1, 0xb33c(%rip)      # 0x10af4
testb %r13b, %r13b
je 0x57e1
movl 0x8(%r15), %eax
leal -0x1(%rax), %ecx
testl %eax, %eax
leaq 0x8(%rbx,%rcx,8), %rcx
movl $0x0, %edx
cmovlel %edx, %eax
cmovgq %rcx, %rbx
subl %eax, %ebp
testl %ebp, %ebp
jg 0x55c1
cmpb $0x1, 0xb314(%rip)      # 0x10afc
je 0x5aee
cmpb $0x1, 0xb30b(%rip)      # 0x10b00
je 0x5af5
movq 0xb316(%rip), %rdi      # 0x10b18
testq %rdi, %rdi
movl 0x8(%rsp), %eax
je 0x5820
callq 0x53d9
movl %eax, %ebx
movl %eax, 0x4(%rsp)
xorl %eax, %eax
testl %ebx, %ebx
jne 0x5b2f
cmpq $0x0, 0xb2a8(%rip)      # 0x10ad0
je 0x5afe
cmpl $0x1, 0xb2bb(%rip)      # 0x10af0
je 0x5845
cmpq $0x0, 0xb299(%rip)      # 0x10ad8
je 0x5b05
cmpq $0x0, 0xb293(%rip)      # 0x10ae0
je 0x5b0c
cmpl $0x1, 0xb29a(%rip)      # 0x10af4
je 0x586a
cmpq $0x0, 0xb284(%rip)      # 0x10ae8
je 0x5b13
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x60(%rsp)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, 0x78(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, 0x20(%rsp)
movl %ecx, 0x38(%rsp)
cmpl $0x0, 0xb221(%rip)      # 0x10ac0
jne 0x5b1d
leaq 0x50(%rsp), %rbx
leaq 0x10(%rsp), %r14
leaq 0x8296(%rip), %r12      # 0xdb4c
leaq 0x82bf(%rip), %r13      # 0xdb7c
movzbl 0xb200(%rip), %eax      # 0x10ac4
xorl $0x1, %eax
movl %eax, 0x50(%rsp)
movl 0xb21f(%rip), %eax      # 0x10af0
movl %eax, 0x70(%rsp)
movq 0xb1f4(%rip), %rax      # 0x10ad0
movq %rax, 0x58(%rsp)
movq 0xb1f0(%rip), %rax      # 0x10ad8
movq %rax, 0x60(%rsp)
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
movq 0xb20c(%rip), %rax      # 0x10b08
movq %rax, 0x80(%rsp)
movzbl 0xb1ed(%rip), %esi      # 0x10af8
xorl $0x1, %esi
movzbl 0xb20b(%rip), %edx      # 0x10b20
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x5b8c
movl %eax, 0x4(%rsp)
testl %eax, %eax
je 0x5946
callq 0x2090
movl 0x4(%rsp), %ecx
movl $0x1, %edi
movq %r13, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x52e4
jmp 0x5aca
movzbl 0xb17b(%rip), %eax      # 0x10ac8
movl %eax, 0x10(%rsp)
movl 0xb19d(%rip), %eax      # 0x10af4
movl %eax, 0x30(%rsp)
movq 0xb17e(%rip), %rax      # 0x10ae0
movq %rax, 0x18(%rsp)
movq 0xb17a(%rip), %rax      # 0x10ae8
movq %rax, 0x20(%rsp)
movl $0xffffffff, 0x34(%rsp) # imm = 0xFFFFFFFF
movq 0xb18e(%rip), %rax      # 0x10b10
movq %rax, 0x40(%rsp)
xorl %ecx, %ecx
cmpq $0x0, 0xb197(%rip)      # 0x10b28
setne %cl
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5b8c
movl %eax, 0x4(%rsp)
testl %eax, %eax
je 0x59c7
callq 0x2090
movl 0x4(%rsp), %ecx
movl $0x1, %edi
movq %r12, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x52e4
jmp 0x5aba
movl $0x28, %edi
callq 0x2180
testq %rax, %rax
je 0x5a5f
movq %rax, %rbp
movq %r14, %r13
movq $0x5, 0x18(%rax)
movq $0x0, 0x20(%rax)
movq 0x68(%rsp), %rax
movq %rax, 0x8(%rbp)
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rbp)
movl 0x74(%rsp), %r12d
movl %r12d, (%rbp)
movl 0x34(%rsp), %r14d
movl %r14d, 0x4(%rbp)
callq 0x2240
movl %eax, 0x4(%rsp)
testl %eax, %eax
jle 0x5a6f
movl %r14d, %edi
callq 0x2110
movl %r12d, %edi
callq 0x2110
movq %rbp, %rdi
callq 0x2030
cmpb $0x1, 0xb0b8(%rip)      # 0x10af8
jne 0x5a52
movl 0x4(%rsp), %edi
leaq 0x4(%rsp), %rsi
xorl %edx, %edx
callq 0x21d0
movl $0x0, 0x4(%rsp)
xorl %r15d, %r15d
jmp 0x5a80
movl $0xc, 0x4(%rsp)
movl $0xc, %r15d
jmp 0x5a91
je 0x5b7f
movl %eax, %r15d
movq %rbp, %rdi
callq 0x2030
movq %r13, %r14
leaq 0x80c2(%rip), %r12      # 0xdb4c
leaq 0x80eb(%rip), %r13      # 0xdb7c
movl 0x38(%rsp), %edi
cmpl $-0x1, %edi
je 0x5aac
callq 0x2110
movl $0xffffffff, 0x38(%rsp) # imm = 0xFFFFFFFF
movl 0x4(%rsp), %r15d
testl %r15d, %r15d
je 0x5aca
movl 0x34(%rsp), %edi
callq 0x2110
cmpl $0x0, 0x4(%rsp)
je 0x5aca
movl 0x74(%rsp), %edi
callq 0x2110
movl $0x1, %edi
callq 0x2230
cmpl $0x0, 0xafe5(%rip)      # 0x10ac0
je 0x58bd
movl 0x4(%rsp), %eax
jmp 0x5b1d
callq 0x2280
jmp 0x5b18
callq 0x2280
jmp 0x5afa
callq 0x2480
xorl %eax, %eax
jmp 0x5b1d
callq 0x2464
jmp 0x5b18
callq 0x2448
jmp 0x5b18
callq 0x242c
jmp 0x5b18
callq 0x2410
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa4c2(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x7f81(%rip), %rsi      # 0xdac1
movl %ebx, %edx
xorl %eax, %eax
callq 0x2160
movl %ebx, %eax
jmp 0x5b1d
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
callq 0x2090
movq (%rbx), %rcx
leaq 0x7f36(%rip), %rsi      # 0xda9a
movl $0x1, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x52e4
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
jmp 0x57e1
movq %rbp, %rdi
callq 0x649c
jmp 0x5afa