// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movl $0xffffffff, -0x14(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x18(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x2c(%rbp)
leaq 0x22e8(%rip), %rdi      # 0xd679
xorl %esi, %esi
movb $0x0, %al
callq 0x3390
movl %eax, -0x14(%rbp)
cmpl $-0x1, -0x14(%rbp)
jne 0xb3d2
movl -0x2c(%rbp), %edx
movl $0x3, %edi
leaq 0x26b7(%rip), %rsi      # 0xda69
movb $0x0, %al
callq 0x3580
leaq 0x26da(%rip), %rdi      # 0xda9a
callq 0x3520
movq $0x0, -0x8(%rbp)
jmp 0xb550
leaq 0x22c6(%rip), %rdi      # 0xd69f
movl $0x1, %esi
movb $0x0, %al
callq 0x3390
movl %eax, -0x18(%rbp)
cmpl $-0x1, -0x18(%rbp)
jne 0xb41d
movl -0x2c(%rbp), %edx
movl $0x3, %edi
leaq 0x26a6(%rip), %rsi      # 0xdaa3
movb $0x0, %al
callq 0x3580
leaq 0x268f(%rip), %rdi      # 0xda9a
callq 0x3520
movq $0x0, -0x8(%rbp)
jmp 0xb550
jmp 0xb41f
movl $0x0, -0x30(%rbp)
movl -0x14(%rbp), %edi
leaq -0x30(%rbp), %rsi
movl $0x4, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x34e0
movq %rax, -0x38(%rbp)
cmpq $-0x1, -0x38(%rbp)
jne 0xb461
movl -0x2c(%rbp), %edx
movl $0x3, %edi
leaq 0x2677(%rip), %rsi      # 0xdacc
movb $0x0, %al
callq 0x3580
jmp 0xb540
cmpl $-0x1, -0x30(%rbp)
jne 0xb482
movl -0x2c(%rbp), %edx
movl $0x3, %edi
leaq 0x268c(%rip), %rsi      # 0xdb02
movb $0x0, %al
callq 0x3580
jmp 0xb540
movl -0x2c(%rbp), %edx
movl $0x4, %edi
leaq 0x26a5(%rip), %rsi      # 0xdb36
movb $0x0, %al
callq 0x3580
movl -0x30(%rbp), %edi
movq -0x28(%rbp), %rsi
callq 0x33a0
movq %rax, -0x40(%rbp)
movl -0x18(%rbp), %edi
leaq -0x40(%rbp), %rsi
movl $0x8, %edx
xorl %ecx, %ecx
callq 0x32b0
cltq
movq %rax, -0x48(%rbp)
cmpq $-0x1, -0x48(%rbp)
jne 0xb4de
movl -0x2c(%rbp), %edx
movl $0x3, %edi
leaq 0x267d(%rip), %rsi      # 0xdb54
movb $0x0, %al
callq 0x3580
jmp 0xb4e0
jmp 0xb4e2
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x3240
movl -0x14(%rbp), %edi
leaq -0x88(%rbp), %rsi
callq 0x35f0
cmpl $0x0, %eax
jne 0xb51f
movq -0x70(%rbp), %rdx
xorl %edi, %edi
leaq 0x266d(%rip), %rsi      # 0xdb83
movb $0x0, %al
callq 0x3580
jmp 0xb53b
xorl %edi, %edi
leaq 0x2686(%rip), %rsi      # 0xdbae
movb $0x0, %al
callq 0x3580
leaq 0x26a3(%rip), %rdi      # 0xdbd9
callq 0x3520
jmp 0xb41f
movl -0x14(%rbp), %edi
callq 0x3280
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)