// 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, ListRuleSet&)
int binarize(Rule& rule, InterNodes& interNodes,string prefix, Direct dir, RuleSet& ruleSet)
{
	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:"<<wrds.size()<<endl;
	if(wrds.size()==1) ruleSet.add(rule);
	for(int k=(int)wrds.size();k>1;k--)
	{
		for(int i=0;(size_t)i<=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;
				//jout<<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);
				ruleSet.add(newRule);
				//jout<<endl;
			}
		}
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x328, %rsp            # imm = 0x328
movq %r8, 0x130(%rsp)
movl %ecx, 0xc(%rsp)
movq %rdx, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq %rdi, 0xb0(%rsp)
movq 0x20(%rdi), %rsi
leaq 0x38(%rsp), %rdi
leaq 0x80(%rsp), %rdx
callq 0xb5b4
leaq 0x1a0(%rsp), %rdi
leaq 0x38(%rsp), %rsi
movl $0x18, %edx
callq 0x3480
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x16b06
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x32e0
leaq 0x2a3a9(%rip), %rbx     # 0x40eb6
leaq 0x38(%rsp), %r14
leaq 0x1a0(%rsp), %r15
leaq 0x10(%rsp), %r12
movq 0x1a0(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x1c0(%rsp,%rax)
jne 0x16b96
leaq 0x48(%rsp), %rax
movq %rax, 0x38(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq %rbx, %rdx
callq 0xb7d0
movq %r15, %rdi
movq %r14, %rsi
callq 0x33a0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x30c0
movl %eax, %r13d
testl %eax, %eax
je 0x16b75
movq %r12, %rdi
movq %r14, %rsi
callq 0x17546
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x16b91
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x32e0
testl %r13d, %r13d
jne 0x16b1f
xorpd %xmm0, %xmm0
movapd %xmm0, 0xd0(%rsp)
movq $0x0, 0xe0(%rsp)
movq 0x10(%rsp), %rdx
movq %rdx, %rax
cmpq %rdx, 0x18(%rsp)
je 0x16db8
xorl %r13d, %r13d
leaq 0x80(%rsp), %rbx
leaq 0x38(%rsp), %r12
xorpd %xmm0, %xmm0
movapd %xmm0, 0x80(%rsp)
movq $0x0, 0x90(%rsp)
leaq 0xd0(%rsp), %rdi
movq %rbx, %rsi
callq 0x17584
movq 0x10(%rsp), %rsi
cmpq %rsi, 0x18(%rsp)
je 0x16c98
leaq (,%r13,2), %rbp
addq %r13, %rbp
movq %r13, %rbx
shlq $0x5, %rbx
xorl %r14d, %r14d
movq 0xd0(%rsp), %r15
cmpq %r14, %r13
jne 0x16c3b
leaq (%r15,%rbp,8), %rdi
addq %rbx, %rsi
callq 0x17546
jmp 0x16c7f
leaq 0x48(%rsp), %rax
movq %rax, 0x38(%rsp)
movq %r12, %rdi
leaq 0x2a267(%rip), %rdx     # 0x40eb6
movq %rdx, %rsi
callq 0xb7d0
leaq (%r15,%rbp,8), %rdi
movq %r12, %rsi
callq 0x7f46
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x16c7f
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x32e0
incq %r14
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq %r14, %rax
ja 0x16c20
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
callq 0x6186
incq %r13
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rax
movq %rax, %rcx
subq %rdx, %rcx
sarq $0x5, %rcx
cmpq %r13, %rcx
ja 0x16bd2
cmpq %rdx, %rax
je 0x16db8
movl $0x1, %r12d
movl $0x20, %r15d
xorl %edi, %edi
leaq 0x38(%rsp), %rbx
leaq 0x1(%rdi), %r8
movq %rax, %rcx
subq %rdx, %rcx
movq %rcx, %rsi
sarq $0x5, %rsi
movq %r8, 0x28(%rsp)
cmpq %r8, %rsi
jbe 0x16d7e
leaq (%rdi,%rdi,2), %r13
movq %r15, %rbp
movq %r12, %r14
movq 0xd0(%rsp), %rax
movq (%rax,%r13,8), %rax
leaq (%rax,%rbp), %rsi
addq $-0x20, %rsi
addq %rbp, %rdx
movq %rbx, %rdi
callq 0x175b0
movq 0xd0(%rsp), %rax
movq (%rax,%r13,8), %rdi
addq %rbp, %rdi
movq %rbx, %rsi
callq 0x3380
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x16d5b
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x32e0
incq %r14
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rax
movq %rax, %rcx
subq %rdx, %rcx
movq %rcx, %rsi
sarq $0x5, %rsi
addq $0x20, %rbp
cmpq %r14, %rsi
ja 0x16d09
incq %r12
addq $0x20, %r15
movq 0x28(%rsp), %rdi
cmpq %rdi, %rsi
ja 0x16ce4
cmpq $0x20, %rcx
jne 0x16db8
movq 0x130(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x3cc20
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rax
movq %rax, %rdi
subq %rdx, %rdi
shrq $0x5, %rdi
cmpl $0x2, %edi
jl 0x17355
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %ecx
subl %edi, %ecx
movl %ecx, 0x34(%rsp)
movl %edi, %ecx
addl $-0x2, %ecx
movl %ecx, 0x30(%rsp)
leaq 0x80(%rsp), %r13
leaq -0x1(%rdi), %rcx
cmpl $0x2, 0xc(%rsp)
movq %rcx, 0x160(%rsp)
je 0x16e13
movsd 0x29380(%rip), %xmm0    # 0x40188
movsd %xmm0, 0xb8(%rsp)
jmp 0x16e2f
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd 0x29366(%rip), %xmm1    # 0x40188
divsd %xmm0, %xmm1
movsd %xmm1, 0xb8(%rsp)
movq %rax, %rcx
subq %rdx, %rcx
sarq $0x5, %rcx
subq %rdi, %rcx
movl $0x8, %r12d
movl $0x18, %r14d
movabsq $0x100000000, %r15      # imm = 0x100000000
movl 0x30(%rsp), %esi
movl %esi, %r9d
xorl %ebx, %ebx
movq %rdi, 0xc8(%rsp)
movl %r9d, %r9d
cmpl $0x0, 0xc(%rsp)
jne 0x16e76
cmpq %rbx, %rcx
jne 0x17306
cmpl $0x1, 0xc(%rsp)
sete %cl
testq %rbx, %rbx
setne %sil
testb %sil, %cl
jne 0x1733b
leaq (%rbx,%rdi), %rcx
decq %rcx
movq %rcx, 0x28(%rsp)
cmpq %rcx, %rbx
jge 0x17306
movq %r9, 0x138(%rsp)
movq %r15, 0x168(%rsp)
movq %r14, 0x170(%rsp)
movl 0x34(%rsp), %ebp
movq %r12, 0x178(%rsp)
movq %rbx, 0xc0(%rsp)
movl 0xc(%rsp), %eax
cmpl $0x1, %eax
jne 0x16ee6
cmpq %rbx, 0x138(%rsp)
jne 0x17298
leaq 0x90(%rsp), %rax
movq %rax, 0x80(%rsp)
movq %r13, %rdi
leaq 0x29fb6(%rip), %rdx     # 0x40eb6
movq %rdx, %rsi
callq 0xb7d0
movq 0x18(%rsp), %rcx
subq 0x10(%rsp), %rcx
sarq $0x5, %rcx
movq 0xb0(%rsp), %rax
cmpq 0xc8(%rsp), %rcx
je 0x16f5e
movq 0xd0(%rsp), %rax
movq 0xc0(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x28(%rsp), %rsi
shlq $0x5, %rsi
addq (%rax,%rcx,8), %rsi
movq 0xa0(%rsp), %rdi
movq 0xa8(%rsp), %rdx
callq 0x15d4a
movq %r13, %rdi
movq %rax, %rsi
callq 0x3150
movq 0xd0(%rsp), %rax
movq 0xc0(%rsp), %r13
leaq (,%r13,2), %rcx
addq %r13, %rcx
movq (%rax,%rcx,8), %rax
leaq 0x120(%rsp), %rcx
movq %rcx, 0x110(%rsp)
movq -0x8(%rax,%r12), %rsi
movq (%rax,%r12), %rdx
addq %rsi, %rdx
leaq 0x110(%rsp), %rdi
callq 0x6afc
cmpq %r13, %rbx
jbe 0x16fe3
movq 0xa0(%rsp), %rdi
leaq 0x110(%rsp), %rsi
movq 0xa8(%rsp), %rdx
callq 0x15d4a
leaq 0x110(%rsp), %rdi
movq %rax, %rsi
callq 0x3150
movq 0xd0(%rsp), %rax
movq (%rax,%r14), %rax
movq 0x28(%rsp), %rcx
shlq $0x5, %rcx
leaq 0x100(%rsp), %rdx
movq %rdx, 0xf0(%rsp)
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
leaq 0xf0(%rsp), %rdi
callq 0x6afc
cmpl $-0x2, %ebp
jg 0x17053
movq 0xa0(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
callq 0x15d4a
leaq 0xf0(%rsp), %rdi
movq %rax, %rsi
callq 0x3150
leaq 0x48(%rsp), %rax
movq %rax, 0x38(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x40(%rsp)
movb %cl, 0x48(%rsp)
leaq 0x68(%rsp), %rax
movq %rax, 0x58(%rsp)
movq %rcx, 0x60(%rsp)
movb %cl, 0x68(%rsp)
leaq 0x38(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x3150
leaq 0x150(%rsp), %rax
movq %rax, 0x140(%rsp)
movq 0x110(%rsp), %rsi
movq 0x118(%rsp), %rdx
addq %rsi, %rdx
leaq 0x140(%rsp), %r13
movq %r13, %rdi
callq 0x6afc
movq %r13, %rdi
leaq 0x29de8(%rip), %rsi     # 0x40eb2
callq 0x3570
movq 0xf0(%rsp), %rsi
movq 0xf8(%rsp), %rdx
movq %r13, %rdi
callq 0x30d0
leaq 0x190(%rsp), %r13
movq %r13, 0x180(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x1711b
movq %rdx, 0x180(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x190(%rsp)
jmp 0x17125
movupd (%rcx), %xmm0
movupd %xmm0, (%r13)
movq 0x8(%rax), %rdx
movq %rdx, 0x188(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x58(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0x3380
movq 0x180(%rsp), %rdi
cmpq %r13, %rdi
je 0x1716f
movq 0x190(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x140(%rsp), %rdi
leaq 0x150(%rsp), %rax
cmpq %rax, %rdi
leaq 0x80(%rsp), %r13
je 0x1719c
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x32e0
movsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x5, %rax
cmpq 0xc8(%rsp), %rax
jne 0x171df
movq 0xb0(%rsp), %rax
movsd 0x40(%rax), %xmm0
mulsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
movq 0x130(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x3cc20
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x1720d
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x17229
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0xf0(%rsp), %rdi
leaq 0x100(%rsp), %rax
cmpq %rax, %rdi
je 0x1724e
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x110(%rsp), %rdi
leaq 0x120(%rsp), %rax
cmpq %rax, %rdi
je 0x17273
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x17298
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x32e0
movq %r15, %rax
sarq $0x20, %rax
cmpq 0x28(%rsp), %rax
movl 0xc(%rsp), %eax
jge 0x172cc
incq %rbx
addq $0x20, %r12
incl %ebp
addq $0x18, %r14
movabsq $0x100000000, %rcx      # imm = 0x100000000
addq %rcx, %r15
testl %eax, %eax
jne 0x16ed3
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rax
movq 0xc8(%rsp), %rdi
movq 0x178(%rsp), %r12
movq 0x170(%rsp), %r14
movq 0x168(%rsp), %r15
movq 0x138(%rsp), %r9
movq 0xc0(%rsp), %rbx
leaq 0x1(%rbx), %rsi
movq %rax, %rcx
subq %rdx, %rcx
sarq $0x5, %rcx
subq %rdi, %rcx
addq $0x20, %r12
incl %r9d
addq $0x18, %r14
movabsq $0x100000000, %r8       # imm = 0x100000000
addq %r8, %r15
cmpq %rbx, %rcx
movq %rsi, %rbx
ja 0x16e63
incl 0x34(%rsp)
decl 0x30(%rsp)
cmpq $0x2, %rdi
movq 0x160(%rsp), %rdi
jg 0x16ded
leaq 0xd0(%rsp), %rdi
callq 0x17698
movq 0x3dc3f(%rip), %rsi     # 0x54fa8
leaq 0x1a0(%rsp), %rdi
callq 0x3230
leaq 0x220(%rsp), %rdi
callq 0x30e0
leaq 0x10(%rsp), %rdi
callq 0x6186
xorl %eax, %eax
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x17441
movq %rax, %rbx
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x17534
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0x17534
movq %rax, %rbx
jmp 0x17534
movq %rax, %rbx
jmp 0x17513
jmp 0x1742c
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x17513
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0x17513
jmp 0x17441
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x1742f
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0x1742f
jmp 0x1742c
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x6186
jmp 0x17506
movq %rax, %rbx
jmp 0x17506
jmp 0x1745b
movq %rax, %rbx
jmp 0x17497
jmp 0x17454
jmp 0x1748a
movq %rax, %rbx
jmp 0x174bc
jmp 0x17485
movq %rax, %rbx
movq 0x140(%rsp), %rdi
leaq 0x150(%rsp), %rax
cmpq %rax, %rdi
je 0x1748d
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0x1748d
movq %rax, %rbx
jmp 0x174e1
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x17662
movq 0xf0(%rsp), %rdi
leaq 0x100(%rsp), %rax
cmpq %rax, %rdi
je 0x174bc
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x110(%rsp), %rdi
leaq 0x120(%rsp), %rax
cmpq %rax, %rdi
je 0x174e1
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x17506
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x32e0
leaq 0xd0(%rsp), %rdi
callq 0x17698
movq 0x3da8e(%rip), %rsi     # 0x54fa8
leaq 0x1a0(%rsp), %rdi
callq 0x3230
leaq 0x220(%rsp), %rdi
callq 0x30e0
leaq 0x10(%rsp), %rdi
callq 0x6186
movq %rbx, %rdi
callq 0x3530