// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
callq 0x70d0
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x5810
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x28(%rbp), %rax
cmpl $0x12, (%rax)
jne 0x6930
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rsi
movl $0x2, %edx
callq 0x5920
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
movl $0x1, %edx
callq 0x5920
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x6c40
cmpl $0x3, -0x1c(%rbp)
je 0x691b
cmpl $0x1, -0x1c(%rbp)
jne 0x6900
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x6f30
movq %rax, -0x38(%rbp)
jmp 0x6911
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x6f30
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x6923
xorl %eax, %eax
movq %rax, -0x40(%rbp)
jmp 0x6923
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x6b63
cmpl $0xa, -0x2c(%rbp)
jne 0x6a07
movq -0x10(%rbp), %rcx
movl 0x4(%rcx), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, 0x4(%rcx)
movl %eax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
movl $0x3, %edx
callq 0x5920
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x6b70
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rsi
movl $0x3, %edx
callq 0x5920
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rdi
addq $0x8, %rdi
movl -0x30(%rbp), %esi
callq 0x6bc0
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
callq 0x6d70
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rdi
callq 0x2030
movq $0x0, -0x8(%rbp)
jmp 0x6b63
movq -0x28(%rbp), %rax
cmpl $0x14, (%rax)
jb 0x6acb
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
movl $0x2, %edx
callq 0x5920
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rsi
movl $0x2, %edx
callq 0x5920
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x6c40
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x1, %edx
callq 0x6d10
movq %rax, %rdi
callq 0x6cb0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x2, %edx
callq 0x6d10
movq %rax, %rdi
callq 0x6cb0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x10(%rax)
movq $0x0, -0x8(%rbp)
jmp 0x6b63
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
movl $0x2, %edx
callq 0x5920
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rsi
movl $0x2, %edx
callq 0x5920
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movl (%rax), %edi
movl %edi, %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
callq 0x6ee0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x6b2b
jmp 0x6b2d
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x6c40
cmpl $0x3, -0x1c(%rbp)
je 0x6b53
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x6f30
movq %rax, -0x48(%rbp)
jmp 0x6b5b
xorl %eax, %eax
movq %rax, -0x48(%rbp)
jmp 0x6b5b
movq -0x48(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)