// File: /workspace/github_c_makefile_star2_O1/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 %rsi, %r14
movq %rdi, %rbx
movq 0x10(%rsi), %r13
movq (%r13), %rdi
callq 0x5334
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5543
movq %r13, 0x20(%rbx)
leaq 0x108(%rsp), %r15
xorl %ebp, %ebp
movq %r15, %rdi
xorl %esi, %esi
movl $0x17, %edx
callq 0x5220
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movl $0x36, %edi
movl $0x1, %esi
xorl %eax, %eax
callq 0x5255
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5543
leaq 0xe8(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
movl $0x17, %edx
callq 0x5220
leaq 0xc8(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
movl $0x14, %edx
callq 0x5220
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)
movl $0x3e, %edi
movl $0x2, %esi
xorl %eax, %eax
callq 0x5255
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5543
cmpl $0x0, 0x28(%r13)
jle 0x507f
xorl %r15d, %r15d
xorl %ebp, %ebp
movq 0x20(%r13), %rax
movq (%rax,%r15,8), %rax
movq 0x8(%rax), %rdi
callq 0xc573
addl %eax, %ebp
incq %r15
movslq 0x28(%r13), %rax
cmpq %rax, %r15
jl 0x5060
testl %ebp, %ebp
jle 0x50f4
leaq 0xa8(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
movl $0x14, %edx
callq 0x5220
leaq 0x88(%rsp), %r12
movq %r12, %rdi
movl $0x1, %esi
movl %ebp, %edx
callq 0x5220
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)
movl $0x33, %edi
movl $0x2, %esi
xorl %eax, %eax
callq 0x5255
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5543
movq 0x20(%r14), %rax
movq 0x60(%rax), %r14
testq %r14, %r14
je 0x5118
cmpl $0x2, (%r14)
jne 0x5112
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4903
movq 0x60(%r14), %r14
jmp 0x50fc
movq 0x8(%rbx), %rax
cmpl $0x41, (%rax)
je 0x51d1
movq (%r13), %rdi
leaq 0x8525(%rip), %rsi      # 0xd655
callq 0x3110
testl %eax, %eax
jne 0x519f
leaq 0x68(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5220
leaq 0x48(%rsp), %r15
movq %r15, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x5220
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)
movl $0x3e, %edi
movl $0x2, %esi
xorl %eax, %eax
callq 0x5255
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5543
movl $0x42, %edi
xorl %esi, %esi
xorl %eax, %eax
callq 0x5255
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5543
movl $0x41, %edi
xorl %esi, %esi
xorl %eax, %eax
callq 0x5255
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5543
movq $0x0, 0x20(%rbx)
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq