// File: /workspace/github_c_makefile_star2_O0/Yamsman[P]scc/src/lex.c
// Function: lex_expand_macro
int lex_expand_macro(lexer *lx, char *ident, s_pos *m_loc, char **cpos, char *cch, s_pos *cloc) {
	lex_target *tgt = lx->tgt;
	char *pos = *cpos;
	char cur = *cch;
	s_pos loc = *cloc;
	char *m_name, *m_exp;

	//Check to see if there is an applicable macro for the given name
	int expanded = 0;
	int m_param = 0;
	symbol *s = symtable_search(lx->stb, ident);
	m_name = (s != NULL) ? s->name : NULL;
	m_exp = (s != NULL) ? s->mac_exp : NULL;
	for (lex_target *i = tgt; i != NULL; i = i->prev) {
		//Only check macro contexts
		if (i->type != TGT_MACRO) continue;

		//If the macro was found in the symbol table, check if it is a macro parameter
		if (m_exp == NULL) {
			symbol *m = symtable_search(lx->stb, i->name);
			if (m == NULL) continue;

			//Check the macro's parameters
			vector *ptable = &m->type->param;
			for (int j=0; j<ptable->len; j++) {
				s_param *p = ptable->table[j];

				//If a match is found, s becomes the "parent" macro
				if (!strcmp(ident, p->name)) {
					s = m;
					m_param = 1;
					m_name = p->name;
					m_exp = i->mp_exp.table[j];
					goto pfound;
				}
			}
		}

		//Check to see if the macro has been expanded
		if (!strcmp(i->name, ident)) {
			expanded = 1;
		}
	}
	if (m_exp == NULL || expanded) {
		return -1;
	}
	
	//Check for and read function macro arguments
	vector macro_params;
pfound:	vector_init(&macro_params, VECTOR_EMPTY);
	int m_error = 0;
	char *prev_pos = pos;
	char prev_cch = cur;
	s_pos prev_loc = loc;
	while (isspace(cur)) cur = lex_prcc(lx, &pos, &cur, &loc);
	if (cur == '(' && s->is_fmacro) {
		//Read macro arguments
		int paren_c = 0;
		cur = lex_prcc(lx, &pos, &cur, &loc);
		while (cur != ')') {
			//Skip whitespace
			while (isspace(cur)) cur = lex_prcc(lx, &pos, &cur, &loc);

			//Read a single argument
			int len = 0;
			int bsize = 256;
			char *buf = malloc(bsize);
			for (;;) {
				//Only stop if a ')' or ',' is encountered
				//outside of nested parentheses
				if (cur == '(') paren_c++;
				if (cur == ',' && paren_c == 0) break;
				if (cur == ')') {
					if (paren_c == 0)
						break;
					paren_c--;
				}
				if (cur == '\0') break;

				//Resize if needed
				if (len+2 == bsize) {
					bsize *= 2;
					buf = realloc(buf, bsize);
				}

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

			//Post-read checks
			if (cur == ',')
				lex_prcc(lx, &pos, &cur, &loc);
			if (cur == '\0') {
				m_error = -1;
				c_error(&loc,
					"Unexpected end of input in macro parameter list\n");
				break;
			}
		}
		lex_prcc(lx, &pos, &cur, &loc);
	} else {
		//Rewind and restore whitespace if no argument list was found
		pos = prev_pos;
		cur = prev_cch;
		loc = prev_loc;
	}

	//Fill expansion buffer for predefined __FILE__ or __LINE__ macros
	if (!strcmp(ident, "__FILE__"))
		snprintf(s->mac_exp, 256, "\"%s\"", lx->tgt->name);
	else if (!strcmp(ident, "__LINE__"))
		snprintf(s->mac_exp, 256, "%i", m_loc->line);
	
	//Ensure the number of arguments matches the amount specified in the macro definition
	if (!m_param && macro_params.len != s->type->param.len) {
		m_error = -1;
		if (!lx->m_cexpr) { //Not an error in preprocessor constant expressions
			c_error(m_loc, "Incorrect number of arguments to function-like macro '%s' "
				"(expected %i, read %i)\n",
				ident, s->type->param.len, macro_params.len);
		}
	}

	//If no issues have been encountered, expand the macro
predef:	if (!m_error) {
		//Commit to the state of the current context
		lx->tgt->pos = pos;
		lx->tgt->cch = cur;
		lx->tgt->loc = loc;

		//Open a context for the macro
		lexer_tgt_open(lx, m_name, TGT_MACRO, m_exp);
		tgt = lx->tgt;
		tgt->loc = *m_loc;

		//Update external values
		*cpos = tgt->pos;
		*cch = tgt->cch;
		*cloc = tgt->loc;

		//Set definitions for each parameter
		for (int i=0; i<macro_params.len; i++)
			vector_add(&tgt->mp_exp, macro_params.table[i]);
	} else {
		//Otherwise, free any arguments
		for (int i=0; i<macro_params.len; i++)
			free(macro_params.table[i]);
	}

	//Free the macro name and parameters
	vector_close(&macro_params);
	free(ident);
	return m_error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movb (%rax), %al
movb %al, -0x49(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x68(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x60(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x7c(%rbp)
movl $0x0, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0xfb60
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0x7ad0
movq -0x88(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x118(%rbp)
jmp 0x7adb
xorl %eax, %eax
movq %rax, -0x118(%rbp)
jmp 0x7adb
movq -0x118(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0x7b04
movq -0x88(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x120(%rbp)
jmp 0x7b0f
xorl %eax, %eax
movq %rax, -0x120(%rbp)
jmp 0x7b0f
movq -0x120(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x90(%rbp)
je 0x7c76
movq -0x90(%rbp), %rax
cmpl $0x1, 0x8(%rax)
je 0x7b45
jmp 0x7c5f
cmpq $0x0, -0x78(%rbp)
jne 0x7c3e
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x90(%rbp), %rax
movq (%rax), %rsi
callq 0xfb60
movq %rax, -0x98(%rbp)
cmpq $0x0, -0x98(%rbp)
jne 0x7b7d
jmp 0x7c5f
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rax
addq $0x28, %rax
movq %rax, -0xa0(%rbp)
movl $0x0, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
movq -0xa0(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x7c3c
movq -0xa0(%rbp), %rax
movq (%rax), %rax
movslq -0xa4(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb0(%rbp)
movq -0x18(%rbp), %rdi
movq -0xb0(%rbp), %rax
movq (%rax), %rsi
callq 0x3120
cmpl $0x0, %eax
jne 0x7c26
movq -0x98(%rbp), %rax
movq %rax, -0x88(%rbp)
movl $0x1, -0x80(%rbp)
movq -0xb0(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x90(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0xa4(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x78(%rbp)
jmp 0x7c91
jmp 0x7c28
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x7b9d
jmp 0x7c3e
movq -0x90(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x3120
cmpl $0x0, %eax
jne 0x7c5d
movl $0x1, -0x7c(%rbp)
jmp 0x7c5f
movq -0x90(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x90(%rbp)
jmp 0x7b25
cmpq $0x0, -0x78(%rbp)
je 0x7c83
cmpl $0x0, -0x7c(%rbp)
je 0x7c8f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8197
jmp 0x7c91
leaq -0xc0(%rbp), %rdi
xorl %esi, %esi
callq 0x10680
movl $0x0, -0xc4(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0xd0(%rbp)
movb -0x49(%rbp), %al
movb %al, -0xd1(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0xe0(%rbp)
callq 0x3240
movq (%rax), %rax
movsbl -0x49(%rbp), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
je 0x7d15
movq -0x10(%rbp), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x49(%rbp), %rdx
leaq -0x68(%rbp), %rcx
callq 0x71a0
movb %al, -0x49(%rbp)
jmp 0x7cde
movsbl -0x49(%rbp), %eax
cmpl $0x28, %eax
jne 0x7f1c
movq -0x88(%rbp), %rax
cmpl $0x0, 0x50(%rax)
je 0x7f1c
movl $0x0, -0xf4(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x49(%rbp), %rdx
leaq -0x68(%rbp), %rcx
callq 0x71a0
movb %al, -0x49(%rbp)
movsbl -0x49(%rbp), %eax
cmpl $0x29, %eax
je 0x7f05
jmp 0x7d64
callq 0x3240
movq (%rax), %rax
movsbl -0x49(%rbp), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
je 0x7d9b
movq -0x10(%rbp), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x49(%rbp), %rdx
leaq -0x68(%rbp), %rcx
callq 0x71a0
movb %al, -0x49(%rbp)
jmp 0x7d64
movl $0x0, -0xf8(%rbp)
movl $0x100, -0xfc(%rbp)     # imm = 0x100
movslq -0xfc(%rbp), %rdi
callq 0x3190
movq %rax, -0x108(%rbp)
movsbl -0x49(%rbp), %eax
cmpl $0x28, %eax
jne 0x7dda
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
movsbl -0x49(%rbp), %eax
cmpl $0x2c, %eax
jne 0x7df1
cmpl $0x0, -0xf4(%rbp)
jne 0x7df1
jmp 0x7e96
movsbl -0x49(%rbp), %eax
cmpl $0x29, %eax
jne 0x7e17
cmpl $0x0, -0xf4(%rbp)
jne 0x7e08
jmp 0x7e96
movl -0xf4(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xf4(%rbp)
movsbl -0x49(%rbp), %eax
cmpl $0x0, %eax
jne 0x7e22
jmp 0x7e96
movl -0xf8(%rbp), %eax
addl $0x2, %eax
cmpl -0xfc(%rbp), %eax
jne 0x7e5b
movl -0xfc(%rbp), %eax
shll %eax
movl %eax, -0xfc(%rbp)
movq -0x108(%rbp), %rdi
movslq -0xfc(%rbp), %rsi
callq 0x31c0
movq %rax, -0x108(%rbp)
movb -0x49(%rbp), %dl
movq -0x108(%rbp), %rax
movl -0xf8(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0xf8(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x10(%rbp), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x49(%rbp), %rdx
leaq -0x68(%rbp), %rcx
callq 0x71a0
jmp 0x7dc2
movq -0x108(%rbp), %rax
movslq -0xf8(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x108(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
callq 0x107d0
movsbl -0x49(%rbp), %eax
cmpl $0x2c, %eax
jne 0x7ed9
movq -0x10(%rbp), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x49(%rbp), %rdx
leaq -0x68(%rbp), %rcx
callq 0x71a0
movsbl -0x49(%rbp), %eax
cmpl $0x0, %eax
jne 0x7f00
movl $0xffffffff, -0xc4(%rbp) # imm = 0xFFFFFFFF
leaq -0x68(%rbp), %rdi
leaq 0xa1ca(%rip), %rsi      # 0x120c1
movb $0x0, %al
callq 0x3880
jmp 0x7f05
jmp 0x7d55
movq -0x10(%rbp), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x49(%rbp), %rdx
leaq -0x68(%rbp), %rcx
callq 0x71a0
jmp 0x7f51
movq -0xd0(%rbp), %rax
movq %rax, -0x48(%rbp)
movb -0xd1(%rbp), %al
movb %al, -0x49(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rdi
leaq 0xa196(%rip), %rsi      # 0x120f2
callq 0x3120
cmpl $0x0, %eax
jne 0x7f90
movq -0x88(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rcx
movl $0x100, %esi            # imm = 0x100
leaq 0xa174(%rip), %rdx      # 0x120fb
movb $0x0, %al
callq 0x30d0
jmp 0x7fcb
movq -0x18(%rbp), %rdi
leaq 0xa165(%rip), %rsi      # 0x12100
callq 0x3120
cmpl $0x0, %eax
jne 0x7fc9
movq -0x88(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x20(%rbp), %rax
movl (%rax), %ecx
movl $0x100, %esi            # imm = 0x100
leaq 0x9568(%rip), %rdx      # 0x1152a
movb $0x0, %al
callq 0x30d0
jmp 0x7fcb
cmpl $0x0, -0x80(%rbp)
jne 0x8028
movl -0xb8(%rbp), %eax
movq -0x88(%rbp), %rcx
movq 0x8(%rcx), %rcx
cmpl 0x30(%rcx), %eax
je 0x8028
movl $0xffffffff, -0xc4(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
cmpl $0x0, 0x6c(%rax)
jne 0x8026
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x30(%rax), %ecx
movl -0xb8(%rbp), %r8d
leaq 0xa0ea(%rip), %rsi      # 0x12109
movb $0x0, %al
callq 0x3880
jmp 0x8028
jmp 0x802a
cmpl $0x0, -0xc4(%rbp)
jne 0x8137
movq -0x48(%rbp), %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x18(%rax)
movb -0x49(%rbp), %cl
movq -0x10(%rbp), %rax
movq (%rax), %rax
movb %cl, 0x20(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x68(%rbp), %rcx
movq %rcx, 0x28(%rax)
movq -0x60(%rbp), %rcx
movq %rcx, 0x30(%rax)
movq -0x58(%rbp), %rcx
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x78(%rbp), %rcx
movl $0x1, %edx
callq 0x6f50
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, 0x28(%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x30(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x38(%rax)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x40(%rbp), %rax
movb 0x20(%rax), %cl
movq -0x30(%rbp), %rax
movb %cl, (%rax)
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %rcx
movq 0x28(%rcx), %rdx
movq %rdx, (%rax)
movq 0x30(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x38(%rcx), %rcx
movq %rcx, 0x10(%rax)
movl $0x0, -0x10c(%rbp)
movl -0x10c(%rbp), %eax
cmpl -0xb8(%rbp), %eax
jge 0x8135
movq -0x40(%rbp), %rdi
addq $0x40, %rdi
movq -0xc0(%rbp), %rax
movslq -0x10c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x107d0
movl -0x10c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10c(%rbp)
jmp 0x80f7
jmp 0x8179
movl $0x0, -0x110(%rbp)
movl -0x110(%rbp), %eax
cmpl -0xb8(%rbp), %eax
jge 0x8177
movq -0xc0(%rbp), %rax
movslq -0x110(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x3030
movl -0x110(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x110(%rbp)
jmp 0x8141
jmp 0x8179
leaq -0xc0(%rbp), %rdi
callq 0x10710
movq -0x18(%rbp), %rdi
callq 0x3030
movl -0xc4(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw %cs:(%rax,%rax)