// File: /workspace/github_cpp_makefile_star2_O2/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
movq %rdi, 0x8(%rsp)
cmpb $0x0, 0x14(%rdi)
je 0x445d
movq %rsi, %r12
movq %rdx, 0x50(%rsp)
pushq $-0x1
popq %rsi
movl $0x400, %edi            # imm = 0x400
callq 0x2875
movq %rax, %r15
leaq 0x70(%rsp), %rbx
movq %r12, (%rbx)
andq $0x0, 0x8(%rbx)
movq %r12, 0x10(%rbx)
callq 0x2aa2
leaq 0x18(%rsp), %rcx
movq %rax, (%rcx)
andq $0x0, 0x8(%rcx)
movq %rbx, 0x10(%rcx)
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %rbx, %r13
leaq 0x18(%rsp), %rdi
callq 0x3416
movq %rax, %rbx
testq %r13, %r13
je 0x4106
movl 0x10(%rbx), %edx
movslq 0x14(%r13), %rsi
subl %esi, %edx
jle 0x4106
addq %r12, %rsi
movq %r15, %rdi
callq 0x2950
movl (%rbx), %eax
cmpl $0x23, %eax
je 0x4116
testl %eax, %eax
jne 0x4129
jmp 0x4482
movq 0x8(%rbx), %rsi
movq 0x8(%rsp), %rdi
callq 0x4769
testq %rax, %rax
jne 0x413f
movslq 0x10(%rbx), %rsi
movl 0x14(%rbx), %edx
subl %esi, %edx
addq %r12, %rsi
movq %r15, %rdi
callq 0x2950
jmp 0x40db
movq %rax, %rbp
cmpl $0x0, 0x18(%rax)
jle 0x4184
xorl %eax, %eax
leaq 0x18(%rsp), %r13
cmpq $0x80, %rax
je 0x4166
andq $0x0, 0x90(%rsp,%rax)
addq $0x8, %rax
jmp 0x414f
movb 0x8(%rbp), %r14b
movq %r13, %rdi
callq 0x3416
movq %rax, %rbx
testb %r14b, %r14b
je 0x419b
cmpl $0x6, (%rbx)
je 0x41b8
jmp 0x454d
movq 0x10(%rbp), %rsi
movq %r15, %rdi
pushq $-0x1
popq %rdx
callq 0x2950
movb $0x1, %r14b
jmp 0x40db
movq %r13, %rdi
pushq $0x1
popq %rsi
callq 0x345a
cmpb $0x1, 0x1c(%rbx)
je 0x41b8
testl $0xfffffffd, (%rbx)     # imm = 0xFFFFFFFD
jne 0x457e
xorl %r14d, %r14d
movq %r13, %rdi
callq 0x3416
movq %rax, 0x10(%rsp)
movq %r13, %rdi
pushq $0x1
popq %rsi
callq 0x345a
xorl %r13d, %r13d
leaq 0x18(%rsp), %rdi
callq 0x3416
movl (%rax), %ecx
testl $0xfffffffd, %ecx       # imm = 0xFFFFFFFD
je 0x425f
cmpl $0x7, %ecx
sete %bl
cmpl $0x8, %ecx
sete %dl
testl %r13d, %r13d
jne 0x4202
andb 0x8(%rbp), %bl
orb %bl, %dl
jne 0x421a
xorl %eax, %eax
cmpl $0x7, %ecx
sete %al
xorl %edx, %edx
cmpl $0x6, %ecx
sete %dl
addl %edx, %r13d
subl %eax, %r13d
jmp 0x41d6
movslq 0x18(%rbp), %rcx
cmpq %rcx, %r14
jge 0x4536
movq 0x10(%rsp), %rcx
cmpq %rax, %rcx
leaq 0x18(%rsp), %r13
je 0x4252
movslq 0x10(%rcx), %rcx
leaq (%r12,%rcx), %rdi
movslq 0x10(%rax), %rsi
subq %rcx, %rsi
callq 0x4598
movq %rax, 0x90(%rsp,%r14,8)
incq %r14
testb %bl, %bl
je 0x41bb
jmp 0x42aa
testl %r13d, %r13d
jg 0x451f
cmpb $0x1, 0x8(%rbp)
je 0x451f
cmpl %r14d, 0x18(%rbp)
leaq 0x18(%rsp), %r13
movq 0x10(%rsp), %rcx
jle 0x4536
cmpq %rax, %rcx
je 0x42aa
movl %r14d, %ebx
movslq 0x10(%rcx), %rcx
leaq (%r12,%rcx), %rdi
movslq 0x10(%rax), %rsi
subq %rcx, %rsi
callq 0x4598
movq %rax, 0x90(%rsp,%rbx,8)
movq %r13, %rdi
pushq $0x1
popq %rsi
callq 0x345a
cmpb $0x1, 0x8(%rbp)
jne 0x42cc
movq %r13, %rdi
callq 0x3416
cmpl $0x7, (%rax)
jne 0x4564
movq %r13, %rdi
callq 0x3416
movq %rax, %rbx
movq %r13, %rdi
pushq $0x1
popq %rsi
callq 0x345a
movl 0x18(%rbp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
xorl %edx, %edx
leaq 0x58(%rsp), %r14
cmpq %rdx, %rax
je 0x4317
cmpq $0x0, 0x90(%rsp,%rdx,8)
jne 0x4312
cmpq $0x0, 0x20(%rbp,%rdx,8)
je 0x4501
incq %rdx
jmp 0x42f6
movq %r12, 0x10(%rsp)
andq $0x0, 0x40(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq 0x10(%rbp), %rax
movq %rax, 0x30(%rsp)
andl $0x0, 0x3c(%rsp)
movq %rax, 0x40(%rsp)
callq 0x2aa2
movq %rax, 0x58(%rsp)
andq $0x0, 0x60(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x68(%rsp)
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x3416
movq %rax, %r13
movl (%rax), %eax
cmpl $0x3, %eax
je 0x43b0
testl %eax, %eax
je 0x440f
testq %r12, %r12
je 0x4390
movslq 0x14(%r12), %rsi
movl 0x10(%r13), %edx
subl %esi, %edx
addq 0x10(%rbp), %rsi
movq %r15, %rdi
callq 0x2950
movslq 0x10(%r13), %rsi
movl 0x14(%r13), %edx
subl %esi, %edx
addq 0x10(%rbp), %rsi
movq %r15, %rdi
callq 0x2950
movq %r13, %r12
leaq 0x58(%rsp), %r14
jmp 0x435a
movq %r14, %rdi
callq 0x3416
cmpl $0x9, (%rax)
jne 0x44ce
movq %rax, %r12
cmpb $0x1, 0x1c(%rax)
je 0x44ce
movq 0x8(%r12), %rdi
callq 0x2250
testl %eax, %eax
jle 0x44e5
decl %eax
cmpl 0x18(%rbp), %eax
jg 0x44e5
movl %eax, %eax
movq 0x90(%rsp,%rax,8), %rsi
testq %rsi, %rsi
jne 0x43ff
movq 0x20(%rbp,%rax,8), %rsi
movq %r15, %rdi
pushq $-0x1
popq %rdx
callq 0x2950
jmp 0x435a
testq %r12, %r12
je 0x4428
movslq 0x14(%r12), %rsi
addq 0x10(%rbp), %rsi
movq %r15, %rdi
pushq $-0x1
popq %rdx
callq 0x2950
leaq 0x58(%rsp), %rdi
callq 0x33d0
xorl %r14d, %r14d
movq 0x10(%rsp), %r12
cmpq $0x80, %r14
je 0x4193
leaq (%rsp,%r14), %rdi
addq $0x90, %rdi
callq 0x353d
addq $0x8, %r14
jmp 0x443a
testq %rdx, %rdx
je 0x4465
movb $0x0, (%rdx)
leaq 0x6ca4(%rip), %rdi      # 0xb110
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x36e9
leaq 0x18(%rsp), %rdi
callq 0x33d0
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
andq $0x0, 0x80(%rsp)
movq (%r15), %rbx
andq $0x0, (%r15)
movq %r15, %rdi
callq 0x28f2
movq 0x50(%rsp), %rax
testq %rax, %rax
je 0x44b9
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 (%rbp), %rsi
movq 0x8(%rsp), %rax
movl 0x10(%rax), %edx
incl %edx
leaq 0x864d(%rip), %rdi      # 0xcb30
jmp 0x44fa
movq (%rbp), %rsi
movq 0x8(%rsp), %rax
movl 0x10(%rax), %edx
incl %edx
leaq 0x86df(%rip), %rdi      # 0xcbd9
xorl %eax, %eax
callq 0x326e
movq (%rbp), %rsi
incl %edx
movq 0x8(%rsp), %rax
movl 0x10(%rax), %ecx
incl %ecx
leaq 0x85ab(%rip), %rdi      # 0xcac3
xorl %eax, %eax
callq 0x326e
movq (%rbp), %rsi
movq 0x8(%rsp), %rax
movl 0x10(%rax), %edx
incl %edx
leaq 0x83d3(%rip), %rdi      # 0xc907
jmp 0x44fa
movq (%rbp), %rsi
movq 0x8(%rsp), %rax
movl 0x10(%rax), %edx
incl %edx
leaq 0x8479(%rip), %rdi      # 0xc9c4
jmp 0x44fa
movq (%rbp), %rsi
movq 0x8(%rsp), %rax
movl 0x10(%rax), %edx
incl %edx
leaq 0x826e(%rip), %rdi      # 0xc7d0
jmp 0x44fa
movq (%rbp), %rsi
movq 0x8(%rsp), %rax
movl 0x10(%rax), %edx
incl %edx
leaq 0x84b6(%rip), %rdi      # 0xca2f
jmp 0x44fa
movq (%rbp), %rsi
movq 0x8(%rsp), %rax
movl 0x10(%rax), %edx
incl %edx
leaq 0x82c8(%rip), %rdi      # 0xc85b
jmp 0x44fa