// File: /workspace/github_c_makefile_star2_O3/Mithreindeir[P]ewcc/src/tac.c
// Function: generate_from_node
struct ir_operand *generate_from_node(struct node *n, struct generator *context, int result)
{
	if (!n) return NULL;
	struct ir_operand *r = NULL;
	struct node *last = context->parent;
	context->parent = n;
	switch (n->type) {
	case node_block: 	block_emit(context, BLOCK(n)); break;
	case node_cond: 	cond_emit(context, COND(n)); break;
	case node_decl: 	decl_emit(context, DECL(n)); break;
	case node_loop:		loop_emit(context, LOOP(n)); break;
	case node_cast:		r = generate_from_node(n->child, context, result); break;
	case node_call: 	r = call_emit(context, CALL(n), result); break;
	case node_ident:    	r = ident_emit(context, IDENT(n), result); break;
	case node_cnum:		r = from_cnum(CNUM(n)); break;
	case node_unop:
		r = unop_emit(context, UNOP(n), result);
		/*The final value gets the type of the unary expression*/
		if (r &&& TYPE(n)) r->size = resolve_size(TYPE(n));
		break;
	case node_binop:
		r = binop_emit(context, BINOP(n), result);
		/*The final value gets the type of the binary expression*/
		if (r &&& TYPE(n)) r->size = resolve_size(TYPE(n));
		break;
	case node_unit:
		for (int i = 0; i < UNIT(n)->num_decls; i++)
			generate_from_node(UNIT(n)->edecls[i], context, IGNORE);
		break;
	case node_func:
		generate_from_node(FUNC(n)->body, context, IGNORE);
		int end = REQ_LABEL(context);
		emit_label(context, end);
		backpatch(FLIST_REF(FUNC(n)->body), end);
		struct ir_stmt *s = ir_stmt_init();
		s->type = stmt_ret;
		emit(context, s);
		break;
	case node_break:
		FLIST(n) = make_list(emit_jump(context, -1, UNCONDITIONAL));
		break;
	case node_continue:
		TLIST(n) = make_list(emit_jump(context, -1, UNCONDITIONAL));
		break;
	case node_return:
		s = ir_stmt_init();
		s->type = stmt_retval;
		s->arg1 = generate_from_node(RET(n), context, VALUE);
		emit(context, s);
		FLIST(n) = make_list(emit_jump(context, -1, UNCONDITIONAL));
		break;
	case node_empty: break;
	default:
		printf("Unknown AST Node %d\n", n->type);
		exit(-1);
		break;
	}
	context->parent = last;
	return r;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
testq %rdi, %rdi
je 0x419a
movq %rsi, %rbx
movq %rdi, %r14
movq 0x20(%rsi), %r13
movq %rdi, 0x20(%rsi)
movl (%rdi), %esi
cmpq $0xf, %rsi
ja 0x4463
leaq 0x51cc(%rip), %rax      # 0x934c
movslq (%rax,%rsi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq 0x8(%r14), %rdi
movq %rbx, %rsi
callq 0x4151
jmp 0x4308
xorl %r15d, %r15d
jmp 0x4456
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x2, %edx
callq 0x524d
movq %rax, %r15
movl $0x10, %edi
callq 0x2100
movq %rax, %r12
movl $0x8, %edi
callq 0x2100
movq %rax, (%r12)
movq %r15, (%rax)
movl $0x1, 0x8(%r12)
movq 0x10(%r14), %rax
movq %r12, 0x8(%rax)
jmp 0x444f
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x4787
jmp 0x444f
movq 0x8(%r14), %rax
movq (%rax), %r14
movl $0x10, %edi
callq 0x2100
movq %rax, %r15
movabsq $0x400000002, %rax      # imm = 0x400000002
movq %rax, (%r15)
movq %r14, 0x8(%r15)
jmp 0x4452
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x4673
jmp 0x444f
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x447b
jmp 0x444f
movq 0x8(%r14), %rax
cmpl $0x0, 0x8(%rax)
jle 0x444f
xorl %r15d, %r15d
xorl %r12d, %r12d
movq (%rax), %rax
movq (%rax,%r12,8), %rdi
movq %rbx, %rsi
movl $0x3, %edx
callq 0x4151
incq %r12
movq 0x8(%r14), %rax
movslq 0x8(%rax), %rcx
cmpq %rcx, %r12
jl 0x425c
jmp 0x4452
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, (%rax)
movq 0x8(%r14), %rdi
movq %rbx, %rsi
movl $0x2, %edx
callq 0x4151
movq %rax, 0x10(%r15)
movq %r15, %rdi
callq 0x5429
cmpq $0x0, 0x10(%rbx)
je 0x43f6
movq 0x18(%rbx), %rax
movq %r15, 0x28(%rax)
movq %rax, 0x20(%r15)
jmp 0x43fa
movq 0x8(%r14), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x4a87
jmp 0x4308
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x48c4
movq %rax, %r15
jmp 0x4452
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x4bb2
jmp 0x432a
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x4e56
testq %rax, %rax
je 0x444f
movq %rax, %r15
movq 0x10(%r14), %rax
testq %rax, %rax
je 0x4452
movq (%rax), %rdi
callq 0x80f0
movl %eax, 0x4(%r15)
jmp 0x4452
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x451a
jmp 0x444f
movq 0x8(%r14), %rax
movq 0x10(%rax), %rdi
movq %rbx, %rsi
movl $0x3, %edx
callq 0x4151
movl 0x4(%rbx), %r15d
leal 0x1(%r15), %eax
movl %eax, 0x4(%rbx)
movq %rbx, %rdi
movl %r15d, %esi
callq 0x5125
movq 0x8(%r14), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rdi
addq $0x10, %rdi
movl %r15d, %esi
callq 0x518b
movl $0x38, %edi
callq 0x2100
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movups %xmm0, 0x18(%rax)
movq $0x0, 0x28(%rax)
movl $0xffffffff, 0x30(%rax) # imm = 0xFFFFFFFF
movl $0x9, (%rax)
movq %rax, %rdi
callq 0x5429
cmpq $0x0, 0x10(%rbx)
je 0x4444
leaq 0x18(%rbx), %rax
movq 0x18(%rbx), %rcx
movq %r14, 0x28(%rcx)
movq %rcx, 0x20(%r14)
jmp 0x444c
movq %r15, 0x10(%rbx)
movq %r15, 0x18(%rbx)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x2, %edx
callq 0x524d
movq %rax, %r15
movl $0x10, %edi
callq 0x2100
movq %rax, %r12
movl $0x8, %edi
callq 0x2100
movq %rax, (%r12)
movq %r15, (%rax)
movl $0x1, 0x8(%r12)
movq 0x10(%r14), %rax
movq %r12, 0x10(%rax)
jmp 0x444f
movq %r14, 0x10(%rbx)
leaq 0x18(%rbx), %rax
movq %r14, (%rax)
xorl %r15d, %r15d
movq %r13, 0x20(%rbx)
movq %r15, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x4f86(%rip), %rdi      # 0x93f0
xorl %eax, %eax
callq 0x2080
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2120