// File: /workspace/github_c_makefile_star2_O1/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 0x41aa
movq %rsi, %rbx
movq %rdi, %r14
movq 0x20(%rsi), %r13
movq %rdi, 0x20(%rsi)
movl (%rdi), %esi
cmpq $0xf, %rsi
ja 0x446c
leaq 0x51e0(%rip), %rax      # 0x9370
movslq (%rax,%rsi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq 0x8(%r14), %rdi
movq %rbx, %rsi
callq 0x4161
jmp 0x4318
xorl %r15d, %r15d
jmp 0x445f
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x2, %edx
callq 0x527c
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 0x4458
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x4790
jmp 0x4458
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 0x445b
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x467c
jmp 0x4458
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x4484
jmp 0x4458
movq 0x8(%r14), %rax
cmpl $0x0, 0x8(%rax)
jle 0x4458
xorl %r15d, %r15d
xorl %r12d, %r12d
movq (%rax), %rax
movq (%rax,%r12,8), %rdi
movq %rbx, %rsi
movl $0x3, %edx
callq 0x4161
incq %r12
movq 0x8(%r14), %rax
movslq 0x8(%rax), %rcx
cmpq %rcx, %r12
jl 0x426c
jmp 0x445b
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 0x4161
movq %rax, 0x10(%r15)
movq %r15, %rdi
callq 0x545c
cmpq $0x0, 0x10(%rbx)
je 0x4402
movq 0x18(%rbx), %rax
movq %r15, 0x28(%rax)
movq %rax, 0x20(%r15)
jmp 0x4406
movq 0x8(%r14), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x4a91
jmp 0x4318
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x48cd
movq %rax, %r15
jmp 0x445b
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x4bc5
jmp 0x433a
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x4e7c
testq %rax, %rax
je 0x4458
movq %rax, %r15
movq 0x10(%r14), %rax
testq %rax, %rax
je 0x445b
movq (%rax), %rdi
callq 0x8178
movl %eax, 0x4(%r15)
jmp 0x445b
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x4523
jmp 0x4458
movq 0x8(%r14), %rax
movq 0x10(%rax), %rdi
movq %rbx, %rsi
movl $0x3, %edx
callq 0x4161
movl 0x4(%rbx), %r15d
leal 0x1(%r15), %eax
movl %eax, 0x4(%rbx)
movq %rbx, %rdi
movl %r15d, %esi
callq 0x5151
movq 0x8(%r14), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rdi
addq $0x10, %rdi
movl %r15d, %esi
callq 0x51b7
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 0x545c
cmpq $0x0, 0x10(%rbx)
je 0x4450
movq 0x18(%rbx), %rax
movq %r14, 0x28(%rax)
movq %rax, 0x20(%r14)
jmp 0x4454
movq %r15, 0x10(%rbx)
movq %r15, 0x18(%rbx)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x2, %edx
callq 0x527c
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 0x4458
movq %r14, 0x10(%rbx)
movq %r14, 0x18(%rbx)
xorl %r15d, %r15d
movq %r13, 0x20(%rbx)
movq %r15, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x4fa1(%rip), %rdi      # 0x9414
xorl %eax, %eax
callq 0x2080
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2120