// File: /workspace/github_c_makefile_star2_O0/qpfiffer[P]38-Moths/src/greshunkel.c
// Function: gshkl_render
char *gshkl_render(const greshunkel_ctext *ctext, const char *to_render, const size_t original_size, size_t *outsize) {
	assert(to_render != NULL);
	assert(ctext != NULL);

	/* We start up a new buffer and copy the old one into it: */
	char *rendered = NULL;
	size_t intermediate_outsize = 0;

	struct compiled_regex all_regex;
	_compile_regex(&all_regex);

	size_t num_read = 0;
	while (num_read < original_size) {
		line current_line = read_line(to_render + num_read);

		line to_append = {0};
		size_t loop_readahead = 0;
		/* The loop needs to read more than the current line, so we pass
		 * in the offset and just let it go. If it processes more than the
		 * size of the current line, we know it did something.
		 * Append the whole line it gets back. */
		to_append = _interpolate_loop(ctext, to_render + num_read, &loop_readahead, &all_regex);

		/* Otherwise just interpolate the line like normal. */
		if (loop_readahead == 0) {
			/* Now let's try to interpolate an if statement, much like a loop. */
			size_t conditional_readahead = 0;
			to_append = _interpolate_conditionals(ctext, to_render + num_read, &conditional_readahead, &all_regex);

			if (conditional_readahead == 0) {
				to_append = _interpolate_line(ctext, current_line, &all_regex);
				num_read += current_line.size;
			} else {
				num_read += conditional_readahead;
			}
		} else {
			num_read += loop_readahead;
		}

		/* Fuck this */
		const size_t old_outsize = intermediate_outsize;
		intermediate_outsize += to_append.size;
		{
			char *med_buf = realloc(rendered, intermediate_outsize + 1);
			if (med_buf == NULL)
				goto error;
			med_buf[intermediate_outsize] = '\0';
			rendered = med_buf;
		}
		memcpy(rendered + old_outsize, to_append.data, to_append.size);
		if (to_append.data != current_line.data)
			free(current_line.data);
		free(to_append.data);
	}
	_destroy_regex(&all_regex);
	/* rendered[intermediate_outsize - 1] = '\0'; */

	if (outsize)
		*outsize = intermediate_outsize - 1;
	return rendered;

error:
	free(rendered);
	return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2b0, %rsp            # imm = 0x2B0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x2a84
jmp 0x2aa3
leaq 0x5aa0(%rip), %rdi      # 0x852b
leaq 0x579b(%rip), %rsi      # 0x822d
movl $0x3d5, %edx            # imm = 0x3D5
leaq 0x5a9f(%rip), %rcx      # 0x853d
callq 0x1100
cmpq $0x0, -0x10(%rbp)
je 0x2aac
jmp 0x2acb
leaq 0x576c(%rip), %rdi      # 0x821f
leaq 0x5773(%rip), %rsi      # 0x822d
movl $0x3d6, %edx            # imm = 0x3D6
leaq 0x5a77(%rip), %rcx      # 0x853d
callq 0x1100
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
leaq -0x238(%rbp), %rdi
callq 0x2d80
movq $0x0, -0x240(%rbp)
movq -0x240(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x2d2b
movq -0x18(%rbp), %rdi
addq -0x240(%rbp), %rdi
callq 0x2fc0
movq %rax, -0x250(%rbp)
movq %rdx, -0x248(%rbp)
leaq -0x260(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1110
movq $0x0, -0x268(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq -0x240(%rbp), %rsi
leaq -0x268(%rbp), %rdx
leaq -0x238(%rbp), %rcx
callq 0x3050
movq %rax, -0x278(%rbp)
movq %rdx, -0x270(%rbp)
movq -0x278(%rbp), %rax
movq %rax, -0x260(%rbp)
movq -0x270(%rbp), %rax
movq %rax, -0x258(%rbp)
cmpq $0x0, -0x268(%rbp)
jne 0x2c70
movq $0x0, -0x280(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq -0x240(%rbp), %rsi
leaq -0x280(%rbp), %rdx
leaq -0x238(%rbp), %rcx
callq 0x3560
movq %rax, -0x290(%rbp)
movq %rdx, -0x288(%rbp)
movq -0x290(%rbp), %rax
movq %rax, -0x260(%rbp)
movq -0x288(%rbp), %rax
movq %rax, -0x258(%rbp)
cmpq $0x0, -0x280(%rbp)
jne 0x2c59
movq -0x10(%rbp), %rdi
movq -0x250(%rbp), %rsi
movq -0x248(%rbp), %rdx
leaq -0x238(%rbp), %rcx
callq 0x4240
movq %rax, -0x2a0(%rbp)
movq %rdx, -0x298(%rbp)
movq -0x2a0(%rbp), %rax
movq %rax, -0x260(%rbp)
movq -0x298(%rbp), %rax
movq %rax, -0x258(%rbp)
movq -0x250(%rbp), %rax
addq -0x240(%rbp), %rax
movq %rax, -0x240(%rbp)
jmp 0x2c6e
movq -0x280(%rbp), %rax
addq -0x240(%rbp), %rax
movq %rax, -0x240(%rbp)
jmp 0x2c85
movq -0x268(%rbp), %rax
addq -0x240(%rbp), %rax
movq %rax, -0x240(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x2a8(%rbp)
movq -0x260(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0x1, %rsi
callq 0x11c0
movq %rax, -0x2b0(%rbp)
cmpq $0x0, -0x2b0(%rbp)
jne 0x2cc6
jmp 0x2d57
movq -0x2b0(%rbp), %rax
movq -0x38(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x2b0(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
addq -0x2a8(%rbp), %rdi
movq -0x258(%rbp), %rsi
movq -0x260(%rbp), %rdx
callq 0x1180
movq -0x258(%rbp), %rax
cmpq -0x248(%rbp), %rax
je 0x2d1a
movq -0x248(%rbp), %rdi
callq 0x1030
movq -0x258(%rbp), %rdi
callq 0x1030
jmp 0x2af2
leaq -0x238(%rbp), %rdi
callq 0x4580
cmpq $0x0, -0x28(%rbp)
je 0x2d4d
movq -0x38(%rbp), %rcx
subq $0x1, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2d68
movq -0x30(%rbp), %rdi
callq 0x1030
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x2b0, %rsp            # imm = 0x2B0
popq %rbp
retq
nopw %cs:(%rax,%rax)