/*************************************************************************************** * Copyright (c) 2014-2022 Zihao Yu, Nanjing University * * NEMU is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * * See the Mulan PSL v2 for more details. ***************************************************************************************/ // Modified by: Zhiyi Zhang in 2024.08 #define TMP_FILE_PATH "/tmp" #define TMP_C_SRC_FILE_PATH TMP_FILE_PATH "/.code.c" #define TMP_C_EXE_FILE_PATH TMP_FILE_PATH "/.expr" #define TMP_COMPILE_CMD "gcc -o " TMP_C_EXE_FILE_PATH " " TMP_C_SRC_FILE_PATH #include #include #include #include #include #include // this should be enough static char buf[65536] = {}; static char code_buf[65536 + 128] = {}; // a little larger than `buf` static char *code_format = "#include \n" "int main() { " " unsigned result = %s; " " printf(\"%%u\", result); " " return 0; " "}"; static void gen_rand_expr() { buf[0] = '\0'; } int main(int argc, char *argv[]) { int seed = time(0); srand(seed); int loop = 1; if (argc > 1) { sscanf(argv[1], "%d", &loop); } int i; for (i = 0; i < loop; i ++) { gen_rand_expr(); sprintf(code_buf, code_format, buf); FILE *fp = fopen(TMP_C_SRC_FILE_PATH, "w"); assert(fp != NULL); fputs(code_buf, fp); fclose(fp); int ret = system(TMP_COMPILE_CMD); if (ret != 0) continue; fp = popen("." TMP_C_EXE_FILE_PATH, "r"); assert(fp != NULL); int result; ret = fscanf(fp, "%d", &result); pclose(fp); printf("%u %s\n", result, buf); } return 0; }