// File: /workspace/github_c_makefile_star2_O3/qpfiffer[P]38-Moths/src/server.c
// Function: handler
static void *handler(void *arg) {
	mqd_t accepted_socket_queue = -1;
	mqd_t handled_queue = -1;
	const handler_arg *args = arg;
	const m38_app *app = args->app;
	const int worker_ident = args->worker_ident;

	accepted_socket_queue = mq_open(ACCEPTED_SOCKET_QUEUE_NAME, O_RDONLY);
	if (accepted_socket_queue == -1) {
		m38_log_msg(LOG_ERR, "Worker %i: Could not open accepted_socket_queue.", worker_ident);
		perror("Worker: ");
		return NULL;
	}

	handled_queue = mq_open(HANDLED_CONNECTION_QUEUE_NAME, O_WRONLY);
	if (handled_queue == -1) {
		m38_log_msg(LOG_ERR, "Worker %i: Could not open handled queue.", worker_ident);
		perror("Worker: ");
		return NULL;
	}

	while(1) {
		int new_fd = 0;
		ssize_t msg_size = mq_receive(accepted_socket_queue, (char *)&new_fd, sizeof(int), NULL);

		if (msg_size == -1) {
			m38_log_msg(LOG_ERR, "Worker %i: Could not read from accepted_socket_queue.", worker_ident);
			break;
		} else if (new_fd == -1) {
			m38_log_msg(LOG_ERR, "Worker %i: Got bogus FD from accepted_socket_queue.", worker_ident);
			break;
		} else {
			m38_log_msg(LOG_FUN, "Worker %i: Handling response.", worker_ident);
			m38_handled_request *req = m38_generate_response(new_fd, app);

			ssize_t msg_size = mq_send(handled_queue, (char *)&req, sizeof(m38_handled_request *), 0);
			if (msg_size == -1)
				m38_log_msg(LOG_ERR, "Worker %i: Could not enqueue handled response.", worker_ident);
		}

		struct mq_attr attr = {0};
		if (mq_getattr(accepted_socket_queue, &attr) == 0) {
			m38_log_msg(LOG_INFO, "Acceptor: Number of items on the queue: %i", attr.mq_curmsgs);
		} else {
			m38_log_msg(LOG_INFO, "Acceptor: Could not pull items from queue.");
			perror("Acceptor MQ Problem: ");
		}
	}

	mq_close(accepted_socket_queue);

	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq (%rdi), %r14
movl 0x8(%rdi), %ebx
leaq 0x2c13(%rip), %rdi      # 0xb649
xorl %esi, %esi
xorl %eax, %eax
callq 0x3370
cmpl $-0x1, %eax
je 0x8b96
movl %eax, %ebp
leaq 0x2c1e(%rip), %rdi      # 0xb66f
movl $0x1, %esi
xorl %eax, %eax
callq 0x3370
cmpl $-0x1, %eax
je 0x8b9f
movl %eax, %r15d
leaq 0xc(%rsp), %rsi
movl $0x0, (%rsi)
movl $0x4, %edx
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x34b0
leaq 0x2fd3(%rip), %rsi      # 0xba5c
cmpq $-0x1, %rax
je 0x8b70
leaq 0x10(%rsp), %r12
leaq 0xc(%rsp), %r13
cmpl $-0x1, 0xc(%rsp)
je 0x8b60
movl $0x4, %edi
leaq 0x3012(%rip), %rsi      # 0xbac6
movl %ebx, %edx
xorl %eax, %eax
callq 0x3540
movl 0xc(%rsp), %edi
movq %r14, %rsi
callq 0x3380
movq %rax, 0x10(%rsp)
movl $0x8, %edx
movl %r15d, %edi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x3290
cmpl $-0x1, %eax
jne 0x8afa
movl $0x3, %edi
leaq 0x2ff3(%rip), %rsi      # 0xbae4
movl %ebx, %edx
xorl %eax, %eax
callq 0x3540
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movl %ebp, %edi
movq %r12, %rsi
callq 0x35b0
testl %eax, %eax
jne 0x8b59
movq 0x28(%rsp), %rdx
xorl %edi, %edi
leaq 0x2fe6(%rip), %rsi      # 0xbb13
xorl %eax, %eax
callq 0x3540
movl $0x0, 0xc(%rsp)
movl $0x4, %edx
movl %ebp, %edi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x34b0
cmpq $-0x1, %rax
jne 0x8a9d
jmp 0x8b69
callq 0x382c
jmp 0x8b34
leaq 0x2f2b(%rip), %rsi      # 0xba92
jmp 0x8b70
leaq 0x2eec(%rip), %rsi      # 0xba5c
movl $0x3, %edi
movl %ebx, %edx
xorl %eax, %eax
callq 0x3540
movl %ebp, %edi
callq 0x3260
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebx, %edi
callq 0x386f
jmp 0x8b85
movl %ebx, %edi
callq 0x384e
jmp 0x8b85