// File: /workspace/github_c_makefile_star2_O1/xtanitfy[P]Murphy/src/net.c
// Function: NETUpdate
static int NETUpdate(void *usr,int idCurr,int *idPrev,int nIdPrev)
{
	NET_s *pNet = (void *)usr;
	CHECK_EXPR_RET(pNet == NULL,-1);

	LAYER_s *pLayer = pNet->layers[idCurr];
	CHECK_EXPR_RET(pLayer == NULL,-1);
	if (pLayer->k == NULL || pLayer->b == NULL \
				|| pLayer->pDK == NULL || pLayer->pDB == NULL) {
		//DBG_PRT;
		return 0;
	}

	double alpha = pNet->para.alpha;
	BLOB_DATA_PTR(pLayer->k,ptrK);
	BLOB_DATA_PTR(pLayer->b,ptrB);
	BLOB_DATA_PTR(pLayer->pDK,ptrDK);
	BLOB_DATA_PTR(pLayer->pDB,ptrDB);

	unsigned long numOut = pLayer->k->numSize;
	unsigned long numIn = pLayer->k->channelSize;
	unsigned long width = pLayer->k->widthSize;
	unsigned long height = pLayer->k->heightSize;
	
	for (int i_out = 0;i_out < numOut;i_out++) {
		for (int i_in = 0;i_in < numIn;i_in++) {
			for (int i_h = 0;i_h < height;i_h++) {
				for (int i_w = 0;i_w < width;i_w++) {
					ptrK[i_out][i_in][i_h][i_w] -=  alpha * \
						ptrDK[i_out][i_in][i_h][i_w];		
				}
			}
		}
	}


	unsigned long b_out = pLayer->b->channelSize;	
	for (int i_out = 0;i_out < b_out;i_out++) {
		ptrB[0][i_out][0][0] = alpha * \
			ptrDB[0][i_out][0][0];	
	}


#if defined(DEBUG)
	char filename[128];
	snprintf(filename,128,"%s_newk.txt",pLayer->para->name);
	pLayer->k->type = BLOB_TYPE_WEIGHT;
	LOG_writeBlobToTxtFile(filename,pLayer->k);

	snprintf(filename,128,"%s_newb.txt",pLayer->para->name);
	pLayer->b->type = BLOB_TYPE_BIAS;
	LOG_writeBlobToTxtFile(filename,pLayer->b);
#endif
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
testq %rdi, %rdi
je 0x324c
movslq %esi, %rax
movq 0x240(%rdi,%rax,8), %rdx
testq %rdx, %rdx
je 0x3268
movq 0x18(%rdx), %rax
xorl %ebx, %ebx
testq %rax, %rax
je 0x328e
movq 0x20(%rdx), %r8
testq %r8, %r8
je 0x328e
movq 0x30(%rdx), %rsi
testq %rsi, %rsi
je 0x328e
movq 0x38(%rdx), %rcx
testq %rcx, %rcx
je 0x328e
movq %rdx, 0x50(%rsp)
movsd 0x208(%rdi), %xmm0
movq 0x10(%rax), %rdi
movq 0x8(%r8), %rdx
movq %rdx, 0x48(%rsp)
movq 0x20(%r8), %rdx
movq %rdx, 0x18(%rsp)
movq %r8, 0x28(%rsp)
movq 0x28(%r8), %rdx
movq %rdx, 0x40(%rsp)
movq 0x8(%rcx), %rdx
movq %rdx, 0x38(%rsp)
movq 0x20(%rcx), %rdx
movq %rdx, 0x20(%rsp)
movq 0x28(%rcx), %rcx
movq %rcx, 0x30(%rsp)
movq %rdi, 0x68(%rsp)
testq %rdi, %rdi
je 0x3191
movq 0x18(%rax), %r11
movq 0x20(%rax), %rbx
movq 0x28(%rax), %r14
movq 0x8(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsi), %rax
movq %rax, 0x8(%rsp)
movq 0x28(%rsi), %rbp
movapd 0x5228(%rip), %xmm1     # 0x82f0
xorpd %xmm0, %xmm1
movq %r14, %rdi
imulq %rbx, %rdi
movq %rdi, %rax
imulq %r11, %rax
shlq $0x3, %rax
movq %rax, 0x60(%rsp)
shlq $0x3, %rdi
leaq (,%r14,8), %r8
movq 0x20(%rsi), %rcx
imulq %rbp, %rcx
movq 0x18(%rsi), %rax
imulq %rcx, %rax
shlq $0x3, %rax
movq %rax, 0x58(%rsp)
shlq $0x3, %rcx
shlq $0x3, %rbp
xorl %r10d, %r10d
testq %r11, %r11
je 0x3173
movq 0x8(%rsp), %r15
movq 0x10(%rsp), %r9
xorl %esi, %esi
testq %rbx, %rbx
je 0x3165
movq %r15, %r13
movq %r9, %r12
xorl %edx, %edx
testq %r14, %r14
je 0x3157
xorl %eax, %eax
movsd (%r13,%rax,8), %xmm2
mulsd %xmm1, %xmm2
addsd (%r12,%rax,8), %xmm2
movsd %xmm2, (%r12,%rax,8)
incq %rax
cmpq %rax, %r14
jne 0x3138
incq %rdx
addq %r8, %r12
addq %rbp, %r13
cmpq %rbx, %rdx
jne 0x3131
incq %rsi
addq %rdi, %r9
addq %rcx, %r15
cmpq %r11, %rsi
jne 0x3124
incq %r10
movq 0x60(%rsp), %rax
addq %rax, 0x10(%rsp)
movq 0x58(%rsp), %rax
addq %rax, 0x8(%rsp)
cmpq 0x68(%rsp), %r10
jne 0x3113
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rax
testq %rax, %rax
movq 0x48(%rsp), %rcx
movq 0x40(%rsp), %rdx
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdi
je 0x31de
imulq 0x18(%rsp), %rdx
shlq $0x3, %rdx
imulq 0x20(%rsp), %rdi
shlq $0x3, %rdi
movsd (%rsi), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%rcx)
addq %rdx, %rcx
addq %rdi, %rsi
decq %rax
jne 0x31c7
movq 0x50(%rsp), %r15
movq 0x8(%r15), %rcx
addq $0x44, %rcx
leaq 0x52cc(%rip), %rdx      # 0x84be
xorl %ebx, %ebx
leaq 0x70(%rsp), %r14
movl $0x80, %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x1140
movq 0x18(%r15), %rsi
movl $0x1, (%rsi)
movq %r14, %rdi
callq 0x3d39
movq 0x8(%r15), %rcx
addq $0x44, %rcx
leaq 0x52a1(%rip), %rdx      # 0x84ca
movl $0x80, %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x1140
movq 0x20(%r15), %rsi
movl $0x2, (%rsi)
movq %r14, %rdi
callq 0x3d39
jmp 0x328e
leaq 0x4df2(%rip), %rdi      # 0x8045
leaq 0x51d3(%rip), %rsi      # 0x842d
leaq 0x5253(%rip), %rdx      # 0x84b4
movl $0x48, %ecx
jmp 0x3282
leaq 0x4dd6(%rip), %rdi      # 0x8045
leaq 0x51b7(%rip), %rsi      # 0x842d
leaq 0x5237(%rip), %rdx      # 0x84b4
movl $0x4b, %ecx
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq