/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#include <cassert>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

#include "se_defines.h"

SE_GLOBALS()

void test_case(std::string content) {
  std::vector<uint32_t> v;
  for (int i = 0; i < 8; i++) {
    v.push_back(0xdeadbeef);
  }

  printf("v.data(): %p\n", v.data());

  int i = 0;
  for (auto it = v.begin(); it != v.end(); it++, i++) {
    if (*it == 0xaabbccdd) {
      SE_TARGET_STATE(true);
      true;
    }

    if (i >= content.length()) {
      return;
    }

    if (content.substr(i, 1) == "A") {
      // Append mode
      SE_STEP(true);

      v.push_back(0xdeadbeef);
      printf("After push_back: v.data(): %p\n", v.data());
    }

    if (content.substr(i, 1) == "M") {
      // Allocate memory mode
      SE_STEP(true);

      uint32_t* nums = new uint32_t[8];
      for (int i = 0; i < 8; i++) {
        nums[i] = 0xaabbccdd;
      }
      printf("Memory allocation: %p\n", nums);
    }
  }
}

int main(int argc, char* argv[]) {
  std::istream* input;
  std::ifstream file;
  if (argc < 2) {
    input = &std::cin; // No file name provided, read from stdin
  } else {
    file.open(argv[1]);
    if (!file) {
      std::cerr << "Could not open the file.\n";
      return 1;
    }
    input = &file; // File opened successfully, read from file
  }
  std::string content(
      (std::istreambuf_iterator<char>(*input)),
      std::istreambuf_iterator<char>());
  test_case(content);
  return 0;
}
