// File: /workspace/github_c_makefile_star2_O1/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 $0xb8, %rsp
movl %ecx, 0x7c(%rsp)
movq %rdx, 0x88(%rsp)
movq %rsi, 0x80(%rsp)
movq %rdi, %r12
movq (%rdi), %rax
movl 0x2c(%rax), %eax
movl %eax, 0x44(%rsp)
leaq 0x10(%rsp), %r13
leaq 0xf(%rsp), %rbp
leaq 0x20(%rsp), %rbx
movq %rdi, 0x70(%rsp)
movq (%r12), %rax
movq 0x18(%rax), %rcx
movq %rcx, 0x10(%rsp)
movb 0x20(%rax), %cl
movb %cl, 0xf(%rsp)
movups 0x28(%rax), %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0x38(%rax), %rax
movq %rax, 0x30(%rsp)
cmpb $0x0, 0xe7b2(%rip)      # 0x14498
jne 0x5cf6
movq %r13, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5b5f
cmpl $0x0, 0x44(%rsp)
jne 0x5d13
movq (%r12), %rax
movq 0x30(%rsp), %rcx
movq %rcx, 0x38(%rax)
movaps 0x20(%rsp), %xmm0
movups %xmm0, 0x28(%rax)
movb 0xf(%rsp), %r14b
testb %r14b, %r14b
jne 0x5d29
cmpl $0x0, 0x68(%r12)
jne 0x609f
movq 0x10(%rsp), %r15
movaps 0x20(%rsp), %xmm0
movaps %xmm0, 0x50(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x60(%rsp)
cmpb $0x2f, %r14b
jne 0x5e09
movq %r13, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5b5f
movzbl 0xf(%rsp), %eax
cmpl $0x2a, %eax
je 0x5d8f
cmpl $0x2f, %eax
jne 0x5dd8
movzbl 0xf(%rsp), %eax
testl %eax, %eax
je 0x5e09
cmpl $0xa, %eax
je 0x5e09
movq %r13, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5b5f
jmp 0x5d69
movq %r13, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5b5f
movb 0xf(%rsp), %r14b
testb %r14b, %r14b
je 0x5dca
movq %r13, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5b5f
xorb $0x2a, %r14b
movb 0xf(%rsp), %al
movl %eax, %ecx
xorb $0x2f, %cl
orb %r14b, %cl
movl %eax, %r14d
jne 0x5da2
cmpb $0x0, 0xf(%rsp)
je 0x5df8
movb $0x20, 0xf(%rsp)
jmp 0x5e09
movq %r15, 0x10(%rsp)
movb %r14b, 0xf(%rsp)
movaps 0x50(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x30(%rsp)
jmp 0x5e09
movq %rbx, %rdi
leaq 0x7e3d(%rip), %rsi      # 0xdc3f
xorl %eax, %eax
callq 0x3250
cmpl $0x0, 0x44(%rsp)
sete %al
orb 0xe681(%rip), %al       # 0x14498
xorl %r14d, %r14d
testb $0x1, %al
jne 0x5e2a
movq (%r12), %rax
movq 0x18(%rax), %rax
movsbl (%rax), %r14d
movq 0x10(%rsp), %rax
movq %rax, 0x98(%rsp)
movb 0xf(%rsp), %al
movb %al, 0x18(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x60(%rsp)
movaps 0x20(%rsp), %xmm0
movaps %xmm0, 0x50(%rsp)
cmpl $0x0, 0x68(%r12)
movslq %r14d, %r15
je 0x5fcc
callq 0x3230
movq %rax, 0x48(%rsp)
movq (%rax), %rax
testb $0x4, 0x1(%rax,%r15,2)
jne 0x5fcc
cmpl $0x5f, %r14d
je 0x5fcc
movsbq 0x18(%rsp), %r14
cmpb $0x5f, %r14b
je 0x5ea1
movzwl (%rax,%r14,2), %eax
andl $0x400, %eax            # imm = 0x400
je 0x5fcc
movq %r15, 0x90(%rsp)
movl $0x100, %edi            # imm = 0x100
callq 0x3170
movq %rax, %rdx
movq 0x30(%rsp), %rax
movq %rax, 0xb0(%rsp)
movaps 0x20(%rsp), %xmm0
movaps %xmm0, 0xa0(%rsp)
cmpb $0x5f, 0x18(%rsp)
je 0x5eed
movq 0x48(%rsp), %rax
movq (%rax), %rax
movzwl (%rax,%r14,2), %eax
andl $0x8, %eax
jne 0x5eed
xorl %eax, %eax
jmp 0x5f57
movl $0x100, %r14d           # imm = 0x100
xorl %r15d, %r15d
movb 0x18(%rsp), %r12b
leaq 0x2(%r15), %rax
movl %r14d, %ecx
cmpq %rcx, %rax
jne 0x5f18
addl %r14d, %r14d
movslq %r14d, %rsi
movq %rdx, %rdi
callq 0x31a0
movq %rax, %rdx
movb %r12b, (%rdx,%r15)
incq %r15
movq %r13, %rsi
movq %rdx, %r12
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5b5f
movq %r12, %rdx
movl %eax, %r12d
cmpb $0x5f, %al
je 0x5efb
movq 0x48(%rsp), %rax
movq (%rax), %rax
movsbq %r12b, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x8, %eax
jne 0x5efb
movl %r15d, %eax
movq 0x70(%rsp), %r12
movb $0x0, (%rdx,%rax)
movq %r12, %rdi
movq %rdx, %rsi
movq %rdx, %r14
leaq 0xa0(%rsp), %rdx
movq %r13, %rcx
movq %rbp, %r8
movq %rbx, %r9
callq 0x6150
movl %eax, %r15d
testl %eax, %eax
je 0x5fb4
movq %r14, %rdi
callq 0x3030
movq 0x98(%rsp), %rax
movq %rax, 0x10(%rsp)
movb 0x18(%rsp), %al
movb %al, 0xf(%rsp)
movaps 0x50(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x30(%rsp)
jmp 0x5fbb
movb $0x1, 0xe4dd(%rip)      # 0x14498
testl %r15d, %r15d
movq 0x90(%rsp), %r15
je 0x5cb9
movq 0x10(%rsp), %rax
movq %rax, 0x18(%rsp)
movsbq 0xf(%rsp), %r12
movaps 0x20(%rsp), %xmm0
movaps %xmm0, 0x50(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x60(%rsp)
callq 0x3230
movq %rax, %r14
movq (%rax), %rax
movq %r12, 0x48(%rsp)
testb $0x20, 0x1(%rax,%r12,2)
je 0x603a
testb $0x20, 0x1(%rax,%r15,2)
jne 0x603a
movsbq 0xf(%rsp), %rax
cmpq $0xa, %rax
je 0x603a
movq (%r14), %rcx
movzwl (%rcx,%rax,2), %eax
andl $0x2000, %eax           # imm = 0x2000
je 0x603a
movq %r13, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5b5f
jmp 0x6010
movq 0x70(%rsp), %rax
movq (%rax), %r12
cmpl $0x1, 0x8(%r12)
jne 0x60c2
cmpb $0x23, 0xf(%rsp)
jne 0x60c2
movq %r13, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5b5f
cmpb $0x23, 0xf(%rsp)
jne 0x60c2
movq %r13, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5b5f
movsbq 0xf(%rsp), %rax
cmpq $0xa, %rax
je 0x608e
movq (%r14), %rcx
movzwl (%rcx,%rax,2), %eax
andl $0x2000, %eax           # imm = 0x2000
jne 0x6066
movb $0x1, 0xe403(%rip)      # 0x14498
movq 0x70(%rsp), %r12
jmp 0x5cdf
movq (%r12), %rax
cmpq $0x0, 0x50(%rax)
je 0x614c
movq %r12, %rdi
callq 0x5ae1
movb $0x1, 0xe3db(%rip)      # 0x14498
jmp 0x5cb9
movq 0x18(%rsp), %rdx
movq %rdx, 0x10(%rsp)
movq 0x48(%rsp), %rax
movb %al, 0xf(%rsp)
movaps 0x50(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0x60(%rsp), %rcx
movq %rcx, 0x30(%rsp)
movq 0x80(%rsp), %rcx
testq %rcx, %rcx
je 0x60fd
subl 0x18(%r12), %edx
movl %edx, (%rcx)
movq 0x88(%rsp), %rdx
testq %rdx, %rdx
je 0x612c
cmpl $0x1, 0x8(%r12)
leaq 0x28(%r12), %rsi
leaq 0x50(%rsp), %rcx
cmoveq %rsi, %rcx
movq 0x10(%rcx), %rsi
movq %rsi, 0x10(%rdx)
movups (%rcx), %xmm0
movups %xmm0, (%rdx)
cmpl $0x0, 0x7c(%rsp)
jne 0x613a
movb $0x0, 0xe35e(%rip)      # 0x14498
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x613a