// File: /workspace/github_c_makefile_star2_O2/markdryan[P]subtilis/frontend/fn_type.c
// Function: prv_generate_zero_fn
static size_t prv_generate_zero_fn(subtilis_parser_t *p,
				   const subtilis_type_t *type,
				   subtilis_error_t *err)
{
	subtilis_type_section_t *stype;
	subtilis_buffer_t name;
	subtilis_exp_t *e;
	const char *fn_name;
	const subtilis_symbol_t *s;
	subtilis_ir_operand_t result;
	subtilis_ir_operand_t ret_reg;
	subtilis_symbol_table_t *local_st = NULL;
	size_t call_index = SIZE_MAX;
	subtilis_symbol_table_t *old_local_st = p->local_st;
	subtilis_ir_section_t *old_section = p->current;

	subtilis_buffer_init(&name, 64);

	if (type->type == SUBTILIS_TYPE_ARRAY_FN) {
		/* I need a loop here that generates functions recursively. */
		subtilis_error_set_assertion_failed(err);
		return SIZE_MAX;
	}

	local_st = subtilis_symbol_table_new(err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;
	p->local_st = local_st;

	stype = subtilis_type_section_new(type, 0, NULL, NULL, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_buffer_append_string(&name, "!", err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_full_type_name(type, &name, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	fn_name = subtilis_buffer_get_string(&name);
	if (subtilis_string_pool_find(p->prog->string_pool, fn_name,
				      &call_index))
		goto cleanup;

	p->current = subtilis_ir_prog_section_new(
	    p->prog, fn_name, 0, stype, SUBTILIS_BUILTINS_MAX,
	    p->l->stream->name, p->l->line, p->eflag_offset, p->error_offset,
	    &call_index, err);
	stype = NULL;
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	if (type->type != SUBTILIS_TYPE_VOID) {
		if (subtilis_type_if_is_reference(type)) {
			s = subtilis_symbol_table_insert_tmp(local_st, type,
							     NULL, err);
			if (err->type != SUBTILIS_ERROR_OK)
				goto cleanup;

			subtilis_type_if_zero_ref(
			    p, type, SUBTILIS_IR_REG_LOCAL, s->loc, false, err);
			if (err->type != SUBTILIS_ERROR_OK)
				goto cleanup;

			result.reg = subtilis_reference_get_pointer(
			    p, SUBTILIS_IR_REG_LOCAL, s->loc, err);
			if (err->type != SUBTILIS_ERROR_OK)
				goto cleanup;

			/*
			 * Arrays are trickier as they don't have a zero value
			 */

			if (subtilis_type_if_is_array(type)) {
				ret_reg.reg = p->current->ret_reg;
				subtilis_ir_section_add_instr_no_reg2(
				    p->current, SUBTILIS_OP_INSTR_MOV, ret_reg,
				    result, err);
				if (err->type != SUBTILIS_ERROR_OK)
					goto cleanup;

				subtilis_ir_section_add_label(
				    p->current, p->current->end_label, err);
				if (err->type != SUBTILIS_ERROR_OK)
					goto cleanup;
			} else {
				ret_reg.reg = result.reg;
			}
			e = subtilis_exp_new_var(type, ret_reg.reg, err);
			if (err->type != SUBTILIS_ERROR_OK)
				goto cleanup;

			p->current->locals = local_st->max_allocated;
		} else {
			e = subtilis_type_if_zero(p, type, err);
		}
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;

		subtilis_type_if_ret(p, type, e->exp.ir_op.reg, err);
		subtilis_exp_delete(e);
	} else {
		subtilis_ir_section_add_instr_no_arg(
		    p->current, SUBTILIS_OP_INSTR_RET, err);
	}

cleanup:
	p->current = old_section;

	subtilis_type_section_delete(stype);
	p->local_st = old_local_st;
	subtilis_symbol_table_delete(local_st);
	subtilis_buffer_free(&name);

	return call_index;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %r15
orq $-0x1, 0x18(%rsp)
movq %rsi, %r12
movq %rdi, %rbx
movq 0x48(%rdi), %r13
movq 0x68(%rdi), %rbp
leaq 0x20(%rsp), %rdi
pushq $0x40
popq %rsi
callq 0x146a8
cmpl $0xe, (%r12)
jne 0x3ec98
leaq 0x30c2c(%rip), %rdx     # 0x6f8b4
pushq $0xf
popq %rsi
pushq $0x2d
popq %rcx
movq %r15, %rdi
callq 0x1429e
jmp 0x3eca6
movq %r15, %rdi
callq 0x2ca24
cmpl $0x0, (%r15)
je 0x3ecb8
pushq $-0x1
popq %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq %rax, 0x68(%rbx)
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x2d9c4
movq %rax, %rdi
cmpl $0x0, (%r15)
jne 0x3ed3b
movq %rdi, 0x8(%rsp)
leaq 0x2e7d1(%rip), %rsi     # 0x6d4b6
leaq 0x20(%rsp), %rdi
movq %r15, %rdx
callq 0x14837
cmpl $0x0, (%r15)
jne 0x3ed36
leaq 0x20(%rsp), %rsi
movq %r12, %rdi
movq %r15, %rdx
callq 0x2dce5
cmpl $0x0, (%r15)
jne 0x3ed36
leaq 0x20(%rsp), %rdi
callq 0x14a53
movq %rax, %rsi
movq 0x58(%rbx), %rax
movq 0x18(%rax), %rdi
leaq 0x18(%rsp), %rdx
movq %rsi, 0x10(%rsp)
callq 0x2d68e
testb %al, %al
je 0x3ed64
movq 0x8(%rsp), %rdi
movq %r13, 0x48(%rbx)
callq 0x2dc17
movq %rbp, 0x68(%rbx)
movq %r14, %rdi
callq 0x2cac8
leaq 0x20(%rsp), %rdi
callq 0x14a01
movq 0x18(%rsp), %rax
jmp 0x3eca9
movq (%rbx), %rax
movq 0x58(%rbx), %rdi
movq 0x30(%rax), %r9
movl 0x38(%rax), %eax
movq %rax, 0x30(%rsp)
movl 0xb0(%rbx), %r10d
movl 0xb4(%rbx), %r11d
subq $0x8, %rsp
pushq $0x9
popq %r8
movq 0x18(%rsp), %rsi
xorl %edx, %edx
movq 0x10(%rsp), %rcx
pushq %r15
leaq 0x28(%rsp), %rax
pushq %rax
pushq %r11
pushq %r10
pushq 0x58(%rsp)
callq 0x2acef
addq $0x30, %rsp
movq %rax, 0x48(%rbx)
cmpl $0x0, (%r15)
movl $0x0, %edi
jne 0x3ed3b
cmpl $0x8, (%r12)
jne 0x3eddf
pushq $0x46
popq %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x2a9af
jmp 0x3eed7
movq %r12, %rdi
callq 0x35808
testb %al, %al
je 0x3eea3
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
movq %r15, %rcx
callq 0x2cee3
xorl %edi, %edi
cmpl $0x0, (%r15)
jne 0x3ed3b
movq %rax, 0x8(%rsp)
movq (%rax), %rcx
pushq $0x2
popq %rdx
movq %rbx, %rdi
movq %r12, %rsi
xorl %r8d, %r8d
movq %r15, %r9
callq 0x33ebd
cmpl $0x0, (%r15)
jne 0x3eed7
movq 0x8(%rsp), %rax
movq (%rax), %rdx
pushq $0x2
popq %rsi
movq %rbx, %rdi
movq %r15, %rcx
callq 0x4ab28
movq %rax, 0x8(%rsp)
cmpl $0x0, (%r15)
jne 0x3eed7
movq %r12, %rdi
callq 0x357cc
testb %al, %al
je 0x3eede
movq 0x48(%rbx), %rdi
movq 0xa8(%rdi), %rdx
pushq $0x1d
popq %rsi
movq %rdx, 0x10(%rsp)
movq 0x8(%rsp), %rcx
movq %r15, %r8
callq 0x2a9a4
cmpl $0x0, (%r15)
jne 0x3eed7
movq 0x48(%rbx), %rdi
movq 0x98(%rdi), %rsi
movq %r15, %rdx
callq 0x2b781
cmpl $0x0, (%r15)
jne 0x3eed7
jmp 0x3eee8
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x33e86
cmpl $0x0, (%r15)
jne 0x3eed7
movq 0x148(%rax), %rdx
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rcx
movq %rax, %r15
callq 0x356e1
movq %r15, %rdi
callq 0x29d88
xorl %edi, %edi
jmp 0x3ed3b
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x2a226
cmpl $0x0, (%r15)
jne 0x3eed7
movq 0x310(%r14), %rdx
movq 0x48(%rbx), %rcx
movq %rdx, 0x10(%rcx)
jmp 0x3eeb7