// gcc -o level1 -no-pie -fno-stack-protector level1.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

struct topic {
  char question[64];
  char answer[32];
  int (*check_response)(struct topic *topic);
};

void win() {
  char *argv[2];
  argv[0] = "/bin/sh";
  argv[1] = NULL;
  execve(argv[0], argv, NULL);
}

int check_math_response(struct topic *topic) {
  int answer = atoi(topic->answer);
  if (answer == 0x7a69) {
    puts("You got it.");
    return 0;
  } else {
    puts("Try again.");
    return 1;
  }
}

int check_weather_response(struct topic *topic) {
  if (!strcmp(topic->answer, "Yes\n")) {
    puts("It's inevitable.");
    return 0;
  } else {
    puts("Are you sure about that?");
    return 1;
  }
}

int main(int argc, char **argv) {
  // Disable output buffering. Not part of the challenge.
  setvbuf(stdout, NULL, _IONBF, 0);
  setvbuf(stdin, NULL, _IONBF, 0);

  struct topic topics[] = {
      {"What is 3077 * 10 + 567?", "", check_math_response},
      {"Will it ever rain this year?", "", check_weather_response}};

  srand(time(NULL));
  struct topic *topic = &topics[rand() % 2];

  puts(topic->question);
  fgets(topic->answer, sizeof(*topic), stdin);
  printf("addr: %X\n", topic->check_response);
  return topic->check_response(topic);
}
