// File: /workspace/github_c_makefile_star2_O2/Mithreindeir[P]ewcc/src/tac.c
// Function: unop_emit
struct ir_operand *unop_emit(struct generator *context, struct unop *u, int result)
{
	struct ir_stmt *stmt = ir_stmt_init();
	enum operator op = u->op;
	stmt->type = map_stmt(op);
	if (op==o_preinc || op == o_predec || op == o_postinc || op == o_postdec ) {
		stmt->type = (op==o_preinc||op==o_postinc) ? stmt_add : stmt_sub;
		stmt->arg1 = 	generate_from_node(u->term, context, VALUE);
		stmt->arg2 = 	from_cnum(1);
		stmt->result = 	from_reg(context->reg_cnt++);
		emit(context, stmt);
		struct ir_operand *lreg = copy(stmt->result), *preg = stmt->arg1;
		stmt = ir_stmt_init();
		stmt->type = stmt_store;
		stmt->arg1 = generate_from_node(u->term, context, ADDRESS);
		stmt->arg2 = lreg;
		emit(context, stmt);
		/*Post inc/dec returns the old value in a register, then increments
		 * contents at the address*/
		if (op==o_postinc || op==o_postdec)
			return result != IGNORE ? copy(preg) : NULL;
		else /*Pre inc/dec increments it first then returns the final register*/
			return result != IGNORE ? copy(lreg) : NULL;
	} else if (stmt->type == stmt_load &&  TYPE(u->term)->type == type_array) {
		/*Arrays are not pointers to pointers to arrays, just pointers to arrays
		 * So remove the first dereference if storing in an array*/
		if (result==ADDRESS) stmt->type = stmt_move;
		stmt->arg1 = generate_from_node(u->term, context, ADDRESS);
	} else if (stmt->type == stmt_load && result == ADDRESS) {
		//Do not dereference, if an address is needed as a result
		free(stmt);
		return generate_from_node(u->term, context, VALUE);
	} else if (stmt->type == stmt_load) {
		/*If there is a deref/ref involved, let the parent decide*/
		stmt->arg1 = generate_from_node(u->term, context, ADDRESS);
	} else if (stmt->type == stmt_addr) {
		stmt->arg1 = generate_from_node(u->term, context, ADDRESS);
	} else {
		/*Most instructions use the value of the operands*/
		stmt->arg1 = generate_from_node(u->term, 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, %ebx
movq %rsi, %r12
movq %rdi, %r14
callq 0x4d88
movq %rax, %r15
movl 0x8(%r12), %ebp
movl %ebp, %edi
callq 0x3f60
movl %eax, (%r15)
movl %ebp, %ecx
andl $-0x4, %ecx
cmpl $0x14, %ecx
jne 0x4838
movl %ebx, 0x4(%rsp)
movl %ebp, %eax
andl $0x16, %eax
xorl %ecx, %ecx
cmpl $0x14, %eax
sete %cl
xorl $0xb, %ecx
movl %ecx, (%r15)
movq (%r12), %rdi
pushq $0x2
popq %rdx
movq %r14, %rsi
callq 0x3f9f
movq %rax, 0x10(%r15)
pushq $0x1
popq %rdi
callq 0x4733
movq %rax, 0x18(%r15)
movl (%r14), %edi
leal 0x1(%rdi), %eax
movl %eax, (%r14)
callq 0x4ccd
movq %rax, 0x8(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x4b43
movq 0x8(%r15), %rdi
callq 0x4ce1
movq %rax, %r13
movq 0x10(%r15), %r15
callq 0x4d88
movq %rax, %rbx
movl $0x12, (%rax)
movq (%r12), %rdi
pushq $0x1
popq %rdx
movq %r14, %rsi
callq 0x3f9f
movq %rax, 0x10(%rbx)
movq %r13, 0x18(%rbx)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4b43
andl $0x15, %ebp
cmpl $0x14, %ebp
jne 0x4862
cmpl $0x3, 0x4(%rsp)
je 0x48cf
movq %r15, %rdi
jmp 0x48e4
movq (%r12), %r12
cmpl $0x13, %eax
je 0x489b
cmpl $0x11, %eax
jne 0x486e
movq 0x10(%r12), %rax
movq (%rax), %rax
cmpl $0x2, 0x18(%rax)
jne 0x4872
cmpl $0x1, %ebx
jne 0x489b
movl $0x6, (%r15)
jmp 0x489b
cmpl $0x3, 0x4(%rsp)
je 0x48cf
movq %r13, %rdi
jmp 0x48e4
pushq $0x2
jmp 0x489d
cmpl $0x1, %ebx
jne 0x489b
movq %r15, %rdi
callq 0x2030
pushq $0x2
popq %rdx
movq %r12, %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3f9f
pushq $0x1
popq %rdx
movq %r12, %rdi
movq %r14, %rsi
callq 0x3f9f
movq %rax, 0x10(%r15)
movl (%r14), %edi
leal 0x1(%rdi), %eax
movl %eax, (%r14)
callq 0x4ccd
movq %rax, 0x8(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x4b43
cmpl $0x3, %ebx
jne 0x48e0
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r15), %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4ce1