// File: /workspace/github_c_makefile_star2_O3/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
movl $0x38, %edi
callq 0x2100
movq %rax, %r15
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movups %xmm0, 0x18(%rax)
movq $0x0, 0x28(%rax)
movl $0xffffffff, 0x30(%rax) # imm = 0xFFFFFFFF
movl 0x8(%r12), %ebp
movl %ebp, %edi
callq 0x4110
movl %eax, (%r15)
movl %ebp, %ecx
andl $-0x4, %ecx
cmpl $0x14, %ecx
jne 0x4c94
movl %ebp, %eax
andl $0x16, %eax
xorl %ecx, %ecx
cmpl $0x14, %eax
sete %cl
xorl $0xb, %ecx
movl %ecx, (%r15)
movq (%r12), %rdi
movq %r14, %rsi
movl $0x2, %edx
callq 0x4151
movq %rax, 0x10(%r15)
movl $0x10, %edi
callq 0x2100
movabsq $0x400000002, %rcx      # imm = 0x400000002
movq %rcx, (%rax)
movq $0x1, 0x8(%rax)
movq %rax, 0x18(%r15)
movl (%r14), %r13d
leal 0x1(%r13), %eax
movl %eax, (%r14)
movl $0x10, %edi
callq 0x2100
movq $0x0, (%rax)
movl %r13d, 0x8(%rax)
movq %rax, 0x8(%r15)
movq %r15, %rdi
callq 0x5429
cmpq $0x0, 0x10(%r14)
je 0x4cd1
movq 0x18(%r14), %rax
movq %r15, 0x28(%rax)
movq %rax, 0x20(%r15)
jmp 0x4cd5
movq (%r12), %r12
cmpl $0x13, %eax
je 0x4dc2
cmpl $0x11, %eax
jne 0x4d8a
movq 0x10(%r12), %rax
movq (%rax), %rax
cmpl $0x2, 0x18(%rax)
jne 0x4d97
cmpl $0x1, %ebx
jne 0x4dc2
movl $0x6, (%r15)
jmp 0x4dc2
movq %r15, 0x10(%r14)
movq %r15, 0x18(%r14)
movq 0x8(%r15), %rdi
callq 0x53bd
movq %rax, %r13
movq 0x10(%r15), %rax
movq %rax, (%rsp)
movl $0x38, %edi
callq 0x2100
movq %rax, %r15
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movups %xmm0, 0x18(%rax)
movq $0x0, 0x28(%rax)
movl $0xffffffff, 0x30(%rax) # imm = 0xFFFFFFFF
movl $0x12, (%rax)
movq (%r12), %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x4151
movq %rax, 0x10(%r15)
movq %r13, 0x18(%r15)
movq %r15, %rdi
callq 0x5429
cmpq $0x0, 0x10(%r14)
je 0x4d54
movq 0x18(%r14), %rax
addq $0x18, %r14
movq %r15, 0x28(%rax)
movq %rax, 0x20(%r15)
jmp 0x4d5c
movq %r15, 0x10(%r14)
addq $0x18, %r14
movq %r15, (%r14)
andl $0x15, %ebp
cmpl $0x14, %ebp
jne 0x4d79
cmpl $0x3, %ebx
je 0x4e2b
movq (%rsp), %rdi
jmp 0x4e43
cmpl $0x3, %ebx
je 0x4e2b
movq %r13, %rdi
jmp 0x4e43
movq %r12, %rdi
movq %r14, %rsi
movl $0x2, %edx
jmp 0x4dcd
cmpl $0x1, %ebx
jne 0x4dc2
movq %r15, %rdi
callq 0x2030
movq %r12, %rdi
movq %r14, %rsi
movl $0x2, %edx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4151
movq %r12, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x4151
movq %rax, 0x10(%r15)
movl (%r14), %r12d
leal 0x1(%r12), %eax
movl %eax, (%r14)
movl $0x10, %edi
callq 0x2100
movq $0x0, (%rax)
movl %r12d, 0x8(%rax)
movq %rax, 0x8(%r15)
movq %r15, %rdi
callq 0x5429
cmpq $0x0, 0x10(%r14)
je 0x4e1b
movq 0x18(%r14), %rax
addq $0x18, %r14
movq %r15, 0x28(%rax)
movq %rax, 0x20(%r15)
jmp 0x4e23
movq %r15, 0x10(%r14)
addq $0x18, %r14
movq %r15, (%r14)
cmpl $0x3, %ebx
jne 0x4e3c
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x8, %r15
movq (%r15), %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x53bd