// File: /workspace/github_c_makefile_star2_O2/Yamsman[P]scc/src/gen.c
// Function: gen_fdef
void gen_fdef(asm_f *f, ast_n *n) {
	//Make label
	symbol *fsym = n->dat.decl.sym;
	char *fnc_name = fsym->name;
	asmf_add_inst(f, mk_label(fnc_name));
	f->fsym = fsym;

	//Function preamble
	asmf_add_inst(f, mk_inst(INST_PUSH, 1,
		mk_oprd(OPRD_REG, RBP)
	));
	asmf_add_inst(f, mk_inst(INST_MOV, 2,
		mk_oprd(OPRD_REG, RBP),	
		mk_oprd(OPRD_REG, RSP)
	));

	//Calculate local variable area size
	int vsize = 0;
	for (int i=0; i<fsym->lvars.len; i++) {
		symbol *var = fsym->lvars.table[i];
		vsize += type_sizeof(var->type);
	}
	if (vsize > 0) {
		asmf_add_inst(f, mk_inst(INST_SUB, 2,
			mk_oprd(OPRD_REG, RSP),
			mk_oprd(OPRD_IMM, vsize)
		));
	}

	//Function body
	gen_stmt_cmpd(f, n->dat.decl.block);

	//Function end
	//Only generate a return statemnt if one isn't present
	if (f->text_cur->op != INST_RET) {
		//Only 'main' has a defined return value of zero
		//if no return statement is present
		if (!strcmp(fsym->name, "main")) {
			asmf_add_inst(f, mk_inst(INST_MOV, 2,
				mk_oprd(OPRD_REG, RAX),
				mk_oprd(OPRD_IMM, 0)
			));
		}

		asmf_add_inst(f, mk_inst(INST_LEAVE, 0));
		asmf_add_inst(f, mk_inst(INST_RET, 0));
	}
	f->fsym = NULL;
	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdi, %rbx
movq %rsi, 0x40(%rsp)
movq 0x10(%rsi), %r13
movq (%r13), %rdi
callq 0x51dd
movq %rbx, %rdi
movq %rax, %rsi
callq 0x53ce
movq %r13, 0x20(%rbx)
leaq 0x108(%rsp), %r15
xorl %r14d, %r14d
pushq $0x17
popq %rbp
movq %r15, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x50d0
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
pushq $0x36
popq %rdi
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x50ff
movq %rbx, %rdi
movq %rax, %rsi
callq 0x53ce
leaq 0xe8(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x50d0
leaq 0xc8(%rsp), %r12
pushq $0x14
popq %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x50d0
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movups %xmm1, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
pushq $0x3e
popq %rdi
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x50ff
movq %rbx, %rdi
movq %rax, %rsi
callq 0x53ce
xorl %ebp, %ebp
movslq 0x28(%r13), %rax
cmpq %rax, %r14
jge 0x4f5a
movq 0x20(%r13), %rax
movq (%rax,%r14,8), %rax
movq 0x8(%rax), %rdi
callq 0xbaf0
addl %eax, %ebp
incq %r14
jmp 0x4f39
testl %ebp, %ebp
jle 0x4fc7
leaq 0xa8(%rsp), %r15
pushq $0x14
popq %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x50d0
leaq 0x88(%rsp), %r12
pushq $0x1
popq %rsi
movq %r12, %rdi
movl %ebp, %edx
callq 0x50d0
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movups %xmm1, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
pushq $0x33
popq %rdi
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x50ff
movq %rbx, %rdi
movq %rax, %rsi
callq 0x53ce
movq 0x40(%rsp), %rax
movq 0x20(%rax), %rsi
movq %rbx, %rdi
callq 0x47f7
movq 0x8(%rbx), %rax
cmpl $0x41, (%rax)
je 0x5087
movq (%r13), %rdi
leaq 0x7665(%rip), %rsi      # 0xc655
callq 0x3110
testl %eax, %eax
jne 0x5059
leaq 0x68(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x50d0
leaq 0x48(%rsp), %r15
pushq $0x1
popq %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x50d0
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups %xmm1, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
pushq $0x3e
popq %rdi
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x50ff
movq %rbx, %rdi
movq %rax, %rsi
callq 0x53ce
pushq $0x42
popq %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x50ff
movq %rbx, %rdi
movq %rax, %rsi
callq 0x53ce
pushq $0x41
popq %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x50ff
movq %rbx, %rdi
movq %rax, %rsi
callq 0x53ce
andq $0x0, 0x20(%rbx)
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq