// File: /workspace/github_cpp_makefile_star2_O3/thunlp[P]KB2E/TransR/Train_TransR.cpp
// Function: Train::bfgs()
void bfgs()
    {
        res=0;
        int nbatches=100;
        int nepoch = 1000;
        int batchsize = fb_h.size()/nbatches;
        relation_tmp=relation_vec;
        entity_tmp = entity_vec;
        A_tmp = A;
            for (int epoch=0; epoch<nepoch; epoch++)
            {

            	res=0;
             	for (int batch = 0; batch<nbatches; batch++)
             	{
             		for (int k=0; k<batchsize; k++)
             		{
             			int i=rand_max(fb_h.size());
						int j=rand_max(entity_num);
						double pr = 1000*right_mean[fb_r[i]]/(right_mean[fb_r[i]]+left_mean[fb_r[i]]);
						if (method ==0)
                            pr = 500;
						if (rand()%1000<pr)
						{

							 	while (ok[make_pair(fb_h[i],fb_r[i])].count(j)>0)
									j=rand_max(entity_num);
								train_kb(fb_h[i],fb_l[i],fb_r[i],fb_h[i],j,fb_r[i]);

						}
						else
						{
							while (ok[make_pair(j,fb_r[i])].count(fb_l[i])>0)
								j=rand_max(entity_num);
						 	train_kb(fb_h[i],fb_l[i],fb_r[i],j,fb_l[i],fb_r[i]);
                        }
                		norm(relation_tmp[fb_r[i]]);
                		norm(entity_tmp[fb_h[i]]);
						norm(entity_tmp[fb_l[i]]);
						norm(entity_tmp[j]);
						norm(entity_tmp[fb_h[i]],A_tmp[fb_r[i]]);
						norm(entity_tmp[fb_l[i]],A_tmp[fb_r[i]]);
						norm(entity_tmp[j],A_tmp[fb_r[i]]);
             		}
		            relation_vec = relation_tmp;
		            entity_vec = entity_tmp;
		            A = A_tmp;
             	}
                cout<<"epoch:"<<epoch<<' '<<res<<endl;
                FILE* f1 = fopen(("relation2vec."+version).c_str(),"w");
                FILE* f2 = fopen(("entity2vec."+version).c_str(),"w");
                FILE* f3 = fopen(("A."+version).c_str(),"w");
                for (int i=0; i<relation_num; i++)
                {
                    for (int ii=0; ii<m; ii++)
                        fprintf(f1,"%.6lf\t",relation_vec[i][ii]);
                    fprintf(f1,"\n");
                }
                for (int i=0; i<entity_num; i++)
                {
                    for (int ii=0; ii<n; ii++)
                        fprintf(f2,"%.6lf\t",entity_vec[i][ii]);
                    fprintf(f2,"\n");
                }
                for (int i=0; i<relation_num; i++)
                for (int jj=0; jj<n; jj++)
                {
                    for (int ii=0; ii<m; ii++)
                    {
                        fprintf(f3,"%.6lf\t",A[i][jj][ii]);
                    }
                    fprintf(f3,"\n");
                }
                fclose(f1);
                fclose(f2);
                fclose(f3);
            }
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
movq $0x0, 0x40(%rdi)
movq 0x60(%rdi), %rax
subq 0x58(%rdi), %rax
sarq $0x2, %rax
shrq $0x2, %rax
movabsq $0x28f5c28f5c28f5c3, %rcx # imm = 0x28F5C28F5C28F5C3
mulq %rcx
shrq $0x2, %rdx
movq %rdx, 0x48(%rsp)
leaq 0xa0(%rdi), %rsi
addq $0xd0, %rdi
movq %rdi, 0x70(%rsp)
movq %rsi, 0x78(%rsp)
callq 0x535c
leaq 0xb8(%rbx), %rsi
leaq 0xe8(%rbx), %rdi
movq %rdi, 0x60(%rsp)
movq %rsi, 0x68(%rsp)
callq 0x535c
leaq 0x100(%rbx), %rsi
leaq 0x118(%rbx), %rdi
movq %rdi, 0x50(%rsp)
movq %rsi, 0x58(%rsp)
callq 0x54fa
movq $0x0, 0x38(%rsp)
leaq 0x18(%rsp), %r13
movq $0x0, 0x40(%rbx)
xorl %eax, %eax
movl %eax, 0x44(%rsp)
cmpl $0x0, 0x48(%rsp)
jle 0x4ad4
xorl %r15d, %r15d
movl %r15d, 0x10(%rsp)
movq 0x60(%rbx), %rsi
subq 0x58(%rbx), %rsi
shrq $0x2, %rsi
movq %rbx, %rdi
callq 0x567c
movl %eax, %r14d
movl 0x387c5(%rip), %esi     # 0x3cf34
movq %rbx, %rdi
callq 0x567c
movl %eax, %ebp
movslq %r14d, %r12
leaq (,%r12,4), %r14
movq 0x88(%rbx), %rsi
addq %r14, %rsi
leaq 0x38923(%rip), %r15     # 0x3d0b8
movq %r15, %rdi
callq 0x3a94
movsd (%rax), %xmm0
mulsd 0x388f(%rip), %xmm0     # 0x8038
movsd %xmm0, 0x8(%rsp)
movq 0x88(%rbx), %rsi
addq %r14, %rsi
movq %r15, %rdi
callq 0x3a94
movsd (%rax), %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0x88(%rbx), %rsi
addq %r14, %rsi
leaq 0x388a9(%rip), %rdi     # 0x3d088
callq 0x3a94
cmpl $0x0, 0x38(%rbx)
movsd 0x3850(%rip), %xmm0     # 0x8040
je 0x4809
movsd 0x80(%rsp), %xmm1
addsd (%rax), %xmm1
movsd 0x8(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x2080
cltq
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x26, %rcx
addl %edx, %ecx
imull $0x3e8, %ecx, %ecx      # imm = 0x3E8
subl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x48fd
movq 0x58(%rbx), %rax
movq 0x88(%rbx), %rcx
movl (%rax,%r12,4), %eax
movl (%rcx,%r12,4), %ecx
shlq $0x20, %rcx
orq %rax, %rcx
movq %rcx, 0x18(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x41e4
movq 0x10(%rax), %rcx
testq %rcx, %rcx
je 0x497a
movl 0x10(%rsp), %r15d
addq $0x8, %rax
movq %rax, %rdx
xorl %esi, %esi
cmpl %ebp, 0x20(%rcx)
setl %sil
cmovgeq %rcx, %rdx
movq 0x10(%rcx,%rsi,8), %rcx
testq %rcx, %rcx
jne 0x488c
cmpq %rax, %rdx
je 0x497f
cmpl 0x20(%rdx), %ebp
jl 0x497f
movl 0x38679(%rip), %esi     # 0x3cf34
movq %rbx, %rdi
callq 0x567c
movl %eax, %ebp
movq 0x58(%rbx), %rax
movq 0x88(%rbx), %rcx
movl (%rax,%r12,4), %eax
movl (%rcx,%r12,4), %ecx
shlq $0x20, %rcx
orq %rax, %rcx
movq %rcx, 0x18(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x41e4
movq 0x10(%rax), %rcx
testq %rcx, %rcx
jne 0x4885
jmp 0x497f
movq 0x88(%rbx), %rax
movl (%rax,%r12,4), %eax
shlq $0x20, %rax
movl %ebp, %ecx
orq %rax, %rcx
movq %rcx, 0x18(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x41e4
movq 0x70(%rbx), %rsi
addq %r14, %rsi
movq %rax, %rdi
callq 0x56c0
testq %rax, %rax
je 0x4947
movl 0x385f9(%rip), %esi     # 0x3cf34
movq %rbx, %rdi
callq 0x567c
movl %eax, %ebp
jmp 0x48fd
movq 0x58(%rbx), %rax
movq 0x70(%rbx), %rcx
movl (%rax,%r12,4), %esi
movl (%rcx,%r12,4), %edx
movq 0x88(%rbx), %rax
movl (%rax,%r12,4), %ecx
movl %ecx, (%rsp)
movq %rbx, %rdi
movl %ebp, %r8d
movl %edx, %r9d
callq 0x5708
movl 0x10(%rsp), %r15d
jmp 0x49ab
movl 0x10(%rsp), %r15d
movq 0x58(%rbx), %rax
movq 0x70(%rbx), %rcx
movl (%rax,%r12,4), %esi
movl (%rcx,%r12,4), %edx
movq 0x88(%rbx), %rax
movl (%rax,%r12,4), %ecx
movl %ecx, (%rsp)
movq %rbx, %rdi
movl %esi, %r8d
movl %ebp, %r9d
callq 0x5708
movq 0x88(%rbx), %rax
movslq (%rax,%r12,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0xd0(%rbx), %rsi
movq %rbx, %rdi
callq 0x4602
movq 0x58(%rbx), %rax
movslq (%rax,%r12,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0xe8(%rbx), %rsi
movq %rbx, %rdi
callq 0x4602
movq 0x70(%rbx), %rax
movslq (%rax,%r12,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0xe8(%rbx), %rsi
movq %rbx, %rdi
callq 0x4602
movslq %ebp, %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %r14
movq 0xe8(%rbx), %rsi
addq %r14, %rsi
movq %rbx, %rdi
callq 0x4602
movq 0x58(%rbx), %rax
movq 0x88(%rbx), %rcx
movslq (%rax,%r12,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0xe8(%rbx), %rsi
movslq (%rcx,%r12,4), %rax
leaq (%rax,%rax,2), %rdx
shlq $0x3, %rdx
addq 0x118(%rbx), %rdx
movq %rbx, %rdi
callq 0x57d8
movq 0x70(%rbx), %rax
movq 0x88(%rbx), %rcx
movslq (%rax,%r12,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0xe8(%rbx), %rsi
movslq (%rcx,%r12,4), %rax
leaq (%rax,%rax,2), %rdx
shlq $0x3, %rdx
addq 0x118(%rbx), %rdx
movq %rbx, %rdi
callq 0x57d8
addq 0xe8(%rbx), %r14
movq 0x88(%rbx), %rax
movslq (%rax,%r12,4), %rax
leaq (%rax,%rax,2), %rdx
shlq $0x3, %rdx
addq 0x118(%rbx), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x57d8
incl %r15d
cmpl 0x48(%rsp), %r15d
jne 0x474d
movq 0x78(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x535c
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x535c
movq 0x58(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x54fa
movl 0x44(%rsp), %eax
incl %eax
cmpl $0x64, %eax
jne 0x473b
movl $0x6, %edx
movq 0x74bc(%rip), %r14      # 0xbfd8
movq %r14, %rdi
leaq 0x36af(%rip), %rsi      # 0x81d5
callq 0x21f0
movq %r14, %rdi
movq 0x38(%rsp), %rsi
callq 0x22d0
movb $0x20, 0x18(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21f0
movsd 0x40(%rbx), %xmm0
movq %rax, %rdi
callq 0x22a0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
movq %r13, %rdi
leaq 0x364e(%rip), %rsi      # 0x81dc
leaq 0x7633(%rip), %r14      # 0xc1c8
movq %r14, %rdx
callq 0x58f7
movq 0x18(%rsp), %rdi
leaq 0x3641(%rip), %rsi      # 0x81ea
callq 0x2200
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %r12
cmpq %r12, %rdi
je 0x4bcf
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21d0
movq %r13, %rdi
leaq 0x3613(%rip), %rsi      # 0x81ec
movq %r14, %rdx
callq 0x58f7
movq 0x18(%rsp), %rdi
leaq 0x35fd(%rip), %rsi      # 0x81ea
callq 0x2200
movq %rax, 0x8(%rsp)
movq 0x18(%rsp), %rdi
cmpq %r12, %rdi
leaq 0x35f5(%rip), %r15      # 0x81fb
je 0x4c15
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21d0
movq %r13, %rdi
leaq 0x35d9(%rip), %rsi      # 0x81f8
movq %r14, %rdx
callq 0x58f7
movq 0x18(%rsp), %rdi
leaq 0x35b7(%rip), %rsi      # 0x81ea
callq 0x2200
movq %rax, %r14
movq 0x18(%rsp), %rdi
cmpq %r12, %rdi
je 0x4c52
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21d0
movl 0x382d8(%rip), %eax     # 0x3cf30
testl %eax, %eax
jle 0x4cbf
movq %r13, %rbp
xorl %r12d, %r12d
cmpl $0x0, 0x34(%rbx)
movq 0x10(%rsp), %rsi
jle 0x4ca3
xorl %r13d, %r13d
movq 0xa0(%rbx), %rax
leaq (%r12,%r12,2), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r13,8), %xmm0
movq %rsi, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x2240
movq 0x10(%rsp), %rsi
incq %r13
movslq 0x34(%rbx), %rax
cmpq %rax, %r13
jl 0x4c70
movl $0xa, %edi
callq 0x2220
incq %r12
movslq 0x38279(%rip), %rax     # 0x3cf30
cmpq %rax, %r12
jl 0x4c62
movq %rbp, %r13
cmpl $0x0, 0x3826e(%rip)     # 0x3cf34
jle 0x4d34
movq %r13, %rbp
xorl %r12d, %r12d
cmpl $0x0, 0x30(%rbx)
movq 0x8(%rsp), %rdx
jle 0x4d0f
xorl %r13d, %r13d
movq 0xb8(%rbx), %rax
leaq (%r12,%r12,2), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r13,8), %xmm0
movq %rdx, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x2240
movq 0x8(%rsp), %rdx
incq %r13
movslq 0x30(%rbx), %rax
cmpq %rax, %r13
jl 0x4cdc
movl $0xa, %edi
movq %rdx, %rsi
callq 0x2220
incq %r12
movslq 0x3820e(%rip), %rax     # 0x3cf34
cmpq %rax, %r12
jl 0x4cce
movl 0x381ff(%rip), %eax     # 0x3cf30
movq %rbp, %r13
testl %eax, %eax
jle 0x4dc8
movl 0x30(%rbx), %ecx
xorl %r12d, %r12d
testl %ecx, %ecx
jle 0x4db9
xorl %ebp, %ebp
cmpl $0x0, 0x34(%rbx)
leaq 0x34a8(%rip), %r15      # 0x81fb
jle 0x4d95
xorl %r13d, %r13d
movq 0x100(%rbx), %rax
leaq (%r12,%r12,2), %rcx
movq (%rax,%rcx,8), %rax
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r13,8), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x2240
incq %r13
movslq 0x34(%rbx), %rax
cmpq %rax, %r13
jl 0x4d58
movl $0xa, %edi
movq %r14, %rsi
callq 0x2220
incq %rbp
movslq 0x30(%rbx), %rcx
cmpq %rcx, %rbp
jl 0x4d48
movl 0x3817c(%rip), %eax     # 0x3cf30
leaq 0x18(%rsp), %r13
incq %r12
movslq %eax, %rdx
cmpq %rdx, %r12
jl 0x4d42
movq 0x10(%rsp), %rdi
callq 0x21b0
movq 0x8(%rsp), %rdi
callq 0x21b0
movq %r14, %rdi
callq 0x21b0
movq 0x38(%rsp), %rax
incl %eax
movq %rax, 0x38(%rsp)
cmpl $0x3e8, %eax            # imm = 0x3E8
jne 0x4731
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop