// File: thunlp[P]KB2E/TransE/Train_TransE.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++;
        }
        left_entity[relation2id[s3]][entity2id[s1]]++;
        right_entity[relation2id[s3]][entity2id[s2]]++;
        train.add(entity2id[s1],entity2id[s2],relation2id[s3]);
    }
    for (int i=0; i<relation_num; i++)
    {
    	double sum1=0,sum2=0;
    	for (map<int,int>::iterator it = left_entity[i].begin(); it!=left_entity[i].end(); it++)
    	{
    		sum1++;
    		sum2+=it->second;
    	}
    	left_num[i]=sum2/sum1;
    }
    for (int i=0; i<relation_num; i++)
    {
    	double sum1=0,sum2=0;
    	for (map<int,int>::iterator it = right_entity[i].begin(); it!=right_entity[i].end(); it++)
    	{
    		sum1++;
    		sum2+=it->second;
    	}
    	right_num[i]=sum2/sum1;
    }
    cout<<"relation_num="<<relation_num<<endl;
    cout<<"entity_num="<<entity_num<<endl;
    fclose(f_kb);
}

// Assembly:
subq $0x268, %rsp            # imm = 0x268
leaq 0xf692(%rip), %rdi      # 0x12070
leaq 0xf6a1(%rip), %rsi      # 0x12086
callq 0x22b0
movq %rax, 0x260(%rsp)
leaq 0xf68f(%rip), %rdi      # 0x12088
leaq 0xf686(%rip), %rsi      # 0x12086
callq 0x22b0
movq %rax, 0x258(%rsp)
movq 0x260(%rsp), %rdi
leaq 0xf684(%rip), %rsi      # 0x120a0
leaq 0x1784d(%rip), %rdx     # 0x1a270
leaq 0x254(%rsp), %rcx
movb $0x0, %al
callq 0x23e0
cmpl $0x2, %eax
jne 0x2b5a
leaq 0x22f(%rsp), %rdi
movq %rdi, 0x110(%rsp)
callq 0x23b0
movq 0x110(%rsp), %rdx
leaq 0x17811(%rip), %rsi     # 0x1a270
leaq 0x230(%rsp), %rdi
callq 0x39d0
jmp 0x2a6e
leaq 0x22f(%rsp), %rdi
callq 0x2270
movl 0x254(%rsp), %eax
movl %eax, 0x104(%rsp)
leaq 0x48558(%rip), %rdi     # 0x4afe8
leaq 0x230(%rsp), %rsi
callq 0x3a90
movq %rax, 0x108(%rsp)
jmp 0x2aa7
movq 0x108(%rsp), %rax
movl 0x104(%rsp), %ecx
movl %ecx, (%rax)
leaq 0x48559(%rip), %rdi     # 0x4b018
leaq 0x254(%rsp), %rsi
callq 0x3b80
movq %rax, 0xf8(%rsp)
jmp 0x2ad6
movq 0xf8(%rsp), %rdi
leaq 0x230(%rsp), %rsi
callq 0x2040
jmp 0x2aed
movl 0x484c1(%rip), %eax     # 0x4afb4
addl $0x1, %eax
movl %eax, 0x484b8(%rip)     # 0x4afb4
leaq 0x230(%rsp), %rdi
callq 0x2428
jmp 0x2a0d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x22f(%rsp), %rdi
callq 0x2270
jmp 0x34c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x230(%rsp), %rdi
callq 0x2428
jmp 0x34c8
jmp 0x2b5c
movq 0x258(%rsp), %rdi
leaq 0xf535(%rip), %rsi      # 0x120a0
leaq 0x176fe(%rip), %rdx     # 0x1a270
leaq 0x254(%rsp), %rcx
movb $0x0, %al
callq 0x23e0
cmpl $0x2, %eax
jne 0x2ca9
leaq 0x1f7(%rsp), %rdi
movq %rdi, 0xf0(%rsp)
callq 0x23b0
movq 0xf0(%rsp), %rdx
leaq 0x176c2(%rip), %rsi     # 0x1a270
leaq 0x1f8(%rsp), %rdi
callq 0x39d0
jmp 0x2bbd
leaq 0x1f7(%rsp), %rdi
callq 0x2270
movl 0x254(%rsp), %eax
movl %eax, 0xe4(%rsp)
leaq 0x483d9(%rip), %rdi     # 0x4afb8
leaq 0x1f8(%rsp), %rsi
callq 0x3a90
movq %rax, 0xe8(%rsp)
jmp 0x2bf6
movq 0xe8(%rsp), %rax
movl 0xe4(%rsp), %ecx
movl %ecx, (%rax)
leaq 0x4843a(%rip), %rdi     # 0x4b048
leaq 0x254(%rsp), %rsi
callq 0x3b80
movq %rax, 0xd8(%rsp)
jmp 0x2c25
movq 0xd8(%rsp), %rdi
leaq 0x1f8(%rsp), %rsi
callq 0x2040
jmp 0x2c3c
movl 0x4836e(%rip), %eax     # 0x4afb0
addl $0x1, %eax
movl %eax, 0x48365(%rip)     # 0x4afb0
leaq 0x1f8(%rsp), %rdi
callq 0x2428
jmp 0x2b5c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x1f7(%rsp), %rdi
callq 0x2270
jmp 0x34c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x1f8(%rsp), %rdi
callq 0x2428
jmp 0x34c8
leaq 0xf3f5(%rip), %rdi      # 0x120a5
leaq 0xf3cf(%rip), %rsi      # 0x12086
callq 0x22b0
movq %rax, 0x1e8(%rsp)
movq 0x1e8(%rsp), %rdi
leaq 0xf3e4(%rip), %rsi      # 0x120b7
leaq 0x17596(%rip), %rdx     # 0x1a270
movb $0x0, %al
callq 0x23e0
cmpl $0x1, %eax
jne 0x31d7
leaq 0x1c7(%rsp), %rdi
movq %rdi, 0xd0(%rsp)
callq 0x23b0
movq 0xd0(%rsp), %rdx
leaq 0x17562(%rip), %rsi     # 0x1a270
leaq 0x1c8(%rsp), %rdi
callq 0x39d0
jmp 0x2d1d
leaq 0x1c7(%rsp), %rdi
callq 0x2270
movq 0x1e8(%rsp), %rdi
leaq 0xf37e(%rip), %rsi      # 0x120b7
leaq 0x17530(%rip), %rdx     # 0x1a270
xorl %eax, %eax
callq 0x23e0
jmp 0x2d49
leaq 0x19f(%rsp), %rdi
movq %rdi, 0xc8(%rsp)
callq 0x23b0
movq 0xc8(%rsp), %rdx
leaq 0x17503(%rip), %rsi     # 0x1a270
leaq 0x1a0(%rsp), %rdi
callq 0x39d0
jmp 0x2d7c
leaq 0x19f(%rsp), %rdi
callq 0x2270
movq 0x1e8(%rsp), %rdi
leaq 0xf31f(%rip), %rsi      # 0x120b7
leaq 0x174d1(%rip), %rdx     # 0x1a270
xorl %eax, %eax
callq 0x23e0
jmp 0x2da8
leaq 0x177(%rsp), %rdi
movq %rdi, 0xc0(%rsp)
callq 0x23b0
movq 0xc0(%rsp), %rdx
leaq 0x174a4(%rip), %rsi     # 0x1a270
leaq 0x178(%rsp), %rdi
callq 0x39d0
jmp 0x2ddb
leaq 0x177(%rsp), %rdi
callq 0x2270
leaq 0x481f9(%rip), %rdi     # 0x4afe8
leaq 0x1c8(%rsp), %rsi
callq 0x3c70
movq %rax, 0xb8(%rsp)
jmp 0x2e06
movq 0xb8(%rsp), %rax
cmpq $0x0, %rax
jne 0x2f39
movq 0x171b9(%rip), %rdi     # 0x19fd8
leaq 0xf294(%rip), %rsi      # 0x120ba
callq 0x2200
movq %rax, 0xb0(%rsp)
jmp 0x2e35
movq 0xb0(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x21d0
movq %rax, 0xa8(%rsp)
jmp 0x2e54
movq 0xa8(%rsp), %rdi
movq 0x1715d(%rip), %rsi     # 0x19fc0
callq 0x2250
jmp 0x2e6a
jmp 0x2f39
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x1c7(%rsp), %rdi
callq 0x2270
jmp 0x34c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x31c5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x19f(%rsp), %rdi
callq 0x2270
jmp 0x31c5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x31b8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x177(%rsp), %rdi
callq 0x2270
jmp 0x31b8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x178(%rsp), %rdi
callq 0x2428
jmp 0x31b8
leaq 0x480a8(%rip), %rdi     # 0x4afe8
leaq 0x1a0(%rsp), %rsi
callq 0x3c70
movq %rax, 0xa0(%rsp)
jmp 0x2f57
movq 0xa0(%rsp), %rax
cmpq $0x0, %rax
jne 0x2fb9
movq 0x1706c(%rip), %rdi     # 0x19fd8
leaq 0xf147(%rip), %rsi      # 0x120ba
callq 0x2200
movq %rax, 0x98(%rsp)
jmp 0x2f82
movq 0x98(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0x21d0
movq %rax, 0x90(%rsp)
jmp 0x2fa1
movq 0x90(%rsp), %rdi
movq 0x17010(%rip), %rsi     # 0x19fc0
callq 0x2250
jmp 0x2fb7
jmp 0x2fb9
leaq 0x47ff8(%rip), %rdi     # 0x4afb8
leaq 0x178(%rsp), %rsi
callq 0x3c70
movq %rax, 0x88(%rsp)
jmp 0x2fd7
movq 0x88(%rsp), %rax
cmpq $0x0, %rax
jne 0x302a
movl 0x47fc5(%rip), %eax     # 0x4afb0
movl %eax, 0x7c(%rsp)
leaq 0x47fc2(%rip), %rdi     # 0x4afb8
leaq 0x178(%rsp), %rsi
callq 0x3a90
movq %rax, 0x80(%rsp)
jmp 0x300d
movq 0x80(%rsp), %rax
movl 0x7c(%rsp), %ecx
movl %ecx, (%rax)
movl 0x47f8f(%rip), %eax     # 0x4afb0
addl $0x1, %eax
movl %eax, 0x47f86(%rip)     # 0x4afb0
leaq 0x47f87(%rip), %rdi     # 0x4afb8
leaq 0x178(%rsp), %rsi
callq 0x3a90
movq %rax, 0x70(%rsp)
jmp 0x3045
movq 0x70(%rsp), %rsi
leaq 0x48027(%rip), %rdi     # 0x4b078
callq 0x3cd0
movq %rax, 0x68(%rsp)
jmp 0x305d
leaq 0x47f84(%rip), %rdi     # 0x4afe8
leaq 0x1c8(%rsp), %rsi
callq 0x3a90
movq %rax, 0x60(%rsp)
jmp 0x3078
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdi
callq 0x3dc0
movq %rax, 0x58(%rsp)
jmp 0x308e
movq 0x58(%rsp), %rax
movl (%rax), %ecx
incl %ecx
movl %ecx, (%rax)
leaq 0x47f18(%rip), %rdi     # 0x4afb8
leaq 0x178(%rsp), %rsi
callq 0x3a90
movq %rax, 0x50(%rsp)
jmp 0x30b4
movq 0x50(%rsp), %rsi
leaq 0x47fe8(%rip), %rdi     # 0x4b0a8
callq 0x3cd0
movq %rax, 0x48(%rsp)
jmp 0x30cc
leaq 0x47f15(%rip), %rdi     # 0x4afe8
leaq 0x1a0(%rsp), %rsi
callq 0x3a90
movq %rax, 0x40(%rsp)
jmp 0x30e7
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdi
callq 0x3dc0
movq %rax, 0x38(%rsp)
jmp 0x30fd
movq 0x38(%rsp), %rax
movl (%rax), %ecx
incl %ecx
movl %ecx, (%rax)
leaq 0x47ed9(%rip), %rdi     # 0x4afe8
leaq 0x1c8(%rsp), %rsi
callq 0x3a90
movq %rax, 0x30(%rsp)
jmp 0x3123
movq 0x30(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x24(%rsp)
leaq 0x47eb3(%rip), %rdi     # 0x4afe8
leaq 0x1a0(%rsp), %rsi
callq 0x3a90
movq %rax, 0x28(%rsp)
jmp 0x3149
movq 0x28(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x14(%rsp)
leaq 0x47e5d(%rip), %rdi     # 0x4afb8
leaq 0x178(%rsp), %rsi
callq 0x3a90
movq %rax, 0x18(%rsp)
jmp 0x316f
movl 0x14(%rsp), %edx
movl 0x24(%rsp), %esi
movq 0x18(%rsp), %rax
movl (%rax), %ecx
leaq 0x47fb3(%rip), %rdi     # 0x4b138
callq 0x3eb0
jmp 0x318c
leaq 0x178(%rsp), %rdi
callq 0x2428
leaq 0x1a0(%rsp), %rdi
callq 0x2428
leaq 0x1c8(%rsp), %rdi
callq 0x2428
jmp 0x2cc4
leaq 0x1a0(%rsp), %rdi
callq 0x2428
leaq 0x1c8(%rsp), %rdi
callq 0x2428
jmp 0x34c8
movl $0x0, 0x170(%rsp)
movl 0x170(%rsp), %eax
cmpl 0x47dc1(%rip), %eax     # 0x4afb0
jge 0x3316
xorps %xmm0, %xmm0
movsd %xmm0, 0x168(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x160(%rsp)
leaq 0x47e64(%rip), %rdi     # 0x4b078
leaq 0x170(%rsp), %rsi
callq 0x3cd0
movq %rax, %rdi
callq 0x3f40
movq %rax, 0x158(%rsp)
leaq 0x47e40(%rip), %rdi     # 0x4b078
leaq 0x170(%rsp), %rsi
callq 0x3cd0
movq %rax, %rdi
callq 0x3f90
movq %rax, 0x150(%rsp)
leaq 0x158(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0x3f70
testb $0x1, %al
jne 0x3270
jmp 0x32ca
movsd 0xeda8(%rip), %xmm0     # 0x12020
addsd 0x168(%rsp), %xmm0
movsd %xmm0, 0x168(%rsp)
leaq 0x158(%rsp), %rdi
callq 0x3fc0
cvtsi2sdl 0x4(%rax), %xmm0
addsd 0x160(%rsp), %xmm0
movsd %xmm0, 0x160(%rsp)
leaq 0x158(%rsp), %rdi
xorl %esi, %esi
callq 0x3fe0
movq %rax, 0x148(%rsp)
jmp 0x3231
movsd 0x160(%rsp), %xmm0
divsd 0x168(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x47def(%rip), %rdi     # 0x4b0d8
leaq 0x170(%rsp), %rsi
callq 0x4020
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x170(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x170(%rsp)
jmp 0x31e2
movl $0x0, 0x144(%rsp)
movl 0x144(%rsp), %eax
cmpl 0x47c82(%rip), %eax     # 0x4afb0
jge 0x3453
xorps %xmm0, %xmm0
movsd %xmm0, 0x138(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x130(%rsp)
leaq 0x47d55(%rip), %rdi     # 0x4b0a8
leaq 0x144(%rsp), %rsi
callq 0x3cd0
movq %rax, %rdi
callq 0x3f40
movq %rax, 0x128(%rsp)
leaq 0x47d31(%rip), %rdi     # 0x4b0a8
leaq 0x144(%rsp), %rsi
callq 0x3cd0
movq %rax, %rdi
callq 0x3f90
movq %rax, 0x120(%rsp)
leaq 0x128(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x3f70
testb $0x1, %al
jne 0x33af
jmp 0x3409
movsd 0xec69(%rip), %xmm0     # 0x12020
addsd 0x138(%rsp), %xmm0
movsd %xmm0, 0x138(%rsp)
leaq 0x128(%rsp), %rdi
callq 0x3fc0
cvtsi2sdl 0x4(%rax), %xmm0
addsd 0x130(%rsp), %xmm0
movsd %xmm0, 0x130(%rsp)
leaq 0x128(%rsp), %rdi
xorl %esi, %esi
callq 0x3fe0
movq %rax, 0x118(%rsp)
jmp 0x3370
movsd 0x130(%rsp), %xmm0
divsd 0x138(%rsp), %xmm0
movsd %xmm0, (%rsp)
leaq 0x47ce1(%rip), %rdi     # 0x4b108
leaq 0x144(%rsp), %rsi
callq 0x4020
movsd (%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x144(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x144(%rsp)
jmp 0x3321
movq 0x16b7e(%rip), %rdi     # 0x19fd8
leaq 0xec66(%rip), %rsi      # 0x120c7
callq 0x2200
movq %rax, %rdi
movl 0x47b41(%rip), %esi     # 0x4afb0
callq 0x2390
movq %rax, %rdi
movq 0x16b42(%rip), %rsi     # 0x19fc0
callq 0x2250
movq 0x16b4e(%rip), %rdi     # 0x19fd8
leaq 0xec44(%rip), %rsi      # 0x120d5
callq 0x2200
movq %rax, %rdi
movl 0x47b15(%rip), %esi     # 0x4afb4
callq 0x2390
movq %rax, %rdi
movq 0x16b12(%rip), %rsi     # 0x19fc0
callq 0x2250
movq 0x1e8(%rsp), %rdi
callq 0x2210
addq $0x268, %rsp            # imm = 0x268
retq
movq 0x220(%rsp), %rdi
callq 0x23a0
nopw %cs:(%rax,%rax)