// File: /workspace/github_c_makefile_star2_O1/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 0x2eec8
testq %rax, %rax
je 0x1f627
movq %rax, %rbp
movl $0x2, %edx
jmp 0x1f644
movq 0x60(%r14), %rdi
movq %r12, %rsi
callq 0x2eec8
testq %rax, %rax
je 0x1f721
movq %rax, %rbp
movl $0x1, %edx
movl 0x8(%rbp), %eax
leal -0xb(%rax), %ecx
cmpl $0x2, %ecx
jae 0x1f6c8
movq %rdx, %r12
movq (%r14), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x12eda
cmpl $0x0, (%rbx)
jne 0x1f754
movq %r15, %rdi
callq 0x12ad0
cmpl $0x2, (%r15)
jne 0x1f696
leaq 0x54b94(%rip), %rsi     # 0x74212
movq %rax, %rdi
movq %rax, (%rsp)
callq 0x12160
testl %eax, %eax
movq (%rsp), %r8
je 0x1f7bf
movq (%r14), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x54204(%rip), %rax     # 0x738ac
leaq 0x54b63(%rip), %rdx     # 0x74212
leaq 0x5473f(%rip), %rcx     # 0x73df5
movq %rbx, %rdi
movl $0x15, %esi
pushq $0x591                  # imm = 0x591
jmp 0x1f74a
leaq 0x8(%rbp), %r12
cmpl $0x5, %eax
jne 0x1f6ec
cmpb $0x0, 0x2a8(%rbp)
je 0x1f769
movb $0x1, (%r13)
movq (%rbp), %rdx
jmp 0x1f792
movq %r12, %rdi
callq 0x2f882
movq (%r14), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
leaq 0x541a6(%rip), %r10     # 0x738ac
leaq 0x546e8(%rip), %rcx     # 0x73df5
movq %rbx, %rdi
movl $0x4f, %esi
movq %rax, %rdx
pushq $0x59b                  # imm = 0x59B
pushq %r10
jmp 0x1f74b
movq (%r14), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x54179(%rip), %rax     # 0x738ac
leaq 0x546bb(%rip), %rcx     # 0x73df5
movq %rbx, %rdi
movl $0x1b, %esi
movq %r12, %rdx
pushq $0x569                  # imm = 0x569
pushq %rax
callq 0x1442a
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 0x1f792
movq 0x48(%r14), %rdi
movl %eax, %ecx
movl $0x9, %esi
movq %rbx, %r8
callq 0x2c00f
cmpl $0x0, (%rbx)
jne 0x1f754
movq %rax, %rdx
movq %r12, %rdi
movl %edx, %esi
movq %rbx, %rdx
callq 0x2b971
movq %rax, %r12
movq (%r14), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x12eda
cmpl $0x0, (%rbx)
je 0x1f757
movq %r12, %rdi
callq 0x2b4c3
jmp 0x1f754
movb $0x0, (%r13)
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq %rbx, %r9
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x16522