// File: /workspace/github_cpp_makefile_star2_O1/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 0x5be2
leaq 0xf0(%rbx), %rsi
leaq 0x138(%rbx), %rdi
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xb8(%rsp)
callq 0x5be2
leaq 0x168(%rbx), %rsi
leaq 0x180(%rbx), %rdi
movq %rdi, 0xa0(%rsp)
movq %rsi, 0xa8(%rsp)
callq 0x5d5a
leaq 0x108(%rbx), %rsi
leaq 0x150(%rbx), %rdi
movq %rdi, 0x90(%rsp)
movq %rsi, 0x98(%rsp)
callq 0x5be2
leaq 0x198(%rbx), %rsi
leaq 0x1b0(%rbx), %rdi
movq %rdi, 0x80(%rsp)
movq %rsi, 0x88(%rsp)
callq 0x5d5a
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 0x5282
xorl %r15d, %r15d
movl %r15d, 0x24(%rsp)
movq 0x98(%rbx), %rsi
subq 0x90(%rbx), %rsi
shrq $0x2, %rsi
movq %rbx, %rdi
callq 0x5eb8
movl %eax, %ebp
movl 0x380a7(%rip), %esi     # 0x3cf44
movq %rbx, %rdi
callq 0x5eb8
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 0x3807c(%rip), %rdi     # 0x3cf50
leaq 0x14(%rsp), %rsi
callq 0x3cc8
movl (%rax), %eax
movq %rax, 0x18(%rsp)
movq 0xc0(%rbx), %rsi
addq %rbp, %rsi
leaq 0x3820a(%rip), %r13     # 0x3d100
movq %r13, %rdi
callq 0x3eb4
movsd (%rax), %xmm0
mulsd 0x3136(%rip), %xmm0     # 0x8040
movsd %xmm0, 0x58(%rsp)
movq 0xc0(%rbx), %rsi
addq %rbp, %rsi
movq %r13, %rdi
callq 0x3eb4
movsd (%rax), %xmm0
movsd %xmm0, 0xd0(%rsp)
movq 0xc0(%rbx), %rsi
addq %rbp, %rsi
leaq 0x38190(%rip), %rdi     # 0x3d0d0
callq 0x3eb4
cmpl $0x0, 0x70(%rbx)
movsd 0x30f7(%rip), %xmm0     # 0x8048
je 0x4f6a
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 0x508c
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 0x4748
movq 0x10(%rax), %rdx
addq $0x8, %rax
movq %rax, %rcx
testq %rdx, %rdx
je 0x5003
movq %rax, %rcx
xorl %esi, %esi
cmpl %r14d, 0x20(%rdx)
setl %sil
cmovgeq %rdx, %rcx
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x4feb
movq %rax, %rdx
cmpq %rax, %rcx
je 0x5016
cmpl 0x20(%rcx), %r14d
cmovlq %rax, %rcx
movq %rcx, %rdx
cmpq %rax, %rdx
jne 0x5076
movq %rbp, 0x60(%rsp)
movq 0x78(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x4748
movq 0x10(%rax), %rdx
addq $0x8, %rax
movq %rax, %rcx
testq %rdx, %rdx
je 0x505a
movq %rax, %rcx
xorl %esi, %esi
cmpl %r14d, 0x20(%rdx)
setl %sil
cmovgeq %rdx, %rcx
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x5042
movq %rax, %rdx
cmpq %rax, %rcx
je 0x506d
cmpl 0x20(%rcx), %r14d
cmovlq %rax, %rcx
movq %rcx, %rdx
cmpq %rax, %rdx
je 0x510d
movl 0x37ec8(%rip), %esi     # 0x3cf44
movq %rbx, %rdi
callq 0x5eb8
movl %eax, %r14d
jmp 0x4fbb
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 0x4748
movq 0xa8(%rbx), %rsi
addq %rbp, %rsi
movq %rax, %rdi
callq 0x3d2c
testq %rax, %rax
jne 0x50fa
orq %r13, %r15
movq %r15, 0x60(%rsp)
movq 0x78(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x4748
movq 0xa8(%rbx), %rsi
addq %rbp, %rsi
movq %rax, %rdi
callq 0x3d2c
testq %rax, %rax
je 0x512d
movl 0x37e44(%rip), %esi     # 0x3cf44
movq %rbx, %rdi
callq 0x5eb8
movl %eax, %r14d
jmp 0x5095
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 0x514f
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 0x5efc
movl 0x24(%rsp), %r15d
movslq %r12d, %r13
xorps %xmm0, %xmm0
cvtsi2sdl 0x14(%rsp), %xmm0
movq %rbx, %rdi
callq 0x5fb8
movslq 0x14(%rsp), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0x120(%rbx), %rsi
movq %rbx, %rdi
callq 0x4cc8
movslq 0x18(%rsp), %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %r12
movq 0x150(%rbx), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x4cc8
shlq $0x3, %r13
leaq (,%r13,2), %rbp
addq %r13, %rbp
movq 0x138(%rbx), %rsi
addq %rbp, %rsi
movq %rbx, %rdi
callq 0x4cc8
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 0x4cc8
movslq %r14d, %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %r14
movq 0x138(%rbx), %rsi
addq %r14, %rsi
movq %rbx, %rdi
callq 0x4cc8
addq 0x138(%rbx), %rbp
movq 0x180(%rbx), %rdx
addq %r12, %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x6236
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 0x6236
addq 0x138(%rbx), %r14
addq 0x180(%rbx), %r12
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x6236
incl %r15d
movq 0x70(%rsp), %r14
cmpl %r14d, %r15d
jne 0x4e76
movq 0xc8(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x5be2
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x5be2
movq 0xa8(%rsp), %rdi
movq 0xa0(%rsp), %rsi
callq 0x5d5a
movq 0x90(%rsp), %rdi
movq 0x98(%rsp), %rsi
callq 0x5be2
movq 0x80(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x5d5a
movl 0x6c(%rsp), %eax
incl %eax
cmpl $0x64, %eax
jne 0x4e66
movl $0x5, %edx
movq 0x6cd2(%rip), %r14      # 0xbfd8
movq %r14, %rdi
leaq 0x2f2a(%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 0x2ede(%rip), %rsi      # 0x825b
leaq 0x6e54(%rip), %r14      # 0xc1d8
movq %r14, %rdx
callq 0x6364
movq 0x28(%rsp), %rdi
leaq 0x2ea8(%rip), %r13      # 0x8240
movq %r13, %rsi
callq 0x2210
movq %rax, %r12
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %r15
cmpq %r15, %rdi
je 0x53bf
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x28(%rsp), %rdi
leaq 0x2e77(%rip), %rsi      # 0x8242
movq %r14, %rdx
callq 0x6364
movq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x2210
movq %rax, 0x18(%rsp)
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
leaq 0x2e5d(%rip), %rbp      # 0x8251
je 0x5403
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x28(%rsp), %rdi
leaq 0x2e3f(%rip), %rsi      # 0x824e
movq %r14, %rdx
callq 0x6364
movq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x2210
movq %rax, %r14
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x543e
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21e0
cmpl $0x0, 0x37afb(%rip)     # 0x3cf40
jle 0x54a4
xorl %r13d, %r13d
cmpl $0x0, 0x6c(%rbx)
jle 0x5488
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 0x5453
movl $0xa, %edi
movq %r12, %rsi
callq 0x2230
incq %r13
movslq 0x37aa1(%rip), %rax     # 0x3cf40
cmpq %rax, %r13
jl 0x544a
cmpl $0x0, 0x37a99(%rip)     # 0x3cf44
jle 0x5514
xorl %r13d, %r13d
cmpl $0x0, 0x68(%rbx)
movq 0x18(%rsp), %rdx
jle 0x54f8
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 0x54be
movl $0xa, %edi
movq %rdx, %rsi
callq 0x2230
incq %r13
movslq 0x37a35(%rip), %rax     # 0x3cf44
cmpq %rax, %r13
jl 0x54b0
movq %r12, 0x48(%rsp)
cmpl $0x0, 0x37a28(%rip)     # 0x3cf48
jle 0x55a7
xorl %r13d, %r13d
cmpl $0x0, 0x68(%rbx)
jle 0x5598
xorl %ebp, %ebp
cmpl $0x0, 0x6c(%rbx)
leaq 0x2d15(%rip), %r12      # 0x8251
jle 0x557f
xorl %r15d, %r15d
movq 0x168(%rbx), %rax
leaq (,%r13,2), %rcx
addq %r13, %rcx
movq (%rax,%rcx,8), %rax
leaq (%rbp,%rbp,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 0x5541
movl $0xa, %edi
movq %r14, %rsi
callq 0x2230
incq %rbp
movslq 0x68(%rbx), %rax
cmpq %rax, %rbp
jl 0x5531
incq %r13
movslq 0x379a6(%rip), %rax     # 0x3cf48
cmpq %rax, %r13
jl 0x5529
movq 0x48(%rsp), %rdi
callq 0x21c0
movq 0x18(%rsp), %rdi
callq 0x21c0
movq %r14, %rdi
callq 0x21c0
leaq 0x28(%rsp), %rdi
leaq 0x2c89(%rip), %rsi      # 0x8258
leaq 0x6c02(%rip), %rdx      # 0xc1d8
callq 0x6364
movq 0x28(%rsp), %rdi
leaq 0x2c59(%rip), %rsi      # 0x8240
callq 0x2210
movq %rax, %r14
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x560b
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21e0
cmpl $0x0, 0x37936(%rip)     # 0x3cf48
leaq 0x2c38(%rip), %r13      # 0x8251
jle 0x5671
xorl %r12d, %r12d
cmpl $0x0, 0x6c(%rbx)
jle 0x5655
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 0x5627
movl $0xa, %edi
movq %r14, %rsi
callq 0x2230
incq %r12
movslq 0x378dc(%rip), %rax     # 0x3cf48
cmpq %rax, %r12
jl 0x561e
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 0x4e5c
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop