// File: /workspace/github_c_makefile_star2_O0/guszejnovdavid[P]MISFIT/main.cpp
// Function: main
int main(int argc, char *argv[]) {

/******************************************************************************/


using namespace std;
ofstream myfile; //Output file handle

/* Constants and parameters*/
long Ntries; //number of tries for each mass
long index; //index of the process, useful for multi_runs

int returnval; //returned value


/******************************************************************/
     /* Check input */
if ( argc != 2 ) // 3 arguments is the default (Ntries, Nmass, index)
{
    return 0;
}
else 
{
Ntries = strtol(argv[1],NULL,0); //setting endpoint of the calculation
}

cout<<"MISFIT Starting...\n";

#if (VerboseOutput==true)
cout<<"MISFIT MPI Init.\n";
#endif

//  Initialize MPI.
MPI::Init ( argc, argv );

//  Get the individual process ID.
index = MPI::COMM_WORLD.Get_rank();

#if (VerboseOutput==true)
cout<<"MPI datatypes init started.\n";
#endif

//Make Input struct passable by MPI
InitInputDataType();
//Make density struct passable by MPI
InitDensityStructDataType();

#if (VerboseOutput==true)
cout<<index<<": MPI datatypes initialized.\n";
#endif

if(index==0){
	//index=0 is for MASTER process that will distribute tasks between SLAVEs
	cout<<"Master called with rank:"<<index<<"\n";
	returnval=Master(Ntries);
}
else
{
	if(index<MaxCores || MaxCores==0){
	//index!=0 for SLAVE process that will do the actual calculation
		cout<<"Slave called with rank:"<<index<<"\n";
		returnval=Slave();
	}
	else{
		cout<<index<<": Terminate to conserve memory \n";
		returnval=0;
	}

}

//Free MPI datatype
MPI_Type_free(&mpi_inputstruct_type);

//free vectors
//free(offsets);free(blocklengths);

#if (VerboseOutput==true)
cout<<index<<": MPI Finalize.\n";
#endif

//Finalize
MPI::Finalize ( );
	

return returnval;

}

// Assembly:
subq $0x278, %rsp            # imm = 0x278
movl $0x0, 0x274(%rsp)
movl %edi, 0x270(%rsp)
movq %rsi, 0x268(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x8b80
cmpl $0x2, 0x270(%rsp)
je 0x90bd
movl $0x0, 0x274(%rsp)
movl $0x1, 0x50(%rsp)
jmp 0x927a
movq 0x268(%rsp), %rax
movq 0x8(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x8910
movq %rax, 0x60(%rsp)
movq 0x18ef0(%rip), %rdi     # 0x21fd0
leaq 0x12f1d(%rip), %rsi     # 0x1c004
callq 0x8640
jmp 0x90ee
leaq 0x270(%rsp), %rdi
leaq 0x268(%rsp), %rsi
callq 0x9990
jmp 0x9105
movq 0x18eac(%rip), %rdi     # 0x21fb8
callq 0x99c0
movl %eax, 0x40(%rsp)
jmp 0x9117
movl 0x40(%rsp), %eax
cltq
movq %rax, 0x58(%rsp)
callq 0x17020
jmp 0x9129
callq 0x17110
jmp 0x9130
cmpq $0x0, 0x58(%rsp)
jne 0x91b5
movq 0x18e91(%rip), %rdi     # 0x21fd0
leaq 0x12ed2(%rip), %rsi     # 0x1c018
callq 0x8640
movq %rax, 0x38(%rsp)
jmp 0x9152
movq 0x38(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x8c60
movq %rax, 0x30(%rsp)
jmp 0x9168
movq 0x30(%rsp), %rdi
leaq 0x131b5(%rip), %rsi     # 0x1c329
callq 0x8640
jmp 0x917b
movq 0x60(%rsp), %rdi
callq 0xe130
movl %eax, 0x2c(%rsp)
jmp 0x918b
movl 0x2c(%rsp), %eax
movl %eax, 0x54(%rsp)
jmp 0x9252
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x48(%rsp)
movl %eax, 0x44(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x89c0
jmp 0x9293
cmpq $0x0, 0x58(%rsp)
jl 0x91c5
movb $0x1, %al
testb $0x1, %al
jne 0x91c5
jmp 0x921d
movq 0x18e04(%rip), %rdi     # 0x21fd0
leaq 0x12e5e(%rip), %rsi     # 0x1c031
callq 0x8640
movq %rax, 0x20(%rsp)
jmp 0x91df
movq 0x20(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x8c60
movq %rax, 0x18(%rsp)
jmp 0x91f5
movq 0x18(%rsp), %rdi
leaq 0x13128(%rip), %rsi     # 0x1c329
callq 0x8640
jmp 0x9208
callq 0xf9a0
movl %eax, 0x14(%rsp)
jmp 0x9213
movl 0x14(%rsp), %eax
movl %eax, 0x54(%rsp)
jmp 0x9250
movq 0x58(%rsp), %rsi
movq 0x18da7(%rip), %rdi     # 0x21fd0
callq 0x8c60
movq %rax, 0x8(%rsp)
jmp 0x9235
movq 0x8(%rsp), %rdi
leaq 0x12e08(%rip), %rsi     # 0x1c049
callq 0x8640
jmp 0x9248
movl $0x0, 0x54(%rsp)
jmp 0x9252
leaq 0x194d7(%rip), %rdi     # 0x22730
callq 0x8400
jmp 0x9260
callq 0x99f0
jmp 0x9267
movl 0x54(%rsp), %eax
movl %eax, 0x274(%rsp)
movl $0x1, 0x50(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x89c0
movl 0x274(%rsp), %eax
addq $0x278, %rsp            # imm = 0x278
retq
movq 0x48(%rsp), %rdi
callq 0x8be0
nopl (%rax)