Merge "Enforce no declarations after statements"
diff --git a/src/Makefile b/src/Makefile
index ae08818..7d6ee45 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -2,7 +2,7 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-CFLAGS += -Wall -Wextra -Werror
+CFLAGS += -Wall -Wextra -Werror -Wdeclaration-after-statement
 LIBDIR = /usr/lib
 SBINDIR = /usr/sbin
 
diff --git a/src/connect_disconnect_test.c b/src/connect_disconnect_test.c
index 9c29289..0d18a42 100644
--- a/src/connect_disconnect_test.c
+++ b/src/connect_disconnect_test.c
@@ -24,8 +24,10 @@
 
 static void connected(struct qmidev *qmidev, void *data, int status)
 {
+  int result;
+
   assert(status == 0);
-  int result = qmidev_disconnect(qmidev, &disconnected, data);
+  result = qmidev_disconnect(qmidev, &disconnected, data);
   assert(result == 0);
 }
 
diff --git a/src/dev.c b/src/dev.c
index df797bf..3e51142 100644
--- a/src/dev.c
+++ b/src/dev.c
@@ -28,9 +28,11 @@
 
 struct dev *dev_new(struct dev_ops *ops, void *priv)
 {
+  struct dev *dev;
+
   assert(ops);
 
-  struct dev *dev = g_slice_new(struct dev);
+  dev = g_slice_new(struct dev);
   dev->ops = ops;
   dev->priv = priv;
   dev->flags = 0;
@@ -57,11 +59,13 @@
 
 int dev_open(struct dev *dev)
 {
+  int result;
+
   assert(dev);
   assert(!dev_is_open(dev));
   assert(dev->ops->open);
 
-  int result = dev->ops->open(dev);
+  result = dev->ops->open(dev);
 
   if (result == 0)
     dev->flags |= FLAG_OPEN;
@@ -89,11 +93,13 @@
 
 int dev_close(struct dev *dev)
 {
+  int result;
+
   assert(dev);
   assert(dev_is_open(dev));
   assert(dev->ops->close);
 
-  int result = dev->ops->close(dev);
+  result = dev->ops->close(dev);
 
   if (result == 0)
     dev->flags &= ~FLAG_OPEN;
diff --git a/src/file.c b/src/file.c
index 55fee11..0dd4695 100644
--- a/src/file.c
+++ b/src/file.c
@@ -29,15 +29,18 @@
 
 static int file_open(struct dev *dev)
 {
+  struct file_priv *priv;
+  int fd, result;
+
   assert(dev);
-  struct file_priv *priv = dev_priv(dev);
+  priv = dev_priv(dev);
   assert(!FILE_OPEN(priv));
 
-  int fd = open(priv->path, O_RDWR);
+  fd = open(priv->path, O_RDWR);
   if (fd < 0)
     return errno;
 
-  int result = qmidev_set_dev_fd(priv->qmidev, fd);
+  result = qmidev_set_dev_fd(priv->qmidev, fd);
   if (result) {
     close(fd);
     return result;
@@ -50,12 +53,15 @@
 
 static int file_read(struct dev *dev, void *buf, size_t len)
 {
+  struct file_priv *priv;
+  ssize_t result;
+
   assert(dev);
   assert(buf);
-  struct file_priv *priv = dev_priv(dev);
+  priv = dev_priv(dev);
   assert(FILE_OPEN(priv));
 
-  ssize_t result = read(priv->fd, buf, len);
+  result = read(priv->fd, buf, len);
   if (result < 0)
     return result;
 
@@ -66,12 +72,15 @@
 
 static int file_write(struct dev *dev, const void *buf, size_t len)
 {
+  struct file_priv *priv;
+  ssize_t result;
+
   assert(dev);
   assert(buf);
-  struct file_priv *priv = dev_priv(dev);
+  priv = dev_priv(dev);
   assert(FILE_OPEN(priv));
 
-  ssize_t result = write(priv->fd, buf, len);
+  result = write(priv->fd, buf, len);
   if (result < 0)
     return result;
 
@@ -82,11 +91,14 @@
 
 static int file_close(struct dev *dev)
 {
+  struct file_priv *priv;
+  int result;
+
   assert(dev);
-  struct file_priv *priv = dev_priv(dev);
+  priv = dev_priv(dev);
   assert(FILE_OPEN(priv));
 
-  int result = close(priv->fd);
+  result = close(priv->fd);
   if (result < 0)
     return result;
 
@@ -99,8 +111,10 @@
 
 static void file_destroy(struct dev *dev)
 {
+  struct file_priv *priv;
+
   assert(dev);
-  struct file_priv *priv = dev_priv(dev);
+  priv = dev_priv(dev);
   assert(!FILE_OPEN(priv));
   g_free(priv->path);
   g_slice_free(struct file_priv, priv);
diff --git a/src/mock.c b/src/mock.c
index 4d1c10d..9c55c44 100644
--- a/src/mock.c
+++ b/src/mock.c
@@ -28,8 +28,10 @@
 
 static int mock_open(struct dev *dev)
 {
+  struct mock_priv *priv;
+
   assert(dev);
-  struct mock_priv *priv = dev_priv(dev);
+  priv = dev_priv(dev);
   assert(priv);
 
   priv->is_open = 1;
@@ -39,9 +41,11 @@
 
 static int mock_read(struct dev *dev, void *buf, size_t len)
 {
+  struct mock_priv *priv;
+
   assert(dev);
   assert(buf);
-  struct mock_priv *priv = dev_priv(dev);
+  priv = dev_priv(dev);
   assert(priv);
 
   /* TODO: Actually read. */
@@ -52,9 +56,11 @@
 
 static int mock_write(struct dev *dev, const void *buf, size_t len)
 {
+  struct mock_priv *priv;
+
   assert(dev);
   assert(buf);
-  struct mock_priv *priv = dev_priv(dev);
+  priv = dev_priv(dev);
   assert(priv);
   assert(priv->is_open);
 
@@ -66,8 +72,10 @@
 
 static int mock_close(struct dev *dev)
 {
+  struct mock_priv *priv;
+
   assert(dev);
-  struct mock_priv *priv = dev_priv(dev);
+  priv = dev_priv(dev);
   assert(priv);
   assert(priv->is_open);
 
@@ -78,8 +86,10 @@
 
 static void mock_destroy(struct dev *dev)
 {
+  struct mock_priv *priv;
+
   assert(dev);
-  struct mock_priv *priv = dev_priv(dev);
+  priv = dev_priv(dev);
   g_slice_free(struct mock_priv, priv);
 }
 
@@ -93,9 +103,10 @@
 
 struct dev *mock_new(void)
 {
+  struct mock_priv *priv;
   int result;
 
-  struct mock_priv *priv = g_slice_new(struct mock_priv);
+  priv = g_slice_new(struct mock_priv);
   result = pipe(&priv->fds[0]);
   if (result < 0) {
     g_slice_free(struct mock_priv, priv);
@@ -106,6 +117,5 @@
     g_slice_free(struct mock_priv, priv);
     return NULL;
   }
-  struct dev *dev = dev_new(&mock_ops, priv);
-  return dev;
+  return dev_new(&mock_ops, priv);
 }
diff --git a/src/poller.c b/src/poller.c
index a11cef0..aca8de6 100644
--- a/src/poller.c
+++ b/src/poller.c
@@ -32,10 +32,13 @@
 
 struct poller *poller_new(void)
 {
-  int epoll_fd = epoll_create1(0);
+  struct poller *poller;
+  int epoll_fd;
+
+  epoll_fd = epoll_create1(0);
   if (epoll_fd < 0)
     return NULL;
-  struct poller *poller = g_slice_new(struct poller);
+  poller = g_slice_new(struct poller);
   poller->epoll_fd = epoll_fd;
   poller->num_polled = 0;
   return poller;
@@ -57,11 +60,12 @@
 
 struct polled *poller_add(struct poller *poller, int fd)
 {
-  assert(poller);
-
-  struct polled *polled = g_slice_new(struct polled);
+  struct polled *polled;
   struct epoll_event event;
 
+  assert(poller);
+
+  polled = g_slice_new(struct polled);
   polled->poller = poller;
   polled->fd = fd;
   polled->priv = NULL;
@@ -84,11 +88,12 @@
 
 static int poller_update(struct poller *poller, struct polled *polled)
 {
+  struct epoll_event event;
+
   assert(poller);
   assert(polled);
   assert(polled->poller == poller);
 
-  struct epoll_event event;
   event.events = 0;
   event.data.ptr = polled;
 
@@ -104,11 +109,13 @@
 
 static int poller_remove(struct poller *poller, struct polled *polled)
 {
+  int result;
+
   assert(poller);
   assert(polled);
   assert(polled->poller == poller);
 
-  int result = epoll_ctl(poller->epoll_fd, EPOLL_CTL_DEL, polled->fd, NULL);
+  result = epoll_ctl(poller->epoll_fd, EPOLL_CTL_DEL, polled->fd, NULL);
   if (result)
     return result;
 
diff --git a/src/power_stress_test.c b/src/power_stress_test.c
index 23c022b..ab64148 100644
--- a/src/power_stress_test.c
+++ b/src/power_stress_test.c
@@ -19,14 +19,18 @@
 {
   assert(status == 0);
   printf("set_power: status = %d\n", status);
+
+  /* unused */
   qmidev = qmidev;
   data = data;
 }
 
 static void set_next_state(struct qmidev *qmidev, struct context *context)
 {
+  int result;
+
   qmidev_power_state next_state = 1 - context->last_state;
-  int result = qmidev_set_power(qmidev, next_state, &set_power, context);
+  result = qmidev_set_power(qmidev, next_state, &set_power, context);
   assert(result == 0);
 }
 
@@ -54,29 +58,34 @@
 
 static void set_power_callback(struct qmidev *qmidev, void *data, int status)
 {
+  int result;
+
   assert(status == 0);
   printf("set_power_callback: status %d\n", status);
-  int result = qmidev_get_power(qmidev, &got_power, data);
+  result = qmidev_get_power(qmidev, &got_power, data);
   assert(result == 0);
 }
 
 static void connected(struct qmidev *qmidev, void *data, int status)
 {
+  int result;
+
   assert(status == 0);
-  int result = qmidev_set_power_callback(qmidev,
-                                         &power_callback, data,
-                                         &set_power_callback, data);
+  result = qmidev_set_power_callback(qmidev,
+                                     &power_callback, data,
+                                     &set_power_callback, data);
   assert(result == 0);
 }
 
 int main(void)
 {
+  struct qmidev *qmidev;
   struct context context;
   int result;
 
   context.done = 0;
 
-  struct qmidev *qmidev = qmidev_new_file("/dev/cdc-wdm0");
+  qmidev = qmidev_new_file("/dev/cdc-wdm0");
   assert(qmidev);
 
   result = qmidev_connect(qmidev, &connected, &context);
diff --git a/src/qmi.c b/src/qmi.c
index ad18a2a..3119a6c 100644
--- a/src/qmi.c
+++ b/src/qmi.c
@@ -29,12 +29,13 @@
 
 int qmi_result(struct qmimsg *message)
 {
+  struct qmi_result msg_result;
+  int result;
+
   assert(message);
 
-  struct qmi_result msg_result;
-
-  int result = qmimsg_tlv_get(message, QMI_TLV_RESULT_CODE,
-                              sizeof(msg_result), &msg_result);
+  result = qmimsg_tlv_get(message, QMI_TLV_RESULT_CODE,
+                          sizeof(msg_result), &msg_result);
   if (result)
     return QMI_ERR_MESSAGE_INVALID;
 
@@ -53,13 +54,13 @@
                    uint16_t *length_out,
                    char **value_out)
 {
-  assert(message);
-  assert(value_out);
-
   uint16_t length;
   char *value;
   int result;
 
+  assert(message);
+  assert(value_out);
+
   result = qmimsg_tlv_get_varlen(message, type, &length, NULL);
   if (result)
     return result;
diff --git a/src/qmictl.c b/src/qmictl.c
index 1e7717c..0c49e3e 100644
--- a/src/qmictl.c
+++ b/src/qmictl.c
@@ -122,12 +122,14 @@
                                  int result,
                                  struct qmimsg *message)
 {
+  struct release_cid_context *context;
+  struct qmi_cid id;
+
   assert(qmidev);
   assert(data);
   assert(message);
 
-  struct release_cid_context *context = data;
-  struct qmi_cid id;
+  context = data;
 
   if (result)
     goto out;
diff --git a/src/qmidev.c b/src/qmidev.c
index 246ee7b..ca41b27 100644
--- a/src/qmidev.c
+++ b/src/qmidev.c
@@ -100,11 +100,13 @@
 
 struct qmidev *qmidev_new_file(const char *path)
 {
+  struct qmidev *qmidev;
+  struct dev *dev;
+
   assert(path);
 
-  struct qmidev *qmidev = qmidev_new();
-
-  struct dev *dev = file_new(qmidev, path);
+  qmidev = qmidev_new();
+  dev = file_new(qmidev, path);
   if (!dev) {
     free(qmidev);
     return NULL;
@@ -116,9 +118,11 @@
 
 struct qmidev *qmidev_new_mock(void)
 {
-  struct qmidev *qmidev = qmidev_new();
+  struct qmidev *qmidev;
+  struct dev *dev;
 
-  struct dev *dev = mock_new();
+  qmidev = qmidev_new();
+  dev = mock_new();
   qmidev->dev = dev;
 
   return qmidev;
@@ -156,12 +160,13 @@
                                qmidev_listen_callback_fn callback,
                                void *context)
 {
+  struct listener *listener;
+
   assert(qmidev);
   assert(criteria);
   assert(callback);
 
-  struct listener *listener = g_slice_new(struct listener);
-
+  listener = g_slice_new(struct listener);
   listener->client = client;
   listener->criteria = *criteria;
   listener->callback = callback;
@@ -198,6 +203,7 @@
   struct listener *listener = data;
   struct listen_criteria *crit = &listener->criteria;
   int flags = crit->flags;
+  int result;
 
   if (listener->client->service->service_id != context->msg_service)
     return;
@@ -218,9 +224,9 @@
       (crit->message != context->msg_message))
     return;
 
-  int result = listener->callback(context->qmidev,
-                                  listener->context,
-                                  context->message);
+  result = listener->callback(context->qmidev,
+                              listener->context,
+                              context->message);
   if (result) {
     /* The glib documentation does not note this, but it is in fact safe to
        call g_list_remove while called back from g_list_foreach. */
@@ -798,13 +804,16 @@
                    qmidev_response_fn caller_callback,
                    void *caller_context)
 {
+  struct connect_context *context;
+  int result;
+
   assert(qmidev);
   assert(caller_callback);
 
   if (qmidev->connected)
     return QMI_ERR_ALREADY_CONNECTED;
 
-  int result = dev_open(qmidev->dev);
+  result = dev_open(qmidev->dev);
   /* TODO: Should this go through the callback instead? */
   if (result)
     return result;
@@ -813,7 +822,7 @@
 
   qmidev->clients = g_list_prepend(qmidev->clients, make_ctl_client(qmidev));
 
-  struct connect_context *context = g_slice_new(struct connect_context);
+  context = g_slice_new(struct connect_context);
   context->caller_callback = caller_callback;
   context->caller_context = caller_context;
   context->pending_services = g_queue_new();
@@ -884,13 +893,15 @@
                       qmidev_response_fn caller_callback,
                       void *caller_context)
 {
+  struct disconnect_context *context;
+
   assert(qmidev);
   assert(caller_callback);
 
   if (!qmidev->connected)
     return QMI_ERR_NOT_CONNECTED;
 
-  struct disconnect_context *context = g_slice_new(struct disconnect_context);
+  context = g_slice_new(struct disconnect_context);
   context->caller_callback = caller_callback;
   context->caller_context = caller_context;
 
@@ -901,8 +912,9 @@
 
 static int dev_read_fn(void *ctx, void *buf, size_t len)
 {
+  struct qmidev *qmidev;
   assert(ctx);
-  struct qmidev *qmidev = ctx;
+  qmidev = ctx;
   assert(qmidev->dev);
 
   return dev_read(qmidev->dev, buf, len);
@@ -910,12 +922,15 @@
 
 static void dev_read_cb(struct polled *polled)
 {
+  struct qmidev *qmidev;
+  struct qmimsg *msg;
+  int result;
+
   assert(polled);
-  struct qmidev *qmidev = polled_priv(polled);
+  qmidev = polled_priv(polled);
   assert(qmidev);
 
-  struct qmimsg *msg;
-  int result = qmimsg_read(&dev_read_fn, qmidev, &msg);
+  result = qmimsg_read(&dev_read_fn, qmidev, &msg);
   if (result)
     return;
   if (call_listeners(qmidev, msg) && qmidev_log_unhandled_messages) {
@@ -927,8 +942,10 @@
 
 static void dev_close_cb(struct polled *polled)
 {
+  struct qmidev *qmidev;
+
   assert(polled);
-  struct qmidev *qmidev = polled_priv(polled);
+  qmidev = polled_priv(polled);
   assert(qmidev);
 
   qmidev_clear_dev_fd(qmidev);
@@ -968,8 +985,10 @@
 
 static int dev_write_fn(void *ctx, const void *buf, size_t len)
 {
+  struct qmidev *qmidev;
+
   assert(ctx);
-  struct qmidev *qmidev = ctx;
+  qmidev = ctx;
   assert(qmidev->dev);
 
   return dev_write(qmidev->dev, buf, len);
@@ -994,10 +1013,12 @@
 
 void qmidev_call_later(struct qmidev *qmidev, callback_fn func, void *context)
 {
+  struct callback *callback;
+
   assert(qmidev);
   assert(func);
 
-  struct callback *callback = g_slice_new(struct callback);
+  callback = g_slice_new(struct callback);
   callback->func = func;
   callback->context = context;
   qmidev->callbacks = g_list_append(qmidev->callbacks, callback);
diff --git a/src/qmidms.c b/src/qmidms.c
index 6c68162..ef67be1 100644
--- a/src/qmidms.c
+++ b/src/qmidms.c
@@ -50,19 +50,23 @@
 
 static void client_create(struct qmidev *qmidev, struct client *client)
 {
-  qmidev = qmidev;
   struct qmidms_priv *priv = g_slice_new(struct qmidms_priv);
   event_init(&priv->operating_mode,
              QMIDMS_EVENT_OPERATING_MODE,
              &operating_mode_changed);
   client_set_priv(client, priv);
+
+  /* unused */
+  qmidev = qmidev;
 }
 
 static void client_destroy(struct qmidev *qmidev, struct client *client)
 {
-  qmidev = qmidev;
   struct qmidms_priv *priv = client_priv(client);
   g_slice_free(struct qmidms_priv, priv);
+
+  /* unused */
+  qmidev = qmidev;
 }
 
 struct service qmidms_service = {
diff --git a/src/qmiinfo.c b/src/qmiinfo.c
index 6577e8b..1ed58f2 100644
--- a/src/qmiinfo.c
+++ b/src/qmiinfo.c
@@ -23,25 +23,30 @@
 static void got_ids(struct qmidev *qmidev, void *data, int status,
                     const char *esn, const char *imei, const char *meid)
 {
+  int result;
+
   assert(status == 0);
   printf("ESN: %s\nIMEI: %s\nMEID: %s\n", esn, imei, meid);
-  int result = qmidev_disconnect(qmidev, &disconnected, data);
+  result = qmidev_disconnect(qmidev, &disconnected, data);
   assert(result == 0);
 }
 
 static void connected(struct qmidev *qmidev, void *data, int status)
 {
+  int result;
+
   assert(status == 0);
-  int result = qmidev_get_ids(qmidev, &got_ids, data);
+  result = qmidev_get_ids(qmidev, &got_ids, data);
   assert(result == 0);
 }
 
 int main(void)
 {
+  struct qmidev *qmidev;
   int done = 0;
   int result;
 
-  struct qmidev *qmidev = qmidev_new_file("/dev/cdc-wdm0");
+  qmidev = qmidev_new_file("/dev/cdc-wdm0");
   assert(qmidev);
 
   result = qmidev_connect(qmidev, &connected, &done);
diff --git a/src/qmimsg.c b/src/qmimsg.c
index 57a8ce4..e579431 100644
--- a/src/qmimsg.c
+++ b/src/qmimsg.c
@@ -184,6 +184,10 @@
  */
 static int qmi_check(struct qmimsg *m)
 {
+  size_t header_length;
+  char *end;
+  struct tlv *tlv;
+
   assert(m);
   assert(m->buf);
 
@@ -206,7 +210,6 @@
     return 0;
   }
 
-  size_t header_length;
   if (qmi_is_control(m))
     header_length = sizeof(struct qmux) + sizeof(struct control_header);
   else
@@ -218,12 +221,12 @@
   }
 
   if (qmux_length(m) - header_length != qmi_tlv_length(m)) {
-    fprintf(stderr, "qmi_check: QMUX length and QMI TLV lengths don't match\n");
+    fprintf(stderr,
+            "qmi_check: QMUX length and QMI TLV lengths don't match\n");
     return 0;
   }
 
-  char *end = qmi_end(m);
-  struct tlv *tlv;
+  end = qmi_end(m);
   for (tlv = qmi_tlv(m); tlv < (struct tlv *)end; tlv = tlv_next(tlv)) {
     if (tlv->value > end) {
       fprintf(stderr, "qmi_check: TLV header runs over buffer\n");
@@ -247,9 +250,11 @@
                uint8_t qmi_flags, uint16_t transaction, uint16_t message,
                struct qmimsg **message_out)
 {
+  struct qmimsg *m;
+
   assert(service || (transaction <= UINT8_MAX));
 
-  struct qmimsg *m = g_slice_new(struct qmimsg);
+  m = g_slice_new(struct qmimsg);
 
   if (!service)
     m->len = 1 + sizeof(struct qmux) + sizeof(struct control_header);
@@ -354,6 +359,8 @@
 
 void qmimsg_print(struct qmimsg *m)
 {
+  struct tlv *tlv;
+
   assert(m);
 
   fprintf(stderr,
@@ -371,7 +378,6 @@
           qmi_message(m),
           qmi_tlv_length(m));
 
-  struct tlv *tlv;
   for (tlv = qmi_tlv_first(m); tlv; tlv = qmi_tlv_next(m, tlv)) {
     fprintf(stderr, "TLV:  type=0x%02x length=0x%04x\n",
             tlv->type, tlv->length);
@@ -392,11 +398,12 @@
                                    void *value,
                                    int length_exact)
 {
+  struct tlv *tlv;
+
   assert(message);
   assert(message->buf);
   assert(length);
 
-  struct tlv *tlv;
   for (tlv = qmi_tlv_first(message); tlv; tlv = qmi_tlv_next(message, tlv)) {
     if (tlv->type == type) {
       if (length_exact && (tlv->length != *length)) {
@@ -431,10 +438,11 @@
 void qmimsg_tlv_foreach(struct qmimsg *message,
                         qmimsg_tlv_foreach_fn func, void *context)
 {
+  struct tlv *tlv;
+
   assert(message);
   assert(func);
 
-  struct tlv *tlv;
   for (tlv = qmi_tlv_first(message); tlv; tlv = qmi_tlv_next(message, tlv)) {
     func(context, tlv->type, tlv->length, tlv->value);
   }
@@ -444,6 +452,9 @@
 int qmimsg_tlv_add(struct qmimsg *m,
                    uint8_t type, uint16_t length, const void *value)
 {
+  struct tlv *tlv;
+  size_t tlv_len;
+
   assert(m);
   assert((length == 0) || value);
 
@@ -451,7 +462,7 @@
   assert(qmi_check(m));
 
   /* Find length of new TLV. */
-  size_t tlv_len = sizeof(struct tlv) + length;
+  tlv_len = sizeof(struct tlv) + length;
 
   /* Check for overflow of message size. */
   if (qmux_length(m) + tlv_len > UINT16_MAX) {
@@ -463,7 +474,7 @@
   m->buf = g_realloc(m->buf, m->len);
 
   /* Fill in new TLV. */
-  struct tlv *tlv = (struct tlv *)(qmi_end(m) - tlv_len);
+  tlv = (struct tlv *)(qmi_end(m) - tlv_len);
   tlv->type = type;
   tlv->length = length;
   if (value)
diff --git a/src/qmiwds.c b/src/qmiwds.c
index d34de71..cd13388 100644
--- a/src/qmiwds.c
+++ b/src/qmiwds.c
@@ -57,16 +57,15 @@
 
 static void client_create(struct qmidev *qmidev, struct client *client)
 {
+  struct qmiwds_priv *priv;
   struct listen_criteria criteria;
 
-  qmidev = qmidev;
-  struct qmiwds_priv *priv = g_slice_new(struct qmiwds_priv);
-
   criteria.flags = LISTEN_MATCH_QMI_FLAGS |
                    LISTEN_MATCH_MESSAGE;
   criteria.qmi_flags = QMI_FLAG_SVC_INDICATION;
   criteria.message = QMIWDS_MSG_PACKET_STATUS;
 
+  priv = g_slice_new(struct qmiwds_priv);
   priv->packet_status_listener = qmidev_listen(qmidev,
                                                client,
                                                &criteria,
@@ -76,13 +75,18 @@
   priv->packet_status_callback = NULL;
   priv->packet_status_context = NULL;
   client_set_priv(client, priv);
+
+  /* unused */
+  qmidev = qmidev;
 }
 
 static void client_destroy(struct qmidev *qmidev, struct client *client)
 {
-  qmidev = qmidev;
   struct qmiwds_priv *priv = client_priv(client);
   g_slice_free(struct qmiwds_priv, priv);
+
+  /* unused */
+  qmidev = qmidev;
 }
 
 struct service qmiwds_service = {
@@ -145,8 +149,11 @@
     qmidev_response_fn set_callback,
     void *set_context)
 {
-  struct client *client = qmidev_default_client(qmidev, &qmiwds_service);
-  struct qmiwds_priv *priv = client_priv(client);
+  struct client *client;
+  struct qmiwds_priv *priv;
+
+  client = qmidev_default_client(qmidev, &qmiwds_service);
+  priv = client_priv(client);
   priv->packet_status_callback = callback;
   priv->packet_status_context = context;
   /* TODO: Call later. */
diff --git a/src/start_network_stress_test.c b/src/start_network_stress_test.c
index db7a125..1ee6de8 100644
--- a/src/start_network_stress_test.c
+++ b/src/start_network_stress_test.c
@@ -19,31 +19,35 @@
 
 static void start(struct qmidev *qmidev, struct context *context)
 {
+  int result;
+
   assert(!context->session_id);
-  int result = qmidev_start_network(qmidev, &started, context);
+  result = qmidev_start_network(qmidev, &started, context);
   assert(result == 0);
 }
 
 static void stop(struct qmidev *qmidev, struct context *context)
 {
+  int result;
+
   assert(context->session_id);
-  int result = qmidev_stop_network(qmidev, context->session_id,
-                                   &stopped, context);
+  result = qmidev_stop_network(qmidev, context->session_id,
+                               &stopped, context);
   assert(result == 0);
 }
 
 static void started(struct qmidev *qmidev, void *data, int status,
                     qmidev_session_id session_id, uint16_t end)
 {
-  /* unused */
-  qmidev = qmidev;
-
   struct context *context = data;
   printf("started: status = %d session_id = 0x%08x call_end_reason = %d\n",
          status, session_id, end);
   assert(status == 0);
   context->session_id = session_id;
   stop(qmidev, context);
+
+  /* unused */
+  qmidev = qmidev;
 }
 
 static void stopped(struct qmidev *qmidev, void *data, int status)
@@ -67,22 +71,25 @@
 
 static void connected(struct qmidev *qmidev, void *data, int status)
 {
+  int result;
+
   assert(status == 0);
-  int result = qmidev_set_packet_status_callback(qmidev,
-                                                 &packet_status_changed, data,
-                                                 NULL, NULL);
+  result = qmidev_set_packet_status_callback(qmidev,
+                                             &packet_status_changed, data,
+                                             NULL, NULL);
   assert(result == 0);
   start(qmidev, data);
 }
 
 int main(void)
 {
+  struct qmidev *qmidev;
   struct context context;
   int result;
 
   context.session_id = 0;
 
-  struct qmidev *qmidev = qmidev_new_file("/dev/cdc-wdm0");
+  qmidev = qmidev_new_file("/dev/cdc-wdm0");
   assert(qmidev);
 
   result = qmidev_connect(qmidev, &connected, &context);