// File: /workspace/github_c_makefile_star2_O1/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
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r14
movaps %xmm0, (%r14)
movl (%rdi), %eax
movl %eax, (%r14)
movw $0x1, %ax
movw %ax, 0x4(%r14)
movl 0x4(%rdi), %ecx
movl %ecx, 0x8(%r14)
movw %ax, 0xc(%r14)
callq 0x2090
movq 0x8(%rbx), %rcx
movq 0x10(%rbx), %r8
leaq 0x790a(%rip), %rsi      # 0xdee6
xorl %ebp, %ebp
movl $0x4, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x530c
movw %bp, 0x16(%rsp)
movw %bp, 0x1e(%rsp)
movl $0x2, %esi
movq %r14, %rdi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x21a0
testl %eax, %eax
jle 0x6654
movswl 0x16(%rsp), %ecx
testb $0x8, %cl
jne 0x6685
testb $0x8, 0x1e(%rsp)
jne 0x6685
testb $0x1, %cl
je 0x662c
movl (%rbx), %edi
movl 0x4(%rbx), %esi
callq 0x679b
testb $0x1, 0x1e(%rsp)
je 0x663d
movl (%rbx), %esi
movl 0x4(%rbx), %edi
callq 0x679b
testb $0x10, 0x16(%rsp)
jne 0x66f4
testb $0x10, 0x1e(%rsp)
je 0x666b
jmp 0x66f4
cmpl $-0x1, %eax
jne 0x666b
callq 0x2050
movq %rax, %rcx
xorl %eax, %eax
cmpl $0x4, (%rcx)
setne %al
negl %eax
cmpl $0x0, 0xa43e(%rip)      # 0x10ab0
jne 0x6710
testl %eax, %eax
jns 0x65ec
jmp 0x6710
leaq 0xc(%rsp), %r14
leaq 0x24(%rsp), %r15
leaq 0x7887(%rip), %r12      # 0xdf1d
xorl %r13d, %r13d
testb $0x8, 0x16(%rsp,%r13,8)
je 0x66e9
movl $0x0, 0xc(%rsp)
movl $0x4, 0x24(%rsp)
movl 0x10(%rsp,%r13,8), %edi
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rcx
movq %r15, %r8
callq 0x2130
callq 0x2090
movl 0xc(%rsp), %r8d
movl $0x1, %edi
movq %r12, %rsi
movl %eax, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0x530c
incq %r13
cmpq $0x1, %r13
je 0x6699
jmp 0x6710
callq 0x2090
leaq 0x7860(%rip), %rsi      # 0xdf60
movl $0x4, %edi
movl %eax, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x530c
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 0x6746
callq 0x2030
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x6754
callq 0x2030
movq %rbx, %rdi
callq 0x2030
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq