// File: /workspace/github_c_makefile_star2_O1/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, (%rsp)
cmpl $0x1, %edi
jle 0x5b93
movq %rsi, %r13
movl %edi, %ebp
movq 0x8(%rsi), %r14
leaq 0x8491(%rip), %rsi      # 0xda00
movq %r14, %rdi
callq 0x2140
testl %eax, %eax
je 0x55e1
leaq 0x8481(%rip), %rsi      # 0xda03
movq %r14, %rdi
callq 0x2140
testl %eax, %eax
je 0x55e5
leaq 0x8471(%rip), %rsi      # 0xda06
movq %r14, %rdi
callq 0x2140
testl %eax, %eax
je 0x5b8a
leaq 0x845d(%rip), %rsi      # 0xda09
movq %r14, %rdi
callq 0x2140
movb $0x1, %cl
movb $0x1, %dl
testl %eax, %eax
je 0x55e9
movq 0xaa35(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x843f(%rip), %rsi      # 0xda0c
movq %r14, %rdx
xorl %eax, %eax
callq 0x2150
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x5c1d
xorl %ecx, %ecx
jmp 0x55e7
movb $0x1, %cl
xorl %edx, %edx
movb %cl, 0xb4c5(%rip)       # 0x10ab4
movb %dl, 0xb4c3(%rip)       # 0x10ab8
addq $0x10, %r13
movl (%rsp), %eax
testl %eax, %eax
setne %cl
cmpl $0x2, %ebp
sete %dl
orb %cl, %dl
jne 0x5882
addl $-0x2, %ebp
leaq 0xab37(%rip), %r14      # 0x10150
xorl %r15d, %r15d
movq 0x10(%r14), %rbx
testq %rbx, %rbx
je 0x565c
movl %ebp, 0x4(%rsp)
movq %r13, 0x8(%rsp)
movq (%r13), %r13
xorl %ebp, %ebp
movq 0x18(%r14,%rbp,8), %rsi
movq %r13, %rdi
callq 0x2140
testl %eax, %eax
sete %r12b
je 0x5661
incq %rbp
cmpq %rbp, %rbx
jne 0x5634
movq 0x8(%rsp), %r13
movl 0x4(%rsp), %ebp
jmp 0x56cf
xorl %r12d, %r12d
jmp 0x56cf
movl 0x4(%r14), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x4(%r14)
movl 0x4(%rsp), %ebp
cmpl 0x8(%r14), %ebp
jge 0x56a2
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
callq 0x2090
movq 0x8(%rsp), %r13
movq (%r13), %rcx
movl $0x1, %edi
leaq 0x8392(%rip), %rsi      # 0xda29
movl %eax, %edx
xorl %eax, %eax
callq 0x530c
jmp 0x56cf
testl %eax, %eax
movq 0x8(%rsp), %r13
jle 0x56c9
callq 0x2090
movq (%r13), %rcx
movl $0x2, %edi
leaq 0x8397(%rip), %rsi      # 0xda57
movl %eax, %edx
xorl %eax, %eax
callq 0x530c
decl %ebp
addq $0x8, %r13
testb %r12b, %r12b
jne 0x56e5
addq $0x38, %r14
incq %r15
cmpq $0x16, %r15
jne 0x561c
movl (%r14), %eax
cmpq $0x16, %rax
ja 0x584e
leaq 0x7b57(%rip), %rcx      # 0xd250
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
callq 0x2090
movq (%r13), %rcx
movl $0x1, %edi
leaq 0x8388(%rip), %rsi      # 0xdaa6
movl %eax, %edx
xorl %eax, %eax
callq 0x530c
jmp 0x584e
movq (%r13), %rax
movq %rax, 0xb3c1(%rip)      # 0x10af8
jmp 0x584e
movb $0x1, 0xb3ad(%rip)      # 0x10af0
jmp 0x584e
movb $0x1, 0xb399(%rip)      # 0x10ae8
jmp 0x584e
movq (%r13), %rax
movq %rax, 0xb379(%rip)      # 0x10ad8
jmp 0x584e
movb $0x1, 0xb389(%rip)      # 0x10af4
jmp 0x584e
leaq 0xa9c9(%rip), %rax      # 0x10140
orb $0x4, (%rax)
jmp 0x584e
movq (%r13), %rax
movq %rax, 0xb33e(%rip)      # 0x10ac8
jmp 0x584e
leaq 0xa9aa(%rip), %rax      # 0x10140
orb $0x10, (%rax)
jmp 0x584e
movq (%r13), %rax
movq %rax, 0xb327(%rip)      # 0x10ad0
jmp 0x584e
leaq 0xa98b(%rip), %rax      # 0x10140
orb $0x1, (%rax)
jmp 0x584e
movq (%r13), %rax
movq %rax, 0xb2f8(%rip)      # 0x10ac0
jmp 0x584e
movq (%r13), %rax
movq %rax, 0xb328(%rip)      # 0x10b00
jmp 0x584e
movq (%r13), %rax
movq %rax, 0xb333(%rip)      # 0x10b18
jmp 0x584e
movl $0x2, %eax
jmp 0x5808
movl $0x1, 0xb2e8(%rip)      # 0x10ae0
jmp 0x584e
movb $0x1, 0xb2eb(%rip)      # 0x10aec
jmp 0x584e
movl $0xa, %eax
movl %eax, 0xb2d2(%rip)      # 0x10ae0
movl %eax, 0xb2d0(%rip)      # 0x10ae4
jmp 0x584e
movq (%r13), %rax
movq %rax, 0xb2e7(%rip)      # 0x10b08
jmp 0x584e
leaq 0xa916(%rip), %rax      # 0x10140
orb $0x8, (%rax)
jmp 0x584e
leaq 0xa90a(%rip), %rax      # 0x10140
orb $0x2, (%rax)
jmp 0x584e
movl $0x1, 0xb29f(%rip)      # 0x10ae4
jmp 0x584e
movb $0x1, 0xb2c2(%rip)      # 0x10b10
cmpl $0x0, (%rsp)
jne 0x5866
movl 0x8(%r14), %eax
testl %eax, %eax
jle 0x5866
leal -0x1(%rax), %ecx
subl %eax, %ebp
leaq 0x8(%r13,%rcx,8), %r13
movl (%rsp), %eax
testl %eax, %eax
jne 0x5882
movl $0x0, %r15d
leaq 0xa8d6(%rip), %r14      # 0x10150
testl %ebp, %ebp
jg 0x561c
cmpl $-0x4, %eax
je 0x5bb1
cmpl $-0x3, %eax
je 0x5b9a
cmpb $0x1, 0xb251(%rip)      # 0x10aec
je 0x5bcf
cmpb $0x1, 0xb248(%rip)      # 0x10af0
je 0x5bd6
movq 0xb253(%rip), %rdi      # 0x10b08
testq %rdi, %rdi
je 0x58ca
callq 0x5401
movl %eax, (%rsp)
testl %eax, %eax
jne 0x5c00
cmpq $0x0, 0xb1ee(%rip)      # 0x10ac0
je 0x5bdf
cmpl $0x1, 0xb201(%rip)      # 0x10ae0
je 0x58ef
cmpq $0x0, 0xb1df(%rip)      # 0x10ac8
je 0x5be6
cmpq $0x0, 0xb1d9(%rip)      # 0x10ad0
je 0x5bed
cmpl $0x1, 0xb1e0(%rip)      # 0x10ae4
je 0x5914
cmpq $0x0, 0xb1ca(%rip)      # 0x10ad8
je 0x5bf4
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x60(%rsp)
xorl %eax, %eax
movq %rax, 0x80(%rsp)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, 0x78(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, 0x20(%rsp)
movq %rax, 0x40(%rsp)
movl %ecx, 0x38(%rsp)
cmpl $0x0, 0xb158(%rip)      # 0x10ab0
jne 0x5c1a
leaq 0x50(%rsp), %rbx
leaq 0x10(%rsp), %r13
leaq 0x823a(%rip), %r15      # 0xdba9
leaq 0x8263(%rip), %r12      # 0xdbd9
movzbl 0xb137(%rip), %eax      # 0x10ab4
xorl $0x1, %eax
movl %eax, 0x50(%rsp)
movl 0xb156(%rip), %eax      # 0x10ae0
movl %eax, 0x70(%rsp)
movq 0xb12b(%rip), %rax      # 0x10ac0
movq %rax, 0x58(%rsp)
movq 0xb127(%rip), %rax      # 0x10ac8
movq %rax, 0x60(%rsp)
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
movq 0xb143(%rip), %rax      # 0x10af8
movq %rax, 0x80(%rsp)
movzbl 0xb124(%rip), %esi      # 0x10ae8
xorl $0x1, %esi
movzbl 0xb142(%rip), %edx      # 0x10b10
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x5c39
movl %eax, (%rsp)
testl %eax, %eax
je 0x59fd
callq 0x2090
movl (%rsp), %ecx
movl $0x1, %edi
movq %r12, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x530c
jmp 0x5b6e
movzbl 0xb0b4(%rip), %eax      # 0x10ab8
movl %eax, 0x10(%rsp)
movl 0xb0d6(%rip), %eax      # 0x10ae4
movl %eax, 0x30(%rsp)
movq 0xb0b7(%rip), %rax      # 0x10ad0
movq %rax, 0x18(%rsp)
movq 0xb0b3(%rip), %rax      # 0x10ad8
movq %rax, 0x20(%rsp)
movl $0xffffffff, 0x34(%rsp) # imm = 0xFFFFFFFF
movq 0xb0c7(%rip), %rax      # 0x10b00
movq %rax, 0x40(%rsp)
xorl %ecx, %ecx
cmpq $0x0, 0xb0d0(%rip)      # 0x10b18
setne %cl
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5c39
movl %eax, (%rsp)
testl %eax, %eax
je 0x5a7c
callq 0x2090
movl (%rsp), %ecx
movl $0x1, %edi
movq %r15, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x530c
jmp 0x5b5f
movl $0x28, %edi
callq 0x2170
testq %rax, %rax
je 0x5b12
movq %rax, %rbp
movq %r12, %rbx
movq %r15, %r14
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), %r15d
movl %r15d, (%rbp)
movl 0x34(%rsp), %r12d
movl %r12d, 0x4(%rbp)
callq 0x2230
movl %eax, (%rsp)
testl %eax, %eax
jle 0x5b1b
movl %r12d, %edi
callq 0x2110
movl %r15d, %edi
callq 0x2110
movq %rbp, %rdi
callq 0x2030
cmpb $0x1, 0xaff1(%rip)      # 0x10ae8
jne 0x5b06
movl (%rsp), %edi
movq %rsp, %rsi
xorl %edx, %edx
callq 0x21c0
movl $0x0, (%rsp)
movq %r14, %r15
jmp 0x5b24
movl $0xc, (%rsp)
jmp 0x5b3a
movq %r14, %r15
je 0x5c2f
cmpl $0x0, (%rsp)
movq %rbx, %r12
leaq 0x50(%rsp), %rbx
je 0x5b3a
movq %rbp, %rdi
callq 0x2030
movl 0x38(%rsp), %edi
cmpl $-0x1, %edi
je 0x5b50
callq 0x2110
movl $0xffffffff, 0x38(%rsp) # imm = 0xFFFFFFFF
cmpl $0x0, (%rsp)
je 0x5b5f
movl 0x34(%rsp), %edi
callq 0x2110
cmpl $0x0, (%rsp)
je 0x5b6e
movl 0x74(%rsp), %edi
callq 0x2110
movl $0x1, %edi
callq 0x2220
cmpl $0x0, 0xaf31(%rip)      # 0x10ab0
je 0x5976
jmp 0x5c1a
movb $0x1, %dl
xorl %ecx, %ecx
jmp 0x55e9
callq 0x2270
jmp 0x5bf9
movq 0xa457(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq -0x8(%r13), %rdx
leaq 0x7f10(%rip), %rsi      # 0xdabf
jmp 0x5bc6
movq 0xa440(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq -0x8(%r13), %rdx
leaq 0x7f28(%rip), %rsi      # 0xdaee
xorl %eax, %eax
callq 0x2150
jmp 0x5c1a
callq 0x2270
jmp 0x5bdb
callq 0x2470
xorl %eax, %eax
jmp 0x5c1d
callq 0x2454
jmp 0x5bf9
callq 0x2438
jmp 0x5bf9
callq 0x241c
jmp 0x5bf9
callq 0x2400
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x5c1d
movq 0xa3f1(%rip), %rcx      # 0xfff8
movq (%rcx), %rdi
leaq 0x7f0d(%rip), %rsi      # 0xdb1e
movl %eax, %edx
xorl %eax, %eax
callq 0x2150
movl (%rsp), %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, %rdi
callq 0x6591
jmp 0x5bdb