Generate a defmacro form that transforms code like this:
(integer-reader 4)

Pattern to recognize: Macro call that should be transformed by integer-reader

Macro definition:
(defmacro integer-reader (bytes)
  "Create a function to read integers from a binary stream."
  (let ((bits (* bytes 8)))
    (labels ((return-form (signed)
               (if signed
                   `(if (logbitp ,(1- bits) result)
                        (dpb result (byte ,(1- bits) 0) -1)
                        result)
                   `result))
             (generate-reader (signed)
               `(defun ,(integer-reader-name bytes signed) (socket)
                  (declare (type stream socket)
                           #.*optimize*)
                  ,(if (= bytes 1)
                       `(let ((result (the (unsigned-byte 8) (read-byte socket))))
                          (declare (type (unsigned-byte 8) result))
                          ,(return-form signed))
                       `(let ((result 0))
                          (declare (type (unsigned-byte ,bits) result))
                          ,@(loop :for byte :from (1- bytes) :downto 0
                                  :collect `(setf (ldb (byte 8 ,(* 8 byte))
                                                       result)
                                                  (the (unsigned-byte 8)
                                                       (read-byte socket))))
                          ,(return-form signed))))))
      `(progn
         ;; This causes weird errors on SBCL in some circumstances. Disabled for now.
         ;;         (declaim (inline ,(integer-reader-name bytes t)
         ;;                          ,(integer-reader-name bytes nil)))
         (declaim (ftype (function (t) (signed-byte ,bits))
                         ,(integer-reader-name bytes t)))
         ,(generate-reader t)
         (declaim (ftype (function (t) (unsigned-byte ,bits))
                         ,(integer-reader-name bytes nil)))
         ,(generate-reader nil)))))

Macroexpansion:
(progn (declaim (ftype (function (t) (signed-byte 32)) read-int4)) (defun read-int4 (socket) (declare (type stream socket) (optimize (speed 3) (safety 0) (space 1) (debug 1) (compilation-speed 0))) (let ((result 0)) (declare (type (unsigned-byte 32) result)) (setf (ldb (byte 8 24) result) (the (unsigned-byte 8) (read-byte socket))) (setf (ldb (byte 8 16) result) (the (unsigned-byte 8) (read-byte socket))) (setf (ldb (byte 8 8) result) (the (unsigned-byte 8) (read-byte socket))) (setf (ldb (byte 8 0) result) (the (unsigned-byte 8) (read-byte socket))) (if (logbitp 31 result) (dpb result (byte 31 0) -1) result))) (declaim (ftype (function (t) (unsigned-byte 32)) read-uint4)) (defun read-uint4 (socket) (declare (type stream socket) (optimize (speed 3) (safety 0) (space 1) (debug 1) (compilation-speed 0))) (let ((result 0)) (declare (type (unsigned-byte 32) result)) (setf (ldb (byte 8 24) result) (the (unsigned-byte 8) (read-byte socket))) (setf (ldb (byte 8 16) result) (the (unsigned-byte 8) (read-byte socket))) (setf (ldb (byte 8 8) result) (the (unsigned-byte 8) (read-byte socket))) (setf (ldb (byte 8 0) result) (the (unsigned-byte 8) (read-byte socket))) result)))