// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/model.c
// Function: mlp_model_simulation_most
int mlp_model_simulation_most(Datasetptr d, char* simulationdirectory, int maxhidden, int starthidden, int* tries, MODEL_SEARCH_DIRECTION_TYPE searchdirection)
{
 /*!Last Changed 26.05.2006 added starthidden*/
	/*!Last Changed 17.07.2005*/
	char **two_file;
	int bestmodel, *list, modelcount, i, improved = 1, bestoftwo, *bestmodels, bestmodelcount = 1, *modelstried;
	modelstried = (int *) safecalloc(maxhidden + 1, sizeof(int), "mlp_model_simulation_most", 3);
 if (starthidden < 0 || starthidden > maxhidden)
  {
  	if (searchdirection == FORWARD)
		   bestmodel = 0;
	  else
  		 bestmodel = maxhidden;
  }
 else
   bestmodel = starthidden;
	modelstried[bestmodel] = 1;
	bestmodels = safemalloc((maxhidden + 2) * sizeof(int), "mlp_model_simulation_forw1", 9);
	bestmodels[0] = bestmodel;
 two_file = (char**) safemalloc_2d(sizeof(char *), sizeof(char), 2, 500, "mlp_model_simulation_forw1", 9);
	while (improved)
	 {
		 list = generate_candidate_mlp_models(d, maxhidden, bestmodel, &modelcount, searchdirection); 
			if (searchdirection == FORWARD)
  			qsort(list, modelcount, sizeof(int), sort_function_int_ascending);
			else
				 qsort(list, modelcount, sizeof(int), sort_function_int_descending);
			improved = 0;
			for (i = 0; i < modelcount; i++)
				 if (list[i] != bestmodel)
					 {
						 modelstried[list[i]] = 1;
			  	 bestoftwo = compare_two_mlp_models(two_file, simulationdirectory, d, bestmodel, list[i]);
			  	 if ((bestmodel < list[i] && bestoftwo == 1) || (bestmodel > list[i] && bestoftwo == 0))
							 {
				  		 bestmodel = list[i];
				  			improved = 1;
				  			break;
							 }
					 }
   for (i = 0; i < bestmodelcount; i++)
				 if (bestmodels[i] == bestmodel)
					 {
						 improved = 0;
							break;
					 }
			bestmodels[bestmodelcount] = bestmodel;
			bestmodelcount++;
			safe_free(list);
	 }
	*tries = 0;
	for (i = 0; i <= maxhidden; i++)
		 if (modelstried[i])
				 (*tries)++;
	safe_free(bestmodels);
	safe_free(modelstried);
	free_2d((void**)two_file, 2);
	return bestmodel;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r9d, %ebp
movq %r8, 0x28(%rsp)
movl %ecx, %r14d
movl %edx, %r15d
movq %rsi, 0x40(%rsp)
movq %rdi, %rbx
leal 0x1(%r15), %eax
movslq %eax, %rdi
leaq 0x57947(%rip), %rdx     # 0x9a427
pushq $0x4
popq %rsi
pushq $0x3
popq %rcx
callq 0x35067
testl %ebp, %ebp
movl %r15d, %ecx
movl %ebp, 0x14(%rsp)
cmovel %ebp, %ecx
leaq -0xd755(%rip), %rdx     # 0x353a9
cmoveq 0x894d2(%rip), %rdx     # 0xcbfd8
movq %rdx, 0x30(%rsp)
cmpl %r15d, %r14d
movl %r14d, %ebp
cmovgl %ecx, %ebp
testl %r14d, %r14d
cmovsl %ecx, %ebp
movslq %ebp, %r13
movq %rax, 0x8(%rsp)
movl $0x1, (%rax,%r13,4)
movq %r15, 0x38(%rsp)
movslq %r15d, %rax
movq %rax, 0x20(%rsp)
leaq 0x8(,%rax,4), %rdi
leaq 0x578fb(%rip), %r14     # 0x9a441
pushq $0x9
popq %r12
movq %r14, %rsi
movl %r12d, %edx
callq 0x34f59
movq %rax, %r15
movl %r13d, (%rax)
pushq $0x8
popq %rdi
pushq $0x1
popq %r13
pushq $0x2
popq %rdx
movq %r13, %rsi
movl $0x1f4, %ecx            # imm = 0x1F4
movq %r14, %r8
movl %r12d, %r9d
callq 0x351c3
movq %rax, 0x18(%rsp)
pushq $0x1
popq %r12
testl %r12d, %r12d
je 0x42c52
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
movl %ebp, %edx
leaq 0x4(%rsp), %rcx
movl 0x14(%rsp), %r8d
callq 0x42511
movq %rax, %r14
movslq 0x4(%rsp), %rsi
movq %rax, %rdi
pushq $0x4
popq %rdx
movq 0x30(%rsp), %rcx
callq 0x4240
xorl %r12d, %r12d
movslq 0x4(%rsp), %rax
cmpq %rax, %r12
jge 0x42c1c
movslq (%r14,%r12,4), %rax
cmpl %ebp, %eax
je 0x42c17
movq 0x8(%rsp), %rcx
movl $0x1, (%rcx,%rax,4)
movl (%r14,%r12,4), %r8d
movq 0x18(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq %rbx, %rdx
movl %ebp, %ecx
callq 0x42cac
movl (%r14,%r12,4), %ecx
cmpl %ecx, %ebp
setl %dl
cmpl $0x1, %eax
sete %sil
testb %sil, %dl
jne 0x42c21
cmpl %ecx, %ebp
setg %dl
testl %eax, %eax
sete %al
testb %al, %dl
jne 0x42c21
incq %r12
jmp 0x42bbe
xorl %r12d, %r12d
jmp 0x42c27
pushq $0x1
popq %r12
movl %ecx, %ebp
xorl %eax, %eax
cmpq %rax, %r13
je 0x42c3e
leaq 0x1(%rax), %rcx
cmpl %ebp, (%r15,%rax,4)
movq %rcx, %rax
jne 0x42c29
xorl %r12d, %r12d
movl %ebp, (%r15,%r13,4)
incq %r13
movq %r14, %rdi
callq 0x34ff7
jmp 0x42b81
movq 0x28(%rsp), %rdx
andl $0x0, (%rdx)
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
cmpq %rdi, %rcx
jg 0x42c7c
cmpl $0x0, (%rsi,%rcx,4)
je 0x42c77
incl %eax
movl %eax, (%rdx)
incq %rcx
jmp 0x42c68
movq %r15, %rdi
callq 0x34ff7
movq 0x8(%rsp), %rdi
callq 0x34ff7
pushq $0x2
popq %rsi
movq 0x18(%rsp), %rdi
callq 0x34fc3
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq