// File: /workspace/github_cpp_makefile_star2_O2/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 $0xc8, %rsp
movq %rdi, %rbx
andq $0x0, 0x78(%rdi)
movq 0x98(%rdi), %rax
subq 0x90(%rdi), %rax
sarq $0x2, %rax
pushq $0x64
popq %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %r14
leaq 0xd8(%rdi), %rsi
addq $0x120, %rdi            # imm = 0x120
movq %rdi, 0x90(%rsp)
movq %rsi, 0x58(%rsp)
callq 0x54a0
leaq 0xf0(%rbx), %rsi
leaq 0x138(%rbx), %rdi
movq %rdi, 0x88(%rsp)
movq %rsi, 0x50(%rsp)
callq 0x54a0
leaq 0x168(%rbx), %rsi
leaq 0x180(%rbx), %rdi
movq %rdi, 0x80(%rsp)
movq %rsi, 0x60(%rsp)
callq 0x557e
leaq 0x108(%rbx), %rsi
leaq 0x150(%rbx), %rdi
movq %rdi, 0x78(%rsp)
movq %rsi, 0x48(%rsp)
callq 0x54a0
leaq 0x198(%rbx), %rsi
leaq 0x1b0(%rbx), %rdi
movq %rdi, 0x68(%rsp)
movq %rsi, 0x70(%rsp)
callq 0x557e
xorl %eax, %eax
leaq 0x30(%rbx), %r15
testl %r14d, %r14d
cmovlel %eax, %r14d
movq %r14, 0x98(%rsp)
leaq 0xa8(%rsp), %r12
movq %r15, 0x18(%rsp)
cmpl $0x3e8, %eax            # imm = 0x3E8
je 0x4e51
movq %rax, 0x40(%rsp)
andq $0x0, 0x78(%rbx)
xorl %eax, %eax
cmpl $0x64, %eax
je 0x4b7d
movl %eax, 0x38(%rsp)
xorl %eax, %eax
cmpl 0x98(%rsp), %eax
je 0x4b1e
movl %eax, 0x3c(%rsp)
movq 0x98(%rbx), %rsi
subq 0x90(%rbx), %rsi
shrq $0x2, %rsi
movq %rbx, %rdi
callq 0x565c
movl %eax, %ebp
movl 0x38763(%rip), %esi     # 0x3cf44
movq %rbx, %rdi
callq 0x565c
movl %eax, 0x8(%rsp)
movslq %ebp, %rcx
movq 0x90(%rbx), %rax
leaq (,%rcx,4), %r13
movl (%rax,%rcx,4), %ebp
movq 0xc0(%rbx), %rax
movq %rcx, 0x10(%rsp)
movl (%rax,%rcx,4), %eax
movl %eax, 0xc(%rsp)
leaq 0x38734(%rip), %rdi     # 0x3cf50
leaq 0xc(%rsp), %rsi
callq 0x3736
movslq (%rax), %rax
movq %rax, 0x20(%rsp)
movq 0xc0(%rbx), %rsi
addq %r13, %rsi
leaq 0x388c1(%rip), %r14     # 0x3d100
movq %r14, %rdi
callq 0x386e
movsd (%rax), %xmm0
mulsd 0x37e5(%rip), %xmm0     # 0x8038
movsd %xmm0, 0x28(%rsp)
movq 0xc0(%rbx), %rsi
addq %r13, %rsi
movq %r14, %rdi
callq 0x386e
movsd (%rax), %xmm0
movsd %xmm0, 0xa0(%rsp)
movq 0xc0(%rbx), %rsi
addq %r13, %rsi
leaq 0x38847(%rip), %rdi     # 0x3d0d0
callq 0x386e
cmpl $0x0, 0x70(%rbx)
movsd 0x37a6(%rip), %xmm0     # 0x8040
je 0x48b3
movsd 0xa0(%rsp), %xmm1
addsd (%rax), %xmm1
movsd 0x28(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
callq 0x2070
cltd
movl $0x3e8, %ecx            # imm = 0x3E8
idivl %ecx
cvtsi2sd %edx, %xmm0
movq 0x20(%rsp), %r14
shlq $0x20, %r14
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x494f
orq %rbp, %r14
leaq 0x8(%rsp), %r13
movl 0xc(%rsp), %eax
shlq $0x20, %rax
orq %rbp, %rax
movq %rax, 0xa8(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x40d0
movq %rax, %rdi
movq %r13, %rsi
callq 0x3784
testq %rax, %rax
jne 0x493b
movq %r14, 0x30(%rsp)
movq %r15, %rdi
leaq 0x30(%rsp), %rsi
callq 0x40d0
movq %rax, %rdi
movq %r13, %rsi
callq 0x3784
testq %rax, %rax
je 0x49ce
movl 0x38603(%rip), %esi     # 0x3cf44
movq %rbx, %rdi
callq 0x565c
movl %eax, 0x8(%rsp)
jmp 0x48e7
movl 0x8(%rsp), %eax
movl 0xc(%rsp), %ecx
shlq $0x20, %rcx
movl %eax, %eax
orq %rcx, %rax
movq %rax, 0xa8(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x40d0
movq 0xa8(%rbx), %rsi
addq %r13, %rsi
movq %rax, %rdi
callq 0x3784
testq %rax, %rax
jne 0x49ba
movl 0x8(%rsp), %eax
orq %r14, %rax
movq %rax, 0x30(%rsp)
movq %r15, %rdi
leaq 0x30(%rsp), %rsi
callq 0x40d0
movq 0xa8(%rbx), %rsi
addq %r13, %rsi
movq %rax, %rdi
callq 0x3784
testq %rax, %rax
je 0x49f3
movl 0x38584(%rip), %esi     # 0x3cf44
movq %rbx, %rdi
callq 0x565c
movl %eax, 0x8(%rsp)
jmp 0x4953
movq 0xa8(%rbx), %rax
movq 0x10(%rsp), %rcx
movl (%rax,%rcx,4), %edx
movl 0xc(%rsp), %ecx
movl 0x8(%rsp), %r9d
movl %ecx, (%rsp)
movq %rbx, %rdi
movl %ebp, %esi
movl %ebp, %r8d
jmp 0x4a16
movq 0xa8(%rbx), %rax
movq 0x10(%rsp), %rcx
movl (%rax,%rcx,4), %edx
movl 0xc(%rsp), %ecx
movl 0x8(%rsp), %r8d
movl %ecx, (%rsp)
movq %rbx, %rdi
movl %ebp, %esi
movl %edx, %r9d
callq 0x56a0
movslq %ebp, %r14
cvtsi2sdl 0xc(%rsp), %xmm0
movq %rbx, %rdi
callq 0x5746
movslq 0xc(%rsp), %rax
imulq $0x18, %rax, %rsi
addq 0x120(%rbx), %rsi
movq %rbx, %rdi
callq 0x4656
imulq $0x18, 0x20(%rsp), %rbp
movq 0x150(%rbx), %rsi
addq %rbp, %rsi
movq %rbx, %rdi
callq 0x4656
imulq $0x18, %r14, %r13
movq 0x138(%rbx), %rsi
addq %r13, %rsi
movq %rbx, %rdi
callq 0x4656
movq 0xa8(%rbx), %rax
movq 0x10(%rsp), %r14
movslq (%rax,%r14,4), %rax
imulq $0x18, %rax, %rsi
addq 0x138(%rbx), %rsi
movq %rbx, %rdi
callq 0x4656
movslq 0x8(%rsp), %rax
imulq $0x18, %rax, %rsi
addq 0x138(%rbx), %rsi
movq %rbx, %rdi
callq 0x4656
addq 0x138(%rbx), %r13
movq 0x180(%rbx), %rdx
addq %rbp, %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5976
movq 0xa8(%rbx), %rax
movslq (%rax,%r14,4), %rax
imulq $0x18, %rax, %rsi
addq 0x138(%rbx), %rsi
movq 0x180(%rbx), %rdx
addq %rbp, %rdx
movq %rbx, %rdi
callq 0x5976
movslq 0x8(%rsp), %rax
imulq $0x18, %rax, %rsi
addq 0x138(%rbx), %rsi
addq 0x180(%rbx), %rbp
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x5976
movl 0x3c(%rsp), %eax
incl %eax
jmp 0x47ae
movq 0x58(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x54a0
movq 0x50(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x54a0
movq 0x60(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x557e
movq 0x78(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x54a0
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x557e
movl 0x38(%rsp), %eax
incl %eax
jmp 0x479f
movq 0x7454(%rip), %rdi      # 0xbfd8
leaq 0x369f(%rip), %rsi      # 0x822a
callq 0x2190
movq %rax, %rdi
movq 0x40(%rsp), %rsi
callq 0x22c0
movq %rax, %rdi
pushq $0x20
popq %rsi
callq 0x21e0
movsd 0x78(%rbx), %xmm0
movq %rax, %rdi
callq 0x2290
movq %rax, %rdi
callq 0x20c0
movq %r12, %rdi
leaq 0x3684(%rip), %rsi      # 0x824b
leaq 0x760a(%rip), %r14      # 0xc1d8
movq %r14, %rdx
callq 0x5a8c
movq 0xa8(%rsp), %rdi
leaq 0x364b(%rip), %r13      # 0x8230
movq %r13, %rsi
callq 0x21f0
movq %rax, %rbp
movq %r12, %rdi
callq 0x2338
movq %r12, %rdi
leaq 0x3630(%rip), %rsi      # 0x8232
movq %r14, %rdx
callq 0x5a8c
movq 0xa8(%rsp), %rdi
movq %r13, %rsi
callq 0x21f0
movq %rax, 0x10(%rsp)
movq %r12, %rdi
callq 0x2338
movq %r12, %rdi
leaq 0x360d(%rip), %rsi      # 0x823e
movq %r14, %rdx
callq 0x5a8c
movq 0xa8(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %r13
callq 0x21f0
movq %rax, %rbp
movq %r12, %rdi
callq 0x2338
xorl %r14d, %r14d
movslq 0x382df(%rip), %rax     # 0x3cf40
cmpq %rax, %r14
jge 0x4cb3
xorl %r15d, %r15d
movslq 0x6c(%rbx), %rax
cmpq %rax, %r15
jge 0x4c9e
movq 0x58(%rsp), %rax
movq (%rax), %rax
imulq $0x18, %r14, %rcx
movq (%rax,%rcx), %rax
movsd (%rax,%r15,8), %xmm0
movq %r13, %rdi
leaq 0x35af(%rip), %rsi      # 0x8241
movb $0x1, %al
callq 0x2240
incq %r15
jmp 0x4c69
pushq $0xa
popq %rdi
movq %r13, %rsi
callq 0x2210
incq %r14
movq 0x18(%rsp), %r15
jmp 0x4c5a
xorl %r14d, %r14d
movslq 0x38287(%rip), %rax     # 0x3cf44
cmpq %rax, %r14
jge 0x4d13
xorl %r15d, %r15d
movslq 0x68(%rbx), %rax
cmpq %rax, %r15
jge 0x4cfc
movq 0x50(%rsp), %rax
movq (%rax), %rax
imulq $0x18, %r14, %rcx
movq (%rax,%rcx), %rax
movsd (%rax,%r15,8), %xmm0
movq 0x10(%rsp), %rdi
leaq 0x3551(%rip), %rsi      # 0x8241
movb $0x1, %al
callq 0x2240
incq %r15
jmp 0x4cc5
pushq $0xa
popq %rdi
movq 0x10(%rsp), %rsi
callq 0x2210
incq %r14
movq 0x18(%rsp), %r15
jmp 0x4cb6
xorl %r14d, %r14d
movq %r13, 0x20(%rsp)
movslq 0x38226(%rip), %rax     # 0x3cf48
cmpq %rax, %r14
jge 0x4d92
xorl %r13d, %r13d
movslq 0x68(%rbx), %rax
cmpq %rax, %r13
jge 0x4d88
xorl %r15d, %r15d
movslq 0x6c(%rbx), %rax
cmpq %rax, %r15
jge 0x4d73
movq 0x60(%rsp), %rax
movq (%rax), %rax
imulq $0x18, %r14, %rcx
movq (%rax,%rcx), %rax
imulq $0x18, %r13, %rcx
movq (%rax,%rcx), %rax
movsd (%rax,%r15,8), %xmm0
movq %rbp, %rdi
leaq 0x34da(%rip), %rsi      # 0x8241
movb $0x1, %al
callq 0x2240
incq %r15
jmp 0x4d36
pushq $0xa
popq %rdi
movq %rbp, %rsi
callq 0x2210
incq %r13
movq 0x18(%rsp), %r15
jmp 0x4d2a
incq %r14
movq 0x20(%rsp), %r13
jmp 0x4d1b
movq %r13, %rdi
callq 0x21a0
movq 0x10(%rsp), %rdi
callq 0x21a0
movq %rbp, %rdi
callq 0x21a0
movq %r12, %rdi
leaq 0x3492(%rip), %rsi      # 0x8248
leaq 0x741b(%rip), %rdx      # 0xc1d8
callq 0x5a8c
movq 0xa8(%rsp), %rdi
leaq 0x345f(%rip), %rsi      # 0x8230
callq 0x21f0
movq %rax, %rbp
movq %r12, %rdi
callq 0x2338
xorl %r14d, %r14d
movslq 0x3815d(%rip), %rax     # 0x3cf48
cmpq %rax, %r14
jge 0x4e3d
xorl %r15d, %r15d
movslq 0x6c(%rbx), %rax
cmpq %rax, %r15
jge 0x4e28
movq 0x48(%rsp), %rax
movq (%rax), %rax
imulq $0x18, %r14, %rcx
movq (%rax,%rcx), %rax
movsd (%rax,%r15,8), %xmm0
movq %rbp, %rdi
leaq 0x3425(%rip), %rsi      # 0x8241
movb $0x1, %al
callq 0x2240
incq %r15
jmp 0x4df3
pushq $0xa
popq %rdi
movq %rbp, %rsi
callq 0x2210
incq %r14
movq 0x18(%rsp), %r15
jmp 0x4de4
movq %rbp, %rdi
callq 0x21a0
movq 0x40(%rsp), %rax
incl %eax
jmp 0x4788
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop