// File: /workspace/github_cpp_makefile_star2_O2/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, 0xa0(%rsp)
movl %ecx, 0x4(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, %rbx
xorpd %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
callq 0x515e
movq %rbx, 0x38(%rsp)
movq 0x20(%rbx), %rsi
leaq 0xb0(%rsp), %rdi
leaq 0x80(%rsp), %rdx
callq 0x9f86
leaq 0x1a0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
pushq $0x18
popq %rdx
callq 0x3460
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
callq 0x3578
leaq 0x20c25(%rip), %r14     # 0x32eb6
leaq 0x80(%rsp), %r15
leaq 0x1a0(%rsp), %r12
leaq 0x10(%rsp), %r13
movq 0x1a0(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x1c0(%rsp,%rax)
jne 0x12306
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x9f86
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3380
movq %rbx, %rdi
movq %r14, %rsi
callq 0x57b9
testb %al, %al
jne 0x122f9
movq %r13, %rdi
movq %rbx, %rsi
callq 0x129cc
movq %rbx, %rdi
callq 0x3578
jmp 0x122a6
leaq 0xb0(%rsp), %rdi
callq 0x3578
xorpd %xmm0, %xmm0
leaq 0x60(%rsp), %rdi
movapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
callq 0x129f8
xorl %ebp, %ebp
leaq 0x80(%rsp), %rbx
leaq 0xb0(%rsp), %r12
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rax
movq %rax, %rcx
subq %rdx, %rcx
sarq $0x5, %rcx
cmpq %rbp, %rcx
jbe 0x123f7
xorpd %xmm0, %xmm0
movapd %xmm0, 0x80(%rsp)
andq $0x0, 0x90(%rsp)
leaq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0x12a00
imulq $0x18, %rbp, %r14
movq %rbp, %r15
shlq $0x5, %r15
xorl %r13d, %r13d
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq %r13, %rax
jbe 0x123df
movq 0x60(%rsp), %rbx
cmpq %r13, %rbp
jne 0x123ad
addq %r14, %rbx
addq %r15, %rsi
movq %rbx, %rdi
callq 0x129cc
jmp 0x123da
movq %r12, %rdi
leaq 0x20aff(%rip), %rsi     # 0x32eb6
leaq 0x120(%rsp), %rdx
callq 0x9f86
addq %r14, %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x6f64
movq %r12, %rdi
callq 0x3578
incq %r13
jmp 0x1237d
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
callq 0x5166
incq %rbp
jmp 0x1232f
pushq $0x1
popq %r14
pushq $0x20
popq %r15
xorl %r12d, %r12d
leaq 0xb0(%rsp), %rbx
movq %rax, %rcx
subq %rdx, %rcx
movq %rcx, %rsi
sarq $0x5, %rsi
cmpq %r12, %rsi
jbe 0x12496
imulq $0x18, %r12, %r13
incq %r12
movq %r15, %rbp
movq %r14, 0x8(%rsp)
movq %rax, %rcx
subq %rdx, %rcx
sarq $0x5, %rcx
cmpq %r14, %rcx
jbe 0x12485
movq 0x60(%rsp), %rax
movq (%rax,%r13), %rax
leaq (%rax,%rbp), %rsi
addq $-0x20, %rsi
addq %rbp, %rdx
movq %rbx, %rdi
callq 0x12a2c
movq 0x60(%rsp), %rax
movq (%rax,%r13), %rdi
addq %rbp, %rdi
movq %rbx, %rsi
callq 0x3360
movq %rbx, %rdi
callq 0x3578
incq %r14
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rax
addq $0x20, %rbp
jmp 0x1242b
movq 0x8(%rsp), %r14
incq %r14
addq $0x20, %r15
jmp 0x1240a
cmpq $0x20, %rcx
jne 0x124be
movq 0xa0(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x2f504
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rax
movq %rax, %rcx
subq %rdx, %rcx
shrq $0x5, %rcx
movslq %ecx, %rdi
pushq $0x1
popq %rcx
subl %edi, %ecx
movq %rcx, 0xa8(%rsp)
leal -0x2(%rdi), %ecx
cmpq $0x2, %rdi
jl 0x128a2
leaq -0x1(%rdi), %r8
cmpl $0x2, 0x4(%rsp)
je 0x124fa
movsd 0x1fc96(%rip), %xmm0    # 0x32188
movsd %xmm0, 0x40(%rsp)
jmp 0x12511
cvtsi2sd %r8d, %xmm0
movsd 0x1fc81(%rip), %xmm1    # 0x32188
divsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rsp)
xorl %r15d, %r15d
movl %ecx, 0x58(%rsp)
movl %ecx, %r9d
xorl %r10d, %r10d
pushq $0x18
popq %r12
xorl %r11d, %r11d
movq %rdi, 0x48(%rsp)
movq %r8, 0xf8(%rsp)
movq %rax, %rcx
subq %rdx, %rcx
sarq $0x5, %rcx
subq %rdi, %rcx
cmpq %r11, %rcx
jb 0x1288c
cmpl $0x0, 0x4(%rsp)
jne 0x12558
cmpq %r11, %rcx
jne 0x12876
cmpl $0x1, 0x4(%rsp)
sete %cl
testq %r11, %r11
setne %sil
testb %sil, %cl
jne 0x1288c
movl %r9d, 0x5c(%rsp)
movl %r9d, %eax
movq %r10, 0x108(%rsp)
addq %r10, %rax
movq %rax, 0x118(%rsp)
movq %r11, 0x8(%rsp)
leaq (%r11,%rdi), %rax
decq %rax
movq %rax, 0x50(%rsp)
movq %r15, 0x110(%rsp)
movq %r12, 0x100(%rsp)
xorl %ebp, %ebp
cmpl $0x0, 0x4(%rsp)
setne %al
movq 0x8(%rsp), %rcx
leaq (%rcx,%rbp), %rbx
testq %rbp, %rbp
sete %cl
cmpq 0x50(%rsp), %rbx
jge 0x1283d
orb %cl, %al
je 0x1283d
cmpl $0x1, 0x4(%rsp)
jne 0x125ed
cmpq %rbp, 0x118(%rsp)
jne 0x1282d
leaq 0x80(%rsp), %rdi
leaq 0x208ba(%rip), %rsi     # 0x32eb6
leaq 0xb0(%rsp), %rdx
callq 0x9f86
movq 0x18(%rsp), %rcx
subq 0x10(%rsp), %rcx
sarq $0x5, %rcx
movq 0x38(%rsp), %rax
cmpq 0x48(%rsp), %rcx
je 0x1264a
movq 0x60(%rsp), %rax
imulq $0x18, 0x8(%rsp), %rcx
movq 0x50(%rsp), %rsi
shlq $0x5, %rsi
addq (%rax,%rcx), %rsi
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rdx
callq 0x11932
leaq 0x80(%rsp), %rdi
movq %rax, %rsi
callq 0x3170
movq 0x60(%rsp), %rax
movq 0x8(%rsp), %r14
imulq $0x18, %r14, %rcx
movq (%rax,%rcx), %rsi
addq %r15, %rsi
leaq 0x120(%rsp), %rdi
callq 0x33c0
cmpq %r14, %rbx
jbe 0x126a8
movq 0x28(%rsp), %rdi
leaq 0x120(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x11932
leaq 0x120(%rsp), %rdi
movq %rax, %rsi
callq 0x3170
movq 0x60(%rsp), %rax
movq 0x50(%rsp), %rsi
shlq $0x5, %rsi
addq (%rax,%r12), %rsi
leaq 0x140(%rsp), %rdi
callq 0x33c0
movq 0xa8(%rsp), %rax
addl %ebp, %eax
cmpl $-0x2, %eax
jg 0x126fd
movq 0x28(%rsp), %rdi
leaq 0x140(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x11932
leaq 0x140(%rsp), %rdi
movq %rax, %rsi
callq 0x3170
leaq 0xc0(%rsp), %rax
movq %rax, 0xb0(%rsp)
andq $0x0, 0xb8(%rsp)
xorl %ecx, %ecx
movb %cl, 0xc0(%rsp)
leaq 0xe0(%rsp), %rax
movq %rax, 0xd0(%rsp)
andq $0x0, 0xd8(%rsp)
movb %cl, 0xe0(%rsp)
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x80(%rsp), %rsi
callq 0x3170
leaq 0x160(%rsp), %r14
movq %r14, %rdi
leaq 0x120(%rsp), %rsi
leaq 0x20741(%rip), %rdx     # 0x32eb2
callq 0xbf0b
leaq 0x180(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
leaq 0x140(%rsp), %rdx
callq 0xbeec
leaq 0xd0(%rsp), %rdi
movq %r13, %rsi
callq 0x3360
movq %r13, %rdi
callq 0x3578
movq %r14, %rdi
callq 0x3578
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0xf0(%rsp)
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x5, %rax
cmpq 0x48(%rsp), %rax
jne 0x127ee
movq 0x38(%rsp), %rax
movsd 0x40(%rax), %xmm0
mulsd 0x40(%rsp), %xmm0
movsd %xmm0, 0xf0(%rsp)
movq 0xa0(%rsp), %rdi
movq %rbx, %rsi
callq 0x2f504
movq %rbx, %rdi
callq 0x12aa2
leaq 0x140(%rsp), %rdi
callq 0x3578
leaq 0x120(%rsp), %rdi
callq 0x3578
leaq 0x80(%rsp), %rdi
callq 0x3578
incq %rbp
addq $0x18, %r12
addq $0x20, %r15
jmp 0x125ae
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rax
movq 0x48(%rsp), %rdi
movq 0xf8(%rsp), %r8
movq 0x110(%rsp), %r15
movl 0x5c(%rsp), %r9d
movq 0x108(%rsp), %r10
movq 0x100(%rsp), %r12
movq 0x8(%rsp), %r11
incq %r11
addq $0x18, %r12
decq %r10
incl %r9d
addq $0x20, %r15
jmp 0x12532
movl 0x58(%rsp), %ecx
decl %ecx
incq 0xa8(%rsp)
movq %r8, %rdi
jmp 0x124d5
leaq 0x60(%rsp), %rdi
callq 0x12ab8
leaq 0x1a0(%rsp), %rdi
callq 0x3260
leaq 0x10(%rsp), %rdi
callq 0x5166
xorl %eax, %eax
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x12962
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x3578
jmp 0x129ba
movq %rax, %rbx
jmp 0x129ba
movq %rax, %rbx
jmp 0x129ad
jmp 0x1292c
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x3578
jmp 0x129ad
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x3578
jmp 0x1292f
jmp 0x1292c
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x5166
jmp 0x129a3
jmp 0x12962
jmp 0x12967
jmp 0x12944
movq %rax, %rbx
jmp 0x12989
movq %rax, %rbx
jmp 0x1297c
jmp 0x1296c
movq %rax, %rbx
leaq 0x160(%rsp), %rdi
callq 0x3578
jmp 0x1296f
movq %rax, %rbx
jmp 0x129a3
movq %rax, %rbx
jmp 0x12996
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x12aa2
leaq 0x140(%rsp), %rdi
callq 0x3578
leaq 0x120(%rsp), %rdi
callq 0x3578
leaq 0x80(%rsp), %rdi
callq 0x3578
leaq 0x60(%rsp), %rdi
callq 0x12ab8
leaq 0x1a0(%rsp), %rdi
callq 0x3260
leaq 0x10(%rsp), %rdi
callq 0x5166
movq %rbx, %rdi
callq 0x34f0