// File: /workspace/github_c_makefile_star2_O1/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
movl $0x38, %edi
callq 0x2100
movq %rax, %r14
movl $0x0, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movups %xmm0, 0x18(%rax)
movq $0x0, 0x28(%rax)
movl $0xffffffff, 0x30(%rax) # imm = 0xFFFFFFFF
movl 0x10(%r15), %r13d
movl %r13d, %edi
callq 0x4120
movl %eax, (%r14)
cmpl $0x12, %eax
jne 0x4f21
movq 0x8(%r15), %rdi
movq %rbx, %rsi
movl $0x2, %edx
callq 0x4161
movq %rax, 0x18(%r14)
movq (%r15), %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x4161
movq %rax, 0x10(%r14)
movq %r14, %rdi
callq 0x545c
cmpq $0x0, 0x10(%rbx)
je 0x500e
movq 0x18(%rbx), %rax
addq $0x18, %rbx
movq %r14, 0x28(%rax)
movq %rax, 0x20(%r14)
jmp 0x5016
cmpl $0xa, %r13d
jne 0x4fbb
movl 0x4(%rbx), %r12d
leal 0x1(%r12), %eax
movl %eax, 0x4(%rbx)
movq (%r15), %rdi
movq %rbx, %rsi
movl $0x3, %edx
callq 0x4161
movq %rbx, %rdi
movl %r12d, %esi
callq 0x5151
movq 0x8(%r15), %rdi
movq %rbx, %rsi
movl $0x3, %edx
callq 0x4161
movq (%r15), %rax
movq 0x10(%rax), %rdi
addq $0x8, %rdi
movl %r12d, %esi
callq 0x51b7
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 0x52f7
movq 0x20(%rbx), %rcx
movq 0x10(%rcx), %rcx
movq %rax, 0x10(%rcx)
movq %r14, %rdi
callq 0x2030
jmp 0x5126
movl %eax, %r12d
movq (%r15), %rdi
movq %rbx, %rsi
movl $0x2, %edx
callq 0x4161
movq %rax, 0x10(%r14)
movq 0x8(%r15), %rdi
movq %rbx, %rsi
movl $0x2, %edx
callq 0x4161
movq %rax, 0x18(%r14)
cmpl $0x14, %r12d
jb 0x5039
movq %r14, %rdi
callq 0x545c
cmpq $0x0, 0x10(%rbx)
je 0x5083
movq 0x18(%rbx), %rax
movq %r14, 0x28(%rax)
movq %rax, 0x20(%r14)
jmp 0x5087
movq %r14, 0x10(%rbx)
addq $0x18, %rbx
movq %r14, (%rbx)
cmpl $0x3, %ebp
je 0x5126
cmpl $0x1, %ebp
jne 0x5030
movq 0x10(%r14), %rdi
jmp 0x513e
movq 0x18(%r14), %rdi
jmp 0x513e
movl (%rbx), %r15d
leal 0x1(%r15), %eax
movl %eax, (%rbx)
movl $0x10, %edi
callq 0x2100
movq $0x0, (%rax)
movl %r15d, 0x8(%rax)
movq %rax, 0x8(%r14)
movq %r14, %rdi
callq 0x545c
cmpq $0x0, 0x10(%rbx)
je 0x5116
movq 0x18(%rbx), %rax
addq $0x18, %rbx
movq %r14, 0x28(%rax)
movq %rax, 0x20(%r14)
jmp 0x511e
movq %r14, 0x10(%rbx)
movq %r14, 0x18(%rbx)
movl $0x1, %ebp
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x1, %edx
callq 0x527c
movq %rax, %r14
movl $0x10, %edi
callq 0x2100
movq %rax, %r15
movl $0x8, %edi
callq 0x2100
movq %rax, (%r15)
movq %r14, (%rax)
movl %ebp, 0x8(%r15)
movq 0x20(%rbx), %rax
movq 0x10(%rax), %rax
movq %r15, 0x8(%rax)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x2, %edx
callq 0x527c
movq %rax, %r14
movl $0x10, %edi
callq 0x2100
movq %rax, %r15
movl $0x8, %edi
callq 0x2100
movq %rax, (%r15)
movq %r14, (%rax)
movl %ebp, 0x8(%r15)
movq 0x20(%rbx), %rax
movq 0x10(%rax), %rax
movq %r15, 0x10(%rax)
jmp 0x5126
movq %r14, 0x10(%rbx)
addq $0x18, %rbx
movq %r14, (%rbx)
cmpl $0x3, %ebp
jne 0x5137
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x8, %r14
movq (%r14), %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x53ee