// File: /workspace/github_c_makefile_star2_O1/qpfiffer[P]38-Moths/src/greshunkel.c
// Function: interpolate_loop
static line
_interpolate_loop(const greshunkel_ctext *ctext, const char *buf, size_t *num_read, const struct compiled_regex *all_regex) {
	line to_return = {0};
	*num_read = 0;

	match_t match[4] = {{0}};
	/* TODO: Support loops inside of loops. That probably means a
	 * while loop here. */
	if (regexec_2_0_beta(&all_regex->c_loop_regex, buf, 4, match) == 0) {
		/* Variables we're going to need: */
		const match_t loop_variable = match[1];
		const match_t variable_name = match[2];
		match_t loop_meat = match[3];
		/* Make sure they were matched: */
		assert(variable_name.rm_so != -1 && variable_name.rm_eo != -1);
		assert(loop_variable.rm_so != -1 && loop_variable.rm_eo != -1);
		assert(loop_meat.rm_so != -1 && loop_meat.rm_eo != -1);

		size_t possible_dif = 0;
		const char *closest_BBL = NULL;
		closest_BBL = strstr(loop_meat.start, "xXx BBL xXx");
		possible_dif = closest_BBL - buf;
		if (possible_dif != (unsigned int)loop_meat.rm_so) {
			loop_meat.rm_eo = possible_dif;
			loop_meat.len = loop_meat.rm_eo - loop_meat.rm_so;
		}

		/* We found a fucking loop, holy shit */
		*num_read = loop_meat.rm_eo + strlen("xXx BBL xXx");

		const greshunkel_tuple *tuple;
		char just_match_str[variable_name.len + 1];
		just_match_str[variable_name.len] = '\0';
		strncpy(just_match_str, variable_name.start, variable_name.len);

		if (!((tuple = find_needle(ctext, just_match_str, 1)) && tuple->type == GSHKL_ARR)) {
			printf("Did not match a variable that needed to be matched.\n");
			printf("Line: %s\n", buf);
			assert(tuple != NULL);
			assert(tuple->type == GSHKL_ARR);
		}

		line to_render_line;
		to_render_line.data = strndup(loop_meat.start, loop_meat.len);
		to_render_line.size = loop_meat.len;

		vector *cur_vector_p = tuple->value.arr;

		/* This is the thing we're going to render over and over and over again. */
		char *loop_variable_name_rendered = strndup(loop_variable.start, loop_variable.len);

		/* Now we loop through the array incredulously. */
		unsigned int j;
		for (j = 0; j < cur_vector_p->count; j++) {
			const greshunkel_tuple *current_loop_var = vector_get(cur_vector_p, j);
			line rendered_piece = {0};

			/* Recurse contexts until my fucking mind melts. */
			greshunkel_ctext *_temp_ctext = _gshkl_init_child_context(ctext);
			if (current_loop_var->type == GSHKL_STR) {
				gshkl_add_string(_temp_ctext, loop_variable_name_rendered, current_loop_var->value.str);
			} else if (current_loop_var->type == GSHKL_SUBCTEXT) {
				gshkl_add_sub_context(_temp_ctext, loop_variable_name_rendered, current_loop_var->value.sub_ctext);
			} else {
				printf("Weird type given during loop interpolation.");
				printf("Line: %s\n", buf);
				assert(1 == 0);
			}

			rendered_piece.data = gshkl_render(_temp_ctext, to_render_line.data, to_render_line.size, &rendered_piece.size);
			gshkl_free_context(_temp_ctext);

			const size_t old_size = to_return.size;
			to_return.size += rendered_piece.size;
			to_return.data = realloc(to_return.data, to_return.size);
			memcpy(to_return.data + old_size, rendered_piece.data, rendered_piece.size);
			free(rendered_piece.data);
		}

		free(loop_variable_name_rendered);
		free(to_render_line.data);
	}

	return to_return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
movq $0x0, (%rdx)
xorps %xmm0, %xmm0
leaq -0xf0(%rbp), %rax
movaps %xmm0, 0x50(%rax)
movaps %xmm0, 0x40(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
leaq 0x80(%rcx), %rdi
movl $0x4, %edx
movq %rax, %rcx
callq 0x3b54
xorl %edx, %edx
movl $0x0, %ebx
testl %eax, %eax
jne 0x2c34
cmpl $-0x1, -0xc0(%rbp)
je 0x2c85
cmpl $-0x1, -0xbc(%rbp)
je 0x2c85
movq %r15, -0x40(%rbp)
cmpl $-0x1, -0xd8(%rbp)
je 0x2ca4
cmpl $-0x1, -0xd4(%rbp)
je 0x2ca4
movq %r12, -0x58(%rbp)
movl -0xa8(%rbp), %r12d
cmpl $-0x1, %r12d
je 0x2cc3
movl -0xa4(%rbp), %r15d
cmpl $-0x1, %r15d
je 0x2cc3
movq -0xd0(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0xb8(%rbp), %r13
movq -0xb0(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0xa0(%rbp), %rbx
movq -0x98(%rbp), %rdi
leaq 0x3c86(%rip), %rsi      # 0x66dd
movq %rdi, -0x48(%rbp)
callq 0x1200
subq -0x40(%rbp), %rax
movl %eax, %ecx
subl %r12d, %ecx
cmpq %r12, %rax
movslq %ecx, %rcx
cmovnel %eax, %r15d
cmoveq %rbx, %rcx
movq %rcx, -0x50(%rbp)
movq %rsp, %r12
movslq %r15d, %rax
addq $0xb, %rax
movq %rax, (%r14)
movq %rsp, %r14
leaq 0x10(%r13), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movb $0x0, (%r13,%r14)
movq %r14, %rdi
movq -0x30(%rbp), %rsi
movq %r13, %rdx
callq 0x1050
movq -0x58(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x3c01
testq %rax, %rax
je 0x2ce2
movq %rax, %rbx
cmpl $0x0, 0x20(%rax)
je 0x2afc
leaq 0x4151(%rip), %rdi      # 0x6c2c
callq 0x1070
leaq 0x3c02(%rip), %rdi      # 0x66e9
movq -0x40(%rbp), %rsi
xorl %eax, %eax
callq 0x10e0
cmpl $0x0, 0x20(%rbx)
jne 0x2d1f
movq %r12, -0x78(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x1150
movq %rax, -0x48(%rbp)
movq 0x28(%rbx), %r14
movq -0x80(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x1150
movq %rax, -0x38(%rbp)
xorl %r15d, %r15d
cmpq $0x0, 0x10(%r14)
je 0x2c10
xorl %r12d, %r12d
movq $0x0, -0x30(%rbp)
movq %r14, -0x88(%rbp)
movq %r14, %rdi
movl %r15d, %esi
callq 0x4baf
movq %rax, %r14
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
callq 0x1d48
movq %rax, %rbx
movq %r13, 0x10(%rax)
movl 0x20(%r14), %eax
cmpl $0x2, %eax
je 0x2b8e
cmpl $0x1, %eax
jne 0x2c46
addq $0x28, %r14
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movq %r14, %rdx
callq 0x1f63
jmp 0x2b9e
movq 0x28(%r14), %rdx
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
callq 0x1df6
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
leaq -0x70(%rbp), %rcx
callq 0x24d1
movq %rax, -0x68(%rbp)
movq %rbx, %rdi
callq 0x246c
movq -0x70(%rbp), %r14
leaq (%r14,%r12), %rbx
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
callq 0x11d0
movq %rax, -0x30(%rbp)
addq %rax, %r12
movq -0x68(%rbp), %r13
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x1190
movq %r13, %rdi
callq 0x1030
incl %r15d
movq %rbx, %r12
movq -0x88(%rbp), %r14
cmpq %r15, 0x10(%r14)
movq -0x58(%rbp), %r13
ja 0x2b46
jmp 0x2c1a
movq $0x0, -0x30(%rbp)
xorl %ebx, %ebx
movq -0x38(%rbp), %rdi
callq 0x1030
movq -0x48(%rbp), %rdi
callq 0x1030
movq -0x78(%rbp), %rsp
movq -0x30(%rbp), %rdx
movq %rbx, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3acd(%rip), %rdi      # 0x671a
xorl %eax, %eax
callq 0x10e0
leaq 0x3a8e(%rip), %rdi      # 0x66e9
movq -0x40(%rbp), %rsi
xorl %eax, %eax
callq 0x10e0
leaq 0x3ad9(%rip), %rdi      # 0x6746
leaq 0x35ca(%rip), %rsi      # 0x623e
leaq 0x3994(%rip), %rcx      # 0x660f
movl $0x297, %edx            # imm = 0x297
callq 0x1110
leaq 0x394c(%rip), %rdi      # 0x65d8
leaq 0x35ab(%rip), %rsi      # 0x623e
leaq 0x3975(%rip), %rcx      # 0x660f
movl $0x263, %edx            # imm = 0x263
callq 0x1110
leaq 0x39cc(%rip), %rdi      # 0x6677
leaq 0x358c(%rip), %rsi      # 0x623e
leaq 0x3956(%rip), %rcx      # 0x660f
movl $0x264, %edx            # imm = 0x264
callq 0x1110
leaq 0x39e4(%rip), %rdi      # 0x66ae
leaq 0x356d(%rip), %rsi      # 0x623e
leaq 0x3937(%rip), %rcx      # 0x660f
movl $0x265, %edx            # imm = 0x265
callq 0x1110
leaq 0x3f43(%rip), %rdi      # 0x6c2c
callq 0x1070
leaq 0x39f4(%rip), %rdi      # 0x66e9
movq -0x40(%rbp), %rsi
xorl %eax, %eax
callq 0x10e0
leaq 0x39ec(%rip), %rdi      # 0x66f3
leaq 0x3530(%rip), %rsi      # 0x623e
leaq 0x38fa(%rip), %rcx      # 0x660f
movl $0x27b, %edx            # imm = 0x27B
callq 0x1110
leaq 0x39db(%rip), %rdi      # 0x6701
leaq 0x3511(%rip), %rsi      # 0x623e
leaq 0x38db(%rip), %rcx      # 0x660f
movl $0x27c, %edx            # imm = 0x27C
callq 0x1110