// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movl %r9d, %ebp
movq %r8, 0x38(%rsp)
movl %ecx, %ebx
movl %edx, %r14d
movq %rsi, 0x50(%rsp)
movq %rdi, 0x30(%rsp)
leal 0x1(%r14), %eax
movl %eax, 0x18(%rsp)
movslq %eax, %rdi
leaq 0x60bde(%rip), %rdx     # 0xaa4f7
movl $0x4, %esi
movl $0x3, %ecx
callq 0x3a1fc
testl %ebp, %ebp
movl %r14d, %ecx
movl %ebp, 0x1c(%rsp)
cmovel %ebp, %ecx
leaq -0xf360(%rip), %rdx     # 0x3a5db
cmoveq 0x8a695(%rip), %rdx     # 0xd3fd8
movq %rdx, 0x40(%rsp)
cmpl %r14d, %ebx
movl %ebx, %r12d
cmovgl %ecx, %r12d
testl %ebx, %ebx
cmovsl %ecx, %r12d
movslq %r12d, %rbx
movq %rax, 0x10(%rsp)
movl $0x1, (%rax,%rbx,4)
movq %r14, 0x20(%rsp)
movslq %r14d, %rax
leaq 0x8(,%rax,4), %rdi
leaq 0x60b93(%rip), %r15     # 0xaa511
movq %r15, %rsi
movl $0x9, %edx
callq 0x3a0e1
movl %ebx, (%rax)
movq %rax, %rbx
movl $0x1, %r14d
movl $0x8, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x1f4, %ecx            # imm = 0x1F4
movq %r15, %r8
movl $0x9, %r9d
callq 0x3a3ac
movq %rax, 0x28(%rsp)
movq %rbx, 0x48(%rsp)
movq 0x30(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl %r12d, %edx
leaq 0xc(%rsp), %rcx
movl 0x1c(%rsp), %r8d
callq 0x49374
movq %rax, %rbp
movslq 0xc(%rsp), %rsi
movl $0x4, %edx
movq %rax, %rdi
movq 0x40(%rsp), %rcx
callq 0x4240
movl 0xc(%rsp), %eax
testl %eax, %eax
jle 0x49a6d
xorl %ebx, %ebx
movslq (%rbp,%rbx,4), %rcx
cmpl %r12d, %ecx
je 0x49a62
movq 0x10(%rsp), %rax
movl $0x1, (%rax,%rcx,4)
movl (%rbp,%rbx,4), %r8d
movq 0x28(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x30(%rsp), %rdx
movl %r12d, %ecx
callq 0x49b0c
movl (%rbp,%rbx,4), %r15d
cmpl %r15d, %r12d
setl %cl
cmpl $0x1, %eax
sete %dl
movl $0x1, %r13d
testb %dl, %cl
jne 0x49a73
cmpl %r15d, %r12d
setg %cl
testl %eax, %eax
sete %al
testb %al, %cl
jne 0x49a73
movl 0xc(%rsp), %eax
incq %rbx
movslq %eax, %rcx
cmpq %rcx, %rbx
jl 0x49a02
xorl %r13d, %r13d
movl %r12d, %r15d
xorl %eax, %eax
movq 0x48(%rsp), %rbx
cmpl %r15d, (%rbx,%rax,4)
je 0x49a8a
incq %rax
cmpq %rax, %r14
jne 0x49a7a
jmp 0x49a8d
xorl %r13d, %r13d
movl %r15d, (%rbx,%r14,4)
incq %r14
movq %rbp, %rdi
callq 0x3a180
movl %r15d, %r12d
testl %r13d, %r13d
jne 0x499c2
movq 0x38(%rsp), %rsi
movl $0x0, (%rsi)
cmpl $0x0, 0x20(%rsp)
js 0x49ad9
movl 0x18(%rsp), %eax
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x10(%rsp), %rdi
cmpl $0x0, (%rdi,%rdx,4)
je 0x49ad1
incl %ecx
movl %ecx, (%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0x49ac7
movq %rbx, %rdi
callq 0x3a180
movq 0x10(%rsp), %rdi
callq 0x3a180
movq 0x28(%rsp), %rdi
movl $0x2, %esi
callq 0x3a14b
movl %r15d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq