// File: /workspace/github_c_makefile_star2_O3/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 $0xa8, %rsp
movl %ecx, 0x1c(%rsp)
movq %rdx, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdi, %r13
movq (%rdi), %r15
movl 0x2c(%r15), %r12d
movb 0xe778(%rip), %r14b     # 0x14498
leaq 0xb(%rsp), %rbp
leaq 0x20(%rsp), %rbx
movq %rdi, 0x80(%rsp)
movl %r12d, 0x3c(%rsp)
movq 0x18(%r15), %rax
movq %rax, 0x10(%rsp)
movb 0x20(%r15), %al
movb %al, 0xb(%rsp)
movups 0x28(%r15), %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0x38(%r15), %rax
movq %rax, 0x30(%rsp)
testb $0x1, %r14b
jne 0x5d71
leaq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5be3
testl %r12d, %r12d
jne 0x5d89
movq 0x30(%rsp), %rax
movq %rax, 0x38(%r15)
movaps 0x20(%rsp), %xmm0
movups %xmm0, 0x28(%r15)
movb 0xb(%rsp), %al
testb %al, %al
je 0x5dff
movq 0x10(%rsp), %r15
movaps 0x20(%rsp), %xmm0
movaps %xmm0, 0x50(%rsp)
movq 0x30(%rsp), %rcx
movq %rcx, 0x60(%rsp)
cmpb $0x2f, %al
jne 0x5e8d
leaq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5be3
movzbl 0xb(%rsp), %eax
cmpl $0x2a, %eax
je 0x5e0f
cmpl $0x2f, %eax
jne 0x5e55
testb %al, %al
je 0x5e8d
movzbl %al, %eax
cmpl $0xa, %eax
je 0x5e8d
leaq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5be3
movb 0xb(%rsp), %al
jmp 0x5dd5
cmpl $0x0, 0x68(%r13)
je 0x5e8d
jmp 0x611a
leaq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5be3
movb 0xb(%rsp), %r15b
testb %r15b, %r15b
je 0x5e75
leaq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5be3
xorb $0x2a, %r15b
movb 0xb(%rsp), %al
movl %eax, %ecx
xorb $0x2f, %cl
orb %r15b, %cl
movl %eax, %r15d
jne 0x5e24
movb $0x20, 0xb(%rsp)
jmp 0x5e8d
movq %r15, 0x10(%rsp)
movb $0x2f, 0xb(%rsp)
movaps 0x50(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x30(%rsp)
jmp 0x5e8d
movq %rbx, %rdi
leaq 0x7dc0(%rip), %rsi      # 0xdc3f
xorl %eax, %eax
callq 0x3250
movb 0xe60b(%rip), %r14b     # 0x14498
testl %r12d, %r12d
sete %al
orb %r14b, %al
xorl %r15d, %r15d
testb $0x1, %al
jne 0x5ea9
movq (%r13), %rax
movq 0x18(%rax), %rax
movsbl (%rax), %r15d
movq 0x10(%rsp), %rax
movq %rax, 0x78(%rsp)
movq %r13, %rbp
movb 0xb(%rsp), %r13b
movaps 0x20(%rsp), %xmm0
movaps %xmm0, 0x50(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x60(%rsp)
movl 0x68(%rbp), %r12d
callq 0x3230
movq %rax, %r14
movl %r13d, 0xc(%rsp)
movsbq %r13b, %rsi
movq %rbp, %r13
movslq %r15d, %rdx
testl %r12d, %r12d
leaq 0xb(%rsp), %rbp
je 0x604b
movq (%r14), %rax
testb $0x4, 0x1(%rax,%rdx,2)
jne 0x604b
cmpl $0x5f, %r15d
je 0x604b
cmpb $0x5f, 0xc(%rsp)
je 0x5f26
movzwl (%rax,%rsi,2), %eax
andl $0x400, %eax            # imm = 0x400
je 0x604b
movq %rsi, 0x70(%rsp)
movq %rdx, 0x88(%rsp)
movl $0x100, %edi            # imm = 0x100
callq 0x3170
movq %rax, %rdx
movq 0x30(%rsp), %rax
movq %rax, 0xa0(%rsp)
movaps 0x20(%rsp), %xmm0
movaps %xmm0, 0x90(%rsp)
cmpb $0x5f, 0xc(%rsp)
je 0x5f76
movq (%r14), %rax
movq 0x70(%rsp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x8, %eax
jne 0x5f76
xorl %eax, %eax
jmp 0x5fe3
movl $0x100, %r13d           # imm = 0x100
xorl %r12d, %r12d
movl 0xc(%rsp), %eax
movl %eax, %r15d
leaq 0x2(%r12), %rax
movl %r13d, %ecx
cmpq %rcx, %rax
jne 0x5fa4
addl %r13d, %r13d
movslq %r13d, %rsi
movq %rdx, %rdi
callq 0x31a0
movq %rax, %rdx
movb %r15b, (%rdx,%r12)
incq %r12
leaq 0x10(%rsp), %rsi
movq %rdx, %r15
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5be3
movq %r15, %rdx
movl %eax, %r15d
cmpb $0x5f, %al
je 0x5f86
movq (%r14), %rax
movsbq %r15b, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x8, %eax
jne 0x5f86
movl %r12d, %eax
movq 0x80(%rsp), %r13
movb $0x0, (%rdx,%rax)
movq %r13, %rdi
movq %rdx, %rsi
movq %rdx, %r15
leaq 0x90(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %rbp, %r8
movq %rbx, %r9
callq 0x61d8
testl %eax, %eax
je 0x613a
movq %r15, %rdi
callq 0x3030
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0xc(%rsp), %eax
movb %al, 0xb(%rsp)
movaps 0x50(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x88(%rsp), %rdx
movq 0x70(%rsp), %rsi
movq 0x30(%rsp), %rax
movq %rax, 0x60(%rsp)
movaps 0x20(%rsp), %xmm0
movaps %xmm0, 0x50(%rsp)
movq (%r14), %rcx
testb $0x20, 0x1(%rcx,%rsi,2)
movl 0xc(%rsp), %eax
movl 0x3c(%rsp), %r12d
je 0x60b3
testb $0x20, 0x1(%rcx,%rdx,2)
movl 0xc(%rsp), %eax
jne 0x60b3
cmpb $0xa, 0xc(%rsp)
je 0x6152
leaq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5be3
movsbq 0xb(%rsp), %rax
cmpq $0xa, %rax
je 0x60b3
movq (%r14), %rcx
movzwl (%rcx,%rax,2), %ecx
andl $0x2000, %ecx           # imm = 0x2000
jne 0x6088
movq (%r13), %r15
cmpl $0x1, 0x8(%r15)
jne 0x6156
cmpb $0x23, %al
jne 0x6156
leaq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5be3
cmpb $0x23, 0xb(%rsp)
jne 0x6156
leaq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5be3
movsbq 0xb(%rsp), %rax
cmpq $0xa, %rax
je 0x610b
movq (%r14), %rcx
movzwl (%rcx,%rax,2), %eax
andl $0x2000, %eax           # imm = 0x2000
jne 0x60e1
movb $0x1, 0xe386(%rip)      # 0x14498
movb $0x1, %r14b
jmp 0x5d71
movq (%r13), %rax
cmpq $0x0, 0x50(%rax)
je 0x61ce
movq %r13, %rdi
callq 0x5b65
movb $0x1, 0xe360(%rip)      # 0x14498
jmp 0x6146
movb $0x1, 0xe357(%rip)      # 0x14498
movl 0x3c(%rsp), %r12d
movq (%r13), %r15
movb $0x1, %r14b
jmp 0x5d37
movq (%r13), %r15
movl 0x1c(%rsp), %edx
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rcx
movq 0x60(%rsp), %rax
movq %rax, 0x30(%rsp)
movaps 0x50(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
testq %rcx, %rcx
je 0x6188
movq 0x78(%rsp), %rax
subl 0x18(%r15), %eax
movl %eax, (%rcx)
testq %rsi, %rsi
je 0x61ad
cmpl $0x1, 0x8(%r15)
leaq 0x28(%r15), %rax
leaq 0x50(%rsp), %rcx
cmoveq %rax, %rcx
movq 0x10(%rcx), %rax
movq %rax, 0x10(%rsi)
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
testl %edx, %edx
jne 0x61b8
movb $0x0, 0xe2e0(%rip)      # 0x14498
movl 0xc(%rsp), %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, 0xc(%rsp)
jmp 0x61b8