// File: /workspace/github_c_makefile_star2_O3/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), %rbp
movq (%rbp), %rdi
callq 0x53b4
movq %rbx, %rdi
movq %rax, %rsi
callq 0x55c6
movq %rbp, 0x20(%rbx)
leaq 0x108(%rsp), %r12
xorl %r15d, %r15d
movq %r12, %rdi
xorl %esi, %esi
movl $0x17, %edx
callq 0x52a0
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movl $0x36, %edi
movl $0x1, %esi
xorl %eax, %eax
callq 0x52d5
movq %rbx, %rdi
movq %rax, %rsi
callq 0x55c6
leaq 0xe8(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
movl $0x17, %edx
callq 0x52a0
leaq 0xc8(%rsp), %r13
movq %r13, %rdi
xorl %esi, %esi
movl $0x14, %edx
callq 0x52a0
movups (%r13), %xmm0
movups 0x10(%r13), %xmm1
movups %xmm1, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movl $0x3e, %edi
movl $0x2, %esi
xorl %eax, %eax
callq 0x52d5
movq %rbx, %rdi
movq %rax, %rsi
callq 0x55c6
cmpl $0x0, 0x28(%rbp)
jle 0x5175
xorl %r12d, %r12d
movq 0x20(%rbp), %rax
movq (%rax,%r12,8), %rax
movq 0x8(%rax), %rdi
callq 0xc4a6
addl %eax, %r15d
incq %r12
movslq 0x28(%rbp), %rax
cmpq %rax, %r12
jl 0x50dd
testl %r15d, %r15d
jle 0x5175
leaq 0xa8(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
movl $0x14, %edx
callq 0x52a0
leaq 0x88(%rsp), %r13
movq %r13, %rdi
movl $0x1, %esi
movl %r15d, %edx
callq 0x52a0
movups (%r13), %xmm0
movups 0x10(%r13), %xmm1
movups %xmm1, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movl $0x33, %edi
movl $0x2, %esi
xorl %eax, %eax
callq 0x52d5
movq %rbx, %rdi
movq %rax, %rsi
callq 0x55c6
movq 0x20(%r14), %rax
movq 0x60(%rax), %r14
testq %r14, %r14
je 0x5199
cmpl $0x2, (%r14)
jne 0x5193
movq %rbx, %rdi
movq %r14, %rsi
callq 0x497b
movq 0x60(%r14), %r14
jmp 0x517d
movq 0x8(%rbx), %rax
cmpl $0x41, (%rax)
je 0x5252
movq (%rbp), %rdi
leaq 0x84a4(%rip), %rsi      # 0xd655
callq 0x3110
testl %eax, %eax
jne 0x5220
leaq 0x68(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x52a0
leaq 0x48(%rsp), %r15
movq %r15, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x52a0
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 0x52d5
movq %rbx, %rdi
movq %rax, %rsi
callq 0x55c6
movl $0x42, %edi
xorl %esi, %esi
xorl %eax, %eax
callq 0x52d5
movq %rbx, %rdi
movq %rax, %rsi
callq 0x55c6
movl $0x41, %edi
xorl %esi, %esi
xorl %eax, %eax
callq 0x52d5
movq %rbx, %rdi
movq %rax, %rsi
callq 0x55c6
movq $0x0, 0x20(%rbx)
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq