// File: /workspace/github_c_makefile_star2_O2/Yamsman[P]scc/src/lex.c
// Function: lex_nchar
char lex_nchar(lexer *lx, int *len, s_pos *nloc, int pflag) {
	char *pos, *prev_pos;
	char cch, prev_cch;
	s_pos loc, prev_loc;
	
	/*
	 * Get the next character
	 */
	static int is_reset = 0;
	int is_start = (lx->tgt->loc.col == 0);
reset:	pos = lx->tgt->pos;
	cch = lx->tgt->cch;
	loc = lx->tgt->loc;
tpaste:	if (!is_reset) lex_prcc(lx, &pos, &cch, &loc);
	if (is_start) lx->tgt->loc = loc;

	//Release the current context if the end has been reached
	if (cch == '\0' && lx->m_exp) {
		//The last context will stay open until the lexer is closed
		if (lx->tgt->prev == NULL)
			return '\0';

		lexer_tgt_close(lx);
		is_reset = 1;
		goto reset;
	}

	/*
	 * Replace comments with whitespace
	 */
	prev_pos = pos; prev_cch = cch; prev_loc = loc;
	if (cch == '/') {
		lex_prcc(lx, &pos, &cch, &loc);
		if (cch == '/') {
			//C++ style comments
			for (;;) {
				if (cch == '\n' || cch == '\0') break;
				lex_prcc(lx, &pos, &cch, &loc);
			}
			if (cch == '\0') {
				//error
			}
		} else if (cch == '*') {
			//C style comments
			lex_prcc(lx, &pos, &cch, &loc);
			char prevc = cch;
			for (;;) {
				if (prevc == '\0') break;
				lex_prcc(lx, &pos, &cch, &loc);
				if (prevc == '*' && cch == '/') break;
				prevc = cch;
			}
			if (cch == '\0') {
				c_error(&loc, "Unterminated comment\n");
			} else {
				cch = ' ';
			}
		} else {
			//Rewind
			pos = prev_pos; cch = prev_cch; loc = prev_loc;
		}
	}

	/*
	 * Macro expansion
	 * Only attempt if currently at beginning of an identifier and expansion is enabled
	*/
	char prev = (is_reset || is_start) ? '\0' : *lx->tgt->pos;
	prev_pos = pos; prev_cch = cch; prev_loc = loc;
	if (lx->m_exp && (!isalpha(prev) && prev != '_') && (isalpha(cch) || cch == '_')) {
		//Read identifier
		int len = 0;
		int bsize = 256;
		char cur = cch;
		char *buf = malloc(bsize);
		s_pos m_loc = loc;
		while (isalnum(cur) || cur == '_') {
			//Resize if needed
			if (len+2 == bsize) {
				bsize *= 2;
				buf = realloc(buf, bsize);
			}

			//Copy character to buffer
			buf[len++] = cur;
			cur = lex_prcc(lx, &pos, &cch, &loc);
		}
		buf[len] = '\0';

		//Attempt to expand the macro
		if (!lex_expand_macro(lx, buf, &m_loc, &pos, &cch, &loc)) {
			is_reset = 1;
			goto reset;
		} else {
			free(buf);
		}

		//If the macro was not expanded, restore to before the identifier was read
		pos = prev_pos; cch = prev_cch; loc = prev_loc;
	}

	/*
	 * Handle token pasting
	 */
	//Only attempt once per chunk of whitespace
	prev_pos = pos; prev_cch = cch; prev_loc = loc;
	if (isspace(cch) && !isspace(prev))
		while (isspace(cch) && cch != '\n') lex_prcc(lx, &pos, &cch, &loc);

	//Perform token pasting if a double pound sign exists
	int has_dns = 0;
	if (lx->tgt->type == TGT_MACRO && cch == '#') {
		//Check for a '##'
		lex_prcc(lx, &pos, &cch, &loc);
		if (cch == '#') {
			has_dns = 1;
			do lex_prcc(lx, &pos, &cch, &loc);
			while (isspace(cch) && cch != '\n');
		}
	}
	if (has_dns) {
		is_reset = 1;
		goto tpaste;
	} else {
		pos = prev_pos; cch = prev_cch; loc = prev_loc;
	}

	//Update external values if applicable and return the next character
	if (len != NULL) *len = pos - lx->tgt->pos;
	if (nloc != NULL) {
		if (lx->tgt->type != TGT_MACRO)
			*nloc = loc;
		else
			*nloc = lx->tgt->loc;
	}
	if (!pflag)
		is_reset = 0;

	return cch;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %ecx, 0x48(%rsp)
movq %rdx, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdi, %r13
movq (%rdi), %r12
movl 0x2c(%r12), %eax
movl %eax, 0x2c(%rsp)
movb 0xd9a1(%rip), %r14b     # 0x13498
leaq 0x8(%rsp), %rbp
leaq 0x10(%rsp), %rbx
movq %rdi, 0x68(%rsp)
movq 0x18(%r12), %rax
movq %rax, 0x8(%rsp)
movb 0x20(%r12), %al
movb %al, 0x7(%rsp)
movups 0x28(%r12), %xmm0
movaps %xmm0, 0x10(%rsp)
movq 0x38(%r12), %rax
movq %rax, 0x20(%rsp)
testb $0x1, %r14b
jne 0x5b44
movq %rbp, %rsi
leaq 0x7(%rsp), %rdx
movq %rbx, %rcx
callq 0x59a3
cmpl $0x0, 0x2c(%rsp)
jne 0x5b60
movq 0x20(%rsp), %rax
movq %rax, 0x38(%r12)
movaps 0x10(%rsp), %xmm0
movups %xmm0, 0x28(%r12)
movb 0x7(%rsp), %al
testb %al, %al
jne 0x5b73
cmpl $0x0, 0x68(%r13)
jne 0x5ea6
movq 0x8(%rsp), %r15
movaps 0x10(%rsp), %xmm0
movaps %xmm0, 0x30(%rsp)
movq 0x20(%rsp), %rcx
movq %rcx, 0x40(%rsp)
cmpb $0x2f, %al
jne 0x5c5c
movq %rbp, %rsi
leaq 0x7(%rsp), %rdx
movq %rbx, %rcx
callq 0x59a3
movzbl 0x7(%rsp), %eax
cmpl $0x2a, %eax
je 0x5bdd
cmpl $0x2f, %eax
jne 0x5c24
testb %al, %al
je 0x5c5c
movzbl %al, %eax
cmpl $0xa, %eax
je 0x5c5c
movq %rbp, %rsi
leaq 0x7(%rsp), %rdx
movq %rbx, %rcx
callq 0x59a3
movb 0x7(%rsp), %al
jmp 0x5bb3
movq %rbp, %rsi
leaq 0x7(%rsp), %rdx
movq %rbx, %rcx
callq 0x59a3
movb 0x7(%rsp), %bpl
testb %bpl, %bpl
je 0x5c44
leaq 0x8(%rsp), %rsi
leaq 0x7(%rsp), %rdx
movq %rbx, %rcx
callq 0x59a3
xorb $0x2a, %bpl
movb 0x7(%rsp), %al
movl %eax, %ecx
xorb $0x2f, %cl
orb %bpl, %cl
movl %eax, %ebp
jne 0x5bf2
movb $0x20, 0x7(%rsp)
jmp 0x5c5c
movq %r15, 0x8(%rsp)
movb $0x2f, 0x7(%rsp)
movaps 0x30(%rsp), %xmm0
movaps %xmm0, 0x10(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x20(%rsp)
jmp 0x5c5c
movq %rbx, %rdi
leaq 0x6ff1(%rip), %rsi      # 0xcc3f
xorl %eax, %eax
callq 0x35e4
movb 0xd83c(%rip), %r14b     # 0x13498
cmpl $0x0, 0x2c(%rsp)
sete %al
orb %r14b, %al
xorl %ebp, %ebp
testb $0x1, %al
jne 0x5c78
movq (%r13), %rax
movq 0x18(%rax), %rax
movsbl (%rax), %ebp
movq 0x8(%rsp), %rax
movq %rax, 0x50(%rsp)
movb 0x7(%rsp), %r12b
movaps 0x10(%rsp), %xmm0
movaps %xmm0, 0x30(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0x68(%r13), %r15d
callq 0x3230
movq %rax, %r14
movsbq %r12b, %rcx
movslq %ebp, %rsi
testl %r15d, %r15d
movl %r12d, %r15d
je 0x5de5
movq (%r14), %rax
testb $0x4, 0x1(%rax,%rsi,2)
jne 0x5de5
cmpl $0x5f, %ebp
je 0x5de5
cmpb $0x5f, %r15b
je 0x5ce6
movzwl (%rax,%rcx,2), %eax
andl $0x400, %eax            # imm = 0x400
je 0x5de5
movq %rcx, 0x70(%rsp)
movq %rsi, 0x78(%rsp)
movl $0x100, %edi            # imm = 0x100
callq 0x3170
movq %rax, %r12
movq 0x20(%rsp), %rax
movq %rax, 0x90(%rsp)
movaps 0x10(%rsp), %xmm0
movaps %xmm0, 0x80(%rsp)
movl $0x100, %ebp            # imm = 0x100
xorl %r13d, %r13d
movl %r15d, 0x4c(%rsp)
cmpb $0x5f, %r15b
je 0x5d3a
movq (%r14), %rax
movsbq %r15b, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x8, %eax
je 0x5d73
leaq 0x2(%r13), %rax
movl %ebp, %ecx
cmpq %rcx, %rax
jne 0x5d55
addl %ebp, %ebp
movslq %ebp, %rsi
movq %r12, %rdi
callq 0x31a0
movq %rax, %r12
movb %r15b, (%r12,%r13)
incq %r13
leaq 0x8(%rsp), %rsi
leaq 0x7(%rsp), %rdx
movq %rbx, %rcx
callq 0x59a3
movl %eax, %r15d
jmp 0x5d24
movl %r13d, %eax
movb $0x0, (%r12,%rax)
movq 0x68(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
leaq 0x80(%rsp), %rdx
leaq 0x8(%rsp), %rbp
movq %rbp, %rcx
leaq 0x7(%rsp), %r8
movq %rbx, %r9
callq 0x5f4e
testl %eax, %eax
je 0x5ebd
movq %r12, %rdi
callq 0x3030
movq 0x50(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x4c(%rsp), %r15d
movb %r15b, 0x7(%rsp)
movaps 0x30(%rsp), %xmm0
movaps %xmm0, 0x10(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x78(%rsp), %rsi
movq 0x70(%rsp), %rcx
movq 0x20(%rsp), %rax
movq %rax, 0x40(%rsp)
movaps 0x10(%rsp), %xmm0
movaps %xmm0, 0x30(%rsp)
movq (%r14), %rdx
movzwl (%rdx,%rcx,2), %ecx
btl $0xd, %ecx
movl %r15d, %eax
leaq 0x8(%rsp), %rbp
jae 0x5e46
testb $0x20, 0x1(%rdx,%rsi,2)
movl %r15d, %eax
jne 0x5e46
movl %r15d, %eax
cmpb $0xa, %al
je 0x5e46
andl $0x2000, %ecx           # imm = 0x2000
je 0x5e46
movq %rbp, %rsi
leaq 0x7(%rsp), %rdx
movq %rbx, %rcx
callq 0x59a3
movq (%r14), %rcx
movsbq 0x7(%rsp), %rax
movzwl (%rcx,%rax,2), %ecx
jmp 0x5e1b
movq (%r13), %r12
cmpl $0x1, 0x8(%r12)
jne 0x5ed0
cmpb $0x23, %al
jne 0x5ed0
movq %rbp, %rsi
leaq 0x7(%rsp), %rdx
movq %rbx, %rcx
callq 0x59a3
cmpb $0x23, 0x7(%rsp)
jne 0x5ed0
movq %rbp, %rsi
leaq 0x7(%rsp), %rdx
movq %rbx, %rcx
callq 0x59a3
movsbq 0x7(%rsp), %rax
cmpq $0xa, %rax
je 0x5e97
movq (%r14), %rcx
movzwl (%rcx,%rax,2), %eax
andl $0x2000, %eax           # imm = 0x2000
jne 0x5e6d
movb $0x1, 0xd5fa(%rip)      # 0x13498
movb $0x1, %r14b
jmp 0x5b44
movq (%r13), %rax
cmpq $0x0, 0x50(%rax)
je 0x5f49
movq %r13, %rdi
callq 0x592a
movb $0x1, 0xd5d4(%rip)      # 0x13498
movq (%r13), %r12
movb $0x1, %r14b
jmp 0x5b06
movq 0x40(%rsp), %rax
movq %rax, 0x20(%rsp)
movaps 0x30(%rsp), %xmm0
movaps %xmm0, 0x10(%rsp)
movq 0x58(%rsp), %rax
testq %rax, %rax
je 0x5efa
movq 0x50(%rsp), %rcx
subl 0x18(%r12), %ecx
movl %ecx, (%rax)
movq 0x60(%rsp), %rdx
testq %rdx, %rdx
je 0x5f26
cmpl $0x1, 0x8(%r12)
leaq 0x28(%r12), %rax
leaq 0x30(%rsp), %rcx
cmoveq %rax, %rcx
movq 0x10(%rcx), %rax
movq %rax, 0x10(%rdx)
movups (%rcx), %xmm0
movups %xmm0, (%rdx)
cmpl $0x0, 0x48(%rsp)
jne 0x5f34
movb $0x0, 0xd564(%rip)      # 0x13498
movl %r15d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
jmp 0x5f34