// File: /workspace/github_c_makefile_star2_O3/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 $0x248, %rsp            # imm = 0x248
movq %rdx, 0x10(%rsp)
movq %rsi, 0x40(%rsp)
testq %rsi, %rsi
je 0x2813
movq %rdi, %r14
testq %rdi, %rdi
je 0x2832
movq %rcx, %rbx
leaq 0x482a(%rip), %rsi      # 0x6d20
leaq 0x48(%rsp), %rdi
movl $0x1, %edx
callq 0x1140
testl %eax, %eax
jne 0x2851
leaq 0x88(%rsp), %r15
leaq 0x4824(%rip), %rsi      # 0x6d40
movq %r15, %rdi
movl $0x1, %edx
callq 0x1140
testl %eax, %eax
jne 0x2870
leaq 0xc8(%rsp), %r12
leaq 0x4830(%rip), %rsi      # 0x6d70
movq %r12, %rdi
movl $0x1, %edx
callq 0x1140
testl %eax, %eax
jne 0x288f
leaq 0x108(%rsp), %rbp
leaq 0x484c(%rip), %rsi      # 0x6db0
movq %rbp, %rdi
movl $0x1, %edx
callq 0x1140
testl %eax, %eax
jne 0x28ae
leaq 0x148(%rsp), %rdi
leaq 0x4848(%rip), %rsi      # 0x6dd0
movl $0x1, %edx
callq 0x1140
testl %eax, %eax
jne 0x28cd
leaq 0x188(%rsp), %rdi
leaq 0x4847(%rip), %rsi      # 0x6df0
movl $0x1, %edx
callq 0x1140
testl %eax, %eax
jne 0x28ec
leaq 0x1c8(%rsp), %rdi
leaq 0x4856(%rip), %rsi      # 0x6e20
movl $0x1, %edx
callq 0x1140
testl %eax, %eax
jne 0x290b
leaq 0x208(%rsp), %rdi
leaq 0x4875(%rip), %rsi      # 0x6e60
movl $0x1, %edx
callq 0x1140
testl %eax, %eax
jne 0x292a
cmpq $0x0, 0x10(%rsp)
je 0x275f
movq %rbx, 0x38(%rsp)
movq $0x0, (%rsp)
movl $0x2401, %ecx           # imm = 0x2401
leaq 0x48(%rsp), %r15
xorl %r13d, %r13d
xorl %ebp, %ebp
movq %r14, 0x18(%rsp)
movq %r13, 0x8(%rsp)
movq 0x40(%rsp), %rax
leaq (%rax,%rbp), %r12
xorl %ebx, %ebx
movzbl (%r12,%rbx), %eax
incq %rbx
cmpl $0xd, %eax
ja 0x263a
btl %eax, %ecx
jae 0x263a
leaq 0x1(%rbx), %rsi
movl $0x1, %edi
callq 0x1160
movq %rax, 0x20(%rsp)
movq %rax, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1050
movq $0x0, 0x30(%rsp)
movq %r14, %rdi
movq %r12, %rsi
leaq 0x30(%rsp), %rdx
movq %r15, %rcx
callq 0x2949
movq %r14, %rcx
movq 0x30(%rsp), %r14
testq %r14, %r14
jne 0x26bf
movq $0x0, 0x28(%rsp)
movq %rcx, %rdi
movq %r12, %rsi
leaq 0x28(%rsp), %rdx
movq %r15, %r12
movq %r15, %rcx
callq 0x2d33
movq 0x28(%rsp), %r14
testq %r14, %r14
je 0x273c
movq %rax, %r13
movq %rdx, %r15
movq (%rsp), %rbx
movq 0x8(%rsp), %r12
leaq (%r12,%r13), %rsi
incq %rsi
movq %rbx, %rdi
callq 0x11d0
testq %rax, %rax
je 0x2770
movq %r13, %rdx
addq %r12, %r13
movq %rax, %rbx
movb $0x0, (%rax,%r13)
addq %r12, %rax
movq %rax, %rdi
movq %r15, %rsi
callq 0x1190
movq 0x20(%rsp), %rdi
cmpq %rdi, %r15
je 0x2711
callq 0x1030
movq %rbx, (%rsp)
addq %r14, %rbp
movq %r15, %rdi
callq 0x1030
cmpq 0x10(%rsp), %rbp
movq 0x18(%rsp), %r14
movl $0x2401, %ecx           # imm = 0x2401
leaq 0x48(%rsp), %r15
jb 0x262a
jmp 0x277f
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq 0x20(%rsp), %rdx
movq %r12, %rcx
callq 0x37df
movq %rax, %r13
movq %rdx, %r15
movq %rbx, %r14
jmp 0x26c5
movq $-0x1, %r13
movq $0x0, (%rsp)
jmp 0x279f
movq %rbx, %rdi
callq 0x1030
xorl %eax, %eax
jmp 0x2801
decq %r13
movq 0x38(%rsp), %rbx
leaq 0x88(%rsp), %r15
leaq 0xc8(%rsp), %r12
leaq 0x108(%rsp), %rbp
leaq 0x48(%rsp), %rdi
callq 0x10b0
movq %r15, %rdi
callq 0x10b0
movq %r12, %rdi
callq 0x10b0
movq %rbp, %rdi
callq 0x10b0
leaq 0x148(%rsp), %rdi
callq 0x10b0
leaq 0x188(%rsp), %rdi
callq 0x10b0
leaq 0x1c8(%rsp), %rdi
callq 0x10b0
leaq 0x208(%rsp), %rdi
callq 0x10b0
testq %rbx, %rbx
je 0x27fd
movq %r13, (%rbx)
movq (%rsp), %rax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3d22(%rip), %rdi      # 0x653c
leaq 0x3a1d(%rip), %rsi      # 0x623e
leaq 0x3d26(%rip), %rcx      # 0x654e
movl $0x3d5, %edx            # imm = 0x3D5
callq 0x1110
leaq 0x39f7(%rip), %rdi      # 0x6230
leaq 0x39fe(%rip), %rsi      # 0x623e
leaq 0x3d07(%rip), %rcx      # 0x654e
movl $0x3d6, %edx            # imm = 0x3D6
callq 0x1110
leaq 0x3d49(%rip), %rdi      # 0x65a1
leaq 0x39df(%rip), %rsi      # 0x623e
leaq 0x3d45(%rip), %rcx      # 0x65ab
movl $0x3b1, %edx            # imm = 0x3B1
callq 0x1110
leaq 0x3d2a(%rip), %rdi      # 0x65a1
leaq 0x39c0(%rip), %rsi      # 0x623e
leaq 0x3d26(%rip), %rcx      # 0x65ab
movl $0x3b4, %edx            # imm = 0x3B4
callq 0x1110
leaq 0x3d0b(%rip), %rdi      # 0x65a1
leaq 0x39a1(%rip), %rsi      # 0x623e
leaq 0x3d07(%rip), %rcx      # 0x65ab
movl $0x3b7, %edx            # imm = 0x3B7
callq 0x1110
leaq 0x3cec(%rip), %rdi      # 0x65a1
leaq 0x3982(%rip), %rsi      # 0x623e
leaq 0x3ce8(%rip), %rcx      # 0x65ab
movl $0x3ba, %edx            # imm = 0x3BA
callq 0x1110
leaq 0x3ccd(%rip), %rdi      # 0x65a1
leaq 0x3963(%rip), %rsi      # 0x623e
leaq 0x3cc9(%rip), %rcx      # 0x65ab
movl $0x3bd, %edx            # imm = 0x3BD
callq 0x1110
leaq 0x3cae(%rip), %rdi      # 0x65a1
leaq 0x3944(%rip), %rsi      # 0x623e
leaq 0x3caa(%rip), %rcx      # 0x65ab
movl $0x3c0, %edx            # imm = 0x3C0
callq 0x1110
leaq 0x3c8f(%rip), %rdi      # 0x65a1
leaq 0x3925(%rip), %rsi      # 0x623e
leaq 0x3c8b(%rip), %rcx      # 0x65ab
movl $0x3c3, %edx            # imm = 0x3C3
callq 0x1110
leaq 0x3c70(%rip), %rdi      # 0x65a1
leaq 0x3906(%rip), %rsi      # 0x623e
leaq 0x3c6c(%rip), %rcx      # 0x65ab
movl $0x3c6, %edx            # imm = 0x3C6
callq 0x1110