// File: /workspace/github_c_makefile_star2_O3/markdryan[P]subtilis/frontend/parser_cond.c
// Function: subtilis_parser_if
void subtilis_parser_if(subtilis_parser_t *p, subtilis_token_t *t,
			subtilis_error_t *err)
{
	subtilis_exp_t *e;
	const char *tbuf;
	subtilis_ir_operand_t cond;
	subtilis_ir_operand_t true_label;
	subtilis_ir_operand_t false_label;
	subtilis_ir_operand_t end_label;
	int key_type;

	e = subtilis_parser_conditional_exp(p, t, &cond, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	if ((t->type != SUBTILIS_TOKEN_KEYWORD) ||
	    (t->tok.keyword.type != SUBTILIS_KEYWORD_THEN)) {
		tbuf = subtilis_token_get_text(t);
		subtilis_error_set_expected(err, "THEN", tbuf,
					    p->l->stream->name, p->l->line);
		goto cleanup;
	}

	true_label.reg = subtilis_ir_section_new_label(p->current);
	false_label.reg = subtilis_ir_section_new_label(p->current);

	subtilis_ir_section_add_instr_reg(p->current, SUBTILIS_OP_INSTR_JMPC,
					  cond, true_label, false_label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;
	subtilis_ir_section_add_label(p->current, true_label.reg, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	key_type = subtilis_parser_if_compound(p, t, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	if (key_type == SUBTILIS_KEYWORD_ELSE) {
		end_label.reg = subtilis_ir_section_new_label(p->current);
		subtilis_ir_section_add_instr_no_reg(
		    p->current, SUBTILIS_OP_INSTR_JMP, end_label, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
		subtilis_ir_section_add_label(p->current, false_label.reg, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
		subtilis_lexer_get(p->l, t, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return;
		subtilis_parser_compound(p, t, SUBTILIS_KEYWORD_ENDIF, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
		subtilis_ir_section_add_label(p->current, end_label.reg, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
	} else {
		subtilis_ir_section_add_label(p->current, false_label.reg, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
	}

	subtilis_lexer_get(p->l, t, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

cleanup:
	subtilis_exp_delete(e);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
movq %rsp, %rdx
movq %r14, %rcx
callq 0x1e3d8
cmpl $0x0, (%r14)
je 0x1e4ac
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
cmpl $0x1, (%r12)
jne 0x1e5b9
cmpl $0x82, 0x8(%r12)
jne 0x1e5b9
movq 0x48(%r15), %rdi
callq 0x2cc99
movq %rax, %rbp
movq 0x48(%r15), %rdi
callq 0x2cc99
movq %rax, %r13
movq 0x48(%r15), %rdi
movq (%rsp), %rdx
movl $0x43, %esi
movq %rbp, %rcx
movq %rax, %r8
movq %r14, %r9
callq 0x2bd42
cmpl $0x0, (%r14)
jne 0x1e5f2
movq 0x48(%r15), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x2cca6
cmpl $0x0, (%r14)
jne 0x1e5f2
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x151c4
cmpl $0x0, (%r14)
jne 0x1e5f2
movq 0x48(%r15), %rdi
cmpl $0x23, %eax
jne 0x1e608
callq 0x2cc99
movq %rax, %rbp
movq 0x48(%r15), %rdi
movl $0x45, %esi
movq %rax, %rdx
movq %r14, %rcx
callq 0x2be1f
cmpl $0x0, (%r14)
jne 0x1e5f2
movq 0x48(%r15), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x2cca6
cmpl $0x0, (%r14)
jne 0x1e5f2
movq (%r15), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x12edc
cmpl $0x0, (%r14)
jne 0x1e49d
movq %r15, %rdi
movq %r12, %rsi
movl $0x27, %edx
movq %r14, %rcx
callq 0x1e3ce
cmpl $0x0, (%r14)
jne 0x1e5f2
movq 0x48(%r15), %rdi
movq %rbp, %rsi
jmp 0x1e60b
movq %r12, %rdi
callq 0x12ad0
movq (%r15), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
leaq 0x550c9(%rip), %r10     # 0x7369c
leaq 0x54bbb(%rip), %rdx     # 0x73195
movq %r14, %rdi
movl $0x1d, %esi
movq %rax, %rcx
pushq $0x52
pushq %r10
callq 0x143da
addq $0x10, %rsp
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2b166
movq %r13, %rsi
movq %r14, %rdx
callq 0x2cca6
cmpl $0x0, (%r14)
jne 0x1e5f2
movq (%r15), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x12edc
jmp 0x1e5f2
nopl (%rax)