// File: /workspace/github_c_makefile_star2_O1/Mithreindeir[P]ewcc/src/frontend/cdecl.c
// Function: parse_function
struct stmt *parse_function(struct parser *p)
{
	enum type_specifier dtype = type_int;
	if (match(p, t_void)) dtype=type_void;
	else if (match(p, t_int))  dtype = type_int;
	else if (match(p, t_char)) dtype = type_char;
	else printf("Warning: return type defaults to int\n");
	/*Validify function declaration, then parse body */
	struct declaration *decl;
	decl = malloc(sizeof(struct declaration));
	decl->initializer = NULL;
	decl->ident = NULL;
	decl->type = NULL;
	if (!parse_declarator(p, decl)) {
		free(decl);
		return 0;
	}
	if (!decl->type) syntax_error(p, "Invalid declarator");
	/*The type must be a function */
	if (decl->type->type != type_fcn) {
		free(decl->ident);
		type_free(decl->type);
		free(decl);
		return 0;
	}

	struct type *decldtype = type_init(type_datatype);
	decldtype->info.data_type = dtype;
	decl->type = types_merge(decl->type, decldtype);
	set_type(get_scope(p), decl->ident, decl->type);
	/*Create function from declaration */
	struct stmt *block;
	if (!(block = parse_block(p))) {
		free(decl->ident);
		type_free(decl->type);
		free(decl);
		return 0;
	}
	struct stmt *fn = func_init(decl->ident, decl->type, block);
	free(decl);
	struct type *ftype = FUNC(fn)->ftype;
	/*lastly, add parameters to block scope */
	for (int i = 0; i < ftype->info.fcn.paramc; i++) {
		char *pn = ftype->info.fcn.paramv[i];
		struct type *pt = ftype->info.fcn.paramt[i];
		set_type(BLOCK(FUNC(fn)->body)->scope, pn, pt);
	}

	return fn;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdi, %rbx
movl $0x3, %esi
callq 0x7cb0
xorl %ebp, %ebp
testq %rax, %rax
jne 0x637c
movq %rbx, %rdi
movl $0x4, %esi
callq 0x7cb0
movl $0x1, %ebp
testq %rax, %rax
jne 0x637c
movq %rbx, %rdi
movl $0x5, %esi
callq 0x7cb0
movl $0x2, %ebp
testq %rax, %rax
jne 0x637c
leaq 0x3353(%rip), %rdi      # 0x96c5
callq 0x2060
movl $0x1, %ebp
movl $0x18, %edi
callq 0x2100
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x60fd
testl %eax, %eax
je 0x647d
movq 0x8(%r14), %r15
testq %r15, %r15
jne 0x63c2
leaq 0x32f8(%rip), %rsi      # 0x96b2
movq %rbx, %rdi
callq 0x7d15
cmpl $0x3, 0x18(%r15)
jne 0x646a
xorl %edi, %edi
callq 0x7f9c
movl %ebp, (%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x7fb6
movq %rax, %r15
movq %rax, 0x8(%r14)
movq %rbx, %rdi
callq 0x7d5d
movq (%r14), %r12
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x7e98
movq %rbx, %rdi
callq 0x76bc
testq %rax, %rax
je 0x646d
movq %r12, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x5b28
movq %rax, %rbx
movq %r14, %rdi
callq 0x2030
movq 0x8(%rbx), %rax
movq 0x8(%rax), %r14
cmpl $0x0, (%r14)
jle 0x6487
xorl %r15d, %r15d
movq 0x8(%r14), %rax
movq 0x10(%r14), %rcx
movq (%rax,%r15,8), %rsi
movq (%rcx,%r15,8), %rdx
movq 0x8(%rbx), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rdi
callq 0x7e98
incq %r15
movslq (%r14), %rax
cmpq %rax, %r15
jl 0x6438
jmp 0x6487
movq (%r14), %r12
movq %r12, %rdi
callq 0x2030
movq %r15, %rdi
callq 0x7fd6
movq %r14, %rdi
callq 0x2030
xorl %ebx, %ebx
movq %rbx, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop