/* CSE 333 Su12 Lecture 2 demo: mystery.c */
/* Gribble/Perkins */

/* What happens when we execute this program? */
/* More important: why? */
/* Hint: Compile with -g and run program with valgrind. */

#include <stdio.h>
#include <stdlib.h>

/* fake "check for error" function - always returns false */
int verify_something(void) {
  return 0;
}

/* Return a new string of length bufsize */
unsigned char *mystery_function(unsigned short bufsize) {
  unsigned char *tmp_buf;

  if (bufsize == 0)
    return NULL;

  tmp_buf = malloc(bufsize);
  if (tmp_buf == NULL)
    return NULL;

  // exit if something bad happened
  if (verify_something() == 0)
    return NULL;

  return tmp_buf;
}

/* Allocate new string and free it */
int main(int argc, char **argv) {
  unsigned char *buf;

  buf = mystery_function(50);
  if (buf != NULL)
    free(buf);
  return 0;
}