// File: /workspace/github_cpp_makefile_star2_O3/exrd[P]neteruhsp/neteruhsp/neteruhsp.cc
// Function: neteruhsp::prepro_line_expand(neteruhsp::prepro_context_t*, char const*, bool*)
char* prepro_line_expand( prepro_context_t* pctx, const char* line, bool* out_is_replaced )
{
	// このディレクティブが無効なら何もしない
	if ( !pctx->is_current_region_valid_ )
	{
		if ( out_is_replaced != nullptr )
		{
			*out_is_replaced = false;
		}
		return create_string( "" );
	}

	// マクロ展開
	auto* const sb = create_string_buffer();

	tokenize_context_t tokenize_ctx{};
	parse_context_t parse_ctx{};
	initialize_tokenize_context( &tokenize_ctx, line );
	initialize_parse_context( &parse_ctx, tokenize_ctx );

	bool is_replaced = false;
	{
		const token_t* prev = nullptr;

		for ( ; ; )
		{
			const auto* const st = read_token( parse_ctx );

			if ( prev != nullptr )
			{
				const auto len = st->cursor_begin_ - prev->cursor_end_;
				if ( len > 0 )
				{
					string_buffer_append( sb, line + prev->cursor_end_, len );
				}
			}
			prev = st;

			if ( st->tag_ == TOKEN_EOF )
			{
				break;
			}

			if ( st->tag_ == TOKEN_IDENTIFIER )
			{
				const auto* const macro = prepro_find_macro( pctx, st->content_ );
				if ( macro != nullptr )
				{
					if ( macro->param_num_ > 0 )
					{
						// パラメータ取得
						macro_arg_t marg[ MACRO_PARAM_MAX ];
						int marg_num = 0;
						for ( auto& it : marg )
						{
							initialize_macro_arg( &it );
						}

						if ( macro->is_ctype_ )
						{
							const auto* const lt = read_token( parse_ctx );
							if ( lt->tag_ != TOKEN_LPARENTHESIS )
							{
								raise_error( "プリプロセス：マクロ展開（%s）：ctypeと定義されていますが、引数が括弧で始まっていません@@ %d行目", macro->name_, pctx->line_ + 1 );
								return nullptr;
							}
						}
						else
						{
							const auto* const nst = read_token( parse_ctx );
							unread_token( parse_ctx );

							if ( !nst->left_space_ && ( nst->tag_ != TOKEN_EOS && nst->tag_ != TOKEN_EOF ) )
							{
								raise_error( "プリプロセス：マクロ展開（%s）：ctypeと定義されていないマクロ展開において、マクロの後にスペースが存在しません@@ %d行目", macro->name_, pctx->line_ + 1 );
								return nullptr;
							}
						}

						for ( ; ; )
						{
							const auto* const nst = read_token( parse_ctx );
							unread_token( parse_ctx );

							bool is_break = false;
							int parenthesis_depth = 0;

							for ( ; ; )
							{
								const auto* const nt = read_token( parse_ctx );
								if ( nt->tag_ == TOKEN_EOS || nt->tag_ == TOKEN_EOF )
								{
									if ( parenthesis_depth > 0 || macro->is_ctype_ )
									{
										raise_error( "プリプロセス：マクロ展開（%s）に失敗：パラメータ読み取り中に、予期しないステートメントの終わり（:）か行末に到達しました@@ %d行目", macro->name_, pctx->line_ + 1 );
										return nullptr;
									}

									// ここまで食う
									if ( marg_num >= macro->param_num_ )
									{
										raise_error( "プリプロセス：マクロ展開（%s）に失敗：与えられて引数が多すぎます@@ %d行目", macro->name_, pctx->line_ + 1 );
										return nullptr;
									}
									auto& ma = marg[ marg_num ];
									++marg_num;

									// 引数省略
									if ( nst != nt )
									{
										ma.arg_param_ = create_string( line + nst->cursor_begin_, nt->cursor_begin_ - nst->cursor_begin_ );
									}

									unread_token( parse_ctx );
									is_break = true;
									break;
								}
								const bool is_rp_end = ( macro->is_ctype_ && nt->tag_ == TOKEN_RPARENTHESIS );
								if ( parenthesis_depth == 0 && ( is_rp_end || nt->tag_ == TOKEN_COMMA ) )
								{
									// ここまで食う
									if ( marg_num >= macro->param_num_ )
									{
										raise_error( "プリプロセス：マクロ展開（%s）に失敗：与えられて引数が多すぎます@@ %d行目", macro->name_, pctx->line_ + 1 );
										return nullptr;
									}
									auto& ma = marg[ marg_num ];
									++marg_num;

									// 引数省略
									if ( nst != nt )
									{
										ma.arg_param_ = create_string( line + nst->cursor_begin_, nt->cursor_begin_ - nst->cursor_begin_ );
									}

									if ( is_rp_end )
									{
										unread_token( parse_ctx );// 食いすぎなので戻す
										is_break = true;
									}
									break;
								}
								if ( nt->tag_ == TOKEN_LPARENTHESIS )
								{
									++parenthesis_depth;
								}
								if ( nt->tag_ == TOKEN_RPARENTHESIS )
								{
									--parenthesis_depth;
								}
								if ( nt->tag_ == TOKEN_EOF )
								{
									if ( macro->is_ctype_ )
									{
										raise_error( "プリプロセス：マクロ展開（%s）に失敗：ctypeで定義されていますが、括弧の終わりがみつかりません@@ %d行目", macro->name_, pctx->line_ + 1 );
										return nullptr;
									}

									// ここまで食う
									if ( marg_num >= macro->param_num_ )
									{
										raise_error( "プリプロセス：マクロ展開（%s）に失敗：与えられて引数が多すぎます@@ %d行目", macro->name_, pctx->line_ + 1 );
										return nullptr;
									}
									auto& ma = marg[ marg_num ];
									++marg_num;

									// 引数省略
									if ( nst != nt )
									{
										ma.arg_param_ = create_string( line + nst->cursor_begin_ );
									}

									is_break = true;
									break;
								}
							}

							if ( is_break )
							{ break; }
						}

						// 引数閉じ終わり？
						if ( macro->is_ctype_ )
						{
							const auto* const rt = read_token( parse_ctx );
							if ( rt->tag_ != TOKEN_RPARENTHESIS )
							{
								raise_error( "プリプロセス：マクロ展開（%s）に失敗：ctypeと定義されていますが、引数が括弧で終わっていません@@ %d行目", macro->name_, pctx->line_ + 1 );
								return nullptr;
							}
						}

						// 次の取得用にずらしておく
						prev = read_token( parse_ctx );
						unread_token( parse_ctx );

						// 省略チェック
						for ( int i = 0; i < macro->param_num_; ++i )
						{
							const auto& mp = macro->params_[i];
							const auto& ma = marg[i];
							if ( ma.arg_param_ == nullptr && mp.default_param_ == nullptr )
							{
								raise_error( "プリプロセス：マクロ展開（%s）に失敗：%d番目の引数は省略できません@@ %d行目", macro->name_, i + 1, pctx->line_ + 1 );
								return nullptr;
							}
						}

						// 置き換え
						{
							parse_context_t lpctx{};
							tokenize_context_t ltctx{};
							initialize_tokenize_context( &ltctx, macro->replacing_ );
							initialize_parse_context( &lpctx, ltctx );

							const token_t* iprev = nullptr;
							for ( ; ; )
							{
								const auto* const ist = read_token( lpctx );
								if ( ist->tag_ == TOKEN_EOF )
								{
									break;
								}

								if ( ist->tag_ == TOKEN_PP_ARG_INDICATOR )
								{
									const auto* const it = read_token( lpctx );
									if ( it->tag_ != TOKEN_INTEGER || it->left_space_ )
									{
										raise_error( "プリプロセス：マクロ展開（%s）に失敗：展開後のパラメータ%%のあと、スペースなしで数値を入れる必要があります@@ %d行目", macro->name_, pctx->line_ + 1 );
										return nullptr;
									}

									const auto itidx = atoi( it->content_ ) - 1;
									if ( itidx < 0 || itidx > macro->param_num_ )
									{
										raise_error( "プリプロセス：マクロ展開（%s）に失敗：パラメータ%が存在しません@@ %d行目", macro->name_, pctx->line_ + 1 );
										return nullptr;
									}
									const auto& mp = macro->params_[itidx];
									const auto& ma = marg[itidx];

									string_buffer_append( sb, ( ma.arg_param_ == nullptr ? mp.default_param_ : ma.arg_param_ ) );
									iprev = it;
								}
								else
								{
									if ( iprev != nullptr )
									{
										string_buffer_append( sb, macro->replacing_ + iprev->cursor_end_, ist->cursor_begin_ - iprev->cursor_end_ );
									}
									string_buffer_append( sb, macro->replacing_ + ist->cursor_begin_, ist->cursor_end_ - ist->cursor_begin_ );
									iprev = ist;
								}
							}

							if ( iprev != nullptr )
							{
								string_buffer_append( sb, macro->replacing_ + iprev->cursor_end_ );
							}

							uninitialize_parse_context( &lpctx );
							uninitialize_tokenize_context( &ltctx );
						}

						for ( auto& it : marg )
						{
							uninitialize_macro_arg( &it );
						}
					}
					else
					{
						// 単純置き換え
						string_buffer_append( sb, macro->replacing_ );
					}

					is_replaced = true;
					continue;
				}
			}

			string_buffer_append( sb, line + st->cursor_begin_, st->cursor_end_ - st->cursor_begin_ );
			continue;
		}
	}

	uninitialize_parse_context( &parse_ctx );
	uninitialize_tokenize_context( &tokenize_ctx );

	// 抜き取り
	auto* res = sb->buffer_;
	sb->buffer_ = nullptr;
	destroy_string_buffer( sb );

	if ( out_is_replaced != nullptr )
	{
		*out_is_replaced = is_replaced;
	}
	return res;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
cmpb $0x0, 0x14(%rdi)
je 0x49f2
movq %rsi, %r12
movq %rdi, %r13
movq %rdx, 0x50(%rsp)
movl $0x18, %edi
callq 0x21d0
movq %rax, %r15
movl $0x401, %edi            # imm = 0x401
callq 0x21d0
movq %rax, (%r15)
movq $0x400, 0x8(%r15)       # imm = 0x400
movl $0xffffffff, 0x10(%r15) # imm = 0xFFFFFFFF
movb $0x0, (%rax)
leaq 0x70(%rsp), %rbx
movq %r12, (%rbx)
xorl %r14d, %r14d
movq %r14, 0x8(%rbx)
movq %r12, 0x10(%rbx)
movl $0x1, %edi
movl $0x10, %esi
callq 0x20f0
leaq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movq %r14, 0x8(%rcx)
movq %rbx, 0x10(%rcx)
xorl %ebp, %ebp
xorl %r14d, %r14d
movq %rbp, %rbx
leaq 0x18(%rsp), %rdi
callq 0x3619
movq %rax, %rbp
testq %rbx, %rbx
je 0x45ea
movl 0x10(%rbp), %edx
movslq 0x14(%rbx), %rsi
subl %esi, %edx
jle 0x45ea
addq %r12, %rsi
movq %r15, %rdi
callq 0x2a19
movl (%rbp), %eax
cmpl $0x23, %eax
je 0x45fb
testl %eax, %eax
jne 0x460c
jmp 0x4a17
movq 0x8(%rbp), %rsi
movq %r13, %rdi
callq 0x4d12
testq %rax, %rax
jne 0x4622
movslq 0x10(%rbp), %rsi
movl 0x14(%rbp), %edx
subl %esi, %edx
addq %r12, %rsi
movq %r15, %rdi
callq 0x2a19
jmp 0x45bf
movq %rax, %rbx
cmpl $0x0, 0x18(%rax)
jle 0x4699
xorps %xmm0, %xmm0
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movb 0x8(%rbx), %bpl
leaq 0x18(%rsp), %rdi
callq 0x3619
movq %rax, %r14
cmpb $0x1, %bpl
movq %r13, (%rsp)
jne 0x46af
cmpl $0x6, (%r14)
leaq 0x18(%rsp), %rbp
je 0x46d5
jmp 0x4adc
movq 0x10(%rbx), %rsi
movq %r15, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x2a19
jmp 0x49ea
movl $0x1, %esi
leaq 0x18(%rsp), %rbp
movq %rbp, %rdi
callq 0x3669
cmpb $0x1, 0x1c(%r14)
je 0x46d5
testl $0xfffffffd, (%r14)     # imm = 0xFFFFFFFD
jne 0x4b06
xorl %r14d, %r14d
movq %rbp, %rdi
callq 0x3619
movq %rax, 0x8(%rsp)
movl $0x1, %esi
movq %rbp, %rdi
callq 0x3669
xorl %r13d, %r13d
movq %rbp, %rdi
callq 0x3619
movl (%rax), %ecx
testl $0xfffffffd, %ecx       # imm = 0xFFFFFFFD
je 0x47b1
cmpl $0x7, %ecx
sete %bpl
cmpl $0x8, %ecx
sete %dl
testl %r13d, %r13d
jne 0x4726
andb 0x8(%rbx), %bpl
orb %bpl, %dl
jne 0x4743
xorl %eax, %eax
cmpl $0x7, %ecx
sete %al
xorl %edx, %edx
cmpl $0x6, %ecx
sete %dl
addl %edx, %r13d
subl %eax, %r13d
leaq 0x18(%rsp), %rbp
jmp 0x46f5
movslq 0x18(%rbx), %rcx
cmpq %rcx, %r14
jge 0x4ac7
movq 0x8(%rsp), %rcx
cmpq %rax, %rcx
je 0x479e
movslq 0x10(%rcx), %rcx
leaq (%r12,%rcx), %rdx
movq %rdx, 0x8(%rsp)
movslq 0x10(%rax), %r13
subq %rcx, %r13
leaq 0x1(%r13), %rdi
callq 0x21d0
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movq 0x8(%rsp), %rsi
movq %r13, %rdx
callq 0x20c0
movq 0x10(%rsp), %rax
movb $0x0, (%rax,%r13)
movq %rax, 0x90(%rsp,%r14,8)
incq %r14
testb %bpl, %bpl
leaq 0x18(%rsp), %rbp
je 0x46d8
jmp 0x4821
testl %r13d, %r13d
jg 0x4ab2
cmpb $0x1, 0x8(%rbx)
je 0x4ab2
cmpl %r14d, 0x18(%rbx)
movq 0x8(%rsp), %rcx
jle 0x4ac7
cmpq %rax, %rcx
je 0x4821
movl %r14d, %edx
movq %rdx, 0x10(%rsp)
movslq 0x10(%rcx), %rcx
leaq (%r12,%rcx), %r14
movslq 0x10(%rax), %r13
subq %rcx, %r13
leaq 0x1(%r13), %rdi
callq 0x21d0
movq %rax, %rbp
movq %rax, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x20c0
movb $0x0, (%rbp,%r13)
movq 0x10(%rsp), %rax
movq %rbp, 0x90(%rsp,%rax,8)
leaq 0x18(%rsp), %rbp
movl $0x1, %esi
movq %rbp, %rdi
callq 0x3669
cmpb $0x1, 0x8(%rbx)
jne 0x4845
movq %rbp, %rdi
callq 0x3619
cmpl $0x7, (%rax)
jne 0x4af1
movq %rbp, %rdi
callq 0x3619
movq %rbp, %rdi
movq %rax, %rbp
movl $0x1, %esi
callq 0x3669
movslq 0x18(%rbx), %rax
testq %rax, %rax
movq (%rsp), %rcx
jle 0x488b
xorl %edx, %edx
cmpq $0x0, 0x90(%rsp,%rdx,8)
jne 0x4883
cmpq $0x0, 0x20(%rbx,%rdx,8)
je 0x4a9a
incq %rdx
cmpq %rdx, %rax
jne 0x486c
movq %r12, 0x8(%rsp)
xorl %r14d, %r14d
movq %r14, 0x40(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq 0x10(%rbx), %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x3c(%rsp)
movq %rax, 0x40(%rsp)
movl $0x1, %edi
movl $0x10, %esi
callq 0x20f0
movq %rax, 0x58(%rsp)
movq %r14, 0x60(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x68(%rsp)
xorl %r12d, %r12d
leaq 0x58(%rsp), %r14
movq %r14, %rdi
callq 0x3619
movq %rax, %r13
movl (%rax), %eax
cmpl $0x3, %eax
jne 0x4951
movq %r14, %rdi
callq 0x3619
cmpl $0x9, (%rax)
jne 0x4a80
movq %rax, %r12
cmpb $0x1, 0x1c(%rax)
je 0x4a80
movq 0x8(%r12), %rdi
callq 0x2250
testl %eax, %eax
jle 0x4a6b
decl %eax
cmpl 0x18(%rbx), %eax
jg 0x4a6b
movl %eax, %eax
movq 0x90(%rsp,%rax,8), %rsi
testq %rsi, %rsi
jne 0x4942
movq 0x20(%rbx,%rax,8), %rsi
movq %r15, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x2a19
jmp 0x48e1
testl %eax, %eax
je 0x498f
testq %r12, %r12
je 0x4971
movslq 0x14(%r12), %rsi
movl 0x10(%r13), %edx
subl %esi, %edx
addq 0x10(%rbx), %rsi
movq %r15, %rdi
callq 0x2a19
movslq 0x10(%r13), %rsi
movl 0x14(%r13), %edx
subl %esi, %edx
addq 0x10(%rbx), %rsi
movq %r15, %rdi
callq 0x2a19
movq %r13, %r12
jmp 0x48dc
testq %r12, %r12
je 0x49aa
movslq 0x14(%r12), %rsi
addq 0x10(%rbx), %rsi
movq %r15, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x2a19
leaq 0x58(%rsp), %rdi
callq 0x35d0
xorl %ebx, %ebx
movq (%rsp), %r13
movq 0x8(%rsp), %r12
movq 0x90(%rsp,%rbx), %rdi
testq %rdi, %rdi
je 0x49dd
callq 0x21a0
movq $0x0, 0x90(%rsp,%rbx)
addq $0x8, %rbx
cmpq $0x80, %rbx
jne 0x49bf
movb $0x1, %r14b
jmp 0x45bf
testq %rdx, %rdx
je 0x49fa
movb $0x0, (%rdx)
leaq 0x770f(%rip), %rdi      # 0xc110
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3992
leaq 0x18(%rsp), %rdi
callq 0x35d0
xorl %eax, %eax
movq %rax, 0x80(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movq (%r15), %rbx
movq %rax, (%r15)
movq %r15, %rdi
callq 0x29cc
movq %r15, %rdi
callq 0x21a0
movq 0x50(%rsp), %rax
testq %rax, %rax
je 0x4a56
movb %r14b, (%rax)
movq %rbx, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rsi
movq (%rsp), %rax
movl 0x10(%rax), %edx
incl %edx
leaq 0x915b(%rip), %rdi      # 0xdbd9
jmp 0x4a93
movq (%rbx), %rsi
movq (%rsp), %rax
movl 0x10(%rax), %edx
incl %edx
leaq 0x909d(%rip), %rdi      # 0xdb30
xorl %eax, %eax
callq 0x344d
movq (%rbx), %rsi
incl %edx
movl 0x10(%rcx), %ecx
incl %ecx
leaq 0x9018(%rip), %rdi      # 0xdac3
xorl %eax, %eax
callq 0x344d
movq (%rbx), %rsi
movq (%rsp), %rax
movl 0x10(%rax), %edx
incl %edx
leaq 0x8e42(%rip), %rdi      # 0xd907
jmp 0x4a93
movq (%rbx), %rsi
movq (%rsp), %rax
movl 0x10(%rax), %edx
incl %edx
leaq 0x8eea(%rip), %rdi      # 0xd9c4
jmp 0x4a93
movq (%rbx), %rsi
movq (%rsp), %rax
movl 0x10(%rax), %edx
incl %edx
leaq 0x8ce1(%rip), %rdi      # 0xd7d0
jmp 0x4a93
movq (%rbx), %rsi
movq (%rsp), %rax
movl 0x10(%rax), %edx
incl %edx
leaq 0x8f2b(%rip), %rdi      # 0xda2f
jmp 0x4a93
movq (%rbx), %rsi
movq (%rsp), %rax
movl 0x10(%rax), %edx
incl %edx
leaq 0x8d42(%rip), %rdi      # 0xd85b
jmp 0x4a93