// File: /workspace/github_c_makefile_star2_O2/TeaInside[P]teavpn/src/teavpn/server/tcp.c
// Function: teavpn_tcp_worker_thread
static void *teavpn_tcp_worker_thread(struct worker_thread *worker)
{

	#define packet ((teavpn_packet *)(bufchan[queues[i].bufchan_index].buffer))

	register int16_t i;
	register ssize_t nwrite;

	while (true) {
		pthread_mutex_lock(&(worker->mutex));
		pthread_cond_wait(&(worker->cond), &(worker->mutex));

		worker->busy = true;

		i = worker_job_pull();

		/**
		 * There is no queue.
		 */
		if (i == -1) {
			goto job_done;
		}

		packet->info.seq = ++(connections[queues[i].conn_index].seq);

		nwrite = write(connections[queues[i].conn_index].fd, packet, packet->info.len);

		debug_log(3, "[%ld] Write to client %s:%d %ld bytes (server_seq: %ld) (client_seq: %ld) (seq %s)",
			connections[queues[i].conn_index].seq,
			inet_ntoa(connections[queues[i].conn_index].addr.sin_addr),
			ntohs(connections[queues[i].conn_index].addr.sin_port),
			nwrite,
			connections[queues[i].conn_index].seq,
			packet->info.seq,
			(connections[queues[i].conn_index].seq == packet->info.seq) ? "match" : "invalid"
		);

		/**
		 * Connection closed by client.
		 */
		if (nwrite == 0) {
			close(connections[queues[i].conn_index].fd);
			debug_log(1, "(%s:%d) connection closed",
				inet_ntoa(connections[queues[i].conn_index].addr.sin_addr),
				ntohs(connections[queues[i].conn_index].addr.sin_port)
			);
			connection_zero(i);
			goto job_release;
		}


		if (nwrite < 0) {
			char *remote_addr = inet_ntoa(connections[queues[i].conn_index].addr.sin_addr);
			uint16_t remote_port = ntohs(connections[queues[i].conn_index].addr.sin_port);

			debug_log(0, "Error write to %s:%d", remote_addr, remote_port);
			perror("Error write to connection fd");

			/**
			 * Increment the error counter.
			 */
			connections[queues[i].conn_index].error++;

			/**
			 * Force disconnect client if it has
			 * reached the max number of errors.
			 */
			if (connections[queues[i].conn_index].error > MAX_CLIENT_ERR) {
				debug_log(0,
					"Client %s:%d has been disconnected because it has reached the max number of errors",
					remote_port,
					remote_port
				);
				close(connections[queues[i].conn_index].fd);
				connection_zero(i);
			}
		}


		job_release:
		bufchan[queues[i].bufchan_index].ref_count--;
		// printf("i = %d;  r = %d\n", queues[i].bufchan_index, bufchan[queues[i].bufchan_index].ref_count);

		pthread_mutex_lock(&worker_job_pull_mutex);
		queues[i].used = false;
		queues[i].taken = false;
		pthread_mutex_unlock(&worker_job_pull_mutex);

		job_done:
		worker->busy = false;
		pthread_mutex_unlock(&(worker->mutex));
	}
	return NULL;

	#undef packet
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r15
leaq 0x10(%rdi), %r14
leaq 0x38(%rdi), %rax
movq %rax, -0x38(%rbp)
leaq 0x6771(%rip), %r13      # 0xb3d0
movq %r14, -0x30(%rbp)
movq %r14, %rdi
callq 0x23f0
movq -0x38(%rbp), %rdi
movq %r14, %rsi
callq 0x20e0
movb $0x1, (%r15)
movq %r13, %rdi
callq 0x23f0
movq 0x66de(%rip), %rax      # 0xb368
incq %rax
xorl %r12d, %r12d
cmpq $0x30, %r12
je 0x4caa
cmpb $0x1, -0x1(%rax)
jne 0x4ca1
cmpb $0x1, (%rax)
jne 0x4cb1
incq %r12
addq $0x18, %rax
jmp 0x4c90
movw $0xffff, %r12w          # imm = 0xFFFF
jmp 0x4cb4
movb $0x1, (%rax)
movq %r13, %rdi
callq 0x22b0
cmpw $-0x1, %r12w
je 0x4f7c
movq 0x66aa(%rip), %rax      # 0xb378
movq 0x6693(%rip), %rcx      # 0xb368
movswq %r12w, %rdx
imulq $0x18, %rdx, %rbx
movswq 0x10(%rcx,%rbx), %rdx
imulq $0x50, %rdx, %rdx
movq 0x8(%rax,%rdx), %rdi
incq %rdi
movq %rdi, 0x8(%rax,%rdx)
movq 0x6675(%rip), %rsi      # 0xb370
movswq 0x12(%rcx,%rbx), %rcx
imulq $0x1030, %rcx, %rcx     # imm = 0x1030
addq %rcx, %rsi
addq $0x10, %rsi
movq %rdi, 0x8(%rsi)
movl (%rax,%rdx), %edi
movzwl 0x4(%rsi), %edx
callq 0x20c0
movq %rax, %r13
leaq 0x66fb(%rip), %rax      # 0xb424
cmpb $0x3, (%rax)
jb 0x4dd6
movq 0x663f(%rip), %rax      # 0xb378
movq 0x6628(%rip), %rcx      # 0xb368
movswq 0x10(%rcx,%rbx), %rcx
imulq $0x50, %rcx, %rcx
movq %r15, %r14
movq 0x8(%rax,%rcx), %r15
movl 0x44(%rax,%rcx), %edi
callq 0x20d0
movq 0x6616(%rip), %rdx      # 0xb378
movq 0x65ff(%rip), %rsi      # 0xb368
movswq 0x10(%rsi,%rbx), %rcx
imulq $0x50, %rcx, %rdi
movzwl 0x42(%rdx,%rdi), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
movq 0x8(%rdx,%rdi), %r9
movq 0x65e5(%rip), %rdx      # 0xb370
movswq 0x12(%rsi,%rbx), %rsi
imulq $0x1030, %rsi, %rsi     # imm = 0x1030
movq 0x18(%rdx,%rsi), %r10
cmpq %r10, %r9
leaq 0x2787(%rip), %r11      # 0x752e
leaq 0x277a(%rip), %rdx      # 0x7528
cmoveq %rdx, %r11
leaq 0x2ead(%rip), %rdi      # 0x7c66
movq %r15, %rsi
movq %r14, %r15
movq -0x30(%rbp), %r14
movq %rax, %rdx
movq %r13, %r8
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x5aec
popq %rcx
popq %rdx
testq %r13, %r13
je 0x4eaf
jns 0x4f3a
movq 0x658c(%rip), %rax      # 0xb378
movq 0x6575(%rip), %rcx      # 0xb368
movswq 0x10(%rcx,%rbx), %rcx
imulq $0x50, %rcx, %rcx
movl 0x44(%rax,%rcx), %edi
callq 0x20d0
movq 0x656b(%rip), %rcx      # 0xb378
movq 0x6554(%rip), %rdx      # 0xb368
movswq 0x10(%rdx,%rbx), %rdx
imulq $0x50, %rdx, %rdx
movzwl 0x42(%rcx,%rdx), %ecx
rolw $0x8, %cx
movzwl %cx, %r13d
leaq 0x2e87(%rip), %rdi      # 0x7cb9
movq %rax, %rsi
movl %r13d, %edx
xorl %eax, %eax
callq 0x5aec
leaq 0x2e88(%rip), %rdi      # 0x7cce
callq 0x2350
movq 0x6526(%rip), %rax      # 0xb378
movq 0x650f(%rip), %rcx      # 0xb368
movswq 0x10(%rcx,%rbx), %rcx
imulq $0x50, %rcx, %rcx
movb 0x5(%rax,%rcx), %dl
incb %dl
movb %dl, 0x5(%rax,%rcx)
cmpb $0x10, %dl
jb 0x4f3a
leaq 0x2d04(%rip), %rdi      # 0x7b81
movl %r13d, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x5aec
movq 0x64e7(%rip), %rax      # 0xb378
movq 0x64d0(%rip), %rcx      # 0xb368
movswq 0x10(%rcx,%rbx), %rcx
imulq $0x50, %rcx, %rcx
movl (%rax,%rcx), %edi
callq 0x21a0
jmp 0x4f31
movq 0x64c2(%rip), %rax      # 0xb378
movq 0x64ab(%rip), %rcx      # 0xb368
movswq 0x10(%rcx,%rbx), %rcx
imulq $0x50, %rcx, %rcx
movl (%rax,%rcx), %edi
callq 0x21a0
leaq 0x654e(%rip), %rax      # 0xb424
cmpb $0x0, (%rax)
je 0x4f31
movq 0x6496(%rip), %rax      # 0xb378
movq 0x647f(%rip), %rcx      # 0xb368
movswq 0x10(%rcx,%rbx), %rcx
imulq $0x50, %rcx, %rcx
movl 0x44(%rax,%rcx), %edi
callq 0x20d0
movq 0x6475(%rip), %rcx      # 0xb378
movq 0x645e(%rip), %rdx      # 0xb368
movswq 0x10(%rdx,%rbx), %rdx
imulq $0x50, %rdx, %rdx
movzwl 0x42(%rcx,%rdx), %ecx
rolw $0x8, %cx
movzwl %cx, %edx
leaq 0x2c0a(%rip), %rdi      # 0x7b31
movq %rax, %rsi
xorl %eax, %eax
callq 0x5aec
movzwl %r12w, %edi
callq 0x58c5
movq 0x642f(%rip), %rax      # 0xb370
movq 0x6420(%rip), %rcx      # 0xb368
movswq 0x12(%rcx,%rbx), %rcx
imulq $0x1030, %rcx, %rcx     # imm = 0x1030
decw (%rax,%rcx)
leaq 0x6470(%rip), %r13      # 0xb3d0
movq %r13, %rdi
callq 0x23f0
movq 0x63f9(%rip), %rax      # 0xb368
andw $0x0, (%rax,%rbx)
movq %r13, %rdi
callq 0x22b0
movb $0x0, (%r15)
movq %r14, %rdi
callq 0x22b0
jmp 0x4c63