// File: /workspace/github_cpp_makefile_star2_O2/thunlp[P]KB2E/TransR/Train_TransR.cpp
// Function: prepare()
void prepare()
{
    FILE* f1 = fopen("../data/entity2id.txt","r");
	FILE* f2 = fopen("../data/relation2id.txt","r");
	int x;
	while (fscanf(f1,"%s%d",buf,&x)==2)
	{
		string st=buf;
		entity2id[st]=x;
		id2entity[x]=st;
		entity_num++;
	}
	while (fscanf(f2,"%s%d",buf,&x)==2)
	{
		string st=buf;
		relation2id[st]=x;
		id2relation[x]=st;
		relation_num++;
	}
    FILE* f_kb = fopen("../data/train.txt","r");
	while (fscanf(f_kb,"%s",buf)==1)
    {
        string s1=buf;
        fscanf(f_kb,"%s",buf);
        string s2=buf;
        fscanf(f_kb,"%s",buf);
        string s3=buf;
        if (entity2id.count(s1)==0)
        {
            cout<<"miss entity:"<<s1<<endl;
        }
        if (entity2id.count(s2)==0)
        {
            cout<<"miss entity:"<<s2<<endl;
        }
        if (relation2id.count(s3)==0)
        {
            relation2id[s3] = relation_num;
            relation_num++;
        }
        entity2num[entity2id[s1]]++;
        entity2num[entity2id[s2]]++;
        left_entity[relation2id[s3]][entity2id[s1]].push_back(entity2id[s2]);
        right_entity[relation2id[s3]][entity2id[s2]].push_back(entity2id[s1]);
        train.add(entity2id[s1],entity2id[s2],relation2id[s3]);
    }
    for (int i=0; i<relation_num; i++)
    {
    	double sum1=0,sum2=0,sum3 = 0;
    	for (map<int,vector<int> >::iterator it = left_entity[i].begin(); it!=left_entity[i].end(); it++)
    	{
    		sum1++;
    		sum2+=it->second.size();
    		sum3+=sqr(it->second.size());
    	}
    	left_mean[i]=sum2/sum1;

    	left_var[i]=sum3/sum1-sqr(left_mean[i]);
    }
    for (int i=0; i<relation_num; i++)
    {
    	double sum1=0,sum2=0,sum3=0;
    	for (map<int,vector<int> >::iterator it = right_entity[i].begin(); it!=right_entity[i].end(); it++)
    	{
    		sum1++;
    		sum2+=it->second.size();
    		sum3+=sqr(it->second.size());
    	}
    	right_mean[i]=sum2/sum1;
    	right_var[i]=sum3/sum1-sqr(right_mean[i]);
    }

    fclose(f_kb);
    cout<<"relation_num="<<relation_num<<endl;
    cout<<"entity_num="<<entity_num<<endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
leaq 0x471a(%rip), %rdi      # 0x70b0
leaq 0x4729(%rip), %rbx      # 0x70c6
movq %rbx, %rsi
callq 0x21f0
movq %rax, %r14
leaq 0x4719(%rip), %rdi      # 0x70c8
movq %rbx, %rsi
callq 0x21f0
movq %rax, %rbx
leaq 0x883f(%rip), %r12      # 0xb200
leaq 0x18(%rsp), %r13
leaq 0x20(%rsp), %rbp
movq %r14, %rdi
leaq 0x470b(%rip), %rsi      # 0x70e0
movq %r12, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2300
cmpl $0x2, %eax
jne 0x2a38
movq %rbp, %rdi
movq %r12, %rsi
leaq 0x68(%rsp), %rdx
callq 0x331c
movl 0x18(%rsp), %r15d
leaq 0x39575(%rip), %rdi     # 0x3bf78
movq %rbp, %rsi
callq 0x3358
movl %r15d, (%rax)
leaq 0x39593(%rip), %rdi     # 0x3bfa8
movq %r13, %rsi
callq 0x33be
movq %rax, %rdi
movq %rbp, %rsi
callq 0x20e0
incl 0x39516(%rip)           # 0x3bf44
movq %rbp, %rdi
callq 0x2338
jmp 0x29cb
leaq 0x87c1(%rip), %r15      # 0xb200
leaq 0x18(%rsp), %r12
leaq 0x20(%rsp), %r13
leaq 0x39588(%rip), %rbp     # 0x3bfd8
movq %rbx, %rdi
leaq 0x4686(%rip), %rsi      # 0x70e0
movq %r15, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2300
cmpl $0x2, %eax
jne 0x2ab9
movq %r13, %rdi
movq %r15, %rsi
leaq 0x68(%rsp), %rdx
callq 0x331c
movl 0x18(%rsp), %r14d
leaq 0x394c0(%rip), %rdi     # 0x3bf48
movq %r13, %rsi
callq 0x3358
movl %r14d, (%rax)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x33be
movq %rax, %rdi
movq %r13, %rsi
callq 0x20e0
incl 0x39491(%rip)           # 0x3bf40
movq %r13, %rdi
callq 0x2338
jmp 0x2a50
leaq 0x4625(%rip), %rdi      # 0x70e5
leaq 0x45ff(%rip), %rsi      # 0x70c6
callq 0x21f0
movq %rax, %rbx
leaq 0x4621(%rip), %rbp      # 0x70f7
leaq 0x8723(%rip), %r12      # 0xb200
leaq 0x68(%rsp), %r13
leaq 0x3948f(%rip), %r14     # 0x3bf78
movq %rax, 0x40(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2300
cmpl $0x1, %eax
jne 0x2d47
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x331c
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2300
movq %r13, %rdi
movq %r12, %rsi
leaq 0x48(%rsp), %rdx
callq 0x331c
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2300
movq %r12, %rsi
leaq 0x48(%rsp), %rdi
leaq 0x1f(%rsp), %rdx
callq 0x331c
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
callq 0x340c
testq %rax, %rax
jne 0x2b93
movq 0x8466(%rip), %rdi      # 0xafd8
leaq 0x4581(%rip), %rsi      # 0x70fa
callq 0x2190
movq %rax, %rdi
leaq 0x20(%rsp), %rsi
callq 0x2160
movq %rax, %rdi
callq 0x20c0
movq %r14, %rdi
movq %r13, %rsi
callq 0x340c
testq %rax, %rax
jne 0x2bc9
movq 0x842e(%rip), %rdi      # 0xafd8
leaq 0x4549(%rip), %rsi      # 0x70fa
callq 0x2190
movq %rax, %rdi
movq %r13, %rsi
callq 0x2160
movq %rax, %rdi
callq 0x20c0
leaq 0x39378(%rip), %rdi     # 0x3bf48
leaq 0x48(%rsp), %rsi
callq 0x340c
testq %rax, %rax
jne 0x2bfe
movl 0x3935b(%rip), %ebx     # 0x3bf40
leaq 0x3935c(%rip), %rdi     # 0x3bf48
leaq 0x48(%rsp), %rsi
callq 0x3358
movl %ebx, (%rax)
incl 0x39342(%rip)           # 0x3bf40
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
callq 0x3358
leaq 0x393f6(%rip), %rdi     # 0x3c008
movq %rax, %rsi
callq 0x3426
incl (%rax)
movq %r14, %rdi
movq %r13, %rsi
callq 0x3358
leaq 0x393da(%rip), %rdi     # 0x3c008
movq %rax, %rsi
callq 0x3426
incl (%rax)
leaq 0x39309(%rip), %rdi     # 0x3bf48
leaq 0x48(%rsp), %rsi
callq 0x3358
leaq 0x393e8(%rip), %rdi     # 0x3c038
movq %rax, %rsi
callq 0x3474
movq %rax, %rbx
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
callq 0x3358
movq %rbx, %rdi
movq %rax, %rsi
callq 0x34c2
movq %rax, %rbx
movq %r14, %rdi
movq %r13, %rsi
callq 0x3358
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3510
leaq 0x392b5(%rip), %rdi     # 0x3bf48
leaq 0x48(%rsp), %rsi
callq 0x3358
leaq 0x393c4(%rip), %rdi     # 0x3c068
movq %rax, %rsi
callq 0x3474
movq %rax, %rbx
movq %r14, %rdi
movq %r13, %rsi
callq 0x3358
movq %rbx, %rdi
movq %rax, %rsi
callq 0x34c2
movq %rax, %rbx
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
callq 0x3358
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3510
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
callq 0x3358
movl (%rax), %ebx
movq %r14, %rdi
movq %r13, %rsi
callq 0x3358
movl (%rax), %r15d
leaq 0x39244(%rip), %rdi     # 0x3bf48
leaq 0x48(%rsp), %rsi
callq 0x3358
movl (%rax), %ecx
leaq 0x39441(%rip), %rdi     # 0x3c158
movl %ebx, %esi
movl %r15d, %edx
callq 0x352e
leaq 0x48(%rsp), %rdi
callq 0x2338
movq %r13, %rdi
callq 0x2338
leaq 0x20(%rsp), %rdi
callq 0x2338
movq 0x40(%rsp), %rbx
jmp 0x2aee
xorl %ecx, %ecx
leaq 0x392e8(%rip), %rbx     # 0x3c038
leaq 0x20(%rsp), %r14
leaq 0x3933c(%rip), %r15     # 0x3c098
leaq 0x39395(%rip), %r12     # 0x3c0f8
movl %ecx, 0x20(%rsp)
movl 0x391d3(%rip), %eax     # 0x3bf40
cmpl %eax, %ecx
jge 0x2e96
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3474
movq 0x18(%rax), %r13
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd %xmm0, 0x8(%rsp)
movsd %xmm0, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3474
addq $0x8, %rax
cmpq %rax, %r13
je 0x2e1f
movsd 0x10(%rsp), %xmm0
addsd 0x4266(%rip), %xmm0     # 0x7020
movsd %xmm0, 0x10(%rsp)
movq 0x30(%r13), %rax
subq 0x28(%r13), %rax
sarq $0x2, %rax
movq %rax, %xmm0
punpckldq 0x4287(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x428f(%rip), %xmm0     # 0x7070
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
mulsd %xmm1, %xmm1
movsd (%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
movq %r13, %rdi
callq 0x2100
movq %rax, %r13
jmp 0x2d98
movsd 0x8(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x35b6
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd (%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x35b6
movsd (%rax), %xmm0
mulsd %xmm0, %xmm0
movsd (%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x35b6
movsd (%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x20(%rsp), %ecx
incl %ecx
jmp 0x2d63
xorl %ecx, %ecx
leaq 0x391c9(%rip), %rbx     # 0x3c068
leaq 0x20(%rsp), %r14
leaq 0x3921d(%rip), %r15     # 0x3c0c8
leaq 0x39276(%rip), %r12     # 0x3c128
movl %ecx, 0x20(%rsp)
cmpl %eax, %ecx
jge 0x2fe5
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3474
movq 0x18(%rax), %r13
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd %xmm0, 0x8(%rsp)
movsd %xmm0, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3474
addq $0x8, %rax
cmpq %rax, %r13
je 0x2f68
movsd 0x10(%rsp), %xmm0
addsd 0x411d(%rip), %xmm0     # 0x7020
movsd %xmm0, 0x10(%rsp)
movq 0x30(%r13), %rax
subq 0x28(%r13), %rax
sarq $0x2, %rax
movq %rax, %xmm0
punpckldq 0x413e(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x4146(%rip), %xmm0     # 0x7070
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
mulsd %xmm1, %xmm1
movsd (%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
movq %r13, %rdi
callq 0x2100
movq %rax, %r13
jmp 0x2ee1
movsd 0x8(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x35b6
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd (%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x35b6
movsd (%rax), %xmm0
mulsd %xmm0, %xmm0
movsd (%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x35b6
movsd (%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x20(%rsp), %ecx
incl %ecx
movl 0x38f60(%rip), %eax     # 0x3bf40
jmp 0x2eb2
movq 0x40(%rsp), %rdi
callq 0x21a0
movq 0x7fe2(%rip), %rbx      # 0xafd8
leaq 0x410a(%rip), %rsi      # 0x7107
movq %rbx, %rdi
callq 0x2190
movl 0x38f35(%rip), %esi     # 0x3bf40
movq %rax, %rdi
callq 0x22c0
movq %rax, %rdi
callq 0x20c0
leaq 0x40f3(%rip), %rsi      # 0x7115
movq %rbx, %rdi
callq 0x2190
movl 0x38f14(%rip), %esi     # 0x3bf44
movq %rax, %rdi
callq 0x22c0
movq %rax, %rdi
callq 0x20c0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3054
movq %rax, %rbx
jmp 0x307a
jmp 0x3068
jmp 0x3061
jmp 0x3068
jmp 0x3061
movq %rax, %rbx
jmp 0x308e
jmp 0x3068
movq %rax, %rbx
jmp 0x3084
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x2338
leaq 0x68(%rsp), %rdi
callq 0x2338
leaq 0x20(%rsp), %rdi
callq 0x2338
movq %rbx, %rdi
callq 0x22d0