// File: /workspace/github_c_makefile_star2_O3/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
leaq 0x10(%rdi), %r14
movq %rdi, -0x30(%rbp)
leaq 0x38(%rdi), %r13
leaq 0x62ab(%rip), %rbx      # 0xb3d0
movq %r13, -0x40(%rbp)
movq %r14, %rdi
callq 0x23f0
movq %r13, %rdi
movq %r14, %rsi
callq 0x20e0
movq -0x30(%rbp), %rax
movb $0x1, (%rax)
movq %rbx, %r15
movq %rbx, %rdi
callq 0x23f0
xorl %ebx, %ebx
movq 0x6211(%rip), %rax      # 0xb368
xorl %r12d, %r12d
cmpb $0x1, (%rax,%r12)
jne 0x5169
cmpb $0x1, 0x1(%rax,%r12)
jne 0x517d
addq $0x18, %r12
incl %ebx
cmpq $0x480, %r12            # imm = 0x480
jne 0x515a
jmp 0x5453
movb $0x1, 0x1(%rax,%r12)
movq %r15, %rdi
callq 0x22b0
movq 0x61e6(%rip), %rax      # 0xb378
movq 0x61cf(%rip), %rcx      # 0xb368
movswq 0x10(%rcx,%r12), %rdx
leaq (%rdx,%rdx,4), %rdx
shlq $0x4, %rdx
movq 0x8(%rax,%rdx), %rdi
incq %rdi
movq %rdi, 0x8(%rax,%rdx)
movq 0x61b5(%rip), %rsi      # 0xb370
movswq 0x12(%rcx,%r12), %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 0x623b(%rip), %rax      # 0xb424
cmpb $0x3, (%rax)
jb 0x529b
movq 0x617f(%rip), %rax      # 0xb378
movq 0x6168(%rip), %rcx      # 0xb368
movswq 0x10(%rcx,%r12), %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 0x6151(%rip), %rdx      # 0xb378
movq 0x613a(%rip), %rsi      # 0xb368
movswq 0x10(%rsi,%r12), %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 0x611c(%rip), %rdx      # 0xb370
movswq 0x12(%rsi,%r12), %rsi
imulq $0x1030, %rsi, %rsi     # imm = 0x1030
movq 0x18(%rdx,%rsi), %r10
cmpq %r10, %r9
leaq 0x22be(%rip), %r11      # 0x752e
leaq 0x22b1(%rip), %rdx      # 0x7528
cmoveq %rdx, %r11
leaq 0x29e4(%rip), %rdi      # 0x7c66
movq -0x38(%rbp), %rsi
movq %rax, %rdx
movq %r13, %r8
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x5fb8
addq $0x10, %rsp
testq %r13, %r13
je 0x5384
jns 0x541a
movq 0x60c7(%rip), %rax      # 0xb378
movq 0x60b0(%rip), %rcx      # 0xb368
movswq 0x10(%rcx,%r12), %rcx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movl 0x44(%rax,%rcx), %edi
callq 0x20d0
movq 0x60a2(%rip), %rcx      # 0xb378
movq 0x608b(%rip), %rdx      # 0xb368
movswq 0x10(%rdx,%r12), %rdx
leaq (%rdx,%rdx,4), %rdx
shlq $0x4, %rdx
movzwl 0x42(%rcx,%rdx), %ecx
rolw $0x8, %cx
movzwl %cx, %r13d
leaq 0x29ba(%rip), %rdi      # 0x7cb9
movq %rax, %rsi
movl %r13d, %edx
xorl %eax, %eax
callq 0x5fb8
leaq 0x29bb(%rip), %rdi      # 0x7cce
callq 0x2350
movq 0x6059(%rip), %rax      # 0xb378
movq 0x6042(%rip), %rcx      # 0xb368
movswq 0x10(%rcx,%r12), %rcx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movb 0x5(%rax,%rcx), %dl
incb %dl
movb %dl, 0x5(%rax,%rcx)
cmpb $0x10, %dl
jb 0x541a
leaq 0x2833(%rip), %rdi      # 0x7b81
movl %r13d, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x5fb8
movq 0x6016(%rip), %rax      # 0xb378
movq 0x5fff(%rip), %rcx      # 0xb368
movswq 0x10(%rcx,%r12), %rcx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movl (%rax,%rcx), %edi
callq 0x21a0
jmp 0x5412
movq 0x5fed(%rip), %rax      # 0xb378
movq 0x5fd6(%rip), %rcx      # 0xb368
movswq 0x10(%rcx,%r12), %rcx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movl (%rax,%rcx), %edi
callq 0x21a0
leaq 0x6075(%rip), %rax      # 0xb424
cmpb $0x0, (%rax)
je 0x5412
movq 0x5fbd(%rip), %rax      # 0xb378
movq 0x5fa6(%rip), %rcx      # 0xb368
movswq 0x10(%rcx,%r12), %rcx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movl 0x44(%rax,%rcx), %edi
callq 0x20d0
movq 0x5f98(%rip), %rcx      # 0xb378
movq 0x5f81(%rip), %rdx      # 0xb368
movswq 0x10(%rdx,%r12), %rdx
leaq (%rdx,%rdx,4), %rdx
shlq $0x4, %rdx
movzwl 0x42(%rcx,%rdx), %ecx
rolw $0x8, %cx
movzwl %cx, %edx
leaq 0x2729(%rip), %rdi      # 0x7b31
movq %rax, %rsi
xorl %eax, %eax
callq 0x5fb8
movzwl %bx, %edi
callq 0x5d87
movq 0x5f4f(%rip), %rax      # 0xb370
movq 0x5f40(%rip), %rcx      # 0xb368
movswq 0x12(%rcx,%r12), %rcx
imulq $0x1030, %rcx, %rcx     # imm = 0x1030
decw (%rax,%rcx)
movq %r15, %rdi
callq 0x23f0
movq 0x5f20(%rip), %rax      # 0xb368
movw $0x0, (%rax,%r12)
movq -0x40(%rbp), %r13
movq %r15, %rbx
movq %r15, %rdi
callq 0x22b0
movq -0x30(%rbp), %rax
movb $0x0, (%rax)
movq %r14, %rdi
callq 0x22b0
jmp 0x5129