def METHOD_NAME(ql: Qiling, key: int, msgflg: int):
    def __create_msq(key: int, flags: int) -> int:
        """Create a new message queue for the specified key.
        Returns: msqid of the newly created message queue, -1 if an error has occurred
        """
        if len(ql.os.msq) >= MSGMNI:
            return -1  # ENOSPC
        mode = flags & ((1 << 9) - 1)
        msqid = ql.os.msq.add(QlMsqId(key, ql.os.uid, ql.os.gid, mode))
        ql.log.debug(f'created a new msg queue: key = {key:#x}, mode = 0{mode:o}. assigned id: {msqid:#x}')
        return msqid
    # create new message queue
    if key == IPC_PRIVATE:
        msqid = __create_msq(key, msgflg)
    else:
        msqid, msq = ql.os.msq.get_by_key(key)
        # a message queue with the specified key does not exist
        if msq is None:
            # the user asked to create a new one?
            if msgflg & IPC_CREAT:
                msqid = __create_msq(key, msgflg)
            else:
                return -1  # ENOENT
        # a message queue with the specified key exists
        else:
            # the user asked to create a new one?
            if msgflg & (IPC_CREAT | IPC_EXCL):
                return -1  # EEXIST
            if __perms(ql, msq, msgflg):
                return -1  # EACCES
    return msqid