// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
testq %rsi, %rsi
je 0x6782
movq 0x228(%rdi), %r14
testq %r14, %r14
je 0x6789
movq %rsi, %rbx
movq 0xf8(%rsi), %rdi
testq %rdi, %rdi
je 0x6790
cmpq $0x0, 0x10(%rbx)
je 0x6797
movq 0x8(%rbx), %r15
callq 0x23c0
movq %rax, %r13
movq 0x10(%rbx), %rdi
callq 0x23c0
movq %rax, %r12
movq 0x28(%rbx), %rdi
callq 0x23c0
movq %rax, %rbp
movq %r14, %rdi
callq 0x23c0
movq 0x30(%r14), %rdx
movq 0xf8(%rbx), %rsi
movl $0x7e, %ecx
cmpq 0x30(%rsi), %rdx
jne 0x679c
movq 0x10(%rbx), %rsi
movl $0x7f, %ecx
cmpq 0x30(%rsi), %rdx
jne 0x679c
movq 0x28(%rbx), %r10
movl $0x80, %ecx
cmpq 0x30(%r10), %rdx
jne 0x679c
testq %rdx, %rdx
je 0x65db
movq %r13, 0x8(%rsp)
movq %r15, (%rsp)
xorl %r15d, %r15d
movsd (%r12,%r15,8), %xmm0
subsd (%rax,%r15,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x8(%rsp), %rcx
movsd %xmm0, (%rcx,%r15,8)
movsd (%r12,%r15,8), %xmm0
movl $0x1, %edi
movq %rax, %r13
movq (%rsp), %rax
callq *0x290(%rax)
movq %r13, %rax
mulsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rbp,%r15,8)
incq %r15
cmpq %r15, 0x30(%r14)
ja 0x6589
movq 0x28(%rbx), %r10
movq 0xa8(%rbx), %rcx
movq 0x10(%rcx), %rdx
testq %rdx, %rdx
je 0x67cc
movq 0x10(%rbx), %rcx
movq %rcx, (%rsp)
movq 0x18(%rcx), %r9
testq %r9, %r9
je 0x67d3
movq 0x30(%rbx), %rsi
movq 0x38(%rbx), %rcx
movq 0x8(%rsi), %rdi
movq %rdi, 0x40(%rsp)
movq 0x20(%rsi), %rbx
movq 0x8(%rcx), %rdi
movq %rdi, 0x28(%rsp)
movq 0x20(%rcx), %rdi
movq %rdi, 0x18(%rsp)
movq 0x28(%rcx), %rcx
movq %rcx, 0x20(%rsp)
movq 0x8(%r10), %rcx
movq 0x20(%r10), %r8
movq 0x8(%rdx), %rdi
movq %rdi, 0x38(%rsp)
movq 0x28(%rdx), %r15
imulq 0x20(%rdx), %r15
movq 0x18(%rdx), %r12
imulq 0x28(%rsi), %rbx
movq 0x18(%rsi), %rdx
imulq %rbx, %rdx
movq %rdx, 0x30(%rsp)
leaq (,%r15,8), %rbp
imulq 0x28(%r10), %r8
imulq %r12, %r15
leaq (,%r8,8), %r14
imulq 0x18(%r10), %r8
shlq $0x3, %r15
shlq $0x3, %r8
xorl %r10d, %r10d
movq %rcx, %r11
movq %r9, 0x10(%rsp)
movq %r14, 0x8(%rsp)
testq %r12, %r12
je 0x670c
movq 0x30(%rsp), %rax
movq (%rsp), %rdx
movq 0x10(%rdx), %r13
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
imulq %r10, %rax
movq 0x40(%rsp), %rdx
leaq (%rdx,%rax,8), %rax
movq 0x38(%rsp), %r9
xorl %esi, %esi
xorpd %xmm1, %xmm1
testq %r13, %r13
je 0x66f1
movq %r13, %r14
movq %r11, %rdi
movq %r9, %rdx
movsd (%rdi), %xmm2
mulsd (%rdx), %xmm2
addsd %xmm2, %xmm1
addq %r15, %rdx
addq %r8, %rdi
decq %r14
jne 0x66da
divsd %xmm0, %xmm1
movq %rbx, %rdx
imulq %rsi, %rdx
movsd %xmm1, (%rax,%rdx,8)
incq %rsi
addq %rbp, %r9
cmpq %r12, %rsi
jne 0x66c8
incq %r10
movq 0x8(%rsp), %r14
addq %r14, %r11
movq 0x10(%rsp), %r9
cmpq %r9, %r10
jne 0x669a
movq (%rsp), %rax
movq 0x10(%rax), %rdi
xorps %xmm0, %xmm0
cvtsi2sd %rdi, %xmm0
movq 0x20(%rsp), %r11
imulq 0x18(%rsp), %r11
xorl %edx, %edx
movq 0x28(%rsp), %r10
xorpd %xmm1, %xmm1
testq %rdi, %rdi
je 0x6762
movq %rdi, %rax
movq %rcx, %rsi
addsd (%rsi), %xmm1
addq %r8, %rsi
decq %rax
jne 0x6756
divsd %xmm0, %xmm1
movq %r11, %rax
imulq %rdx, %rax
movsd %xmm1, (%r10,%rax,8)
incq %rdx
addq %r14, %rcx
xorl %eax, %eax
cmpq %r9, %rdx
jne 0x6747
jmp 0x67bd
movl $0x72, %ecx
jmp 0x679c
movl $0x75, %ecx
jmp 0x679c
movl $0x78, %ecx
jmp 0x679c
movl $0x79, %ecx
leaq 0x8a2(%rip), %rdi       # 0x7045
leaq 0x1086(%rip), %rsi      # 0x7830
leaq 0x10b8(%rip), %rdx      # 0x7869
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x92, %ecx
jmp 0x679c
xorl %eax, %eax
jmp 0x67bd
nop