// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %rdx, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
testq %rsi, %rsi
je 0x281e
movq %rdi, %rbp
testq %rdi, %rdi
je 0x283d
movq %rcx, %rbx
leaq 0x4815(%rip), %rsi      # 0x6d20
leaq 0x58(%rsp), %rdi
movl $0x1, %edx
callq 0x1140
testl %eax, %eax
jne 0x285c
leaq 0x98(%rsp), %rdi
leaq 0x480f(%rip), %rsi      # 0x6d40
movl $0x1, %edx
callq 0x1140
testl %eax, %eax
jne 0x287b
leaq 0xd8(%rsp), %rdi
leaq 0x481e(%rip), %rsi      # 0x6d70
movl $0x1, %edx
callq 0x1140
testl %eax, %eax
jne 0x289a
leaq 0x118(%rsp), %rdi
leaq 0x483d(%rip), %rsi      # 0x6db0
movl $0x1, %edx
callq 0x1140
testl %eax, %eax
jne 0x28b9
leaq 0x158(%rsp), %rdi
leaq 0x483c(%rip), %rsi      # 0x6dd0
movl $0x1, %edx
callq 0x1140
testl %eax, %eax
jne 0x28d8
leaq 0x198(%rsp), %rdi
leaq 0x483b(%rip), %rsi      # 0x6df0
movl $0x1, %edx
callq 0x1140
testl %eax, %eax
jne 0x28f7
leaq 0x1d8(%rsp), %rdi
leaq 0x484a(%rip), %rsi      # 0x6e20
movl $0x1, %edx
callq 0x1140
testl %eax, %eax
jne 0x2916
movq %rbx, 0x38(%rsp)
leaq 0x218(%rsp), %rdi
leaq 0x4864(%rip), %rsi      # 0x6e60
movl $0x1, %edx
callq 0x1140
testl %eax, %eax
jne 0x2935
xorl %r14d, %r14d
movl $0x2401, %edx           # imm = 0x2401
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %rbp, 0x40(%rsp)
cmpq 0x50(%rsp), %r14
jae 0x2792
movq %r12, 0x8(%rsp)
movq %r13, 0x10(%rsp)
movq 0x48(%rsp), %rax
leaq (%rax,%r14), %r13
xorl %r12d, %r12d
movzbl (%r13,%r12), %eax
incq %r12
cmpl $0xd, %eax
ja 0x2642
btl %eax, %edx
jae 0x2642
leaq 0x1(%r12), %rsi
movl $0x1, %edi
callq 0x1160
movq %rax, 0x20(%rsp)
movq %rax, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x1050
movq $0x0, 0x30(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
leaq 0x30(%rsp), %rdx
leaq 0x58(%rsp), %rcx
callq 0x2954
movq %rdx, 0x18(%rsp)
movq 0x30(%rsp), %r15
testq %r15, %r15
jne 0x26d8
movq $0x0, 0x28(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
leaq 0x28(%rsp), %rdx
leaq 0x58(%rsp), %r13
movq %r13, %rcx
callq 0x2d3e
movq %rdx, 0x18(%rsp)
movq 0x28(%rsp), %r15
testq %r15, %r15
je 0x2759
movq %rax, %rbx
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
leaq (%rbx,%rbp), %r12
leaq (%rbx,%rbp), %rsi
incq %rsi
movq %r13, %rdi
callq 0x11d0
testq %rax, %rax
je 0x2709
movb $0x0, (%rax,%r12)
xorl %ecx, %ecx
movq %rax, %r13
jmp 0x270e
movl $0x4, %ecx
testq %rax, %rax
je 0x2742
addq %r13, %rbp
movq %rbp, %rdi
movq 0x18(%rsp), %rbp
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x1190
movq 0x20(%rsp), %rdi
cmpq %rdi, %rbp
je 0x2738
callq 0x1030
movq %rbp, %rdi
callq 0x1030
xorl %ecx, %ecx
addq %r15, %r14
testl %ecx, %ecx
movq 0x40(%rsp), %rbp
movl $0x2401, %edx           # imm = 0x2401
je 0x2621
jmp 0x277c
movq %rbp, %rdi
movq %r12, %rsi
movq 0x20(%rsp), %rdx
movq %r13, %rcx
callq 0x3828
movq %rax, %rbx
movq %rdx, 0x18(%rsp)
movq %r12, %r15
jmp 0x26db
cmpl $0x4, %ecx
jne 0x2809
movq %r13, %rdi
callq 0x1030
xorl %r13d, %r13d
jmp 0x2809
leaq 0x58(%rsp), %rdi
callq 0x10b0
leaq 0x98(%rsp), %rdi
callq 0x10b0
leaq 0xd8(%rsp), %rdi
callq 0x10b0
leaq 0x118(%rsp), %rdi
callq 0x10b0
leaq 0x158(%rsp), %rdi
callq 0x10b0
leaq 0x198(%rsp), %rdi
callq 0x10b0
leaq 0x1d8(%rsp), %rdi
callq 0x10b0
leaq 0x218(%rsp), %rdi
callq 0x10b0
movq 0x38(%rsp), %rax
testq %rax, %rax
je 0x2809
decq %r12
movq %r12, (%rax)
jmp 0x2809
movq %r13, %rax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3d17(%rip), %rdi      # 0x653c
leaq 0x3a12(%rip), %rsi      # 0x623e
leaq 0x3d1b(%rip), %rcx      # 0x654e
movl $0x3d5, %edx            # imm = 0x3D5
callq 0x1110
leaq 0x39ec(%rip), %rdi      # 0x6230
leaq 0x39f3(%rip), %rsi      # 0x623e
leaq 0x3cfc(%rip), %rcx      # 0x654e
movl $0x3d6, %edx            # imm = 0x3D6
callq 0x1110
leaq 0x3d3e(%rip), %rdi      # 0x65a1
leaq 0x39d4(%rip), %rsi      # 0x623e
leaq 0x3d3a(%rip), %rcx      # 0x65ab
movl $0x3b1, %edx            # imm = 0x3B1
callq 0x1110
leaq 0x3d1f(%rip), %rdi      # 0x65a1
leaq 0x39b5(%rip), %rsi      # 0x623e
leaq 0x3d1b(%rip), %rcx      # 0x65ab
movl $0x3b4, %edx            # imm = 0x3B4
callq 0x1110
leaq 0x3d00(%rip), %rdi      # 0x65a1
leaq 0x3996(%rip), %rsi      # 0x623e
leaq 0x3cfc(%rip), %rcx      # 0x65ab
movl $0x3b7, %edx            # imm = 0x3B7
callq 0x1110
leaq 0x3ce1(%rip), %rdi      # 0x65a1
leaq 0x3977(%rip), %rsi      # 0x623e
leaq 0x3cdd(%rip), %rcx      # 0x65ab
movl $0x3ba, %edx            # imm = 0x3BA
callq 0x1110
leaq 0x3cc2(%rip), %rdi      # 0x65a1
leaq 0x3958(%rip), %rsi      # 0x623e
leaq 0x3cbe(%rip), %rcx      # 0x65ab
movl $0x3bd, %edx            # imm = 0x3BD
callq 0x1110
leaq 0x3ca3(%rip), %rdi      # 0x65a1
leaq 0x3939(%rip), %rsi      # 0x623e
leaq 0x3c9f(%rip), %rcx      # 0x65ab
movl $0x3c0, %edx            # imm = 0x3C0
callq 0x1110
leaq 0x3c84(%rip), %rdi      # 0x65a1
leaq 0x391a(%rip), %rsi      # 0x623e
leaq 0x3c80(%rip), %rcx      # 0x65ab
movl $0x3c3, %edx            # imm = 0x3C3
callq 0x1110
leaq 0x3c65(%rip), %rdi      # 0x65a1
leaq 0x38fb(%rip), %rsi      # 0x623e
leaq 0x3c61(%rip), %rcx      # 0x65ab
movl $0x3c6, %edx            # imm = 0x3C6
callq 0x1110