// File: /workspace/github_cpp_makefile_star2_O1/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, 0x140(%rsp)
movl %ecx, %ebp
movq %rdx, 0xd8(%rsp)
movq %rsi, 0xd0(%rsp)
movq %rdi, 0x88(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq 0x8(%rsp), %rbx
testq %rbx, %rbx
je 0x16fd6
movl $0x10, %r14d
movq -0x10(%r14), %rdi
cmpq %rdi, %r14
je 0x16fbd
movq (%r14), %rsi
incq %rsi
callq 0x32e0
leaq 0x20(%r14), %rax
addq $0x10, %r14
cmpq %rbx, %r14
movq %rax, %r14
jne 0x16fa9
movq $0x0, 0x8(%rsp)
movq 0x88(%rsp), %rax
movq 0x20(%rax), %rsi
leaq 0x40(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
callq 0xb648
leaq 0x1a0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movl $0x18, %edx
callq 0x3480
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x17026
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32e0
leaq 0x2ae69(%rip), %rbx     # 0x41e96
leaq 0x40(%rsp), %r14
leaq 0x1a0(%rsp), %r15
movq %rsp, %r12
movq 0x1a0(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x1c0(%rsp,%rax)
jne 0x170b4
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq %rbx, %rdx
callq 0xb8a2
movq %r15, %rdi
movq %r14, %rsi
callq 0x33a0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x30c0
movl %eax, %r13d
testl %eax, %eax
je 0x17093
movq %r12, %rdi
movq %r14, %rsi
callq 0x17a82
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x170af
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32e0
testl %r13d, %r13d
jne 0x1703d
xorpd %xmm0, %xmm0
movapd %xmm0, 0x90(%rsp)
movq $0x0, 0xa0(%rsp)
movq 0x98(%rsp), %r14
testq %r14, %r14
je 0x170f9
xorl %ebx, %ebx
movq %rbx, %rdi
callq 0x611e
addq $0x18, %rbx
cmpq %r14, %rbx
jne 0x170dc
movq $0x0, 0x98(%rsp)
movl %ebp, 0x1c(%rsp)
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
je 0x17209
xorl %r13d, %r13d
leaq 0xb0(%rsp), %rbx
leaq 0x40(%rsp), %r12
xorpd %xmm0, %xmm0
movapd %xmm0, 0xb0(%rsp)
movq $0x0, 0xc0(%rsp)
leaq 0x90(%rsp), %rdi
movq %rbx, %rsi
callq 0x17ac0
movq (%rsp), %rsi
cmpq %rsi, 0x8(%rsp)
je 0x171e0
leaq (,%r13,2), %rbp
addq %r13, %rbp
movq %r13, %rbx
shlq $0x5, %rbx
xorl %r14d, %r14d
movq 0x90(%rsp), %r15
cmpq %r14, %r13
jne 0x17184
leaq (%r15,%rbp,8), %rdi
addq %rbx, %rsi
callq 0x17a82
jmp 0x171c8
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %r12, %rdi
leaq 0x2acfe(%rip), %rdx     # 0x41e96
movq %rdx, %rsi
callq 0xb8a2
leaq (%r15,%rbp,8), %rdi
movq %r12, %rsi
callq 0x7f06
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x171c8
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32e0
incq %r14
movq (%rsp), %rsi
movq 0x8(%rsp), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq %r14, %rax
ja 0x17169
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
callq 0x611e
incq %r13
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x5, %rax
cmpq %r13, %rax
ja 0x1711c
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
je 0x17304
movl $0x1, %r12d
movl $0x20, %r15d
xorl %edx, %edx
leaq 0x40(%rsp), %rbx
movq %rdx, %rax
leaq 0x1(%rdx), %rsi
movq (%rsp), %rdx
movq 0x8(%rsp), %rcx
subq %rdx, %rcx
sarq $0x5, %rcx
movq %rsi, 0x30(%rsp)
cmpq %rsi, %rcx
jbe 0x172c4
leaq (%rax,%rax,2), %r13
movq %r15, %rbp
movq %r12, %r14
movq 0x90(%rsp), %rax
movq (%rax,%r13,8), %rax
leaq (%rax,%rbp), %rsi
addq $-0x20, %rsi
addq %rbp, %rdx
movq %rbx, %rdi
callq 0x17aec
movq 0x90(%rsp), %rax
movq (%rax,%r13,8), %rdi
addq %rbp, %rdi
movq %rbx, %rsi
callq 0x3380
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x172a8
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32e0
incq %r14
movq (%rsp), %rdx
movq 0x8(%rsp), %rax
subq %rdx, %rax
sarq $0x5, %rax
addq $0x20, %rbp
cmpq %r14, %rax
ja 0x17256
movq 0x8(%rsp), %rax
subq (%rsp), %rax
movq %rax, %rcx
sarq $0x5, %rcx
incq %r12
addq $0x20, %r15
movq 0x30(%rsp), %rdx
cmpq %rdx, %rcx
ja 0x1722b
cmpq $0x20, %rax
jne 0x17304
movq 0x140(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x3d8e0
movq 0x8(%rsp), %rax
subq (%rsp), %rax
shrq $0x5, %rax
cmpl $0x2, %eax
movl 0x1c(%rsp), %ecx
jl 0x17895
movabsq $0x100000000, %rsi      # imm = 0x100000000
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl $0x1, %edx
subl %eax, %edx
movl %edx, 0x3c(%rsp)
movl %eax, %edi
addl $-0x2, %edi
leaq 0xb0(%rsp), %r14
movq %rax, %r8
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x5, %rax
leaq -0x1(%r8), %rdx
movq %rdx, 0x168(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
movq %r8, 0x28(%rsp)
subq %r8, %rax
movsd 0x29e10(%rip), %xmm1    # 0x41188
divsd %xmm0, %xmm1
movsd %xmm1, 0x170(%rsp)
movl $0x8, %edx
movq %rdx, 0x138(%rsp)
movl $0x18, %edx
movq %rdx, 0x130(%rsp)
movq %rsi, 0x128(%rsp)
movl %edi, 0xe4(%rsp)
xorl %r8d, %r8d
movq %r8, 0x20(%rsp)
movl %edi, %edi
testl %ecx, %ecx
movl %ecx, %edx
jne 0x173c9
cmpq 0x20(%rsp), %rax
jne 0x1782f
cmpl $0x1, %edx
sete %al
movq 0x20(%rsp), %r8
testq %r8, %r8
setne %cl
testb %cl, %al
movl %edx, %ecx
jne 0x17874
movq 0x28(%rsp), %rax
addq %r8, %rax
decq %rax
movq %rax, 0x30(%rsp)
cmpq %rax, %r8
movl %ecx, %edx
jge 0x1782f
movq 0x128(%rsp), %r12
movq 0x130(%rsp), %r13
movl 0x3c(%rsp), %ebp
movq 0x138(%rsp), %r15
movq 0x20(%rsp), %rbx
movq %rdi, 0x178(%rsp)
cmpl $0x1, %edx
jne 0x17436
cmpq %rbx, %rdi
jne 0x17805
leaq 0xc0(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq %r14, %rdi
leaq 0x2aa46(%rip), %rdx     # 0x41e96
movq %rdx, %rsi
callq 0xb8a2
movq 0x8(%rsp), %rcx
subq (%rsp), %rcx
sarq $0x5, %rcx
movq 0x88(%rsp), %rax
cmpq 0x28(%rsp), %rcx
je 0x174a7
movq 0x90(%rsp), %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x30(%rsp), %rsi
shlq $0x5, %rsi
addq (%rax,%rcx,8), %rsi
movq 0xd0(%rsp), %rdi
movq 0xd8(%rsp), %rdx
callq 0x160bc
movq %r14, %rdi
movq %rax, %rsi
callq 0x3150
movq 0x90(%rsp), %rax
movq 0x20(%rsp), %r14
leaq (%r14,%r14,2), %rcx
movq (%rax,%rcx,8), %rax
leaq 0x118(%rsp), %rcx
movq %rcx, 0x108(%rsp)
movq -0x8(%rax,%r15), %rsi
movq (%rax,%r15), %rdx
addq %rsi, %rdx
leaq 0x108(%rsp), %rdi
callq 0x6a88
cmpq %r14, %rbx
jbe 0x17522
movq 0xd0(%rsp), %rdi
leaq 0x108(%rsp), %rsi
movq 0xd8(%rsp), %rdx
callq 0x160bc
leaq 0x108(%rsp), %rdi
movq %rax, %rsi
callq 0x3150
movq 0x90(%rsp), %rax
movq (%rax,%r13), %rax
movq 0x30(%rsp), %rcx
shlq $0x5, %rcx
leaq 0xf8(%rsp), %rdx
movq %rdx, 0xe8(%rsp)
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
leaq 0xe8(%rsp), %rdi
callq 0x6a88
cmpl $-0x2, %ebp
jg 0x17592
movq 0xd0(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
movq 0xd8(%rsp), %rdx
callq 0x160bc
leaq 0xe8(%rsp), %rdi
movq %rax, %rsi
callq 0x3150
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x48(%rsp)
movb %cl, 0x50(%rsp)
leaq 0x70(%rsp), %rax
movq %rax, 0x60(%rsp)
movq %rcx, 0x68(%rsp)
movb %cl, 0x70(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x3150
leaq 0x158(%rsp), %rax
movq %rax, 0x148(%rsp)
movq 0x108(%rsp), %rsi
movq 0x110(%rsp), %rdx
addq %rsi, %rdx
leaq 0x148(%rsp), %r14
movq %r14, %rdi
callq 0x6a88
movq %r14, %rdi
leaq 0x2a889(%rip), %rsi     # 0x41e92
callq 0x3570
movq 0xe8(%rsp), %rsi
movq 0xf0(%rsp), %rdx
movq %r14, %rdi
callq 0x30d0
leaq 0x190(%rsp), %r14
movq %r14, 0x180(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x1765a
movq %rdx, 0x180(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x190(%rsp)
jmp 0x17663
movupd (%rcx), %xmm0
movupd %xmm0, (%r14)
movq 0x8(%rax), %rdx
movq %rdx, 0x188(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x60(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0x3380
movq 0x180(%rsp), %rdi
cmpq %r14, %rdi
je 0x176ad
movq 0x190(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x148(%rsp), %rdi
leaq 0x158(%rsp), %rax
cmpq %rax, %rdi
leaq 0xb0(%rsp), %r14
je 0x176da
movq 0x158(%rsp), %rsi
incq %rsi
callq 0x32e0
cmpl $0x2, 0x1c(%rsp)
jne 0x176f5
movsd 0x170(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
jmp 0x17707
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x80(%rsp)
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x5, %rax
cmpq 0x28(%rsp), %rax
jne 0x1773a
movq 0x88(%rsp), %rax
movsd 0x40(%rax), %xmm0
mulsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0x140(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x3d8e0
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x17768
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x17784
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0xe8(%rsp), %rdi
leaq 0xf8(%rsp), %rax
cmpq %rax, %rdi
je 0x177a9
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x177ce
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x177f3
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x32e0
movabsq $0x100000000, %rsi      # imm = 0x100000000
movq 0x178(%rsp), %rdi
movq %r12, %rax
sarq $0x20, %rax
cmpq 0x30(%rsp), %rax
movl 0x1c(%rsp), %edx
jge 0x1782f
incq %rbx
addq $0x20, %r15
incl %ebp
addq $0x18, %r13
addq %rsi, %r12
testl %edx, %edx
jne 0x17428
movq 0x20(%rsp), %r8
leaq 0x1(%r8), %rcx
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x5, %rax
subq 0x28(%rsp), %rax
addq $0x20, 0x138(%rsp)
incl %edi
addq $0x18, 0x130(%rsp)
addq %rsi, 0x128(%rsp)
cmpq %r8, %rax
movq %rcx, %r8
movl %edx, %ecx
ja 0x173b1
incl 0x3c(%rsp)
movl 0xe4(%rsp), %edi
decl %edi
cmpq $0x2, 0x28(%rsp)
movq 0x168(%rsp), %rax
jg 0x17345
leaq 0x90(%rsp), %rdi
callq 0x17bd4
movq 0x3e6ff(%rip), %rsi     # 0x55fa8
leaq 0x1a0(%rsp), %rdi
callq 0x3230
leaq 0x220(%rsp), %rdi
callq 0x30e0
movq %rsp, %rdi
callq 0x611e
xorl %eax, %eax
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1797f
movq %rax, %rbx
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x17a72
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0x17a72
movq %rax, %rbx
jmp 0x17a72
movq %rax, %rbx
jmp 0x17a51
jmp 0x17968
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x17a51
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0x17a51
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x1796b
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0x1796b
jmp 0x17968
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x611e
jmp 0x17a44
jmp 0x1797f
movq %rax, %rbx
jmp 0x17a44
jmp 0x17999
movq %rax, %rbx
jmp 0x179d5
jmp 0x17992
jmp 0x179c8
movq %rax, %rbx
jmp 0x179fa
jmp 0x179c3
movq %rax, %rbx
movq 0x148(%rsp), %rdi
leaq 0x158(%rsp), %rax
cmpq %rax, %rdi
je 0x179cb
movq 0x158(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0x179cb
movq %rax, %rbx
jmp 0x17a1f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x17b9e
movq 0xe8(%rsp), %rdi
leaq 0xf8(%rsp), %rax
cmpq %rax, %rdi
je 0x179fa
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x17a1f
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x17a44
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x32e0
leaq 0x90(%rsp), %rdi
callq 0x17bd4
movq 0x3e550(%rip), %rsi     # 0x55fa8
leaq 0x1a0(%rsp), %rdi
callq 0x3230
leaq 0x220(%rsp), %rdi
callq 0x30e0
movq %rsp, %rdi
callq 0x611e
movq %rbx, %rdi
callq 0x3530