// File: 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
movq %rsp, %rbp
subq $0x490, %rsp            # imm = 0x490
movq %rdx, -0x3d8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
leaq -0x40(%rbp), %rdi
movq %rdi, -0x3d0(%rbp)
callq 0x8340
movq -0x3d0(%rbp), %rdi
callq 0x8360
movq -0x8(%rbp), %rdi
addq $0x20, %rdi
callq 0x4150
movq %rax, -0x3c8(%rbp)
leaq -0x1e9(%rbp), %rdi
movq %rdi, -0x3c0(%rbp)
callq 0x4660
movq -0x3c8(%rbp), %rsi
movq -0x3c0(%rbp), %rdx
leaq -0x1e8(%rbp), %rdi
callq 0x1c730
jmp 0x364e4
movl $0x10, %edi
movl $0x8, %esi
callq 0x1c7e0
movl %eax, -0x3dc(%rbp)
jmp 0x364fb
movl -0x3dc(%rbp), %edx
leaq -0x1c8(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x45b0
jmp 0x36516
leaq -0x1e8(%rbp), %rdi
callq 0x4728
leaq -0x1e9(%rbp), %rdi
callq 0x43f0
movq -0x1c8(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x1c8(%rbp,%rax), %rdi
callq 0x46a0
movb %al, -0x3dd(%rbp)
jmp 0x3654e
movb -0x3dd(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x3655f
jmp 0x366c3
leaq -0x221(%rbp), %rdi
movq %rdi, -0x3e8(%rbp)
callq 0x4660
movq -0x3e8(%rbp), %rdx
leaq 0x5a8d6(%rip), %rsi     # 0x90e56
leaq -0x220(%rbp), %rdi
callq 0x1c730
jmp 0x3658e
leaq -0x221(%rbp), %rdi
callq 0x43f0
leaq -0x1c8(%rbp), %rdi
leaq -0x220(%rbp), %rsi
callq 0x44a0
jmp 0x365af
leaq 0x5a8a0(%rip), %rsi     # 0x90e56
leaq -0x220(%rbp), %rdi
callq 0xa020
movb %al, -0x3e9(%rbp)
jmp 0x365ca
movb -0x3e9(%rbp), %al
testb $0x1, %al
jne 0x365d9
jmp 0x36688
movl $0x3, -0x228(%rbp)
jmp 0x366a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f8(%rbp)
movl %eax, -0x1fc(%rbp)
jmp 0x3661a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f8(%rbp)
movl %eax, -0x1fc(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0x4728
leaq -0x1e9(%rbp), %rdi
callq 0x43f0
jmp 0x36fea
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f8(%rbp)
movl %eax, -0x1fc(%rbp)
jmp 0x36fde
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f8(%rbp)
movl %eax, -0x1fc(%rbp)
leaq -0x221(%rbp), %rdi
callq 0x43f0
jmp 0x36fde
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f8(%rbp)
movl %eax, -0x1fc(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x4728
jmp 0x36fde
leaq -0x40(%rbp), %rdi
leaq -0x220(%rbp), %rsi
callq 0x37000
jmp 0x3669a
movl $0x0, -0x228(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x4728
movl -0x228(%rbp), %eax
testl %eax, %eax
je 0x366be
jmp 0x366bc
jmp 0x366c3
jmp 0x3652e
leaq -0x240(%rbp), %rdi
callq 0x37070
leaq -0x240(%rbp), %rdi
callq 0x37090
movl $0x0, -0x244(%rbp)
movslq -0x244(%rbp), %rax
movq %rax, -0x3f8(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x87f0
movq %rax, %rcx
movq -0x3f8(%rbp), %rax
cmpq %rcx, %rax
jae 0x368cf
leaq -0x260(%rbp), %rdi
movq %rdi, -0x400(%rbp)
callq 0x8340
movq -0x400(%rbp), %rsi
leaq -0x240(%rbp), %rdi
callq 0x370b0
jmp 0x36737
movl $0x0, -0x264(%rbp)
movslq -0x264(%rbp), %rax
movq %rax, -0x408(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x87f0
movq %rax, %rcx
movq -0x408(%rbp), %rax
cmpq %rcx, %rax
jae 0x3689e
movl -0x244(%rbp), %eax
cmpl -0x264(%rbp), %eax
jne 0x367d0
movslq -0x244(%rbp), %rsi
leaq -0x240(%rbp), %rdi
callq 0x37120
movq %rax, -0x410(%rbp)
movslq -0x244(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x8810
movq -0x410(%rbp), %rdi
movq %rax, %rsi
callq 0x37000
jmp 0x367b4
jmp 0x36888
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f8(%rbp)
movl %eax, -0x1fc(%rbp)
jmp 0x368be
movslq -0x244(%rbp), %rsi
leaq -0x240(%rbp), %rdi
callq 0x37120
movq %rax, -0x420(%rbp)
leaq -0x289(%rbp), %rdi
movq %rdi, -0x418(%rbp)
callq 0x4660
movq -0x418(%rbp), %rdx
leaq 0x5a64b(%rip), %rsi     # 0x90e56
leaq -0x288(%rbp), %rdi
callq 0x1c730
jmp 0x36819
movq -0x420(%rbp), %rdi
leaq -0x288(%rbp), %rsi
callq 0x8540
jmp 0x3682e
leaq -0x288(%rbp), %rdi
callq 0x4728
leaq -0x289(%rbp), %rdi
callq 0x43f0
jmp 0x36888
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f8(%rbp)
movl %eax, -0x1fc(%rbp)
jmp 0x3687a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f8(%rbp)
movl %eax, -0x1fc(%rbp)
leaq -0x288(%rbp), %rdi
callq 0x4728
leaq -0x289(%rbp), %rdi
callq 0x43f0
jmp 0x368be
jmp 0x3688a
movl -0x264(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x264(%rbp)
jmp 0x36741
leaq -0x260(%rbp), %rdi
callq 0x8830
movl -0x244(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x244(%rbp)
jmp 0x366e5
leaq -0x260(%rbp), %rdi
callq 0x8830
jmp 0x36fd2
movl $0x0, -0x290(%rbp)
movslq -0x290(%rbp), %rax
movq %rax, -0x428(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x87f0
movq %rax, %rcx
movq -0x428(%rbp), %rax
cmpq %rcx, %rax
jae 0x36a0f
movl -0x290(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x294(%rbp)
movslq -0x294(%rbp), %rax
movq %rax, -0x430(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x87f0
movq %rax, %rcx
movq -0x430(%rbp), %rax
cmpq %rcx, %rax
jae 0x369f9
movslq -0x290(%rbp), %rsi
leaq -0x240(%rbp), %rdi
callq 0x37120
movq %rax, %rdi
movl -0x294(%rbp), %eax
decl %eax
movslq %eax, %rsi
callq 0x8810
movq %rax, -0x438(%rbp)
movslq -0x294(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x8810
movq -0x438(%rbp), %rsi
movq %rax, %rdx
leaq -0x2b8(%rbp), %rdi
callq 0x37140
jmp 0x36991
movslq -0x290(%rbp), %rsi
leaq -0x240(%rbp), %rdi
callq 0x37120
movq %rax, %rdi
movslq -0x294(%rbp), %rsi
callq 0x8810
movq %rax, %rdi
leaq -0x2b8(%rbp), %rsi
callq 0x4470
leaq -0x2b8(%rbp), %rdi
callq 0x4728
movl -0x294(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x294(%rbp)
jmp 0x36912
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f8(%rbp)
movl %eax, -0x1fc(%rbp)
jmp 0x36fd2
jmp 0x369fb
movl -0x290(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x290(%rbp)
jmp 0x368d9
leaq -0x40(%rbp), %rdi
callq 0x87f0
cmpq $0x1, %rax
jne 0x36a2f
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x88fb0
jmp 0x36a2d
jmp 0x36a2f
leaq -0x40(%rbp), %rdi
callq 0x87f0
movl %eax, -0x2bc(%rbp)
cmpl $0x1, -0x2bc(%rbp)
jle 0x36f9c
movl $0x0, -0x2c0(%rbp)
movslq -0x2c0(%rbp), %rax
movq %rax, -0x440(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x87f0
movq %rax, %rcx
movq -0x440(%rbp), %rax
movslq -0x2bc(%rbp), %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
ja 0x36f86
cmpl $0x0, -0x1c(%rbp)
jne 0x36ac4
movslq -0x2c0(%rbp), %rax
movq %rax, -0x448(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x87f0
movq %rax, %rcx
movq -0x448(%rbp), %rax
movslq -0x2bc(%rbp), %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
je 0x36ac4
jmp 0x36f72
cmpl $0x1, -0x1c(%rbp)
jne 0x36ad8
cmpl $0x0, -0x2c0(%rbp)
je 0x36ad8
jmp 0x36f86
movl -0x2c0(%rbp), %eax
movl %eax, -0x2c4(%rbp)
movl -0x2c4(%rbp), %eax
movl -0x2c0(%rbp), %ecx
addl -0x2bc(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x36f70
cmpl $0x0, -0x1c(%rbp)
jne 0x36b1a
movl -0x2c4(%rbp), %eax
cmpl -0x2c0(%rbp), %eax
je 0x36b1a
jmp 0x36f70
cmpl $0x1, -0x1c(%rbp)
jne 0x36b3e
movl -0x2c4(%rbp), %eax
movl -0x2c0(%rbp), %ecx
addl -0x2bc(%rbp), %ecx
subl $0x2, %ecx
cmpl %ecx, %eax
je 0x36b3e
jmp 0x36f2a
leaq -0x2e9(%rbp), %rdi
movq %rdi, -0x450(%rbp)
callq 0x4660
movq -0x450(%rbp), %rdx
leaq 0x5a2f7(%rip), %rsi     # 0x90e56
leaq -0x2e8(%rbp), %rdi
callq 0x1c730
jmp 0x36b6d
leaq -0x2e9(%rbp), %rdi
callq 0x43f0
movslq -0x2bc(%rbp), %rax
movq %rax, -0x458(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x87f0
movq %rax, %rcx
movq -0x458(%rbp), %rax
cmpq %rcx, %rax
jne 0x36bf0
movq -0x8(%rbp), %rsi
leaq -0x2e8(%rbp), %rdi
callq 0x4050
jmp 0x36bb1
jmp 0x36c63
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f8(%rbp)
movl %eax, -0x1fc(%rbp)
leaq -0x2e9(%rbp), %rdi
callq 0x43f0
jmp 0x36fd2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f8(%rbp)
movl %eax, -0x1fc(%rbp)
jmp 0x36f62
movq -0x10(%rbp), %rax
movq %rax, -0x468(%rbp)
movslq -0x2c0(%rbp), %rsi
leaq -0x240(%rbp), %rdi
callq 0x37120
movq %rax, %rdi
movl -0x2c0(%rbp), %edx
movl -0x2bc(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal -0x1(%rax,%rcx), %eax
movslq %eax, %rsi
callq 0x8810
movq -0x468(%rbp), %rdi
movq -0x3d8(%rbp), %rdx
movq %rax, %rsi
callq 0x35570
movq %rax, -0x460(%rbp)
jmp 0x36c4c
movq -0x460(%rbp), %rsi
leaq -0x2e8(%rbp), %rdi
callq 0x4050
jmp 0x36c61
jmp 0x36c63
movslq -0x2c0(%rbp), %rsi
leaq -0x240(%rbp), %rdi
callq 0x37120
movq %rax, %rdi
movslq -0x2c4(%rbp), %rsi
callq 0x8810
movq %rax, %rsi
leaq -0x310(%rbp), %rdi
callq 0x4160
jmp 0x36c96
movl -0x2c4(%rbp), %eax
subl -0x2c0(%rbp), %eax
cmpl $0x0, %eax
jle 0x36cf5
movq -0x3d8(%rbp), %rdx
movq -0x10(%rbp), %rdi
leaq -0x310(%rbp), %rsi
callq 0x35570
movq %rax, -0x470(%rbp)
jmp 0x36cc7
movq -0x470(%rbp), %rsi
leaq -0x310(%rbp), %rdi
callq 0x4050
jmp 0x36cdc
jmp 0x36cf5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f8(%rbp)
movl %eax, -0x1fc(%rbp)
jmp 0x36f56
movl -0x2c4(%rbp), %eax
incl %eax
movslq %eax, %rsi
leaq -0x240(%rbp), %rdi
callq 0x37120
movq %rax, %rdi
movl -0x2c0(%rbp), %edx
movl -0x2bc(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal -0x1(%rax,%rcx), %eax
movslq %eax, %rsi
callq 0x8810
movq %rax, %rsi
leaq -0x330(%rbp), %rdi
callq 0x4160
jmp 0x36d3c
movl -0x2c0(%rbp), %eax
addl -0x2bc(%rbp), %eax
subl $0x1, %eax
subl -0x2c4(%rbp), %eax
subl $0x1, %eax
cmpl $0x0, %eax
jle 0x36da7
movq -0x3d8(%rbp), %rdx
movq -0x10(%rbp), %rdi
leaq -0x330(%rbp), %rsi
callq 0x35570
movq %rax, -0x478(%rbp)
jmp 0x36d79
movq -0x478(%rbp), %rsi
leaq -0x330(%rbp), %rdi
callq 0x4050
jmp 0x36d8e
jmp 0x36da7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f8(%rbp)
movl %eax, -0x1fc(%rbp)
jmp 0x36f4a
leaq -0x378(%rbp), %rdi
movq %rdi, -0x480(%rbp)
callq 0x37280
movq -0x480(%rbp), %rdi
leaq -0x2e8(%rbp), %rsi
callq 0x4050
jmp 0x36dcf
leaq 0x5a07c(%rip), %rdx     # 0x90e52
leaq -0x3b8(%rbp), %rdi
leaq -0x310(%rbp), %rsi
callq 0x21540
jmp 0x36deb
leaq -0x398(%rbp), %rdi
leaq -0x3b8(%rbp), %rsi
leaq -0x330(%rbp), %rdx
callq 0x214f0
jmp 0x36e07
leaq -0x378(%rbp), %rdi
addq $0x20, %rdi
leaq -0x398(%rbp), %rsi
callq 0x4470
leaq -0x398(%rbp), %rdi
callq 0x4728
leaq -0x3b8(%rbp), %rdi
callq 0x4728
cmpl $0x2, -0x1c(%rbp)
jne 0x36e99
movl -0x2bc(%rbp), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
movsd 0x59317(%rip), %xmm0    # 0x90168
divsd %xmm1, %xmm0
movsd %xmm0, -0x338(%rbp)
jmp 0x36ea9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f8(%rbp)
movl %eax, -0x1fc(%rbp)
jmp 0x36f3e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f8(%rbp)
movl %eax, -0x1fc(%rbp)
leaq -0x3b8(%rbp), %rdi
callq 0x4728
jmp 0x36f3e
movsd 0x592c7(%rip), %xmm0    # 0x90168
movsd %xmm0, -0x338(%rbp)
movslq -0x2bc(%rbp), %rax
movq %rax, -0x488(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x87f0
movq %rax, %rcx
movq -0x488(%rbp), %rax
cmpq %rcx, %rax
jne 0x36ee8
movq -0x8(%rbp), %rax
movsd 0x40(%rax), %xmm0
mulsd -0x338(%rbp), %xmm0
movsd %xmm0, -0x338(%rbp)
movq -0x28(%rbp), %rdi
leaq -0x378(%rbp), %rsi
callq 0x88fb0
jmp 0x36efa
leaq -0x378(%rbp), %rdi
callq 0x372b0
leaq -0x330(%rbp), %rdi
callq 0x4728
leaq -0x310(%rbp), %rdi
callq 0x4728
leaq -0x2e8(%rbp), %rdi
callq 0x4728
movl -0x2c4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c4(%rbp)
jmp 0x36ae4
leaq -0x378(%rbp), %rdi
callq 0x372b0
leaq -0x330(%rbp), %rdi
callq 0x4728
leaq -0x310(%rbp), %rdi
callq 0x4728
leaq -0x2e8(%rbp), %rdi
callq 0x4728
jmp 0x36fd2
jmp 0x36f72
movl -0x2c0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c0(%rbp)
jmp 0x36a55
jmp 0x36f88
movl -0x2bc(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x2bc(%rbp)
jmp 0x36a3e
movl $0x1, -0x228(%rbp)
leaq -0x240(%rbp), %rdi
callq 0x372e0
leaq -0x1c8(%rbp), %rdi
callq 0x42d0
leaq -0x40(%rbp), %rdi
callq 0x8830
xorl %eax, %eax
addq $0x490, %rsp            # imm = 0x490
popq %rbp
retq
leaq -0x240(%rbp), %rdi
callq 0x372e0
leaq -0x1c8(%rbp), %rdi
callq 0x42d0
leaq -0x40(%rbp), %rdi
callq 0x8830
movq -0x1f8(%rbp), %rdi
callq 0x4650
nop