// File: /workspace/github_c_makefile_star2_O3/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:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x210, %rsp            # imm = 0x210
movq %rsi, %r14
movl %edi, %ebp
movl %edi, (%rsp)
movq %rsi, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x2380
xorl %ebx, %ebx
cmpl $0x2, %ebp
jne 0x2753
movq 0x8(%r14), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2290
movq %rax, %rbx
movq 0xc958(%rip), %rdi      # 0xefc8
leaq 0x898d(%rip), %rsi      # 0xb004
movl $0x13, %edx
callq 0x2210
movq %rsp, %rdi
leaq 0x8(%rsp), %rsi
callq 0x22b0
callq 0x2340
movq 0xc91e(%rip), %rax      # 0xefb8
movq 0x8(%rax), %rdi
leaq 0x4(%rsp), %rsi
callq 0x22a0
movslq 0x4(%rsp), %r14
callq 0x7ddd
callq 0x7eb1
testl %r14d, %r14d
je 0x26fe
movq 0xc905(%rip), %rdi      # 0xefc8
leaq 0x8967(%rip), %rsi      # 0xb031
movl $0x17, %edx
callq 0x2210
movq 0xc8ed(%rip), %rdi      # 0xefc8
movq %r14, %rsi
callq 0x21c0
leaq 0x8b4f(%rip), %rsi      # 0xb239
movl $0x1, %edx
movq %rax, %rdi
callq 0x2210
callq 0x35e4
jmp 0x2740
movq 0xc8c3(%rip), %rdi      # 0xefc8
leaq 0x890c(%rip), %rsi      # 0xb018
movl $0x18, %edx
callq 0x2210
movq 0xc8ab(%rip), %rdi      # 0xefc8
xorl %esi, %esi
callq 0x21c0
leaq 0x8b0e(%rip), %rsi      # 0xb239
movl $0x1, %edx
movq %rax, %rdi
callq 0x2210
movq %rbx, %rdi
callq 0x27a8
movl %eax, %ebx
leaq 0xcb27(%rip), %rdi      # 0xf270
callq 0x2130
callq 0x2150
movq 0xc84e(%rip), %rax      # 0xefa8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x18(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x10(%rsp,%rcx)
callq 0x2190
leaq 0x108(%rsp), %rdi
callq 0x20a0
movl %ebx, %eax
addq $0x210, %rsp            # imm = 0x210
popq %rbx
popq %r14
popq %rbp
retq
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x22e0
movq %rbx, %rdi
callq 0x23a0