// File: /workspace/github_c_makefile_star2_O1/olcaytaner[P]Isell/mlp.c
// Function: hessian_matrix_cls
matrix hessian_matrix_cls(Mlpnetwork N, Instanceptr train)
{
	/*!Last Changed 18.06.2005*/
	int weightcount, firstlayer, k, h, hprime, i, iprime, index1, index2, classno;
	double mul, yk, zh, sum1, sum2, zhprime, xi, xiprime, vkh, vkhprime;
	Instanceptr tmp;
	matrix result, testY, testH, inst;
	weightcount = number_of_weights_in_mlp_network(N);
	result = matrix_alloc(weightcount, weightcount);
	testY = matrix_alloc(1, N.P.classnum);
	testH = matrix_alloc(1, N.P.hidden[0]);
	firstlayer = N.weights[0].row * N.weights[0].col;
	tmp = train;
	while (tmp)
	 {
		 classno = give_classno(tmp);
		 inst = instance_to_matrix(tmp);
  	calculate_hidden_values(inst.values[0], N.weights[0], &testH);
		 calculate_output_values_cls(testH.values[0], N.weights[1], &testY);
			/*Both in the second layer*/
   for (k = 0; k < N.weights[1].row; k++)
			 {
					yk = testY.values[0][k];
				 for (h = 0; h < N.weights[1].col; h++)
					 {
						 zh = testH.values[0][h];
  					for (hprime = 0; hprime < N.weights[1].col; hprime++)
							 {
									zhprime = testH.values[0][hprime];
									index1 = firstlayer + k * N.weights[1].col + h;
									index2 = firstlayer + k * N.weights[1].col + hprime;
									result.values[index1][index2] += zh * zhprime * yk * (1 - yk);
							 }
					 }
			 }
   /*Both in the first layer*/
			index1 = 0;
			for (h = 0; h < N.weights[0].row; h++)
			 {
					zh = testH.values[0][h];
				 for (i = 0; i < N.weights[0].col; i++)
					 {
						 xi = inst.values[0][i];
						 index2 = 0;
						 for (hprime = 0; hprime < N.weights[0].row; hprime++)
							 {
								 zhprime = testH.values[0][hprime];
								 if (h == hprime)
									 {
           sum1 = 0;
											for (k = 0; k < N.weights[1].row; k++)
											 {
												 vkhprime = N.weights[1].values[k][hprime + 1];
													yk = testY.values[0][k];
												 if (k == classno)
														 sum1 += vkhprime * (yk - 1);
													else
														 sum1 += vkhprime * yk;
											 }
											sum1 = sum1 * (1 - 2 * zhprime);
									 }
									else
										 sum1 = 0;
									sum2 = 0;
         for (k = 0; k < N.weights[1].row; k++)
									 {
										 vkh = N.weights[1].values[k][h + 1];
											vkhprime = N.weights[1].values[k][hprime + 1];
											yk = testY.values[0][k];
											sum2 += vkh * vkhprime * yk * (1 - yk);
									 }
         sum2 = zh * (1 - zh) * sum2;
								 for (iprime = 0; iprime < N.weights[0].col; iprime++)
									 { 
										 xiprime = inst.values[0][iprime];
    							mul = xi * xiprime * zhprime * (1 - zhprime);
           result.values[index1][index2] += mul * (sum1 + sum2);
										 index2++;
									 }
							 }
						 index1++;
					 }
			 }
   /*One weight in each layer*/
			index1 = 0;
			for (h = 0; h < N.weights[0].row; h++)
			 {
				 zh = testH.values[0][h];
				 for (i = 0; i < N.weights[0].col; i++)
					 {
						 index2 = firstlayer;
							xi = inst.values[0][i];
							mul = xi * zh * (1 - zh);
						 for (k = 0; k < N.weights[1].row; k++)
							 {
								 yk = testY.values[0][k];
								 for (hprime = 0; hprime < N.weights[1].col; hprime++)
									 {
										 if (h == hprime - 1)
												 if (k == classno)
  												 sum1 = yk - 1;
													else
														 sum1 = yk;
											else
											  sum1 = 0;
											zhprime = testH.values[0][hprime];
											vkh = N.weights[1].values[k][h + 1];
											sum2 = zhprime * vkh * yk * (1 - yk);
           result.values[index1][index2] += mul * (sum1 + sum2);
											result.values[index2][index1] += mul * (sum1 + sum2);
										 index2++;
									 }
							 }
							index1++;
					 }
			 }
   matrix_free(inst);
		 tmp = tmp->next;
	 }
	matrix_free(testY);
	matrix_free(testH);
	return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
leaq 0xd0(%rsp), %rcx
movl 0x5c(%rcx), %eax
movq %rdi, %rbx
testl %eax, %eax
js 0x43466
movq (%rcx), %rcx
shlq $0x4, %rax
addq $0x10, %rax
xorl %edx, %edx
xorl %edi, %edi
movl 0x4(%rcx,%rdx), %esi
imull (%rcx,%rdx), %esi
addl %esi, %edi
addq $0x10, %rdx
cmpq %rdx, %rax
jne 0x43451
jmp 0x43468
xorl %edi, %edi
movl %edi, %esi
callq 0x3c6e5
movq %rax, 0x60(%rsp)
movq %rdx, 0x18(%rsp)
leaq 0xd0(%rsp), %r14
movl 0x8(%r14), %esi
movl $0x1, %edi
callq 0x3c6e5
movq %rax, 0x38(%rsp)
movq %rdx, 0x40(%rsp)
movl 0x50(%r14), %esi
movl $0x1, %edi
callq 0x3c6e5
movq %rax, 0x50(%rsp)
movq %rdx, 0x58(%rsp)
testq %rbx, %rbx
je 0x4395a
leaq 0xd0(%rsp), %rax
movq (%rax), %rax
movl 0x4(%rax), %ecx
imull (%rax), %ecx
movl %ecx, 0x4c(%rsp)
movslq %ecx, %rax
movq %rax, 0x80(%rsp)
leaq (,%rax,8), %rax
movq %rax, 0x68(%rsp)
movq %rbx, %rdi
callq 0x2eaca
movl %eax, %ebp
movq %rbx, 0x78(%rsp)
movq %rbx, %rdi
callq 0x2e851
movq %rax, 0x70(%rsp)
movq %rdx, 0x28(%rsp)
movq (%rdx), %rdi
leaq 0xd0(%rsp), %rax
movq (%rax), %r12
movq (%r12), %rsi
movq 0x8(%r12), %rdx
leaq 0x50(%rsp), %rcx
callq 0x43994
movq 0x58(%rsp), %rax
movq %rax, 0x8(%rsp)
movq (%rax), %rdi
movq 0x10(%r12), %rsi
movq 0x18(%r12), %rdx
leaq 0x38(%rsp), %rcx
callq 0x43e8f
movl 0x10(%r12), %eax
testl %eax, %eax
movsd 0x5efc6(%rip), %xmm8    # 0xa2520
jle 0x435fe
movq 0x40(%rsp), %rcx
movq (%rcx), %rcx
movl 0x14(%r12), %edx
movslq %edx, %rsi
leaq (,%rsi,8), %rdi
movq 0x68(%rsp), %r8
xorl %r9d, %r9d
testl %edx, %edx
jle 0x435f3
movsd (%rcx,%r9,8), %xmm0
movq 0x8(%rsp), %r10
movq (%r10), %r10
movq %r9, %r11
imulq %rsi, %r11
addq 0x80(%rsp), %r11
movapd %xmm8, %xmm1
subsd %xmm0, %xmm1
movq 0x18(%rsp), %rbx
leaq (%rbx,%r11,8), %r11
xorl %ebx, %ebx
movsd (%r10,%rbx,8), %xmm2
movq (%r11,%rbx,8), %r14
addq %r8, %r14
xorl %r15d, %r15d
movsd (%r10,%r15,8), %xmm3
mulsd %xmm2, %xmm3
mulsd %xmm0, %xmm3
mulsd %xmm1, %xmm3
addsd (%r14,%r15,8), %xmm3
movsd %xmm3, (%r14,%r15,8)
incq %r15
cmpq %r15, %rdx
jne 0x435c5
incq %rbx
cmpq %rdx, %rbx
jne 0x435b5
incq %r9
addq %rdi, %r8
cmpq %rax, %r9
jne 0x43580
movslq (%r12), %rsi
movl %ebp, %edx
movq %rsi, 0x20(%rsp)
testq %rsi, %rsi
movsd 0x5f793(%rip), %xmm9    # 0xa2da8
jle 0x437ae
movq 0x8(%rsp), %rcx
movq (%rcx), %rsi
movl 0x4(%r12), %edi
movq 0x40(%rsp), %r8
xorl %r9d, %r9d
xorl %r10d, %r10d
testl %edi, %edi
jle 0x437a0
movsd (%rsi,%r9,8), %xmm1
movq 0x28(%rsp), %rcx
movq (%rcx), %r11
movapd %xmm8, %xmm0
subsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
movslq %r10d, %r10
xorl %ecx, %ecx
movq %rcx, 0x10(%rsp)
movsd (%r11,%rcx,8), %xmm1
movq 0x18(%rsp), %rcx
movq %r10, 0x30(%rsp)
movq (%rcx,%r10,8), %rbx
xorl %r14d, %r14d
xorl %r13d, %r13d
movsd (%rsi,%r14,8), %xmm2
xorpd %xmm3, %xmm3
xorpd %xmm4, %xmm4
cmpq %r14, %r9
jne 0x436ea
testl %eax, %eax
jle 0x436d1
movq 0x18(%r12), %rcx
movq (%r8), %r15
xorpd %xmm5, %xmm5
xorl %r10d, %r10d
movsd (%r15,%r10,8), %xmm6
movapd %xmm6, %xmm4
addsd %xmm9, %xmm4
cmpq %r10, %rdx
je 0x436b8
movapd %xmm6, %xmm4
movq (%rcx,%r10,8), %rbp
mulsd 0x8(%rbp,%r14,8), %xmm4
addsd %xmm4, %xmm5
incq %r10
cmpq %r10, %rax
jne 0x436a0
jmp 0x436d5
xorpd %xmm5, %xmm5
movapd %xmm2, %xmm6
addsd %xmm2, %xmm6
movapd %xmm8, %xmm4
subsd %xmm6, %xmm4
mulsd %xmm5, %xmm4
testl %eax, %eax
jle 0x43732
movq 0x18(%r12), %rcx
movq (%r8), %r15
xorpd %xmm3, %xmm3
xorl %r10d, %r10d
movq (%rcx,%r10,8), %rbp
movsd 0x8(%rbp,%r9,8), %xmm5
mulsd 0x8(%rbp,%r14,8), %xmm5
movsd (%r15,%r10,8), %xmm6
mulsd %xmm6, %xmm5
movapd %xmm8, %xmm7
subsd %xmm6, %xmm7
mulsd %xmm5, %xmm7
addsd %xmm7, %xmm3
incq %r10
cmpq %r10, %rax
jne 0x436fd
mulsd %xmm0, %xmm3
addsd %xmm3, %xmm4
movapd %xmm8, %xmm3
subsd %xmm2, %xmm3
movslq %r13d, %r13
leaq (%rbx,%r13,8), %r10
xorl %ecx, %ecx
movsd (%r11,%rcx,8), %xmm5
mulsd %xmm1, %xmm5
mulsd %xmm2, %xmm5
mulsd %xmm3, %xmm5
mulsd %xmm4, %xmm5
addsd (%r10,%rcx,8), %xmm5
movsd %xmm5, (%r10,%rcx,8)
incq %rcx
cmpq %rcx, %rdi
jne 0x4374c
incq %r14
addq %rcx, %r13
cmpq 0x20(%rsp), %r14
jne 0x4367a
movq 0x30(%rsp), %r10
incq %r10
movq 0x10(%rsp), %rcx
incq %rcx
cmpq %rdi, %rcx
jne 0x4365b
incq %r9
cmpq 0x20(%rsp), %r9
jne 0x43633
cmpl $0x0, 0x20(%rsp)
jle 0x43939
movq 0x8(%rsp), %rcx
movq (%rcx), %rsi
movl 0x4(%r12), %ecx
movq %rcx, 0x8(%rsp)
movq 0x40(%rsp), %rcx
movq %rcx, 0x88(%rsp)
xorl %r9d, %r9d
xorl %r10d, %r10d
cmpl $0x0, 0x8(%rsp)
jle 0x4392b
movsd (%rsi,%r9,8), %xmm0
movq 0x28(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, 0x90(%rsp)
movapd %xmm8, %xmm1
subsd %xmm0, %xmm1
movslq %r10d, %r10
leaq 0x1(%r9), %rbx
movq $0x0, 0x10(%rsp)
testl %eax, %eax
jle 0x43910
movq 0x90(%rsp), %rcx
movq 0x10(%rsp), %rdi
movsd (%rcx,%rdi,8), %xmm2
mulsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
movq 0x88(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, 0x30(%rsp)
movl 0x14(%r12), %ebp
xorl %r8d, %r8d
movl 0x4c(%rsp), %r11d
testl %ebp, %ebp
jle 0x43904
movq 0x30(%rsp), %rcx
movsd (%rcx,%r8,8), %xmm3
movapd %xmm3, %xmm4
addsd %xmm9, %xmm4
cmpq %rdx, %r8
je 0x4387c
movapd %xmm3, %xmm4
movq %r12, %r13
movq 0x18(%r12), %rcx
movq (%rcx,%r8,8), %rdi
movapd %xmm8, %xmm5
subsd %xmm3, %xmm5
movslq %r11d, %rcx
leaq (,%rcx,8), %r15
movq 0x18(%rsp), %r14
addq (%r14,%r10,8), %r15
leaq (%r14,%rcx,8), %rcx
xorl %r14d, %r14d
movapd %xmm4, %xmm6
cmpq %r14, %rbx
je 0x438b9
xorpd %xmm6, %xmm6
movsd (%rsi,%r14,8), %xmm7
mulsd 0x8(%rdi,%r9,8), %xmm7
mulsd %xmm3, %xmm7
mulsd %xmm5, %xmm7
addsd %xmm6, %xmm7
mulsd %xmm2, %xmm7
movsd (%r15,%r14,8), %xmm6
addsd %xmm7, %xmm6
movsd %xmm6, (%r15,%r14,8)
movq (%rcx,%r14,8), %r12
addsd (%r12,%r10,8), %xmm7
movsd %xmm7, (%r12,%r10,8)
incq %r14
cmpq %r14, %rbp
jne 0x438ac
addl %r14d, %r11d
movq %r13, %r12
incq %r8
cmpq %rax, %r8
jne 0x43857
incq %r10
movq 0x10(%rsp), %rdi
incq %rdi
movq %rdi, 0x10(%rsp)
cmpq 0x8(%rsp), %rdi
jne 0x43818
incq %r9
cmpq 0x20(%rsp), %r9
jne 0x437de
movq 0x70(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x3dbad
movq 0x78(%rsp), %rbx
movq 0x10(%rbx), %rbx
testq %rbx, %rbx
jne 0x434e7
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x3dbad
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x3dbad
movq 0x60(%rsp), %rax
movq 0x18(%rsp), %rdx
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq