// File: /workspace/github_c_makefile_star2_O1/xtanitfy[P]Murphy/src/layers/output_layer.c
// Function: OutputLayerBackward
int OutputLayerBackward(NET_PARA_s *pNetPara,LAYER_s *pLayer)
{
	CHECK_EXPR_RET(pLayer == NULL,-1);

	BLOB_s *pTableY  = pNetPara->pTableY;
	CHECK_EXPR_RET(pTableY == NULL,-1);
	
	OUTPUT_LAYER_PARA_s *pPara = &pLayer->para->select.outputLayer;
	CHECK_EXPR_RET(pLayer->pError == NULL,-1);
	CHECK_EXPR_RET(pLayer->pData == NULL,-1);
	double *pErrorData = BLOB_data(pLayer->pError);
	double *pData = BLOB_data(pLayer->pData);
	double *pDelta = BLOB_data(pLayer->pDelta);
	double *pTableYData = BLOB_data(pTableY);
	CHECK_EXPR_RET(pTableY->count != pLayer->pError->count,-1);
	CHECK_EXPR_RET(pTableY->count != pLayer->pData->count,-1);
	CHECK_EXPR_RET(pTableY->count != pLayer->pDelta->count,-1);

	for (int i = 0;i < pTableY->count;i++) {
		pErrorData[i] = pData[i] - pTableYData[i]; 
		pDelta[i] = pErrorData[i] * pPara->function(pData[i],FUNC_DERIVA);
	}
	//LOG_writeBlobToTxtFile("outputDelta.txt",pLayer->pDelta);

	/*����loss*/
	//OPTS_s *pOpts = NET_GetOpts();
	//CHECK_EXPR_RET(pOpts == NULL,-1);
	/*����Ȩ�غ�ƫ��*/
	BLOB_s *pDK = pLayer->pDK;
	BLOB_s *pDB = pLayer->pDB;
	BLOB_DATA_PTR(pDK,ptrDK);
	BLOB_DATA_PTR(pDB,ptrDB);
	BLOB_DATA_PTR(pLayer->pDelta,ptrDelta);
	BLOB_s *pPrevData = pLayer->pPrev[0]->pData;
	CHECK_EXPR_RET(pPrevData == NULL,-1);
	BLOB_DATA_PTR(pPrevData,ptrPrev);

	//double alpha = pOpts->alpha;
	double sum = 0;
	//CHECK_EXPR_RET(pLayer->k == NULL,-1);
	//BLOB_DATA_PTR(pLayer->k,ptrK);
	
	BLOB_s *pCurrData = pLayer->pData;
	
	for (int i_out = 0;i_out < pCurrData->channelSize;i_out++) {
		for (int i_in = 0;i_in < pPrevData->channelSize;i_in++) {
			sum = 0;
			for (int i_batch = 0;i_batch < pCurrData->numSize;i_batch++) {
				sum += ptrDelta[i_batch][i_out][0][0] *  \
					ptrPrev[i_batch][i_in][0][0] ;
			}
			ptrDK[i_out][i_in][0][0] = sum/pCurrData->numSize;
		}
	}

	//pDK->type = BLOB_TYPE_WEIGHT;
	//LOG_writeBlobToTxtFile("output_dk.txt",pDK);

	//CHECK_EXPR_RET(pLayer->b == NULL,-1);
	//BLOB_DATA_PTR(pLayer->b,ptrB);
	
	for (int i_out = 0;i_out < pCurrData->channelSize;i_out++) {
		sum = 0;
		for (int i_batch = 0;i_batch < pCurrData->numSize;i_batch++) {
			sum += ptrDelta[i_batch][i_out][0][0];
		}
		ptrDB[0][i_out][0][0] = sum/pCurrData->numSize;
	}
		
	//pDB->type = BLOB_TYPE_BIAS;
	//LOG_writeBlobToTxtFile("output_db.txt",pDB);
	
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
testq %rsi, %rsi
je 0x69d2
movq 0x228(%rdi), %r14
testq %r14, %r14
je 0x69d9
movq %rsi, %rbx
movq 0xf8(%rsi), %rdi
testq %rdi, %rdi
je 0x69e0
cmpq $0x0, 0x10(%rbx)
je 0x69e7
movq 0x8(%rbx), %r15
callq 0x242c
movq %rax, %r13
movq 0x10(%rbx), %rdi
callq 0x242c
movq %rax, %rbp
movq 0x28(%rbx), %rdi
callq 0x242c
movq %rax, %r12
movq %r14, %rdi
callq 0x242c
movq 0x30(%r14), %rsi
movq 0xf8(%rbx), %rdx
movl $0x7e, %ecx
cmpq 0x30(%rdx), %rsi
jne 0x69ec
movq 0x10(%rbx), %rdx
movl $0x7f, %ecx
cmpq 0x30(%rdx), %rsi
jne 0x69ec
movq 0x28(%rbx), %rdx
movl $0x80, %ecx
cmpq 0x30(%rdx), %rsi
jne 0x69ec
movq %rbx, 0x8(%rsp)
cmpq $0x0, 0x30(%r14)
je 0x67a0
movq %r15, %rbx
xorl %r15d, %r15d
movq %r14, 0x10(%rsp)
movsd (%rbp,%r15,8), %xmm0
subsd (%rax,%r15,8), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd %xmm0, (%r13,%r15,8)
movsd (%rbp,%r15,8), %xmm0
movl $0x1, %edi
movq %rbp, %r14
movq %r12, %rbp
movq %r13, %r12
movq %rax, %r13
callq *0x290(%rbx)
movq %r13, %rax
movq %r12, %r13
movq %rbp, %r12
movq %r14, %rbp
movq 0x10(%rsp), %r14
mulsd 0x18(%rsp), %xmm0
movsd %xmm0, (%r12,%r15,8)
incq %r15
cmpq %r15, 0x30(%r14)
ja 0x6742
movq 0x8(%rsp), %rdi
movq 0xa8(%rdi), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x6a1c
movq 0x28(%rdi), %rcx
movq 0x38(%rdi), %rdx
movq 0x8(%rdx), %rsi
movq %rsi, 0x50(%rsp)
movq 0x20(%rdx), %rsi
movq %rsi, 0x40(%rsp)
movq 0x28(%rdx), %rdx
movq %rdx, 0x48(%rsp)
movq 0x8(%rcx), %rdx
movq %rdx, 0x30(%rsp)
movq 0x18(%rcx), %rdx
movq %rdx, 0x28(%rsp)
movq 0x20(%rcx), %rdx
movq %rdx, 0x20(%rsp)
movq 0x28(%rcx), %r10
movq 0x10(%rdi), %rcx
movq %rcx, 0x38(%rsp)
movq 0x18(%rcx), %rbp
testq %rbp, %rbp
je 0x6935
movq 0x30(%rdi), %rcx
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rsp)
movq 0x20(%rcx), %r14
movq 0x8(%rax), %rdx
movq %rdx, 0x60(%rsp)
movq 0x18(%rax), %r12
movq 0x28(%rax), %r13
imulq 0x20(%rax), %r13
imulq 0x28(%rcx), %r14
movq 0x18(%rcx), %rax
imulq %r14, %rax
movq %rax, 0x58(%rsp)
leaq (,%r13,8), %r9
imulq %r12, %r13
shlq $0x3, %r13
movq %r10, %rdi
imulq 0x20(%rsp), %rdi
leaq (,%rdi,8), %rax
movq %rax, 0x10(%rsp)
imulq 0x28(%rsp), %rdi
shlq $0x3, %rdi
xorl %ecx, %ecx
movq 0x1a7c(%rip), %xmm0     # 0x8300
movapd 0x1a84(%rip), %xmm1     # 0x8310
movq 0x30(%rsp), %r15
movq %rbp, 0x18(%rsp)
testq %r12, %r12
je 0x691f
movq 0x58(%rsp), %rdx
imulq %rcx, %rdx
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, %xmm3
punpckldq %xmm0, %xmm3    # xmm3 = xmm3[0],xmm0[0],xmm3[1],xmm0[1]
subpd %xmm1, %xmm3
movapd %xmm3, %xmm2
unpckhpd %xmm3, %xmm2            # xmm2 = xmm2[1],xmm3[1]
addsd %xmm3, %xmm2
movq 0x8(%rsp), %rsi
leaq (%rsi,%rdx,8), %rdx
movq 0x60(%rsp), %r8
xorl %ebp, %ebp
xorpd %xmm3, %xmm3
testq %rax, %rax
je 0x6904
movq %rax, %rbx
movq %r15, %rsi
movq %r8, %r11
movsd (%rsi), %xmm4
mulsd (%r11), %xmm4
addsd %xmm4, %xmm3
addq %r13, %r11
addq %rdi, %rsi
decq %rbx
jne 0x68ec
divsd %xmm2, %xmm3
movq %r14, %rsi
imulq %rbp, %rsi
movsd %xmm3, (%rdx,%rsi,8)
incq %rbp
addq %r9, %r8
cmpq %r12, %rbp
jne 0x68da
incq %rcx
addq 0x10(%rsp), %r15
movq 0x18(%rsp), %rbp
cmpq %rbp, %rcx
jne 0x6896
testq %rbp, %rbp
je 0x6a23
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rcx
movq %rcx, %xmm1
punpckldq 0x19ac(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x19b4(%rip), %xmm1     # 0x8310
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq 0x48(%rsp), %r9
imulq 0x40(%rsp), %r9
imulq 0x20(%rsp), %r10
leaq (,%r10,8), %rdx
imulq 0x28(%rsp), %r10
shlq $0x3, %r10
xorl %esi, %esi
movq 0x50(%rsp), %r8
movq 0x30(%rsp), %r11
xorpd %xmm1, %xmm1
testq %rcx, %rcx
je 0x69b2
movq %rcx, %rax
movq %r11, %rdi
addsd (%rdi), %xmm1
addq %r10, %rdi
decq %rax
jne 0x69a6
divsd %xmm0, %xmm1
movq %r9, %rax
imulq %rsi, %rax
movsd %xmm1, (%r8,%rax,8)
incq %rsi
addq %rdx, %r11
xorl %eax, %eax
cmpq %rbp, %rsi
jne 0x6997
jmp 0x6a0d
movl $0x72, %ecx
jmp 0x69ec
movl $0x75, %ecx
jmp 0x69ec
movl $0x78, %ecx
jmp 0x69ec
movl $0x79, %ecx
leaq 0x1652(%rip), %rdi      # 0x8045
leaq 0x1e49(%rip), %rsi      # 0x8843
leaq 0x1e7b(%rip), %rdx      # 0x887c
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x92, %ecx
jmp 0x69ec
xorl %eax, %eax
jmp 0x6a0d
nop