// File: /workspace/github_c_makefile_star2_O2/Mithreindeir[P]ewcc/src/tac.c
// Function: binop_emit
struct ir_operand *binop_emit(struct generator *context, struct binop *b, int result)
{
	struct ir_stmt *stmt = ir_stmt_init();
	enum operator op = b->op;
	stmt->type = map_stmt(op);
	/*Store dereferences an address, and stores the rhs value in it*/
	if (stmt->type == stmt_store) {
		/*Uses arg1 instead of result because the write is a side effect*/
		stmt->arg2 = 	generate_from_node(b->rhs, context, VALUE);
		stmt->arg1 = generate_from_node(b->lhs, context, ADDRESS);
		emit(context, stmt);
		//return result != IGNORE ? copy(stmt->arg1) : NULL;
		return result != IGNORE ? (result == ADDRESS ? copy(stmt->arg1) : copy(stmt->arg2)) : NULL;
	} else if (op == o_and) {
		/*And short circuit only evaluates the first expression if it is false*/
		int between = REQ_LABEL(context);
		generate_from_node(b->lhs, context, IGNORE);
		emit_label(context, between);
		generate_from_node(b->rhs, context, IGNORE);
		/*Patch the truelist to the next expression*/
		backpatch(TLIST_REF(b->lhs), between);
		/*The truelist becomes the next expressions truelist*/
		TLIST(context->parent) = TLIST(b->rhs);
		/*Both sides falselists point to the same result*/
		FLIST(context->parent) = merge(FLIST(b->lhs), FLIST(b->rhs));
		free(stmt);
		return NULL;
	} else if (RELATIONAL(stmt->type)) {
		/*Relational operators are actually jumps*/
		stmt->arg1 = generate_from_node(b->lhs, context, VALUE);
		stmt->arg2 = generate_from_node(b->rhs, context, VALUE);
		emit(context, stmt);
		/*The conditionally jumps if the expression is false*/
		TLIST(context->parent) = make_list(emit_jump(context, -1, CONDITIONAL));
		/*Unconditional jump to the next statement (will be optimized out)*/
		FLIST(context->parent) = make_list(emit_jump(context, -1, UNCONDITIONAL));
		return NULL;
	} else {
		stmt->arg1 = generate_from_node(b->lhs, context, VALUE);
		stmt->arg2 = generate_from_node(b->rhs, context, VALUE);
		stmt->result = from_reg(context->reg_cnt++);
	}
	emit(context, stmt);
	return result != IGNORE ? copy(stmt->result) : NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %rbx
callq 0x4d88
movq %rax, %r14
movl 0x10(%r15), %r13d
movl %r13d, %edi
callq 0x3f60
movl %eax, (%r14)
cmpl $0x12, %eax
jne 0x4971
movq 0x8(%r15), %rdi
pushq $0x2
popq %rdx
movq %rbx, %rsi
callq 0x3f9f
movq %rax, 0x18(%r14)
movq (%r15), %rdi
pushq $0x1
popq %rdx
movq %rbx, %rsi
callq 0x3f9f
movq %rax, 0x10(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4b43
cmpl $0x3, %ebp
je 0x4ab2
cmpl $0x1, %ebp
jne 0x4a8c
movq 0x10(%r14), %rdi
jmp 0x4ac7
cmpl $0xa, %r13d
jne 0x4a08
movl 0x4(%rbx), %r12d
leal 0x1(%r12), %eax
movl %eax, 0x4(%rbx)
movq (%r15), %rdi
pushq $0x3
popq %rbp
movq %rbx, %rsi
movl %ebp, %edx
callq 0x3f9f
movq %rbx, %rdi
movl %r12d, %esi
callq 0x4ada
movq 0x8(%r15), %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x3f9f
movq (%r15), %rax
movq 0x10(%rax), %rdi
addq $0x8, %rdi
movl %r12d, %esi
callq 0x4b09
movq 0x8(%r15), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rcx
movq 0x20(%rbx), %rdx
movq 0x10(%rdx), %rdx
movq %rcx, 0x8(%rdx)
movq (%r15), %rcx
movq 0x10(%rcx), %rcx
movq 0x10(%rcx), %rdi
movq 0x10(%rax), %rsi
callq 0x4bf5
movq 0x20(%rbx), %rcx
movq 0x10(%rcx), %rcx
movq %rax, 0x10(%rcx)
movq %r14, %rdi
callq 0x2030
jmp 0x4ab2
movl %eax, %r12d
movq (%r15), %rdi
pushq $0x2
popq %r13
movq %rbx, %rsi
movl %r13d, %edx
callq 0x3f9f
movq %rax, 0x10(%r14)
movq 0x8(%r15), %rdi
movq %rbx, %rsi
movl %r13d, %edx
callq 0x3f9f
movq %rax, 0x18(%r14)
cmpl $0x14, %r12d
jb 0x4a92
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4b43
pushq $-0x1
popq %rbp
pushq $0x1
popq %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x4bb4
movq %rax, %rdi
callq 0x4b81
movq 0x20(%rbx), %rcx
movq 0x10(%rcx), %rcx
movq %rax, 0x8(%rcx)
pushq $0x2
popq %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x4bb4
movq %rax, %rdi
callq 0x4b81
movq 0x20(%rbx), %rcx
movq 0x10(%rcx), %rcx
movq %rax, 0x10(%rcx)
jmp 0x4ab2
movq 0x18(%r14), %rdi
jmp 0x4ac7
movl (%rbx), %edi
leal 0x1(%rdi), %eax
movl %eax, (%rbx)
callq 0x4ccd
movq %rax, 0x8(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4b43
cmpl $0x3, %ebp
jne 0x4ac3
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r14), %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4ce1