// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rdi, %r15
leaq 0x10(%rdi), %r14
leaq 0x38(%rdi), %rax
movq %rax, -0x48(%rbp)
leaq 0x621f(%rip), %r13      # 0xb3d0
movq %rdi, -0x30(%rbp)
movq %r14, -0x40(%rbp)
movq %r14, %rdi
callq 0x23f0
movq -0x48(%rbp), %rdi
movq %r14, %rsi
callq 0x20e0
movb $0x1, (%r15)
movq %r13, %rdi
callq 0x23f0
movq 0x6188(%rip), %rax      # 0xb368
incq %rax
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpb $0x1, -0x1(%rax)
jne 0x51f3
cmpb $0x0, (%rax)
je 0x520e
cmpq $0x2f, %r12
leaq 0x1(%r12), %rcx
setae %bl
addq $0x18, %rax
movq %rcx, %r12
cmpq $0x30, %rcx
jne 0x51e8
jmp 0x521e
movb $0x1, (%rax)
movq %r13, %rdi
callq 0x22b0
testb $0x1, %bl
je 0x522b
movq %r13, %rdi
callq 0x22b0
movw $0xffff, %r12w          # imm = 0xFFFF
cmpw $-0x1, %r12w
je 0x5566
movq 0x613b(%rip), %rax      # 0xb378
movq 0x6124(%rip), %rcx      # 0xb368
movswq %r12w, %rbx
leaq (%rbx,%rbx,2), %r14
movswq 0x10(%rcx,%r14,8), %rdx
leaq (%rdx,%rdx,4), %rdx
shlq $0x4, %rdx
movq 0x8(%rax,%rdx), %rsi
incq %rsi
movq %rsi, 0x8(%rax,%rdx)
movq 0x6102(%rip), %rdx      # 0xb370
movswq 0x12(%rcx,%r14,8), %rdi
imulq $0x1030, %rdi, %rdi     # imm = 0x1030
movq %rsi, 0x18(%rdx,%rdi)
movswq 0x10(%rcx,%r14,8), %rsi
leaq (%rsi,%rsi,4), %rsi
shlq $0x4, %rsi
movl (%rax,%rsi), %edi
movswq 0x12(%rcx,%r14,8), %rax
imulq $0x1030, %rax, %rax     # imm = 0x1030
leaq (%rdx,%rax), %rsi
addq $0x10, %rsi
movzwl 0x4(%rsi), %edx
callq 0x20c0
movq %rax, %r13
leaq 0x616b(%rip), %rax      # 0xb424
cmpb $0x3, (%rax)
jb 0x537b
movq 0x60af(%rip), %rax      # 0xb378
movq 0x6098(%rip), %rcx      # 0xb368
leaq (,%rbx,8), %rdx
leaq (%rdx,%rdx,2), %r15
movswq 0x10(%rcx,%r15), %rcx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movq 0x8(%rax,%rcx), %rdx
movq %rdx, -0x38(%rbp)
movl 0x44(%rax,%rcx), %edi
callq 0x20d0
movq 0x6075(%rip), %rdx      # 0xb378
movq 0x605e(%rip), %rsi      # 0xb368
movswq 0x10(%rsi,%r15), %rcx
leaq (%rcx,%rcx,4), %rdi
shlq $0x4, %rdi
movzwl 0x42(%rdx,%rdi), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
movq 0x8(%rdx,%rdi), %r9
movq 0x6040(%rip), %rdx      # 0xb370
movswq 0x12(%rsi,%r15), %rsi
movq -0x30(%rbp), %r15
imulq $0x1030, %rsi, %rsi     # imm = 0x1030
movq 0x18(%rdx,%rsi), %r10
cmpq %r10, %r9
leaq 0x2201(%rip), %r11      # 0x7551
leaq 0x21f4(%rip), %rdx      # 0x754b
cmoveq %rdx, %r11
leaq 0x2904(%rip), %rdi      # 0x7c66
movq -0x38(%rbp), %rsi
movq %rax, %rdx
movq %r13, %r8
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x605c
addq $0x10, %rsp
testq %r13, %r13
je 0x5474
jns 0x5517
movq 0x5fe7(%rip), %rax      # 0xb378
movq 0x5fd0(%rip), %rcx      # 0xb368
leaq (,%rbx,8), %rdx
leaq (%rdx,%rdx,2), %r15
movswq 0x10(%rcx,%r15), %rcx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movl 0x44(%rax,%rcx), %edi
callq 0x20d0
movq 0x5fb6(%rip), %rcx      # 0xb378
movq 0x5f9f(%rip), %rdx      # 0xb368
movswq 0x10(%rdx,%r15), %rdx
leaq (%rdx,%rdx,4), %rdx
shlq $0x4, %rdx
movzwl 0x42(%rcx,%rdx), %ecx
rolw $0x8, %cx
movzwl %cx, %r13d
leaq 0x28ce(%rip), %rdi      # 0x7cb9
movq %rax, %rsi
movl %r13d, %edx
xorl %eax, %eax
callq 0x605c
leaq 0x28cf(%rip), %rdi      # 0x7cce
callq 0x2350
movq 0x5f6d(%rip), %rax      # 0xb378
movq 0x5f56(%rip), %rcx      # 0xb368
movswq 0x10(%rcx,%r15), %rcx
movq -0x30(%rbp), %r15
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movb 0x5(%rax,%rcx), %dl
incb %dl
movb %dl, 0x5(%rax,%rcx)
cmpb $0x10, %dl
jb 0x5517
leaq 0x2743(%rip), %rdi      # 0x7b81
movl %r13d, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x605c
movq 0x5f26(%rip), %rax      # 0xb378
movq 0x5f0f(%rip), %rcx      # 0xb368
movswq 0x10(%rcx,%r14,8), %rcx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movl (%rax,%rcx), %edi
callq 0x21a0
jmp 0x550e
movq 0x5efd(%rip), %rax      # 0xb378
movq 0x5ee6(%rip), %rcx      # 0xb368
movswq 0x10(%rcx,%r14,8), %rcx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movl (%rax,%rcx), %edi
callq 0x21a0
leaq 0x5f85(%rip), %rax      # 0xb424
cmpb $0x0, (%rax)
je 0x550e
movq 0x5ecd(%rip), %rax      # 0xb378
movq 0x5eb6(%rip), %rcx      # 0xb368
leaq (,%rbx,8), %rdx
leaq (%rdx,%rdx,2), %r14
movswq 0x10(%rcx,%r14), %rcx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movl 0x44(%rax,%rcx), %edi
callq 0x20d0
movq 0x5e9c(%rip), %rcx      # 0xb378
movq 0x5e85(%rip), %rdx      # 0xb368
movswq 0x10(%rdx,%r14), %rdx
leaq (%rdx,%rdx,4), %rdx
shlq $0x4, %rdx
movzwl 0x42(%rcx,%rdx), %ecx
rolw $0x8, %cx
movzwl %cx, %edx
leaq 0x262d(%rip), %rdi      # 0x7b31
movq %rax, %rsi
xorl %eax, %eax
callq 0x605c
movzwl %r12w, %edi
callq 0x5e10
movq 0x5e52(%rip), %rax      # 0xb370
movq 0x5e43(%rip), %rcx      # 0xb368
shlq $0x3, %rbx
leaq (%rbx,%rbx,2), %rbx
movswq 0x12(%rcx,%rbx), %rcx
imulq $0x1030, %rcx, %rcx     # imm = 0x1030
decw (%rax,%rcx)
leaq 0x5e8b(%rip), %r13      # 0xb3d0
movq %r13, %rdi
callq 0x23f0
movq 0x5e14(%rip), %rax      # 0xb368
movw $0x0, (%rax,%rbx)
movq %r13, %rdi
callq 0x22b0
movq -0x40(%rbp), %r14
movb $0x0, (%r15)
movq %r14, %rdi
callq 0x22b0
jmp 0x51b9