// File: /workspace/github_c_makefile_star2_O2/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, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
testq %rsi, %rsi
je 0x26a2
movq %rdi, %r13
movq %rcx, 0x30(%rsp)
testq %rdi, %rdi
je 0x26c1
leaq 0x494c(%rip), %rsi      # 0x6d20
leaq 0x58(%rsp), %rdi
pushq $0x1
popq %rdx
callq 0x1140
testl %eax, %eax
jne 0x26e0
leaq 0x98(%rsp), %rdi
leaq 0x4948(%rip), %rsi      # 0x6d40
pushq $0x1
popq %rdx
callq 0x1140
testl %eax, %eax
jne 0x26ff
leaq 0xd8(%rsp), %rdi
leaq 0x4959(%rip), %rsi      # 0x6d70
pushq $0x1
popq %rdx
callq 0x1140
testl %eax, %eax
jne 0x271e
leaq 0x118(%rsp), %rdi
leaq 0x497a(%rip), %rsi      # 0x6db0
pushq $0x1
popq %rdx
callq 0x1140
testl %eax, %eax
jne 0x273d
leaq 0x158(%rsp), %rdi
leaq 0x497b(%rip), %rsi      # 0x6dd0
pushq $0x1
popq %rdx
callq 0x1140
testl %eax, %eax
jne 0x275c
leaq 0x198(%rsp), %rdi
leaq 0x497c(%rip), %rsi      # 0x6df0
pushq $0x1
popq %rdx
callq 0x1140
testl %eax, %eax
jne 0x277b
leaq 0x1d8(%rsp), %rdi
leaq 0x498d(%rip), %rsi      # 0x6e20
pushq $0x1
popq %rdx
callq 0x1140
testl %eax, %eax
jne 0x279a
leaq 0x218(%rsp), %rdi
leaq 0x49ae(%rip), %rsi      # 0x6e60
pushq $0x1
popq %rdx
callq 0x1140
testl %eax, %eax
jne 0x27b9
xorl %r12d, %r12d
movl $0x2401, %ecx           # imm = 0x2401
xorl %r15d, %r15d
xorl %ebx, %ebx
movq %r13, 0x38(%rsp)
cmpq 0x48(%rsp), %r12
jae 0x260c
movq %r15, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq 0x40(%rsp), %rax
movq %r12, 0x50(%rsp)
addq %rax, %r12
xorl %ebx, %ebx
movzbl (%r12,%rbx), %eax
incq %rbx
cmpl $0xd, %eax
ja 0x24f8
btl %eax, %ecx
jae 0x24f8
leaq 0x1(%rbx), %rsi
pushq $0x1
popq %rdi
callq 0x1160
movq %rax, 0x18(%rsp)
movq %rax, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1050
andq $0x0, 0x28(%rsp)
movq %r13, %rdi
movq %r12, %rsi
leaq 0x28(%rsp), %rdx
leaq 0x58(%rsp), %rcx
callq 0x27d8
movq 0x28(%rsp), %rbp
testq %rbp, %rbp
jne 0x2576
andq $0x0, 0x20(%rsp)
movq %r13, %rdi
movq %r12, %rsi
leaq 0x20(%rsp), %rdx
leaq 0x58(%rsp), %r12
movq %r12, %rcx
callq 0x2bac
movq 0x20(%rsp), %rbp
testq %rbp, %rbp
je 0x25eb
movq %rax, %r14
movq %rdx, %r15
movq 0x10(%rsp), %r12
movq 0x8(%rsp), %r13
leaq (%r14,%r13), %rsi
incq %rsi
movq %r12, %rdi
callq 0x11d0
testq %rax, %rax
je 0x2683
movq %rax, %rbx
movq %r14, %rdx
addq %r13, %r14
movb $0x0, (%rax,%r14)
movq %rax, %rdi
addq %r13, %rdi
movq %r15, %rsi
callq 0x1190
movq 0x18(%rsp), %rdi
cmpq %rdi, %r15
je 0x25c9
callq 0x1030
movq 0x50(%rsp), %r12
addq %rbp, %r12
movq %r15, %rdi
callq 0x1030
movq %r14, %r15
movq 0x38(%rsp), %r13
movl $0x2401, %ecx           # imm = 0x2401
jmp 0x24d4
movq %r13, %rdi
movq %rbx, %rsi
movq 0x18(%rsp), %rdx
movq %r12, %rcx
callq 0x32c8
movq %rax, %r14
movq %rdx, %r15
movq %rbx, %rbp
jmp 0x257c
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 0x30(%rsp), %rax
testq %rax, %rax
je 0x268d
decq %r15
movq %r15, (%rax)
jmp 0x268d
movq %r12, %rdi
callq 0x1030
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3e93(%rip), %rdi      # 0x653c
leaq 0x3b8e(%rip), %rsi      # 0x623e
leaq 0x3e97(%rip), %rcx      # 0x654e
movl $0x3d5, %edx            # imm = 0x3D5
callq 0x1110
leaq 0x3b68(%rip), %rdi      # 0x6230
leaq 0x3b6f(%rip), %rsi      # 0x623e
leaq 0x3e78(%rip), %rcx      # 0x654e
movl $0x3d6, %edx            # imm = 0x3D6
callq 0x1110
leaq 0x3eba(%rip), %rdi      # 0x65a1
leaq 0x3b50(%rip), %rsi      # 0x623e
leaq 0x3eb6(%rip), %rcx      # 0x65ab
movl $0x3b1, %edx            # imm = 0x3B1
callq 0x1110
leaq 0x3e9b(%rip), %rdi      # 0x65a1
leaq 0x3b31(%rip), %rsi      # 0x623e
leaq 0x3e97(%rip), %rcx      # 0x65ab
movl $0x3b4, %edx            # imm = 0x3B4
callq 0x1110
leaq 0x3e7c(%rip), %rdi      # 0x65a1
leaq 0x3b12(%rip), %rsi      # 0x623e
leaq 0x3e78(%rip), %rcx      # 0x65ab
movl $0x3b7, %edx            # imm = 0x3B7
callq 0x1110
leaq 0x3e5d(%rip), %rdi      # 0x65a1
leaq 0x3af3(%rip), %rsi      # 0x623e
leaq 0x3e59(%rip), %rcx      # 0x65ab
movl $0x3ba, %edx            # imm = 0x3BA
callq 0x1110
leaq 0x3e3e(%rip), %rdi      # 0x65a1
leaq 0x3ad4(%rip), %rsi      # 0x623e
leaq 0x3e3a(%rip), %rcx      # 0x65ab
movl $0x3bd, %edx            # imm = 0x3BD
callq 0x1110
leaq 0x3e1f(%rip), %rdi      # 0x65a1
leaq 0x3ab5(%rip), %rsi      # 0x623e
leaq 0x3e1b(%rip), %rcx      # 0x65ab
movl $0x3c0, %edx            # imm = 0x3C0
callq 0x1110
leaq 0x3e00(%rip), %rdi      # 0x65a1
leaq 0x3a96(%rip), %rsi      # 0x623e
leaq 0x3dfc(%rip), %rcx      # 0x65ab
movl $0x3c3, %edx            # imm = 0x3C3
callq 0x1110
leaq 0x3de1(%rip), %rdi      # 0x65a1
leaq 0x3a77(%rip), %rsi      # 0x623e
leaq 0x3ddd(%rip), %rcx      # 0x65ab
movl $0x3c6, %edx            # imm = 0x3C6
callq 0x1110