// File: /workspace/github_c_makefile_star2_O2/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 0x5f2e
movq 0x228(%rdi), %r14
testq %r14, %r14
je 0x5f32
movq %rsi, %rbx
movq 0xf8(%rsi), %rdi
testq %rdi, %rdi
je 0x5f36
cmpq $0x0, 0x10(%rbx)
je 0x5f3a
movq 0x8(%rbx), %rax
movq %rax, (%rsp)
callq 0x22e2
movq %rax, 0x10(%rsp)
movq 0x10(%rbx), %rdi
callq 0x22e2
movq %rax, %r12
movq 0x28(%rbx), %rdi
callq 0x22e2
movq %rax, %r13
movq %r14, %rdi
callq 0x22e2
movq %rax, %rbp
movq 0x30(%r14), %rax
movq 0xf8(%rbx), %rcx
cmpq 0x30(%rcx), %rax
jne 0x5f3e
movq 0x10(%rbx), %rcx
cmpq 0x30(%rcx), %rax
jne 0x5f42
movq 0x28(%rbx), %rdx
movl $0x80, %ecx
cmpq 0x30(%rdx), %rax
jne 0x5f45
xorl %r15d, %r15d
cmpq %r15, %rax
jbe 0x5f73
movsd (%r12,%r15,8), %xmm0
subsd (%rbp,%r15,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x10(%rsp), %rax
movsd %xmm0, (%rax,%r15,8)
movsd (%r12,%r15,8), %xmm0
pushq $0x1
popq %rdi
movq (%rsp), %rax
callq *0x290(%rax)
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, (%r13,%r15,8)
incq %r15
movq 0x30(%r14), %rax
jmp 0x5ede
pushq $0x72
jmp 0x5f44
pushq $0x75
jmp 0x5f44
pushq $0x78
jmp 0x5f44
pushq $0x79
jmp 0x5f44
pushq $0x7e
jmp 0x5f44
pushq $0x7f
popq %rcx
leaq 0x10f9(%rip), %rdi      # 0x7045
leaq 0x18dd(%rip), %rsi      # 0x7830
leaq 0x190f(%rip), %rdx      # 0x7869
xorl %eax, %eax
callq 0x1030
pushq $-0x1
popq %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa8(%rbx), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x6104
movq 0x28(%rbx), %rcx
movq 0x30(%rbx), %rdx
movq 0x38(%rbx), %rsi
movq 0x8(%rdx), %rdi
movq %rdi, 0x10(%rsp)
movq 0x8(%rsi), %rdi
movq %rdi, 0x30(%rsp)
movq 0x20(%rsi), %rdi
movq %rdi, 0x18(%rsp)
movq 0x28(%rsi), %rsi
movq %rsi, 0x28(%rsp)
movq 0x8(%rcx), %rsi
movq 0x20(%rcx), %rdi
movq 0x28(%rdx), %r14
movq 0x8(%rax), %r8
movq %r8, 0x40(%rsp)
movq 0x18(%rax), %r12
movq 0x28(%rax), %r13
movq 0x10(%rbx), %r8
imulq 0x28(%rcx), %rdi
leaq (,%rdi,8), %r9
movq %r9, (%rsp)
imulq 0x18(%rcx), %rdi
movq 0x18(%r8), %rcx
movq %rcx, 0x8(%rsp)
imulq 0x20(%rdx), %r14
movq 0x18(%rdx), %rcx
imulq %r14, %rcx
movq %rcx, 0x38(%rsp)
shlq $0x3, %rdi
imulq 0x20(%rax), %r13
leaq (,%r13,8), %rbp
imulq %r12, %r13
shlq $0x3, %r13
xorl %eax, %eax
movq %rsi, 0x20(%rsp)
movq %rsi, %rcx
cmpq 0x8(%rsp), %rax
je 0x60a4
movq 0x38(%rsp), %rdx
imulq %rax, %rdx
movq 0x10(%rsp), %rsi
leaq (%rsi,%rdx,8), %rdx
movq 0x40(%rsp), %r9
xorl %r11d, %r11d
cmpq %r12, %r11
je 0x609b
movq 0x10(%r8), %r10
xorpd %xmm0, %xmm0
movq %r9, %r15
movq %rcx, %rsi
movq %r10, %rbx
subq $0x1, %rbx
jb 0x607e
movsd (%rsi), %xmm1
mulsd (%r15), %xmm1
addsd %xmm1, %xmm0
addq %rdi, %rsi
addq %r13, %r15
jmp 0x6063
cvtsi2sd %r10, %xmm1
divsd %xmm1, %xmm0
movq %r14, %rsi
imulq %r11, %rsi
movsd %xmm0, (%rdx,%rsi,8)
incq %r11
addq %rbp, %r9
jmp 0x604d
incq %rax
addq (%rsp), %rcx
jmp 0x602c
movq 0x28(%rsp), %r11
imulq 0x18(%rsp), %r11
xorl %eax, %eax
xorl %edx, %edx
movq 0x30(%rsp), %r10
movq 0x20(%rsp), %rbx
cmpq 0x8(%rsp), %rdx
je 0x5f64
movq 0x10(%r8), %rcx
xorpd %xmm0, %xmm0
movq %rbx, %rsi
movq %rcx, %r9
subq $0x1, %r9
jb 0x60e5
addsd (%rsi), %xmm0
addq %rdi, %rsi
jmp 0x60d6
cvtsi2sd %rcx, %xmm1
divsd %xmm1, %xmm0
movq %r11, %rcx
imulq %rdx, %rcx
movsd %xmm0, (%r10,%rcx,8)
incq %rdx
addq (%rsp), %rbx
jmp 0x60bd
movl $0x92, %ecx
jmp 0x5f45
nop