// File: /workspace/github_c_makefile_star2_O1/olcaytaner[P]Isell/mlp.c
// Function: backPropMlpn_reg
double backPropMlpn_reg(double *Train, matrixptr weights, matrixptr hidden, matrixptr prevWeights, Mlpparameters P)
{	
	/*!Last Changed 16.03.2004 Oya*/
	matrix output_error, Y;
	matrixptr dweights;
	double sqrderror;
 int li;
	output_error = matrix_alloc(1, P.classnum);
	Y = matrix_alloc(1, P.classnum);
	dweights = allocate_weights(P, NO_INITIALIZATION, 0);/*dweights has the same dimensions as weights*/	
	/*calculate hidden layers*/
	for (li=0; li<P.layernum; li++)
	 {
 		if (li==0)
	  		calculate_hidden_values(Train, weights[0], &hidden[0]);
	 	else
	  		calculate_hidden_values(hidden[li-1].values[0], weights[li], &hidden[li]);
	 }
	if (P.layernum == 0) 	/*calculate outputs*/
		 calculate_output_values_reg(Train, weights[P.layernum], &Y);
	else
		 calculate_output_values_reg(hidden[P.layernum-1].values[0], weights[P.layernum], &Y);	
	sqrderror = calculate_error_values_reg(Train[P.dimnum], Y, &output_error); 	/*calculate error*/
	calculate_change_in_weights(Train, weights, hidden, dweights, output_error, P); 	/*calculate the change in weights*/
	update_weights(weights, dweights, prevWeights, P); 	/*add the change and momentum value to weights*/
	/*update prevWeights*/
	for (li=0; li<(P.layernum+1); li++)
		 matrix_identical(&(prevWeights[li]), dweights[li]);
	matrix_free(output_error);
	matrix_free(Y);
	free_weights(dweights, P.layernum);	
	return sqrderror;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rcx, 0xc0(%rsp)
movq %rdx, %rbp
movq %rsi, %r13
movq %rdi, %rbx
leaq 0x100(%rsp), %r14
movl (%r14), %r15d
movl $0x1, %edi
movl %r15d, %esi
callq 0x3c6e5
movq %rax, 0xb8(%rsp)
movq %rdx, 0xa8(%rsp)
movl $0x1, %edi
movl %r15d, %esi
callq 0x3c6e5
movq %rax, 0xb0(%rsp)
movq %rdx, 0x90(%rsp)
movl $0x12, %ecx
movq %rsp, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl $0x2, %edi
xorl %esi, %esi
callq 0x43f69
movq %rax, %r15
movslq 0x54(%r14), %rax
movq %rax, 0x98(%rsp)
testq %rax, %rax
jle 0x44b0f
movq 0x98(%rsp), %r12
shlq $0x4, %r12
xorl %r14d, %r14d
testq %r14, %r14
je 0x44af3
leaq (%r14,%rbp), %rcx
movq -0x8(%rbp,%r14), %rax
movq (%rax), %rdi
movq (%r13,%r14), %rsi
movq 0x8(%r13,%r14), %rdx
jmp 0x44b01
movq (%r13), %rsi
movq 0x8(%r13), %rdx
movq %rbx, %rdi
movq %rbp, %rcx
callq 0x43994
addq $0x10, %r14
cmpq %r14, %r12
jne 0x44ad6
movq 0x98(%rsp), %rsi
testl %esi, %esi
je 0x44b93
shlq $0x4, %rsi
movq (%r13,%rsi), %rax
testl %eax, %eax
jle 0x44bf7
movq 0x8(%r13,%rsi), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movq -0x8(%rbp,%rsi), %rsi
movq (%rsi), %rsi
movq 0x90(%rsp), %rdi
movq (%rdi), %rdi
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rdx, %r8
decq %r8
xorl %r9d, %r9d
movq (%rcx,%r9,8), %r10
movsd (%r10), %xmm0
cmpl $0x2, %edx
jl 0x44b83
xorl %r11d, %r11d
movsd 0x8(%r10,%r11,8), %xmm1
mulsd (%rsi,%r11,8), %xmm1
addsd %xmm1, %xmm0
incq %r11
cmpq %r11, %r8
jne 0x44b6a
movsd %xmm0, (%rdi,%r9,8)
incq %r9
cmpq %rax, %r9
jne 0x44b59
jmp 0x44bf7
movq (%r13), %rax
testl %eax, %eax
jle 0x44bf7
movq 0x8(%r13), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movq 0x90(%rsp), %rsi
movq (%rsi), %rsi
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rdx, %rdi
decq %rdi
xorl %r8d, %r8d
movq (%rcx,%r8,8), %r9
movsd (%r9), %xmm0
cmpl $0x2, %edx
jl 0x44be9
xorl %r10d, %r10d
movsd 0x8(%r9,%r10,8), %xmm1
mulsd (%rbx,%r10,8), %xmm1
addsd %xmm1, %xmm0
incq %r10
cmpq %r10, %rdi
jne 0x44bd0
movsd %xmm0, (%rsi,%r8,8)
incq %r8
cmpq %rax, %r8
jne 0x44bbf
movq 0xb0(%rsp), %r12
shrq $0x20, %r12
testl %r12d, %r12d
jle 0x44c55
leaq 0x100(%rsp), %rax
movslq 0x4(%rax), %rax
movsd (%rbx,%rax,8), %xmm0
movq 0x90(%rsp), %rax
movq (%rax), %rax
movq 0xa8(%rsp), %rcx
movq (%rcx), %rcx
xorpd %xmm2, %xmm2
xorl %edx, %edx
movapd %xmm0, %xmm1
subsd (%rax,%rdx,8), %xmm1
movsd %xmm1, (%rcx,%rdx,8)
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm2
incq %rdx
cmpq %rdx, %r12
jne 0x44c35
jmp 0x44c59
xorpd %xmm2, %xmm2
movsd %xmm2, 0xa0(%rsp)
movl $0x12, %ecx
movq %rsp, %rdi
leaq 0x100(%rsp), %r14
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
movq 0xa8(%rsp), %rbp
movq %rbp, %r9
callq 0x4448b
movl $0x12, %ecx
movq %rsp, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r13, %rdi
movq %r15, %rsi
movq 0xc0(%rsp), %r13
movq %r13, %rdx
callq 0x44713
movq 0x98(%rsp), %rax
testl %eax, %eax
js 0x44cec
movl %eax, %ebx
shlq $0x4, %rbx
addq $0x10, %rbx
xorl %r14d, %r14d
leaq (%r14,%r13), %rdi
movq (%r15,%r14), %rsi
movq 0x8(%r15,%r14), %rdx
callq 0x3f37d
addq $0x10, %r14
cmpq %r14, %rbx
jne 0x44cd1
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd 0xa0(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0xa0(%rsp)
movq 0xb8(%rsp), %rdi
movq %rbp, %rsi
callq 0x3dbad
movq 0xb0(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x3dbad
leaq 0x100(%rsp), %rax
movl 0x54(%rax), %esi
movq %r15, %rdi
callq 0x442c9
movsd 0xa0(%rsp), %xmm0
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq