// File: /workspace/github_c_makefile_star2_O2/markdryan[P]subtilis/frontend/parser_exp.c
// Function: subtilis_var_lookup_ref
subtilis_exp_t *subtilis_var_lookup_ref(subtilis_parser_t *p,
					subtilis_token_t *t, bool *local,
					subtilis_error_t *err)
{
	const char *tbuf;
	size_t reg;
	const subtilis_symbol_t *s;
	subtilis_exp_t *e;
	subtilis_ir_operand_t op0;
	subtilis_ir_operand_t op1;

	tbuf = subtilis_token_get_text(t);

	reg = SUBTILIS_IR_REG_LOCAL;
	s = subtilis_symbol_table_lookup(p->local_st, tbuf);
	if (!s) {
		s = subtilis_symbol_table_lookup(p->st, tbuf);
		if (!s) {
			subtilis_error_set_unknown_variable(
			    err, tbuf, p->l->stream->name, p->l->line);
			return NULL;
		}
		reg = SUBTILIS_IR_REG_GLOBAL;
	}

	if (s->t.type == SUBTILIS_TYPE_INTEGER) {
		if (!s->is_reg) {
			*local = false;
			if (s->loc != 0) {
				op0.reg = reg;
				op1.integer = s->loc;
				reg = subtilis_ir_section_add_instr(
				    p->current, SUBTILIS_OP_INSTR_ADDI_I32, op0,
				    op1, err);
				if (err->type != SUBTILIS_ERROR_OK)
					return NULL;
			}
			e = subtilis_exp_new_var(&s->t, reg, err);
		} else {
			*local = true;
			e = subtilis_exp_new_var(&s->t, s->loc, err);
		}

		subtilis_lexer_get(p->l, t, err);
		if (err->type != SUBTILIS_ERROR_OK) {
			subtilis_exp_delete(e);
			return NULL;
		}

		return e;
	} else if ((s->t.type == SUBTILIS_TYPE_ARRAY_INTEGER) ||
		   (s->t.type == SUBTILIS_TYPE_ARRAY_BYTE)) {
		subtilis_lexer_get(p->l, t, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return NULL;

		tbuf = subtilis_token_get_text(t);
		if ((t->type != SUBTILIS_TOKEN_OPERATOR) || strcmp(tbuf, "(")) {
			subtilis_error_set_exp_expected(
			    err, "(", p->l->stream->name, p->l->line);

			return NULL;
		}

		*local = false;
		return subtils_parser_element_address(p, t, s, reg, tbuf, err);
	}

	subtilis_error_set_integer_variable_expected(
	    err, subtilis_type_name(&s->t), p->l->stream->name, p->l->line);
	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %rbx
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %r14
movq %rsi, %rdi
callq 0x12a25
movq %rax, %r12
movq 0x68(%r14), %rdi
movq %rax, %rsi
callq 0x2d376
testq %rax, %rax
je 0x1e992
movq %rax, %rbp
pushq $0x2
jmp 0x1e9ac
movq 0x60(%r14), %rdi
movq %r12, %rsi
callq 0x2d376
testq %rax, %rax
je 0x1ea72
movq %rax, %rbp
pushq $0x1
popq %r12
movl 0x8(%rbp), %eax
leal -0xb(%rax), %ecx
cmpl $0x2, %ecx
jae 0x1ea1a
movq (%r14), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x12e0e
cmpl $0x0, (%rbx)
jne 0x1eaa1
movq %r15, %rdi
callq 0x12a25
cmpl $0x2, (%r15)
jne 0x1e9ed
cmpb $0x28, (%rax)
jne 0x1e9ed
cmpb $0x0, 0x1(%rax)
je 0x1eb0c
movq (%r14), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x4ee9d(%rip), %rax     # 0x6d89c
leaq 0x4f7dc(%rip), %rdx     # 0x6e1e2
leaq 0x4f3b8(%rip), %rcx     # 0x6ddc5
pushq $0x15
popq %rsi
movq %rbx, %rdi
pushq $0x591                  # imm = 0x591
jmp 0x1ea99
movq %r13, %rcx
leaq 0x8(%rbp), %r13
cmpl $0x5, %eax
jne 0x1ea3f
cmpb $0x0, 0x2a8(%rbp)
je 0x1eab6
movb $0x1, (%rcx)
movq (%rbp), %r12
jmp 0x1eade
movq %r13, %rdi
callq 0x2dcbb
movq (%r14), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
leaq 0x4ee43(%rip), %r10     # 0x6d89c
leaq 0x4f365(%rip), %rcx     # 0x6ddc5
pushq $0x4f
popq %rsi
movq %rbx, %rdi
movq %rax, %rdx
pushq $0x59b                  # imm = 0x59B
pushq %r10
jmp 0x1ea9a
movq (%r14), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x4ee18(%rip), %rax     # 0x6d89c
leaq 0x4f33a(%rip), %rcx     # 0x6ddc5
pushq $0x1b
popq %rsi
movq %rbx, %rdi
movq %r12, %rdx
pushq $0x569                  # imm = 0x569
pushq %rax
callq 0x140cd
popq %rax
popq %rcx
xorl %r12d, %r12d
movq %r12, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x0, (%rcx)
movq (%rbp), %rax
testq %rax, %rax
je 0x1eade
movq 0x48(%r14), %rdi
movl %eax, %ecx
pushq $0x9
popq %rsi
movq %r12, %rdx
movq %rbx, %r8
callq 0x2a819
cmpl $0x0, (%rbx)
jne 0x1eaa1
movq %rax, %r12
movq %r13, %rdi
movl %r12d, %esi
movq %rbx, %rdx
callq 0x2a226
movq %rax, %r12
movq (%r14), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x12e0e
cmpl $0x0, (%rbx)
je 0x1eaa4
movq %r12, %rdi
callq 0x29d88
jmp 0x1eaa1
movb $0x0, (%r13)
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq %rax, %r8
movq %rbx, %r9
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x16006