// File: /workspace/github_cpp_makefile_star2_O3/thunlp[P]KB2E/CTransR/Train_CTransR.cpp
// Function: Train::bfgs()
void bfgs()
    {
        res=0;
        int nbatches=100;
        int neval = 1000;
        int batchsize = fb_h.size()/nbatches;
        relation_tmp=relation_vec;
        entity_tmp = entity_vec;
        A_tmp = A;
        fb_relation_tmp = fb_relation_vec;
        fb_A_tmp = fb_A;

        for (int eval=0; eval<neval; eval++)
        {

            res=0;
            for (int batch = 0; batch<nbatches; batch++)
            {
                //cout<<batch<<endl;
                for (int kk=0; kk<batchsize; kk++)
                {
                    int i=rand_max(fb_h.size());
                    int j=rand_max(entity_num);
                    int e1 = fb_h[i];
                    int e2 = fb_l[i];
                    int rel = fb_r[i];
                    int fb_rel = cluster2fb[rel];
                    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(e1,rel)].count(j)>0||ok_fb[make_pair(e1,fb_rel)].count(j)>0)
                            j=rand_max(entity_num);
                        train_kb(e1,fb_l[i],rel,e1,j,rel);
                    }
                    else
                    {
                        while (ok[make_pair(j,rel)].count(fb_l[i])>0||ok_fb[make_pair(j,fb_rel)].count(fb_l[i])>0)
                            j=rand_max(entity_num);
                            train_kb(e1,fb_l[i],rel,j,fb_l[i],rel);
                    }
                    same_rel(rel);
                    norm(relation_tmp[rel]);
                    norm(fb_relation_tmp[fb_rel]);
                    norm(entity_tmp[e1]);
                    norm(entity_tmp[fb_l[i]]);
                    norm(entity_tmp[j]);
                    norm(entity_tmp[e1],A_tmp[fb_rel]);
                    norm(entity_tmp[fb_l[i]],A_tmp[fb_rel]);
                    norm(entity_tmp[j],A_tmp[fb_rel]);

                }
                relation_vec = relation_tmp;
                entity_vec = entity_tmp;
                A = A_tmp;
                fb_relation_tmp = fb_relation_vec;
                fb_A_tmp = fb_A;
            }
            cout<<"eval:"<<eval<<' '<<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<fb_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);


            FILE* f4 = fopen(("fb_relation2vec."+version).c_str(),"w");
            for (int i=0; i<fb_relation_num; i++)
            {
                for (int ii=0; ii<m; ii++)
                    fprintf(f4,"%.6lf\t",fb_relation_vec[i][ii]);
                fprintf(f4,"\n");
            }
            fclose(f4);
        }
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdi, %rbx
movq $0x0, 0x78(%rdi)
movq 0x98(%rdi), %rax
subq 0x90(%rdi), %rax
sarq $0x2, %rax
shrq $0x2, %rax
movabsq $0x28f5c28f5c28f5c3, %rcx # imm = 0x28F5C28F5C28F5C3
mulq %rcx
movq %rdx, %r14
shrq $0x2, %r14
leaq 0xd8(%rdi), %rsi
addq $0x120, %rdi            # imm = 0x120
movq %rdi, 0xc0(%rsp)
movq %rsi, 0xc8(%rsp)
callq 0x5b88
leaq 0xf0(%rbx), %rsi
leaq 0x138(%rbx), %rdi
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xb8(%rsp)
callq 0x5b88
leaq 0x168(%rbx), %rsi
leaq 0x180(%rbx), %rdi
movq %rdi, 0xa0(%rsp)
movq %rsi, 0xa8(%rsp)
callq 0x5d26
leaq 0x108(%rbx), %rsi
leaq 0x150(%rbx), %rdi
movq %rdi, 0x90(%rsp)
movq %rsi, 0x98(%rsp)
callq 0x5b88
leaq 0x198(%rbx), %rsi
leaq 0x1b0(%rbx), %rdi
movq %rdi, 0x80(%rsp)
movq %rsi, 0x88(%rsp)
callq 0x5d26
leaq 0x30(%rbx), %rax
movq %rax, 0x78(%rsp)
movq $0x0, 0x50(%rsp)
movq %r14, 0x70(%rsp)
movq $0x0, 0x78(%rbx)
xorl %eax, %eax
movl %eax, 0x6c(%rsp)
testl %r14d, %r14d
jle 0x520a
xorl %r15d, %r15d
movl %r15d, 0x24(%rsp)
movq 0x98(%rbx), %rsi
subq 0x90(%rbx), %rsi
shrq $0x2, %rsi
movq %rbx, %rdi
callq 0x5ea8
movl %eax, %ebp
movl 0x38107(%rip), %esi     # 0x3cf44
movq %rbx, %rdi
callq 0x5ea8
movl %eax, %r14d
movslq %ebp, %r15
movq 0x90(%rbx), %rax
leaq (,%r15,4), %rbp
movl (%rax,%r15,4), %r12d
movq 0xc0(%rbx), %rax
movl (%rax,%r15,4), %eax
movl %eax, 0x14(%rsp)
leaq 0x380dc(%rip), %rdi     # 0x3cf50
leaq 0x14(%rsp), %rsi
callq 0x3c96
movl (%rax), %eax
movq %rax, 0x18(%rsp)
movq 0xc0(%rbx), %rsi
addq %rbp, %rsi
leaq 0x3826a(%rip), %r13     # 0x3d100
movq %r13, %rdi
callq 0x3e80
movsd (%rax), %xmm0
mulsd 0x3196(%rip), %xmm0     # 0x8040
movsd %xmm0, 0x58(%rsp)
movq 0xc0(%rbx), %rsi
addq %rbp, %rsi
movq %r13, %rdi
callq 0x3e80
movsd (%rax), %xmm0
movsd %xmm0, 0xd0(%rsp)
movq 0xc0(%rbx), %rsi
addq %rbp, %rsi
leaq 0x381f0(%rip), %rdi     # 0x3d0d0
callq 0x3e80
cmpl $0x0, 0x70(%rbx)
movsd 0x3157(%rip), %xmm0     # 0x8048
je 0x4f0a
movsd 0xd0(%rsp), %xmm1
addsd (%rax), %xmm1
movsd 0x58(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x58(%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 0x58(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movq %r15, 0x48(%rsp)
jbe 0x5014
movq 0x18(%rsp), %rbp
shlq $0x20, %rbp
orq %r12, %rbp
movl 0x14(%rsp), %eax
shlq $0x20, %rax
orq %r12, %rax
movq %rax, 0x28(%rsp)
movq %rbx, %rdi
leaq 0x28(%rsp), %rsi
callq 0x470e
movq 0x10(%rax), %rcx
testq %rcx, %rcx
je 0x4fab
addq $0x8, %rax
movq %rax, %rdx
xorl %esi, %esi
cmpl %r14d, 0x20(%rcx)
setl %sil
cmovgeq %rcx, %rdx
movq 0x10(%rcx,%rsi,8), %rcx
testq %rcx, %rcx
jne 0x4f88
cmpq %rax, %rdx
je 0x4fab
cmpl 0x20(%rdx), %r14d
jge 0x4ffe
movq %rbp, 0x60(%rsp)
movq 0x78(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x470e
movq 0x10(%rax), %rcx
testq %rcx, %rcx
je 0x5095
addq $0x8, %rax
movq %rax, %rdx
xorl %esi, %esi
cmpl %r14d, 0x20(%rcx)
setl %sil
cmovgeq %rcx, %rdx
movq 0x10(%rcx,%rsi,8), %rcx
testq %rcx, %rcx
jne 0x4fd3
cmpq %rax, %rdx
je 0x5095
cmpl 0x20(%rdx), %r14d
jl 0x5095
movl 0x37f40(%rip), %esi     # 0x3cf44
movq %rbx, %rdi
callq 0x5ea8
movl %eax, %r14d
jmp 0x4f5b
movq 0x18(%rsp), %r13
shlq $0x20, %r13
movl 0x14(%rsp), %eax
shlq $0x20, %rax
movl %r14d, %r15d
orq %r15, %rax
movq %rax, 0x28(%rsp)
movq %rbx, %rdi
leaq 0x28(%rsp), %rsi
callq 0x470e
movq 0xa8(%rbx), %rsi
addq %rbp, %rsi
movq %rax, %rdi
callq 0x3cfc
testq %rax, %rax
jne 0x5082
orq %r13, %r15
movq %r15, 0x60(%rsp)
movq 0x78(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x470e
movq 0xa8(%rbx), %rsi
addq %rbp, %rsi
movq %rax, %rdi
callq 0x3cfc
testq %rax, %rax
je 0x50b5
movl 0x37ebc(%rip), %esi     # 0x3cf44
movq %rbx, %rdi
callq 0x5ea8
movl %eax, %r14d
jmp 0x501d
movq 0xa8(%rbx), %rax
movl (%rax,%r15,4), %edx
movl 0x14(%rsp), %ecx
movl %ecx, (%rsp)
movq %rbx, %rdi
movl %r12d, %esi
movl %r12d, %r8d
movl %r14d, %r9d
jmp 0x50d7
movq 0xa8(%rbx), %rax
movq 0x48(%rsp), %rcx
movl (%rax,%rcx,4), %edx
movl 0x14(%rsp), %ecx
movl %ecx, (%rsp)
movq %rbx, %rdi
movl %r12d, %esi
movl %r14d, %r8d
movl %edx, %r9d
callq 0x5eec
movl 0x24(%rsp), %r15d
movslq %r12d, %r13
xorps %xmm0, %xmm0
cvtsi2sdl 0x14(%rsp), %xmm0
movq %rbx, %rdi
callq 0x5fa8
movslq 0x14(%rsp), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0x120(%rbx), %rsi
movq %rbx, %rdi
callq 0x4c68
movslq 0x18(%rsp), %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %r12
movq 0x150(%rbx), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x4c68
shlq $0x3, %r13
leaq (,%r13,2), %rbp
addq %r13, %rbp
movq 0x138(%rbx), %rsi
addq %rbp, %rsi
movq %rbx, %rdi
callq 0x4c68
movq 0xa8(%rbx), %rax
movq 0x48(%rsp), %r13
movslq (%rax,%r13,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0x138(%rbx), %rsi
movq %rbx, %rdi
callq 0x4c68
movslq %r14d, %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %r14
movq 0x138(%rbx), %rsi
addq %r14, %rsi
movq %rbx, %rdi
callq 0x4c68
addq 0x138(%rbx), %rbp
movq 0x180(%rbx), %rdx
addq %r12, %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x61f0
movq 0xa8(%rbx), %rax
movslq (%rax,%r13,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0x138(%rbx), %rsi
movq 0x180(%rbx), %rdx
addq %r12, %rdx
movq %rbx, %rdi
callq 0x61f0
addq 0x138(%rbx), %r14
addq 0x180(%rbx), %r12
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x61f0
incl %r15d
movq 0x70(%rsp), %r14
cmpl %r14d, %r15d
jne 0x4e16
movq 0xc8(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x5b88
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x5b88
movq 0xa8(%rsp), %rdi
movq 0xa0(%rsp), %rsi
callq 0x5d26
movq 0x90(%rsp), %rdi
movq 0x98(%rsp), %rsi
callq 0x5b88
movq 0x80(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x5d26
movl 0x6c(%rsp), %eax
incl %eax
cmpl $0x64, %eax
jne 0x4e06
movl $0x5, %edx
movq 0x6d4a(%rip), %r14      # 0xbfd8
movq %r14, %rdi
leaq 0x2fa2(%rip), %rsi      # 0x823a
callq 0x2200
movq %r14, %rdi
movq 0x50(%rsp), %rsi
callq 0x22e0
movb $0x20, 0x28(%rsp)
movl $0x1, %edx
movq %rax, %rdi
leaq 0x28(%rsp), %r15
movq %r15, %rsi
callq 0x2200
movsd 0x78(%rbx), %xmm0
movq %rax, %rdi
callq 0x22b0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2120
movq %r15, %rdi
leaq 0x2f56(%rip), %rsi      # 0x825b
leaq 0x6ecc(%rip), %r14      # 0xc1d8
movq %r14, %rdx
callq 0x6315
movq 0x28(%rsp), %rdi
leaq 0x2f20(%rip), %r13      # 0x8240
movq %r13, %rsi
callq 0x2210
movq %rax, %r12
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %r15
cmpq %r15, %rdi
je 0x5347
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x28(%rsp), %rdi
leaq 0x2eef(%rip), %rsi      # 0x8242
movq %r14, %rdx
callq 0x6315
movq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x2210
movq %rax, 0x18(%rsp)
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
leaq 0x2ed5(%rip), %rbp      # 0x8251
je 0x538b
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x28(%rsp), %rdi
leaq 0x2eb7(%rip), %rsi      # 0x824e
movq %r14, %rdx
callq 0x6315
movq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x2210
movq %rax, %r14
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x53c6
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21e0
cmpl $0x0, 0x37b73(%rip)     # 0x3cf40
jle 0x542c
xorl %r13d, %r13d
cmpl $0x0, 0x6c(%rbx)
jle 0x5410
xorl %r15d, %r15d
movq 0xd8(%rbx), %rax
leaq (,%r13,2), %rcx
addq %r13, %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r15,8), %xmm0
movq %r12, %rdi
movq %rbp, %rsi
movb $0x1, %al
callq 0x2250
incq %r15
movslq 0x6c(%rbx), %rax
cmpq %rax, %r15
jl 0x53db
movl $0xa, %edi
movq %r12, %rsi
callq 0x2230
incq %r13
movslq 0x37b19(%rip), %rax     # 0x3cf40
cmpq %rax, %r13
jl 0x53d2
cmpl $0x0, 0x37b11(%rip)     # 0x3cf44
jle 0x549c
xorl %r13d, %r13d
cmpl $0x0, 0x68(%rbx)
movq 0x18(%rsp), %rdx
jle 0x5480
xorl %r15d, %r15d
movq 0xf0(%rbx), %rax
leaq (,%r13,2), %rcx
addq %r13, %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r15,8), %xmm0
movq %rdx, %rdi
movq %rbp, %rsi
movb $0x1, %al
callq 0x2250
movq 0x18(%rsp), %rdx
incq %r15
movslq 0x68(%rbx), %rax
cmpq %rax, %r15
jl 0x5446
movl $0xa, %edi
movq %rdx, %rsi
callq 0x2230
incq %r13
movslq 0x37aad(%rip), %rax     # 0x3cf44
cmpq %rax, %r13
jl 0x5438
movl 0x37aa6(%rip), %ecx     # 0x3cf48
testl %ecx, %ecx
jle 0x553c
movl 0x68(%rbx), %eax
xorl %ebp, %ebp
movq %r12, 0x48(%rsp)
testl %eax, %eax
jle 0x552d
xorl %r13d, %r13d
cmpl $0x0, 0x6c(%rbx)
leaq 0x2d8b(%rip), %r12      # 0x8251
jle 0x5509
xorl %r15d, %r15d
movq 0x168(%rbx), %rax
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
movq (%rax,%rcx,8), %rax
leaq (%r13,%r13,2), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r15,8), %xmm0
movq %r14, %rdi
movq %r12, %rsi
movb $0x1, %al
callq 0x2250
incq %r15
movslq 0x6c(%rbx), %rax
cmpq %rax, %r15
jl 0x54cb
movl $0xa, %edi
movq %r14, %rsi
callq 0x2230
incq %r13
movslq 0x68(%rbx), %rax
cmpq %rax, %r13
jl 0x54bb
movl 0x37a20(%rip), %ecx     # 0x3cf48
movq 0x48(%rsp), %r12
incq %rbp
movslq %ecx, %rdx
cmpq %rdx, %rbp
jl 0x54b4
movq %r12, %rdi
callq 0x21c0
movq 0x18(%rsp), %rdi
callq 0x21c0
movq %r14, %rdi
callq 0x21c0
leaq 0x28(%rsp), %rdi
leaq 0x2cf6(%rip), %rsi      # 0x8258
leaq 0x6c6f(%rip), %rdx      # 0xc1d8
callq 0x6315
movq 0x28(%rsp), %rdi
leaq 0x2cc6(%rip), %rsi      # 0x8240
callq 0x2210
movq %rax, %r14
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x559e
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21e0
cmpl $0x0, 0x379a3(%rip)     # 0x3cf48
leaq 0x2ca5(%rip), %r13      # 0x8251
jle 0x5604
xorl %r12d, %r12d
cmpl $0x0, 0x6c(%rbx)
jle 0x55e8
xorl %r15d, %r15d
movq 0x108(%rbx), %rax
leaq (%r12,%r12,2), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r15,8), %xmm0
movq %r14, %rdi
movq %r13, %rsi
movb $0x1, %al
callq 0x2250
incq %r15
movslq 0x6c(%rbx), %rax
cmpq %rax, %r15
jl 0x55ba
movl $0xa, %edi
movq %r14, %rsi
callq 0x2230
incq %r12
movslq 0x37949(%rip), %rax     # 0x3cf48
cmpq %rax, %r12
jl 0x55b1
movq %r14, %rdi
callq 0x21c0
movq 0x50(%rsp), %rax
incl %eax
movq %rax, 0x50(%rsp)
cmpl $0x3e8, %eax            # imm = 0x3E8
movq 0x70(%rsp), %r14
jne 0x4dfc
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq