// File: /workspace/github_c_makefile_star2_O3/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 0x12ad0
movq %rax, %r12
movq 0x68(%r14), %rdi
movq %rax, %rsi
callq 0x2ebbc
testq %rax, %rax
je 0x1f669
movq %rax, %rbp
movl $0x2, %edx
jmp 0x1f686
movq 0x60(%r14), %rdi
movq %r12, %rsi
callq 0x2ebbc
testq %rax, %rax
je 0x1f753
movq %rax, %rbp
movl $0x1, %edx
movl 0x8(%rbp), %eax
leal -0xb(%rax), %ecx
cmpl $0x2, %ecx
jae 0x1f6fa
movq %rdx, %r12
movq (%r14), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x12edc
cmpl $0x0, (%rbx)
jne 0x1f786
movq %r15, %rdi
callq 0x12ad0
cmpl $0x2, (%r15)
jne 0x1f6c8
cmpb $0x28, (%rax)
jne 0x1f6c8
cmpb $0x0, 0x1(%rax)
je 0x1f7f1
movq (%r14), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x54206(%rip), %rax     # 0x738e0
leaq 0x54b51(%rip), %rdx     # 0x74232
leaq 0x5472d(%rip), %rcx     # 0x73e15
movq %rbx, %rdi
movl $0x15, %esi
pushq $0x591                  # imm = 0x591
jmp 0x1f77c
leaq 0x8(%rbp), %r12
cmpl $0x5, %eax
jne 0x1f71e
cmpb $0x0, 0x2a8(%rbp)
je 0x1f79b
movb $0x1, (%r13)
movq (%rbp), %rdx
jmp 0x1f7c4
movq %r12, %rdi
callq 0x2f5ad
movq (%r14), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
leaq 0x541a8(%rip), %r10     # 0x738e0
leaq 0x546d6(%rip), %rcx     # 0x73e15
movq %rbx, %rdi
movl $0x4f, %esi
movq %rax, %rdx
pushq $0x59b                  # imm = 0x59B
pushq %r10
jmp 0x1f77d
movq (%r14), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x5417b(%rip), %rax     # 0x738e0
leaq 0x546a9(%rip), %rcx     # 0x73e15
movq %rbx, %rdi
movl $0x1b, %esi
movq %r12, %rdx
pushq $0x569                  # imm = 0x569
pushq %rax
callq 0x143da
addq $0x10, %rsp
xorl %r12d, %r12d
movq %r12, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x0, (%r13)
movq (%rbp), %rax
testq %rax, %rax
je 0x1f7c4
movq 0x48(%r14), %rdi
movl %eax, %ecx
movl $0x9, %esi
movq %rbx, %r8
callq 0x2bc8f
cmpl $0x0, (%rbx)
jne 0x1f786
movq %rax, %rdx
movq %r12, %rdi
movl %edx, %esi
movq %rbx, %rdx
callq 0x2b5fb
movq %rax, %r12
movq (%r14), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x12edc
cmpl $0x0, (%rbx)
je 0x1f789
movq %r12, %rdi
callq 0x2b166
jmp 0x1f786
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 0x1649e