// File: /workspace/github_cpp_makefile_star2_O2/kiven-li[P]xbytes/xbytes.cpp
// Function: xbytes::xbytes::output_reduce_code(std::basic_ofstream<char, std::char_traits<char>>&)
void xbytes::output_reduce_code(std::ofstream& ss)
{
	for(int i = 0; i < rules.size(); ++i)
	{
		rule* r = rules[i];
		if(r->code.size() == 0) continue;
		ss << "	case " << i << ":/* "  << r->str_rule << " */" << std::endl;

		string& code = r->code;

		//生成左边符号代码
		string& la = r->left_alias;
		if(la.size() > 0)
		{
			std::size_t spos = 0;
			while(spos < code.size())
			{
				std::size_t pos = code.find(la, spos);
				
				if(pos == std::string::npos) break;
				if(spos == pos) break;
				if(pos > 0 && isalpha(code[pos-1]))
				{
					spos = pos + la.size() - 1;
					continue;
				}
				if(pos < code.size() - 1 && isalpha(code[pos + la.size()]))
				{
					spos = pos + la.size() - 1;
					continue;
				}

				code.replace(pos, la.size(), "tt");
			}
		}

		//生成右边符号代码
		for(int m = 0; m < r->right_alias.size(); ++m)
		{
			string& ra = r->right_alias[m];
			if(ra.size() == 0) continue;

			std::stringstream ss;
			ss << "tv[" << m << "]";

			std::size_t spos = 0;
			while(spos < code.size())
			{
				std::size_t pos = code.find(ra, spos);

				if(pos == std::string::npos) break;
				if(spos == pos) break;
				if(pos > 0 && isalpha(code[pos-1]))
				{
					spos = pos + ra.size() - 1;
					continue;
				}
				if(pos < code.size() - 1 && isalpha(code[pos + ra.size()]))
				{
					spos = pos + ra.size() - 1;
					continue;
				}

				code.replace(pos, ra.size(), ss.str());
			}
		}

		ss << r->code << std::endl;
		ss << "	break;\n" << std::endl;
	}	
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rsi, %rbx
movq %rdi, -0x48(%rbp)
xorl %r14d, %r14d
movq %rsi, -0x40(%rbp)
movq -0x48(%rbp), %rcx
movq 0x20(%rcx), %rax
movq 0x28(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
jbe 0x6e96
movq (%rax,%r14,8), %r15
cmpq $0x0, 0x68(%r15)
je 0x6e8e
leaq 0x60(%r15), %rax
movq %rax, -0x30(%rbp)
movq %rbx, %rdi
leaq 0x3c7c(%rip), %rsi      # 0xa863
callq 0x2220
movq %rax, %rdi
movq %r14, -0x50(%rbp)
movl %r14d, %esi
callq 0x2370
movq %rax, %rdi
leaq 0x3c65(%rip), %rsi      # 0xa86a
callq 0x2220
leaq 0x80(%r15), %rsi
movq %rax, %rdi
callq 0x2200
movq %rax, %rdi
leaq 0x3c4c(%rip), %rsi      # 0xa86f
callq 0x2220
movq %rax, %rdi
callq 0x2110
movq %r15, -0x38(%rbp)
cmpq $0x0, 0x18(%r15)
je 0x6d00
movq -0x38(%rbp), %rax
leaq 0x10(%rax), %r14
xorl %r12d, %r12d
movq -0x38(%rbp), %rax
cmpq 0x68(%rax), %r12
jae 0x6d00
movq -0x30(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x23d0
cmpq $-0x1, %rax
je 0x6d00
movq %rax, %rbx
cmpq %rax, %r12
je 0x6d00
testq %rbx, %rbx
je 0x6c97
movq -0x30(%rbp), %rax
movq (%rax), %rax
movsbl -0x1(%rax,%rbx), %edi
callq 0x2320
testl %eax, %eax
jne 0x6ce1
movq -0x38(%rbp), %rax
movq 0x18(%rax), %r15
movq 0x68(%rax), %rax
decq %rax
cmpq %rax, %rbx
jae 0x6cc6
movq %rbx, %r13
addq %r15, %r13
movq -0x30(%rbp), %rax
movq (%rax), %rax
movsbl (%rax,%r13), %edi
callq 0x2320
testl %eax, %eax
jne 0x6cf5
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
leaq 0x3b9c(%rip), %rcx      # 0xa873
callq 0x2140
jmp 0x6c4a
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
leaq (%rbx,%rax), %r12
decq %r12
jmp 0x6c4a
decq %r13
movq %r13, %r12
jmp 0x6c4a
xorl %r14d, %r14d
movq -0x38(%rbp), %rcx
movq 0x48(%rcx), %rbx
movq 0x50(%rcx), %rax
subq %rbx, %rax
sarq $0x5, %rax
cmpq %r14, %rax
jbe 0x6e5b
movq %r14, %r15
shlq $0x5, %r15
cmpq $0x0, 0x8(%rbx,%r15)
je 0x6e53
leaq -0x200(%rbp), %rdi
callq 0x2190
leaq -0x1f0(%rbp), %rdi
leaq 0x3b2a(%rip), %rsi      # 0xa876
callq 0x2220
movq %rax, %rdi
movl %r14d, %esi
callq 0x2370
movq %rax, %rdi
leaq 0x3872(%rip), %rsi      # 0xa5d8
callq 0x2220
movq %r14, -0x58(%rbp)
addq %r15, %rbx
xorl %r12d, %r12d
movq -0x38(%rbp), %rax
cmpq 0x68(%rax), %r12
jae 0x6e3f
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x23d0
cmpq $-0x1, %rax
je 0x6e3f
movq %rax, %r14
cmpq %rax, %r12
je 0x6e3f
testq %r14, %r14
je 0x6dc3
movq -0x30(%rbp), %rax
movq (%rax), %rax
movsbl -0x1(%rax,%r14), %edi
callq 0x2320
testl %eax, %eax
jne 0x6e24
movq -0x38(%rbp), %rax
movq 0x68(%rax), %rax
decq %rax
movq 0x8(%rbx), %r15
cmpq %rax, %r14
jae 0x6df2
movq %r14, %r13
addq %r15, %r13
movq -0x30(%rbp), %rax
movq (%rax), %rax
movsbl (%rax,%r13), %edi
callq 0x2320
testl %eax, %eax
jne 0x6e34
leaq -0x78(%rbp), %r13
movq %r13, %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x2310
movq -0x30(%rbp), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x20c0
movq %r13, %rdi
callq 0x2100
jmp 0x6d75
movq 0x8(%rbx), %rax
leaq (%r14,%rax), %r12
decq %r12
jmp 0x6d75
decq %r13
movq %r13, %r12
jmp 0x6d75
leaq -0x200(%rbp), %rdi
callq 0x21b0
movq -0x38(%rbp), %rcx
movq -0x58(%rbp), %r14
incq %r14
jmp 0x6d07
movq -0x40(%rbp), %rbx
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
callq 0x2200
movq %rax, %rdi
callq 0x2110
movq %rbx, %rdi
leaq 0x39fd(%rip), %rsi      # 0xa87a
callq 0x2220
movq %rax, %rdi
callq 0x2110
movq -0x50(%rbp), %r14
incq %r14
jmp 0x6baa
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6eaa
movq %rax, %rbx
jmp 0x6ebb
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x2100
leaq -0x200(%rbp), %rdi
callq 0x21b0
movq %rbx, %rdi
callq 0x2380
nop