// File: /workspace/github_cpp_makefile_star2_O3/neubig[P]egret/Egret/src/Binarization.cpp
// Function: binarize(Rule&, InterNodes&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Direct)
int binarize(Rule& rule, InterNodes& interNodes,string prefix, Direct dir)
{
	vector<string> wrds;
	wrds.clear();
	stringstream rss(rule.right.c_str());
	while(!rss.eof())
	{
		string curWrd="";
		rss>>curWrd;
		if(curWrd=="")break;
		wrds.push_back(curWrd);
	}
	vector<vector<string> > strMatrix;
	strMatrix.clear();
	for(int i=0;(size_t)i<wrds.size();i++)
	{
		vector<string> temp;
		strMatrix.push_back(temp);
		for(int j=0;(size_t)j<wrds.size();j++)
		{
			if(i==j)
				strMatrix[i].push_back(wrds[i]);
			else
				strMatrix[i].push_back("");
		}
	}
	for(int i=0;(size_t)i<wrds.size();i++)
	{
		for(int j=i+1;(size_t)j<wrds.size();j++)
		{
			strMatrix[i][j]=strMatrix[i][j-1]+wrds[j];
		}
	}
	jout<<"rightPart length:"<<(unsigned int)wrds.size()<<endl;

	for(int k=(int)wrds.size();k>1;k--)
	{
		for(int i=0;i<=(int)wrds.size()-k;i++)
		{
			if(dir==LEFT&&i!=wrds.size()-k)continue;
			if(dir==RIGHT&&i!=0)break;
			for(int j=i;j<i+k-1;j++)
			{
				if(dir==LEFT&&j!=i)break;
				if(dir==RIGHT&&j!=i+k-2)continue;
				cout<<i<<","<<j<<","<<k<<":["<<i<<","<<j<<"] ["<<j+1<<","<<i+k-1<<"]"<<endl;
				
				string par="";
				if(k==wrds.size())
					par=rule.left;
				else
					par=interNodes.query(strMatrix[i][i+k-1],prefix);
				string leftSon=strMatrix[i][j];
				if(j-i>0)
					leftSon=interNodes.query(leftSon,prefix);
				string rightSon=strMatrix[j+1][i+k-1];
				if(i+k-1-j-1>0)
					rightSon=interNodes.query(rightSon,prefix);
				Rule newRule;
				newRule.left=par;
				newRule.right=leftSon+" "+rightSon;
				if(dir==PERMUTE)
					newRule.prob=double(1)/(double)(k-1);
				else 
					newRule.prob=1;
				if(k==wrds.size())
					newRule.prob*=rule.prob;
				//newRule.print(cout);
				//cout<<endl;
			}
		}
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movl %ecx, %ebp
movq %rdx, 0xa0(%rsp)
movq %rsi, 0x98(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq %rdi, 0x120(%rsp)
movq 0x20(%rdi), %rsi
leaq 0x28(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0xb5b4
leaq 0x170(%rsp), %rdi
leaq 0x28(%rsp), %rsi
movl $0x18, %edx
callq 0x3480
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1528a
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x32e0
leaq 0x2bc25(%rip), %r14     # 0x40eb6
leaq 0x28(%rsp), %r15
leaq 0x170(%rsp), %r12
movq %rsp, %r13
movq 0x170(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x190(%rsp,%rax)
jne 0x15316
leaq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %r14, %rdx
callq 0xb7d0
movq %r12, %rdi
movq %r15, %rsi
callq 0x33a0
movq %r15, %rdi
movq %r14, %rsi
callq 0x30c0
movl %eax, %ebx
testl %eax, %eax
je 0x152f6
movq %r13, %rdi
movq %r15, %rsi
callq 0x17546
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x15312
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x32e0
testl %ebx, %ebx
jne 0x152a1
movl %ebp, 0x24(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0xc0(%rsp)
movq $0x0, 0xd0(%rsp)
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
je 0x154fe
xorl %ebx, %ebx
leaq 0x70(%rsp), %r14
leaq 0x28(%rsp), %r13
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
movq $0x0, 0x80(%rsp)
leaq 0xc0(%rsp), %rdi
movq %r14, %rsi
callq 0x17584
movq (%rsp), %rsi
cmpq %rsi, 0x8(%rsp)
je 0x15408
leaq (%rbx,%rbx,2), %rbp
movq %rbx, %r14
shlq $0x5, %r14
xorl %r15d, %r15d
movq 0xc0(%rsp), %r12
cmpq %r15, %rbx
jne 0x153ac
leaq (%r12,%rbp,8), %rdi
addq %r14, %rsi
callq 0x17546
jmp 0x153f0
leaq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %r13, %rdi
leaq 0x2baf6(%rip), %rdx     # 0x40eb6
movq %rdx, %rsi
callq 0xb7d0
leaq (%r12,%rbp,8), %rdi
movq %r13, %rsi
callq 0x7f46
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x153f0
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x32e0
incq %r15
movq (%rsp), %rsi
movq 0x8(%rsp), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq %r15, %rax
ja 0x15391
leaq 0x70(%rsp), %r14
movq %r14, %rdi
callq 0x6186
incq %rbx
movq (%rsp), %rdx
movq 0x8(%rsp), %rax
movq %rax, %rcx
subq %rdx, %rcx
sarq $0x5, %rcx
cmpq %rbx, %rcx
ja 0x1534e
cmpq %rdx, %rax
je 0x154fe
movl $0x1, %r12d
movl $0x20, %r15d
xorl %esi, %esi
leaq 0x28(%rsp), %r14
leaq 0x1(%rsi), %rdi
movq %rax, %rcx
subq %rdx, %rcx
sarq $0x5, %rcx
movq %rdi, 0xb0(%rsp)
cmpq %rdi, %rcx
jbe 0x154e6
leaq (%rsi,%rsi,2), %r13
movq %r15, %rbp
movq %r12, %rbx
movq 0xc0(%rsp), %rax
movq (%rax,%r13,8), %rax
leaq (%rax,%rbp), %rsi
addq $-0x20, %rsi
addq %rbp, %rdx
movq %r14, %rdi
callq 0x175b0
movq 0xc0(%rsp), %rax
movq (%rax,%r13,8), %rdi
addq %rbp, %rdi
movq %r14, %rsi
callq 0x3380
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x154c7
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x32e0
incq %rbx
movq (%rsp), %rdx
movq 0x8(%rsp), %rax
movq %rax, %rcx
subq %rdx, %rcx
sarq $0x5, %rcx
addq $0x20, %rbp
cmpq %rbx, %rcx
ja 0x15475
incq %r12
addq $0x20, %r15
movq 0xb0(%rsp), %rsi
cmpq %rsi, %rcx
ja 0x15450
leaq 0x3fdfb(%rip), %rdi     # 0x55300
leaq 0x2ac84(%rip), %rsi     # 0x40190
callq 0x1760a
leaq 0x3fdf8(%rip), %r14     # 0x55310
cmpl $0x0, (%r14)
je 0x15568
movq %rax, %rbx
movq 0x8(%rsp), %rax
subq (%rsp), %rax
shrq $0x5, %rax
movq (%rbx), %rdi
movl %eax, %esi
callq 0x31d0
cmpl $0x0, (%r14)
je 0x15568
movq (%rbx), %rbx
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31e0
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rcx, %r15
subq %rax, %r15
shrq $0x5, %r15
cmpl $0x2, %r15d
movl 0x24(%rsp), %r8d
jl 0x15b5a
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq %rcx, %rdx
subq %rax, %rdx
movq %rdx, %rsi
shrq $0x5, %rsi
movslq %esi, %rsi
leaq -0x1(%r15), %rdi
cmpq %rsi, %r15
movq %rdi, 0x148(%rsp)
jg 0x15b48
cmpl $0x2, %r8d
je 0x155cf
movsd 0x2abc4(%rip), %xmm0    # 0x40188
movsd %xmm0, 0xa8(%rsp)
jmp 0x155eb
xorps %xmm0, %xmm0
cvtsi2sd %edi, %xmm0
movsd 0x2abaa(%rip), %xmm1    # 0x40188
divsd %xmm0, %xmm1
movsd %xmm1, 0xa8(%rsp)
xorl %r12d, %r12d
testl %r8d, %r8d
jne 0x15603
sarq $0x5, %rdx
subq %r15, %rdx
cmpq %r12, %rdx
jne 0x15b24
cmpl $0x1, %r8d
sete %dl
testq %r12, %r12
setne %sil
testb %sil, %dl
jne 0x15b48
leaq (%r12,%r15), %r14
decq %r14
cmpq %r14, %r12
jge 0x15b24
leaq (%r12,%r15), %rax
leal (%r12,%r15), %ecx
decl %ecx
movl %ecx, 0xbc(%rsp)
addl $-0x2, %eax
movq %rax, 0xb0(%rsp)
movq %r12, %rbp
cmpl $0x1, %r8d
jne 0x15664
cmpq 0xb0(%rsp), %rbp
je 0x15664
incq %rbp
movq %rbp, %r13
jmp 0x15b02
movq 0x3f965(%rip), %rdi     # 0x54fd0
movl %r12d, %esi
callq 0x3500
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2a9f0(%rip), %rsi     # 0x40075
callq 0x3350
movq %rbx, %rdi
movl %ebp, %esi
callq 0x3500
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2a9cf(%rip), %rsi     # 0x40075
callq 0x3350
movq %rbx, %rdi
movl %r15d, %esi
callq 0x3500
movq %rax, %rbx
movl $0x2, %edx
movq %rax, %rdi
leaq 0x2aada(%rip), %rsi     # 0x401a2
callq 0x3350
movq %rbx, %rdi
movl %r12d, %esi
callq 0x3500
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2a98b(%rip), %rsi     # 0x40075
callq 0x3350
movq %rbx, %rdi
movl %ebp, %esi
callq 0x3500
movq %rax, %rbx
movl $0x3, %edx
movq %rax, %rdi
leaq 0x2aa9a(%rip), %rsi     # 0x401a5
callq 0x3350
leaq 0x1(%rbp), %r13
movq %rbx, %rdi
movl %r13d, %esi
callq 0x3500
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2a944(%rip), %rsi     # 0x40075
callq 0x3350
movq %rbx, %rdi
movl %r14d, %esi
callq 0x3500
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2b30a(%rip), %rsi     # 0x40a5d
callq 0x3350
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31e0
leaq 0x80(%rsp), %rax
movq %rax, 0x70(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x2b71e(%rip), %rdx     # 0x40eb6
movq %rdx, %rsi
callq 0xb7d0
movq 0x8(%rsp), %rcx
subq (%rsp), %rcx
sarq $0x5, %rcx
movq 0x120(%rsp), %rax
cmpq %r15, %rcx
je 0x157e6
movq 0xc0(%rsp), %rax
leaq (%r12,%r12,2), %rcx
movq %r14, %rsi
shlq $0x5, %rsi
addq (%rax,%rcx,8), %rsi
movq 0x98(%rsp), %rdi
movq 0xa0(%rsp), %rdx
callq 0x15d4a
leaq 0x70(%rsp), %rdi
movq %rax, %rsi
callq 0x3150
movq 0xc0(%rsp), %rax
leaq (%r12,%r12,2), %rcx
movq (%rax,%rcx,8), %rax
movq %rbp, %rcx
shlq $0x5, %rcx
leaq 0x110(%rsp), %rdx
movq %rdx, 0x100(%rsp)
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
leaq 0x100(%rsp), %rdi
callq 0x6afc
cmpq %r12, %rbp
jbe 0x15865
movq 0x98(%rsp), %rdi
leaq 0x100(%rsp), %rsi
movq 0xa0(%rsp), %rdx
callq 0x15d4a
leaq 0x100(%rsp), %rdi
movq %rax, %rsi
callq 0x3150
movq 0xc0(%rsp), %rax
leaq (,%r13,2), %rcx
addq %r13, %rcx
movq (%rax,%rcx,8), %rax
movq %r14, %rcx
shlq $0x5, %rcx
leaq 0xf0(%rsp), %rdx
movq %rdx, 0xe0(%rsp)
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
leaq 0xe0(%rsp), %rdi
callq 0x6afc
subl 0xbc(%rsp), %ebp
cmpl $-0x2, %ebp
jg 0x158e5
movq 0x98(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
movq 0xa0(%rsp), %rdx
callq 0x15d4a
leaq 0xe0(%rsp), %rdi
movq %rax, %rsi
callq 0x3150
leaq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x30(%rsp)
movb %cl, 0x38(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, 0x48(%rsp)
movq %rcx, 0x50(%rsp)
movb %cl, 0x58(%rsp)
leaq 0x28(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x3150
leaq 0x138(%rsp), %rax
movq %rax, 0x128(%rsp)
movq 0x100(%rsp), %rsi
movq 0x108(%rsp), %rdx
addq %rsi, %rdx
leaq 0x128(%rsp), %rbx
movq %rbx, %rdi
callq 0x6afc
movq %rbx, %rdi
leaq 0x2b559(%rip), %rsi     # 0x40eb2
callq 0x3570
movq 0xe0(%rsp), %rsi
movq 0xe8(%rsp), %rdx
movq %rbx, %rdi
callq 0x30d0
leaq 0x160(%rsp), %rbx
movq %rbx, 0x150(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x159aa
movq %rdx, 0x150(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x160(%rsp)
jmp 0x159b2
movupd (%rcx), %xmm0
movupd %xmm0, (%rbx)
movq 0x8(%rax), %rdx
movq %rdx, 0x158(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x48(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0x3380
movq 0x150(%rsp), %rdi
cmpq %rbx, %rdi
je 0x159fc
movq 0x160(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x128(%rsp), %rdi
leaq 0x138(%rsp), %rax
cmpq %rax, %rdi
je 0x15a21
movq 0x138(%rsp), %rsi
incq %rsi
callq 0x32e0
movsd 0xa8(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x5, %rax
cmpq %r15, %rax
jne 0x15a5e
movq 0x120(%rsp), %rax
movsd 0x40(%rax), %xmm0
mulsd 0xa8(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x15a7a
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x15a96
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0xe0(%rsp), %rdi
leaq 0xf0(%rsp), %rax
cmpq %rax, %rdi
je 0x15abb
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x100(%rsp), %rdi
leaq 0x110(%rsp), %rax
cmpq %rax, %rdi
je 0x15ae0
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x15b02
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x32e0
movslq %r13d, %rax
cmpq %r14, %rax
movl 0x24(%rsp), %r8d
jge 0x15b1b
movq %r13, %rbp
testl %r8d, %r8d
jne 0x15649
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
leaq 0x1(%r12), %rsi
movq %rcx, %rdx
subq %rax, %rdx
movq %rdx, %rdi
shrq $0x5, %rdi
movslq %edi, %rdi
subq %r15, %rdi
cmpq %rdi, %r12
movq %rsi, %r12
jl 0x155ee
cmpq $0x2, %r15
movq 0x148(%rsp), %r15
jg 0x15591
leaq 0xc0(%rsp), %rdi
callq 0x17698
movq 0x3f43a(%rip), %rsi     # 0x54fa8
leaq 0x170(%rsp), %rdi
callq 0x3230
leaq 0x1f0(%rsp), %rdi
callq 0x30e0
movq %rsp, %rdi
callq 0x6186
xorl %eax, %eax
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x15d39
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0x15d39
movq %rax, %rbx
jmp 0x15d39
jmp 0x15d08
movq %rax, %rbx
jmp 0x15d18
jmp 0x15c32
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x15d18
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0x15d18
jmp 0x15d08
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x15c35
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0x15c35
jmp 0x15c32
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x6186
jmp 0x15d0b
jmp 0x15c88
jmp 0x15c59
jmp 0x15ce1
jmp 0x15c5e
jmp 0x15d08
movq %rax, %rbx
jmp 0x15c95
movq %rax, %rbx
jmp 0x15cba
movq %rax, %rbx
movq 0x128(%rsp), %rdi
leaq 0x138(%rsp), %rax
cmpq %rax, %rdi
je 0x15c8b
movq 0x138(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0x15c8b
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x17662
movq 0xe0(%rsp), %rdi
leaq 0xf0(%rsp), %rax
cmpq %rax, %rdi
je 0x15cba
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x100(%rsp), %rdi
leaq 0x110(%rsp), %rax
cmpq %rax, %rdi
je 0x15ce4
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0x15ce4
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x15d0b
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0x15d0b
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x17698
movq 0x3f289(%rip), %rsi     # 0x54fa8
leaq 0x170(%rsp), %rdi
callq 0x3230
leaq 0x1f0(%rsp), %rdi
callq 0x30e0
movq %rsp, %rdi
callq 0x6186
movq %rbx, %rdi
callq 0x3530
nop