// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/mlp.c
// Function: mlpn_general
Mlpnetwork mlpn_general(matrix Train, matrix Validation, Mlpparametersptr P, SUPERVISED_ALGORITHM_TYPE algorithm_type)
{
	int iindex, tr, k;
	double sqrdError = 0, minError, cvError, loglikelihood;
	int *I;
	Mlpnetwork nN, minN;
	matrixptr prevWeights;
	matrixptr H;
	nN = allocate_mlpnnetwork(*P);
	prevWeights = allocate_weights(*P, INITIAL_VALUE, 0);
	H = allocate_hiddens(*P);
	P->eta = P->initialeta;
	for (tr = 0; tr < P->epochs; tr++)
  {
   sqrdError = 0;
   I = random_array(P->inputnum);
   for (k = 0; k < P->inputnum; k++)
    {
     iindex = I[k];
     switch (algorithm_type)
      {
       case CLASSIFICATION:
       default            :sqrdError = sqrdError + backPropMlpn_cls(Train.values[iindex], nN.weights, H, prevWeights, *P);
                           break;
       case REGRESSION    :sqrdError = sqrdError + backPropMlpn_reg(Train.values[iindex], nN.weights, H, prevWeights, *P);
                           break;
       case AUTOENCODER   :sqrdError = sqrdError + backPropMlpn_autoencoder(Train.values[iindex], nN.weights, H, prevWeights, *P);
                           break;
      }
    }
   safe_free(I);
   sqrdError = sqrdError / P->inputnum;
   P->sqrderror = sqrdError;
   P->eta = P->eta * P->etadecrease;
   /*check validation data and update the best network if necessary*/
   nN.P = *P;
   switch (algorithm_type)
    {
     case CLASSIFICATION:
     default            :cvError = Validation.row - testMlpnNetwork_cls(Validation, nN, &loglikelihood);
                         break;
     case REGRESSION    :cvError = testMlpnNetwork_reg(Validation, nN);
                         break;
     case AUTOENCODER   :cvError = testMlpnNetwork_autoencoder(Validation, nN);
                         break;
    }
   if (tr > 0)
    {
     if (cvError < minError)
      {
       P->bestepoch = tr;
       minError = cvError;
       free_mlpnnetwork(&minN);
       minN = copy_of_Mlpnetwork(nN);
      }
    }
   else
    {
     P->bestepoch = tr;
     minError = cvError;
     minN = copy_of_Mlpnetwork(nN);
    }
  }
	free_weights(prevWeights, P->layernum);
	free_hiddens(H, P->layernum);
	free_mlpnnetwork(&nN);
 return minN;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %r9, %r14
movq %r8, 0xb0(%rsp)
movq %rcx, 0xa8(%rsp)
movq %rdx, 0xd0(%rsp)
movq %rdi, %r13
leaq 0x178(%rsp), %r15
movl $0x90, %edx
movq %r15, %rdi
movq %r9, %rsi
callq 0x4310
pushq $0x12
popq %r12
movq %r12, %rcx
movq %rsp, %rdi
movq %r15, %rsi
rep  movsq (%rsi), %es:(%rdi)
xorl %ebp, %ebp
xorl %edi, %edi
xorl %esi, %esi
callq 0x3eb62
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
movq %rax, -0x8(%rdi)
movl $0x90, %edx
movq %r15, %rsi
callq 0x4310
movq %r12, %rcx
movq %rsp, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
pushq $0x1
popq %rdi
xorl %esi, %esi
callq 0x3eb62
movq %rax, %r15
movq %r12, %rcx
movq %rsp, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x3ee10
movq %rax, %r12
movsd 0x20(%r14), %xmm0
movsd %xmm0, 0x38(%r14)
movq %r13, 0xc0(%rsp)
cmpl 0x14(%r14), %ebp
jge 0x3dc60
movq %rbp, 0xb8(%rsp)
movl 0x8(%r14), %edi
callq 0x35cf7
movq %rax, %rbp
xorpd %xmm0, %xmm0
movsd %xmm0, 0xa0(%rsp)
xorl %r13d, %r13d
movslq 0x8(%r14), %rax
cmpq %rax, %r13
jge 0x3daff
movslq (%rbp,%r13,4), %rax
movq 0xd0(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movl 0x240(%rsp), %ecx
cmpl $0x1, %ecx
je 0x3daa5
cmpl $0x2, %ecx
jne 0x3dac4
pushq $0x12
popq %rcx
movq %rsp, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x3f87d
jmp 0x3dae1
pushq $0x12
popq %rcx
movq %rsp, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x3f634
jmp 0x3dae1
pushq $0x12
popq %rcx
movq %rsp, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x3f3eb
movsd 0xa0(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0xa0(%rsp)
incq %r13
jmp 0x3da57
movq %rbp, %rdi
callq 0x34ff7
cvtsi2sdl 0x8(%r14), %xmm0
movsd 0xa0(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x88(%r14)
movsd 0x38(%r14), %xmm0
mulsd 0x40(%r14), %xmm0
movsd %xmm0, 0x38(%r14)
movl $0x90, %edx
leaq 0xe8(%rsp), %rdi
movq %r14, %rsi
callq 0x4310
movl 0x240(%rsp), %eax
cmpl $0x1, %eax
je 0x3db8f
cmpl $0x2, %eax
movq 0xc0(%rsp), %r13
pushq $0x13
popq %rcx
movq %rsp, %rdi
leaq 0xe0(%rsp), %rsi
jne 0x3dbc3
rep  movsq (%rsi), %es:(%rdi)
movq 0xa8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x3ffab
movapd %xmm0, %xmm1
jmp 0x3dbee
pushq $0x13
popq %rcx
movq %rsp, %rdi
leaq 0xe0(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq 0xa8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x3de19
movapd %xmm0, %xmm1
movq 0xc0(%rsp), %r13
jmp 0x3dbee
rep  movsq (%rsi), %es:(%rdi)
movq 0xa8(%rsp), %rbp
movq %rbp, %rdi
movq 0xb0(%rsp), %rsi
leaq 0xd8(%rsp), %rdx
callq 0x3dcdc
movl %ebp, %ecx
subl %eax, %ecx
cvtsi2sd %ecx, %xmm1
movq 0xb8(%rsp), %rbp
testl %ebp, %ebp
je 0x3dc20
movsd 0xc8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3dc59
movsd %xmm1, 0xa0(%rsp)
movl %ebp, 0x18(%r14)
movq %r13, %rdi
callq 0x3eeb2
jmp 0x3dc2e
movsd %xmm1, 0xa0(%rsp)
andl $0x0, 0x18(%r14)
pushq $0x13
popq %rcx
movq %rsp, %rdi
leaq 0xe0(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r13, %rdi
callq 0x3d854
movsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
incl %ebp
jmp 0x3da29
movl 0x54(%r14), %esi
movq %r15, %rdi
callq 0x3eec0
movl 0x54(%r14), %esi
movq %r12, %rdi
callq 0x3ef02
leaq 0xe0(%rsp), %rdi
callq 0x3eeb2
movq %r13, %rax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq