// File: /workspace/github_c_makefile_star2_O3/MartinDrab[P]netpipe/netpipe.c
// Function: ProcessChannel
static void _ProcessChannel(PCHANNEL_DATA Data)
{
	int ret = 0;
#ifndef _WIN32
	struct pollfd fds[2];
#else
	pollfd fds[2];
#endif

	memset(fds, 0, sizeof(fds));
	fds[0].fd = Data->SourceSocket;
	fds[0].events = POLLIN;
	fds[1].fd = Data->DestSocket;
	fds[1].events = POLLIN;
	LogInfo("Starting to process the connection (%s <--> %s)", Data->SourceAddress, Data->DestAddress);
	do {
		fds[0].revents = 0;
		fds[1].revents = 0;
		ret = poll(fds, sizeof(fds) / sizeof(fds[0]), 1000);
		if (ret > 0) {
			if ((fds[0].revents & POLLERR) ||
				(fds[1].revents & POLLERR)) {
				for (size_t i = 0; i < sizeof(fds) / sizeof(fds[0]); ++i) {
					if (fds[i].revents & POLLERR) {
						int err = 0;
						socklen_t errLen = sizeof(err);

						getsockopt(fds[i].fd, SOL_SOCKET, SO_ERROR, (void*)&err, &errLen);
						LogError("Error occurred during channel processing on socket #%zu: %i", i, err);
					}
				}

				break;
			}

			if (fds[0].revents & POLLIN)
				ret = _StreamData(Data->SourceSocket, Data->DestSocket, fds[0].revents);

			if (fds[1].revents & POLLIN)
				ret = _StreamData(Data->DestSocket, Data->SourceSocket, fds[1].revents);

			if ((fds[0].revents & POLLHUP) ||
				(fds[1].revents & POLLHUP)) {
				LogInfo("Connection closed");
				break;
			}
		} else if (ret == SOCKET_ERROR && errno == EINTR)
			ret = 0;
	} while (!_terminated && ret >= 0);

	shutdown(Data->DestSocket, SD_BOTH);
	closesocket(Data->DestSocket);
	shutdown(Data->SourceSocket, SD_BOTH);
	closesocket(Data->SourceSocket);
	if (Data->SourceAddress != NULL)
		free(Data->SourceAddress);
	
	if (Data->DestAddress != NULL)
		free(Data->DestAddress);
	
	free(Data);

	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movl (%rdi), %eax
leaq 0x18(%rsp), %rdx
movl %eax, -0x8(%rdx)
movw $0x1, %ax
movw %ax, -0x4(%rdx)
movl 0x4(%rdi), %ecx
movl %ecx, (%rdx)
movw %ax, 0x4(%rdx)
callq 0x2090
movq 0x8(%rbx), %rcx
movq 0x10(%rbx), %r8
leaq 0x79ad(%rip), %rsi      # 0xde89
xorl %ebp, %ebp
movl $0x4, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x52e4
leaq 0x10(%rsp), %r14
movw %bp, 0x16(%rsp)
movw %bp, 0x1e(%rsp)
movl $0x2, %esi
movq %r14, %rdi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x21b0
testl %eax, %eax
jle 0x655f
movswl 0x16(%rsp), %edx
testb $0x8, %dl
jne 0x6590
movzwl 0x1e(%rsp), %ecx
testb $0x8, %cl
jne 0x6590
testb $0x1, %dl
je 0x6539
movl (%rbx), %edi
movl 0x4(%rbx), %esi
callq 0x66af
movzwl 0x1e(%rsp), %ecx
testb $0x1, %cl
je 0x6548
movl (%rbx), %esi
movl 0x4(%rbx), %edi
callq 0x66af
testb $0x10, 0x16(%rsp)
jne 0x6608
testb $0x10, 0x1e(%rsp)
je 0x6576
jmp 0x6608
cmpl $-0x1, %eax
jne 0x6576
callq 0x2050
movq %rax, %rcx
xorl %eax, %eax
cmpl $0x4, (%rcx)
setne %al
negl %eax
cmpl $0x0, 0xa543(%rip)      # 0x10ac0
jne 0x6624
testl %eax, %eax
jns 0x64f1
jmp 0x6624
movb $0x1, %r15b
leaq 0x24(%rsp), %r12
leaq 0x7921(%rip), %r13      # 0xdec0
xorl %ebp, %ebp
testb $0x8, 0x6(%r14)
je 0x65f0
movl $0x0, 0xc(%rsp)
movl $0x4, 0x24(%rsp)
movl (%r14), %edi
movl $0x1, %esi
movl $0x4, %edx
leaq 0xc(%rsp), %rcx
movq %r12, %r8
callq 0x2130
callq 0x2090
movl 0xc(%rsp), %r8d
movl $0x1, %edi
movq %r13, %rsi
movl %eax, %edx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x52e4
movl $0x1, %ebp
leaq 0x18(%rsp), %r14
testb $0x1, %r15b
movl $0x0, %r15d
jne 0x65a1
jmp 0x6624
callq 0x2090
leaq 0x78ef(%rip), %rsi      # 0xdf03
movl $0x4, %edi
movl %eax, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x52e4
movl 0x4(%rbx), %edi
movl $0x2, %esi
callq 0x20b0
movl 0x4(%rbx), %edi
callq 0x2110
movl (%rbx), %edi
movl $0x2, %esi
callq 0x20b0
movl (%rbx), %edi
callq 0x2110
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x665a
callq 0x2030
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x6668
callq 0x2030
movq %rbx, %rdi
callq 0x2030
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq