// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/model.c
// Function: mlp_model_selection_bic
Mlpnetwork mlp_model_selection_bic(Instanceptr data, Instanceptr validation, double alpha, int maxhidden, int problemtype)
{
	/*!Last Changed 25.06.2005*/
	/*!Last Changed 25.06.2005*/
 int i, hiddennodes[1] = {1};
	Mlpparameters mlpparams;
	matrix train, cv, hessian, eigenvectors;
	Mlpnetwork neuralnetwork, best;
	double* eigenvalues, d, bicerror, trainerror, besterror = +INT_MAX, loglikelihood;
	train = instancelist_to_matrix(data, problemtype, -1, MULTIVARIATE_LINEAR);
	cv = instancelist_to_matrix(validation, problemtype, -1, MULTIVARIATE_LINEAR);
 if (current_dataset->classno == 0)
	  mlpparams = prepare_Mlpparameters(data, validation, 1, alpha, 1, hiddennodes, 0, 0.0, 0.0, 0, REGRESSION);
 else
   mlpparams = prepare_Mlpparameters(data, validation, 1, alpha, 1, hiddennodes, 0, 0.0, 0.0, 0, CLASSIFICATION);
	for (i = 1; i <= maxhidden; i++)
	 {
			mlpparams.hidden[0] = i;
			neuralnetwork = mlp_network_train_general(train, cv, &mlpparams);
			if (current_dataset->classno == 0)
			 {
     hessian = hessian_matrix_reg(neuralnetwork, data);
					trainerror = test_mlp_network(neuralnetwork, train);
			 }
			else
			 {
     hessian = hessian_matrix_cls(neuralnetwork, data);
			  loglikelihood = log_likelihood_of_mlp_network(neuralnetwork, train);
			 }
   eigenvalues = find_eigenvalues_eigenvectors_symmetric(&eigenvectors, hessian);
			matrix_free(eigenvectors);
			d = number_of_free_parameters_of_hessian(eigenvalues, hessian.row, alpha);
			if (current_dataset->classno == 0)
				 bicerror = bic_squared_error(trainerror, d, train.row);
			else
  			bicerror = bic_loglikelihood(loglikelihood, d, train.row);
			if (i != 1 && bicerror < besterror)
				 free_mlpnnetwork(&best);
			if (i == 1 || bicerror < besterror)
			 {
					best = copy_of_Mlpnetwork(neuralnetwork);
					besterror = bicerror;
			 }
			free_mlpnnetwork(&neuralnetwork);
			matrix_free(hessian);
	 }
	matrix_free(train);
	matrix_free(cv);
	return best;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2d8, %rsp            # imm = 0x2D8
movl %r8d, %ebx
movl %ecx, 0x98(%rsp)
movsd %xmm0, 0xd8(%rsp)
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, 0xa0(%rsp)
leaq 0xac(%rsp), %rax
movl $0x1, (%rax)
pushq $-0x1
popq %r13
pushq $-0x2
popq %rbp
movq %rsi, %rdi
movl %r8d, %esi
movl %r13d, %edx
movl %ebp, %ecx
callq 0x2c130
movq %rax, %r15
movq %rdx, 0xd0(%rsp)
movq %r14, %rdi
movl %ebx, %esi
movl %r13d, %edx
movl %ebp, %ecx
callq 0x2c130
movq %rax, 0xc8(%rsp)
movq %rdx, 0xc0(%rsp)
leaq 0x8cc14(%rip), %rax     # 0xcd870
movq (%rax), %rax
xorl %ecx, %ecx
cmpl $0x0, 0x10(%rax)
sete %cl
andl $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
movl %ecx, 0x10(%rsp)
leaq 0x118(%rsp), %rdi
pushq $0x1
popq %rbx
xorpd %xmm1, %xmm1
xorps %xmm2, %xmm2
movq %r12, 0xb8(%rsp)
movq %r12, %rsi
movq %r14, %rdx
movl %ebx, %ecx
movsd 0xd8(%rsp), %xmm0
movl %ebx, %r8d
leaq 0xac(%rsp), %r9
callq 0x3df0e
xorl %eax, %eax
movl 0x98(%rsp), %ecx
testl %ecx, %ecx
cmovgl %ecx, %eax
negl %eax
movq %rax, 0xf0(%rsp)
movsd 0x54848(%rip), %xmm0    # 0x95518
movsd %xmm0, 0x98(%rsp)
leaq 0x240(%rsp), %r13
movq %r15, 0xb0(%rsp)
movq 0xf0(%rsp), %rax
addl %ebx, %eax
cmpl $0x1, %eax
je 0x40f1a
movq %rbx, 0x100(%rsp)
movl %ebx, 0x160(%rsp)
leaq 0x1a8(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq 0xd0(%rsp), %r12
movq %r12, %rdx
movq 0xc8(%rsp), %rcx
movq 0xc0(%rsp), %r8
leaq 0x118(%rsp), %r9
callq 0x3d931
movl $0x98, %edx
movq %r13, %rdi
movq %r14, %rsi
callq 0x4310
leaq 0x8cb18(%rip), %rax     # 0xcd870
movq (%rax), %rax
cmpl $0x0, 0x10(%rax)
movq %r15, %rbx
movq %r13, %r15
pushq $0x13
popq %r13
movq %r13, %rcx
movq %rsp, %rdi
movq %r15, %rsi
je 0x40dac
rep  movsq (%rsi), %es:(%rdi)
movq 0xb8(%rsp), %rdi
callq 0x3e0b0
movq %rax, %rbp
movq %rdx, %r14
movq %r13, %rcx
movq %rsp, %rdi
movq %r15, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3dc9a
movsd %xmm0, 0xf8(%rsp)
jmp 0x40de2
rep  movsq (%rsi), %es:(%rdi)
movq 0xb8(%rsp), %rdi
callq 0x3e658
movq %rax, %rbp
movq %rdx, %r14
movq %r13, %rcx
movq %rsp, %rdi
movq %r15, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3dd9a
movsd %xmm0, 0xe8(%rsp)
movq %r15, %r13
leaq 0x108(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x3c5c1
movq %rax, %r12
movq 0x108(%rsp), %rdi
movq 0x110(%rsp), %rsi
callq 0x3929f
movq %r12, %rdi
movl %ebp, %esi
movsd 0xd8(%rsp), %xmm0
callq 0x4074c
movaps %xmm0, %xmm1
leaq 0x8ca43(%rip), %rax     # 0xcd870
movq (%rax), %rax
cmpl $0x0, 0x10(%rax)
movq 0x100(%rsp), %rbx
je 0x40e59
movsd 0xf8(%rsp), %xmm0
movq 0xb0(%rsp), %r15
movl %r15d, %edi
callq 0x40fa5
jmp 0x40e72
movsd 0xe8(%rsp), %xmm0
movq 0xb0(%rsp), %r15
movl %r15d, %edi
callq 0x40f59
movapd %xmm0, %xmm1
cmpl $0x1, %ebx
movsd %xmm0, 0xe0(%rsp)
je 0x40ea9
movsd 0x98(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x40ea9
movq 0xa0(%rsp), %rdi
callq 0x3eeb2
movsd 0xe0(%rsp), %xmm1
cmpl $0x1, %ebx
je 0x40ebd
movsd 0x98(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x40f00
pushq $0x13
popq %rcx
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x1a8(%rsp), %r12
movq %r12, %rdi
callq 0x3d854
movl $0x98, %edx
movq 0xa0(%rsp), %rdi
movq %r12, %rsi
callq 0x4310
movsd 0xe0(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movq %r13, %rdi
callq 0x3eeb2
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3929f
incl %ebx
jmp 0x40ce9
movq %r15, %rdi
movq 0xd0(%rsp), %rsi
callq 0x3929f
movq 0xc8(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x3929f
movq 0xa0(%rsp), %rax
addq $0x2d8, %rsp            # imm = 0x2D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq