// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x5947
movq $0x0, -0x8(%rbp)
jmp 0x5cb0
movq $0x0, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x48(%rbp)
subq $0xf, %rax
ja 0x5c7e
movq -0x48(%rbp), %rax
leaq 0x6a02(%rip), %rcx      # 0xc388
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x5cc0
jmp 0x5c9c
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x5dc0
jmp 0x5c9c
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x5fc0
jmp 0x5c9c
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x60a0
jmp 0x5c9c
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x5920
movq %rax, -0x28(%rbp)
jmp 0x5c9c
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movl -0x1c(%rbp), %edx
callq 0x6260
movq %rax, -0x28(%rbp)
jmp 0x5c9c
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rsi
movl -0x1c(%rbp), %edx
callq 0x63b0
movq %rax, -0x28(%rbp)
jmp 0x5c9c
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
callq 0x64f0
movq %rax, -0x28(%rbp)
jmp 0x5c9c
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movl -0x1c(%rbp), %edx
callq 0x6540
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x5aa0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
cmpq $0x0, %rax
je 0x5aa0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rdi
callq 0xb380
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0x5c9c
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movl -0x1c(%rbp), %edx
callq 0x6850
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x5aeb
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
cmpq $0x0, %rax
je 0x5aeb
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rdi
callq 0xb380
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0x5c9c
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
cmpl 0x8(%rcx), %eax
jge 0x5b33
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x18(%rbp), %rsi
movl $0x3, %edx
callq 0x5920
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x5af7
jmp 0x5c9c
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rsi
movl $0x3, %edx
callq 0x5920
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, 0x4(%rcx)
movl %eax, -0x38(%rbp)
movq -0x18(%rbp), %rdi
movl -0x38(%rbp), %esi
callq 0x6b70
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rdi
addq $0x10, %rdi
movl -0x38(%rbp), %esi
callq 0x6bc0
callq 0x70d0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl $0x9, (%rax)
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x6c40
jmp 0x5c9c
movq -0x18(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x2, %edx
callq 0x6d10
movq %rax, %rdi
callq 0x6cb0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x10(%rax)
jmp 0x5c9c
movq -0x18(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x2, %edx
callq 0x6d10
movq %rax, %rdi
callq 0x6cb0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x8(%rax)
jmp 0x5c9c
callq 0x70d0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl $0x8, (%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
movl $0x2, %edx
callq 0x5920
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x6c40
movq -0x18(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x2, %edx
callq 0x6d10
movq %rax, %rdi
callq 0x6cb0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x10(%rax)
jmp 0x5c9c
jmp 0x5c9c
movq -0x10(%rbp), %rax
movl (%rax), %esi
leaq 0x673d(%rip), %rdi      # 0xc3c8
movb $0x0, %al
callq 0x2060
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2100
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)