import sys
import os
import subprocess
import string
import random

import signal
import subprocess
import time

import collections
import itertools
import sys
import os.path

num_of_programs = sys.argv[1]

for i in range (int(num_of_programs)):
	os.system("cp ./dataset/cudatest"+str(i)+".cu temp")
	os.system("cp ./dataset/ctest"+str(i)+".c temp")
	cudafile = "./dataset/cudatest"+str(i)+".cu"
	cfile = "./dataset/ctest"+str(i)+".c"

	flag = 0;
	with open("./temp","w") as outfile:
		with open(cudafile,"r") as infile:
			for line in infile:
				if "return 0; }" in line:
					outfile.write("return 0; }")
					continue;
				if "printf" in line:
					continue;
				if "<<<block_no,block_size>>>" in line:
					line2 = "FILE *fp_energy = fopen(\"./module-power-input.txt\",\"a\");\n"
					outfile.write(line2)
					line2 = "if(fp_energy) {\n"
					outfile.write(line2)
					line2 = "char Buf[7] = \"Start\\n\";\n"
					outfile.write(line2)
					line2 = "fwrite(Buf, 1, 6, fp_energy);\n"
					outfile.write(line2)
					line2 = "fclose(fp_energy);}\n"
					outfile.write(line2)
					line2 = "for(int ii=0; ii<100; ii++){\n"
					outfile.write(line2)
					outfile.write(line)
				elif "cudaThreadSynchronize();" in line:
					outfile.write(line)
					line2 = "}\n"
					outfile.write(line2)
					line2 = "fp_energy = fopen(\"./module-power-input.txt\",\"a\");\n"
					outfile.write(line2)
					line2 = "if(fp_energy) {\n"
					outfile.write(line2)
					line2 = "char Buf[5] = \"End\\n\";\n"
					outfile.write(line2)
					line2 = "fwrite(Buf, 1, 4, fp_energy);\n"
					outfile.write(line2)
					line2 = "fclose(fp_energy);}\n"
					outfile.write(line2)
				else:
					outfile.write(line)
	os.system("cp temp ./dataset/cudatest"+str(i)+".cu")
	flag = 0;
	with open("./temp","w") as outfile:
		with open(cfile,"r") as infile:
			for line in infile:
				if "return 0; }" in line:
					outfile.write("return 0; }")
					continue;
				if "printf" in line:
					continue;
				if "if(file_for_block_of_interest) {" in line: 
					continue;
				if "char Buf[2]" in line:
					continue;
				if "fwrite(Buf, 1, 1, file_for_block_of_interest);" in line:
					continue;
				if "fclose(file_for_block_of_interest);}" in line:
					continue;
				if "FILE *file_for_block_of_interest = fopen" in line:
					line2 = "FILE *fp_energy = fopen(\"./module-power-input.txt\",\"a\");\n"
					outfile.write(line2)
					line2 = "if(fp_energy) {\n"
					outfile.write(line2)
					line2 = "char Buf[7] = \"Start\\n\";\n"
					outfile.write(line2)
					line2 = "fwrite(Buf, 1, 6, fp_energy);\n"
					outfile.write(line2)
					line2 = "fclose(fp_energy);}\n"
					outfile.write(line2)
					line2 = "for(int ii=0; ii<100; ii++){\n"
					outfile.write(line2)
				if ("file_for_block_of_interest = fopen" in line) and ("FILE" not in line):
					line2 = "}\n"
					outfile.write(line2)
					line2 = "fp_energy = fopen(\"./module-power-input.txt\",\"a\");\n"
					outfile.write(line2)
					line2 = "if(fp_energy) {\n"
					outfile.write(line2)
					line2 = "char Buf[5] = \"End\\n\";\n"
					outfile.write(line2)
					line2 = "fwrite(Buf, 1, 4, fp_energy);\n"
					outfile.write(line2)
					line2 = "fclose(fp_energy);}\n"
					outfile.write(line2)
				else:
					outfile.write(line)	
	os.system("cp temp ./dataset/ctest"+str(i)+".c")