// File: /workspace/github_c_makefile_star2_O2/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 0x246b(%rip), %rdi      # 0xa649
xorl %esi, %esi
xorl %eax, %eax
callq 0x3370
cmpl $-0x1, %eax
je 0x8323
movl %eax, %ebp
leaq 0x2476(%rip), %rdi      # 0xa66f
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x3370
cmpl $-0x1, %eax
je 0x832c
movl %eax, %r12d
pushq $0x4
popq %r15
leaq 0x10(%rsp), %r13
andl $0x0, 0xc(%rsp)
movl %ebp, %edi
leaq 0xc(%rsp), %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x34b0
cmpq $-0x1, %rax
je 0x82ef
cmpl $-0x1, 0xc(%rsp)
je 0x82f8
pushq $0x4
popq %rdi
leaq 0x2879(%rip), %rsi      # 0xaac6
movl %ebx, %edx
xorl %eax, %eax
callq 0x3540
movl 0xc(%rsp), %edi
movq %r14, %rsi
callq 0x3380
movq %rax, 0x10(%rsp)
movl %r12d, %edi
movq %r13, %rsi
pushq $0x8
popq %rdx
xorl %ecx, %ecx
callq 0x3290
cmpl $-0x1, %eax
jne 0x828f
pushq $0x3
popq %rdi
leaq 0x285e(%rip), %rsi      # 0xaae4
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 %r13, %rsi
callq 0x35b0
testl %eax, %eax
jne 0x82ce
movq 0x28(%rsp), %rdx
xorl %edi, %edi
leaq 0x2851(%rip), %rsi      # 0xab13
xorl %eax, %eax
callq 0x3540
jmp 0x8218
xorl %edi, %edi
leaq 0x2867(%rip), %rsi      # 0xab3e
xorl %eax, %eax
callq 0x3540
leaq 0x2884(%rip), %rdi      # 0xab69
callq 0x34f0
jmp 0x8218
leaq 0x2766(%rip), %rsi      # 0xaa5c
jmp 0x82ff
leaq 0x2793(%rip), %rsi      # 0xaa92
pushq $0x3
popq %rdi
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
leaq 0x26cf(%rip), %rsi      # 0xa9f9
jmp 0x8333
leaq 0x2700(%rip), %rsi      # 0xaa33
pushq $0x3
popq %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x3540
leaq 0x26e4(%rip), %rdi      # 0xaa2a
callq 0x34f0
jmp 0x8312