// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x9777
leaq 0x18c4(%rip), %rdi      # 0xb015
leaq 0x20b8(%rip), %rsi      # 0xb810
leaq 0x20ea(%rip), %rdx      # 0xb849
movl $0x72, %ecx
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9eb1
movq -0x10(%rbp), %rax
movq 0x228(%rax), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x97ba
leaq 0x1881(%rip), %rdi      # 0xb015
leaq 0x2075(%rip), %rsi      # 0xb810
leaq 0x20a7(%rip), %rdx      # 0xb849
movl $0x75, %ecx
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9eb1
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
addq $0x288, %rax            # imm = 0x288
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0xf8(%rax)
jne 0x9807
leaq 0x1834(%rip), %rdi      # 0xb015
leaq 0x2028(%rip), %rsi      # 0xb810
leaq 0x205a(%rip), %rdx      # 0xb849
movl $0x78, %ecx
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9eb1
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x983f
leaq 0x17fc(%rip), %rdi      # 0xb015
leaq 0x1ff0(%rip), %rsi      # 0xb810
leaq 0x2022(%rip), %rdx      # 0xb849
movl $0x79, %ecx
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9eb1
movq -0x18(%rbp), %rax
movq 0xf8(%rax), %rdi
callq 0x2ce0
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x2ce0
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x2ce0
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rdi
callq 0x2ce0
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0xf8(%rcx), %rcx
cmpq 0x30(%rcx), %rax
je 0x98c8
leaq 0x1773(%rip), %rdi      # 0xb015
leaq 0x1f67(%rip), %rsi      # 0xb810
leaq 0x1f99(%rip), %rdx      # 0xb849
movl $0x7e, %ecx
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9eb1
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
cmpq 0x30(%rcx), %rax
je 0x990b
leaq 0x1730(%rip), %rdi      # 0xb015
leaq 0x1f24(%rip), %rsi      # 0xb810
leaq 0x1f56(%rip), %rdx      # 0xb849
movl $0x7f, %ecx
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9eb1
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x28(%rcx), %rcx
cmpq 0x30(%rcx), %rax
je 0x994e
leaq 0x16ed(%rip), %rdi      # 0xb015
leaq 0x1ee1(%rip), %rsi      # 0xb810
leaq 0x1f13(%rip), %rdx      # 0xb849
movl $0x80, %ecx
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9eb1
movl $0x0, -0x4c(%rbp)
movslq -0x4c(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jae 0x99e9
movq -0x38(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x48(%rbp), %rax
movslq -0x4c(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xb8(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x38(%rbp), %rcx
movslq -0x4c(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movl $0x1, %edi
callq *%rax
movaps %xmm0, %xmm1
movsd -0xb8(%rbp), %xmm0
mulsd %xmm1, %xmm0
movq -0x40(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x9955
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x100(%rbp)
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0xf8(%rbp)
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0xf0(%rbp)
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xe8(%rbp)
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0xe0(%rbp)
movq -0x60(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0xd8(%rbp)
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0xd0(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x20(%rax), %rax
movq %rax, -0xc8(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
movq %rax, -0xc0(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
jne 0x9b03
leaq 0x1538(%rip), %rdi      # 0xb015
leaq 0x1d2c(%rip), %rsi      # 0xb810
leaq 0x1d5e(%rip), %rdx      # 0xb849
movl $0x92, %ecx
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9eb1
movq -0x80(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x118(%rbp)
movq -0x80(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x110(%rbp)
movq -0x80(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x108(%rbp)
movq -0x80(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x88(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x90(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
movslq -0x9c(%rbp), %rax
movq -0x98(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jae 0x9d50
movl $0x0, -0xa0(%rbp)
movslq -0xa0(%rbp), %rax
movq -0x80(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jae 0x9d3a
xorps %xmm0, %xmm0
movsd %xmm0, -0x90(%rbp)
movl $0x0, -0xa4(%rbp)
movslq -0xa4(%rbp), %rax
movq -0x98(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x9c9a
movq -0x108(%rbp), %rcx
movq -0x110(%rbp), %rsi
movq -0x118(%rbp), %rdi
movq -0xc0(%rbp), %rdx
movq -0xc8(%rbp), %r9
movq -0xd0(%rbp), %r10
movq -0x78(%rbp), %rax
movslq -0xa4(%rbp), %r8
imulq %r9, %r10
imulq %rdx, %r10
imulq %r10, %r8
shlq $0x3, %r8
addq %r8, %rax
movslq -0x9c(%rbp), %r8
imulq %rdx, %r9
imulq %r9, %r8
shlq $0x3, %r8
addq %r8, %rax
imulq $0x0, %rdx, %rdx
movsd (%rax,%rdx,8), %xmm0
movq -0x88(%rbp), %rax
movslq -0xa4(%rbp), %rdx
imulq %rsi, %rdi
imulq %rcx, %rdi
imulq %rdi, %rdx
shlq $0x3, %rdx
addq %rdx, %rax
movslq -0xa0(%rbp), %rdx
imulq %rcx, %rsi
imulq %rsi, %rdx
shlq $0x3, %rdx
addq %rdx, %rax
imulq $0x0, %rcx, %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x90(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x9baf
movq -0xf0(%rbp), %rcx
movq -0xf8(%rbp), %rsi
movq -0x100(%rbp), %rdi
movsd -0x90(%rbp), %xmm0
movq -0x98(%rbp), %rax
movq 0x10(%rax), %xmm1
movaps 0x1606(%rip), %xmm2     # 0xb2d0
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x160a(%rip), %xmm2     # 0xb2e0
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movq -0x68(%rbp), %rax
movslq -0x9c(%rbp), %rdx
imulq %rsi, %rdi
imulq %rcx, %rdi
imulq %rdi, %rdx
shlq $0x3, %rdx
addq %rdx, %rax
movslq -0xa0(%rbp), %rdx
imulq %rcx, %rsi
imulq %rsi, %rdx
shlq $0x3, %rdx
addq %rdx, %rax
imulq $0x0, %rcx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x9b85
jmp 0x9d3c
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x9b63
movl $0x0, -0xa8(%rbp)
movslq -0xa8(%rbp), %rax
movq -0x98(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jae 0x9eaa
xorps %xmm0, %xmm0
movsd %xmm0, -0x90(%rbp)
movl $0x0, -0xac(%rbp)
movslq -0xac(%rbp), %rax
movq -0x98(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x9e11
movq -0xc0(%rbp), %rcx
movq -0xc8(%rbp), %rsi
movq -0xd0(%rbp), %rdi
movq -0x78(%rbp), %rax
movslq -0xac(%rbp), %rdx
imulq %rsi, %rdi
imulq %rcx, %rdi
imulq %rdi, %rdx
shlq $0x3, %rdx
addq %rdx, %rax
movslq -0xa8(%rbp), %rdx
imulq %rcx, %rsi
imulq %rsi, %rdx
shlq $0x3, %rdx
addq %rdx, %rax
imulq $0x0, %rcx, %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x9d87
movq -0xd8(%rbp), %rcx
movq -0xe0(%rbp), %rsi
movq -0xe8(%rbp), %rdx
movsd -0x90(%rbp), %xmm0
movq -0x98(%rbp), %rax
movq 0x10(%rax), %xmm1
movaps 0x148f(%rip), %xmm2     # 0xb2d0
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x1493(%rip), %xmm2     # 0xb2e0
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movq -0x70(%rbp), %rax
imulq %rsi, %rdx
imulq %rcx, %rdx
imulq $0x0, %rdx, %rdx
shlq $0x3, %rdx
addq %rdx, %rax
movslq -0xa8(%rbp), %rdx
imulq %rcx, %rsi
imulq %rsi, %rdx
shlq $0x3, %rdx
addq %rdx, %rax
imulq $0x0, %rcx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x9d5a
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopl (%rax)