// File: /workspace/github_cpp_makefile_star2_O2/richardposner[P]BioNetFit2/src/code/Utils.cpp
// Function: generate_gdat_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int)
int generate_gdat_file(string exp_file, string gdat_path, int seed){ //razi added to generate artificial gdat files
	//read exp-file
	//copy to gdat file
cout<<"\nUtils::generate_gdat_file: PiD["<<seed <<"] Starts generating file: "<< gdat_path << "   from file :" << exp_file<<"\n.";
try{
	std::ifstream ifs(exp_file);
	std::ofstream ofs(gdat_path);
	std::string line, oline;
	std::string word;
	int wcnt, linecnt;
	int val, rnd1; //Raquel removed unused, rnd;
	std::string::size_type pos = 0;
	std::string::size_type prev = 0;
	boost::random::mt19937 generalRand;

//cout<<"Utils:g-1   try to generate file:" <<gdat_path << " from file: " << exp_file<< endl;
	if ((ifs.is_open()) && (ofs.is_open())) {


		linecnt=0;
		while (getline(ifs, line)) {
			if (line.length() > 0){
				linecnt++;
				if (line.at(0) == '#'){
					oline=line;
//cout<<"Utils:g-2   A header line is read:" <<line << endl;
				}else{

//cout<<"Utils:g-3   A valid line is read:" <<line << endl;
					//data line break line into smaller pieces

					pos = 0; prev = 0; wcnt=0; oline=""; line=line+"\t"; //add tab to the end

					srand(time(0)+seed);
					while ((pos = line.find("\t", prev)) != std::string::npos)
					{
						if (pos != prev) //otherwise, most likely the first tab
						{
							wcnt++;
							word = line.substr(prev, pos - prev);


//cout<<"Utils:g-4   A word is read:" <<word << endl;
							if (wcnt == 1){  //the first word is time, don't touch it
								oline=oline + "\t" + word;
							}else{
								//for all pieces, if numerical value , change all of it except the first one
								val = atoi(word.c_str());


								rnd1 = rand()%100;
								val = val + (int) (rnd1);
								if (val < 0){ val=-val;}

//cout<<"Utils:g-6  the new value is :" <<val<< endl;
								oline = oline +"\t" + toString(val);
//cout<<"Utils:g-7  output line is :" <<oline <<endl;

							}
						}
						prev = pos + 1;
					}
					//write the output line into the output file
				}
//cout<<"Utils:g-9  try to dump output line:" <<oline <<endl;
				ofs << oline << std::endl;
			}
		}
		ifs.close();
		ofs.close();
//cout<<"Utils:g-10   finished generating file:" <<gdat_path << " from file: " << exp_file<< " successfully." <<endl;
		return 0;  //successful
	}else{
//cout<<"Utils:g-11   try to generate file:" <<gdat_path << " from file: " << exp_file<< ", Can't open input or output file !!!" <<endl;
		return -1;
	}
}
catch(int err){
	cout<< "Error occurred when generating artificial gdata file....\n\n"; return -1;
}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xea8, %rsp            # imm = 0xEA8
movl %edx, %r15d
movq %rsi, %rbx
movq %rdi, %r14
movq 0xc79fe(%rip), %rdi     # 0x165f98
leaq 0x6191a(%rip), %rsi     # 0xffebb
callq 0x289f0
movq %rax, %rdi
movq %r15, 0x50(%rsp)
movl %r15d, %esi
callq 0x29290
leaq 0x6191f(%rip), %rsi     # 0xffedc
movq %rax, %rdi
callq 0x289f0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x28910
leaq 0x384ed(%rip), %rsi     # 0xd6ac4
movq %rax, %rdi
callq 0x289f0
movq %rax, %rdi
movq %r14, %rsi
callq 0x28910
leaq 0x3eabb(%rip), %rsi     # 0xdd0ac
movq %rax, %rdi
callq 0x289f0
leaq 0x2d8(%rsp), %rdi
pushq $0x8
popq %rdx
movq %r14, %rsi
callq 0x29100
leaq 0xd8(%rsp), %rdi
pushq $0x10
popq %rdx
movq %rbx, %rsi
callq 0x294c0
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
leaq 0x20(%rsp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %cl, (%rax)
movb %cl, (%rdx)
leaq 0x68(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0x4e0(%rsp), %rdi
callq 0x8be76
leaq 0x350(%rsp), %rdi
callq 0x28200
pushq $-0x1
popq %rbx
testb %al, %al
je 0x9e8b8
leaq 0x148(%rsp), %rdi
callq 0x28200
testb %al, %al
je 0x9e8b8
leaq 0x3656e(%rip), %r14     # 0xd4c00
leaq 0xb8(%rsp), %r15
leaq 0x78(%rsp), %rbp
leaq 0x98(%rsp), %r13
leaq 0x2d8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x28180
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x9e89c
cmpq $0x0, 0x38(%rsp)
je 0x9e6a7
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x294a0
cmpb $0x23, (%rax)
jne 0x9e70f
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x28570
leaq 0xd8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x28910
movq %rax, %rdi
callq 0x284a0
jmp 0x9e6a7
leaq 0x10(%rsp), %rdi
leaq 0x364e6(%rip), %rsi     # 0xd4c01
callq 0x29490
movq %r15, %rdi
leaq 0x30(%rsp), %rbx
movq %rbx, %rsi
movq %r14, %rdx
callq 0x334ed
movq %rbx, %rdi
movq %r15, %rsi
callq 0x28c40
movq %r15, %rdi
callq 0x295a0
movq $0x0, 0x8(%rsp)
xorl %edi, %edi
callq 0x289a0
movq 0x50(%rsp), %rcx
leal (%rax,%rcx), %edi
callq 0x289d0
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x29510
cmpq $-0x1, %rax
je 0x9e6f3
movq %rax, %rbx
movq %rax, %rcx
subq %r12, %rcx
je 0x9e88c
movq %r15, %rdi
leaq 0x30(%rsp), %rsi
movq %r12, %rdx
callq 0x28a90
leaq 0x58(%rsp), %rdi
movq %r15, %rsi
callq 0x28c40
movq %r15, %rdi
callq 0x295a0
cmpl $0x0, 0x8(%rsp)
je 0x9e847
movq 0x58(%rsp), %rdi
callq 0x29050
movl %eax, %r12d
callq 0x28280
movq %r14, %rdx
movl %eax, %r14d
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
callq 0x334ed
movl %r14d, %eax
cltd
pushq $0x64
popq %rcx
idivl %ecx
addl %edx, %r12d
movl %r12d, %esi
negl %esi
cmovsl %r12d, %esi
movq %r13, %rdi
callq 0x9e9c3
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x4e6ed
leaq 0x363ec(%rip), %r14     # 0xd4c00
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x28c40
movq %r15, %rdi
callq 0x295a0
movq %r13, %rdi
callq 0x295a0
movq %rbp, %rdi
callq 0x295a0
jmp 0x9e88c
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x334ed
movq %r15, %rdi
movq %rbp, %rsi
leaq 0x58(%rsp), %rdx
callq 0x334ce
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x28c40
movq %r15, %rdi
callq 0x295a0
movq %rbp, %rdi
callq 0x295a0
pushq $0x1
popq %rax
movq %rax, 0x8(%rsp)
incq %rbx
movq %rbx, %r12
leaq 0x30(%rsp), %rbx
jmp 0x9e766
leaq 0x2d8(%rsp), %rdi
callq 0x281c0
xorl %ebx, %ebx
leaq 0xd8(%rsp), %rdi
callq 0x28e60
leaq 0x58(%rsp), %rdi
callq 0x295a0
leaq 0x10(%rsp), %rdi
callq 0x295a0
leaq 0x30(%rsp), %rdi
callq 0x295a0
leaq 0xd8(%rsp), %rdi
callq 0x28f10
leaq 0x2d8(%rsp), %rdi
callq 0x28230
movl %ebx, %eax
addq $0xea8, %rsp            # imm = 0xEA8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r14
movq %rax, %rbx
jmp 0x9e978
movq %rdx, %r14
movq %rax, %rbx
jmp 0x9e985
jmp 0x9e947
jmp 0x9e947
jmp 0x9e947
jmp 0x9e91c
movq %rdx, %r14
movq %rax, %rbx
jmp 0x9e939
jmp 0x9e947
movq %rdx, %r14
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x295a0
leaq 0x78(%rsp), %rdi
callq 0x295a0
jmp 0x9e94d
jmp 0x9e947
movq %rdx, %r14
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x295a0
leaq 0x10(%rsp), %rdi
callq 0x295a0
leaq 0x30(%rsp), %rdi
callq 0x295a0
leaq 0xd8(%rsp), %rdi
callq 0x28f10
leaq 0x2d8(%rsp), %rdi
callq 0x28230
cmpl $0x1, %r14d
jne 0x9e9bb
movq %rbx, %rdi
callq 0x283c0
movq 0xc75fe(%rip), %rdi     # 0x165f98
leaq 0x61556(%rip), %rsi     # 0xffef7
callq 0x289f0
callq 0x29150
pushq $-0x1
popq %rbx
jmp 0x9e8f0
movq %rax, %rbx
callq 0x29150
movq %rbx, %rdi
callq 0x292f0