// File: /workspace/github_c_makefile_star2_O2/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, %rbx
movq %rsi, %r14
movq %rdi, %r15
andq $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
pushq $0x4
popq %rdx
movq %rax, %rcx
callq 0x354a
xorl %edx, %edx
movl $0x0, %r12d
testl %eax, %eax
jne 0x2aa2
cmpl $-0x1, -0xc0(%rbp)
je 0x2af3
cmpl $-0x1, -0xbc(%rbp)
je 0x2af3
movq %r14, -0x40(%rbp)
cmpl $-0x1, -0xd8(%rbp)
je 0x2b12
cmpl $-0x1, -0xd4(%rbp)
je 0x2b12
movq %r15, -0x58(%rbp)
movl -0xa8(%rbp), %r12d
cmpl $-0x1, %r12d
je 0x2b31
movl -0xa4(%rbp), %r15d
cmpl $-0x1, %r15d
je 0x2b31
movq -0xd0(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0xb8(%rbp), %r13
movq -0xb0(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0xa0(%rbp), %r14
movq -0x98(%rbp), %rdi
leaq 0x3e06(%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 %r14, %rcx
movq %rcx, -0x50(%rbp)
movq %rsp, %r14
movslq %r15d, %rax
addq $0xb, %rax
movq %rax, (%rbx)
movq %rsp, %rbx
leaq 0x10(%r13), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movb $0x0, (%r13,%rbx)
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movq %r13, %rdx
callq 0x1050
pushq $0x1
popq %rdx
movq -0x58(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x35f0
testq %rax, %rax
je 0x2b50
movq %rax, %rbx
movq %r14, -0x78(%rbp)
cmpl $0x0, 0x20(%rax)
je 0x297e
leaq 0x42cf(%rip), %rdi      # 0x6c2c
callq 0x1070
leaq 0x3d80(%rip), %rdi      # 0x66e9
movq -0x40(%rbp), %rsi
xorl %eax, %eax
callq 0x10e0
cmpl $0x0, 0x20(%rbx)
jne 0x2b8d
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x1150
movq %rax, -0x48(%rbp)
movq 0x28(%rbx), %rbx
movq -0x80(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x1150
movq %rax, -0x30(%rbp)
xorl %r15d, %r15d
movq $0x0, -0x38(%rbp)
xorl %r12d, %r12d
movq %rbx, -0x88(%rbp)
movl %r15d, %eax
cmpq %rax, 0x10(%rbx)
jbe 0x2a88
movq %rbx, %rdi
movl %r15d, %esi
callq 0x44dc
movq %rax, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
callq 0x1cb0
movq %rax, %r14
movq %r13, 0x10(%rax)
movl 0x20(%rbx), %eax
cmpl $0x2, %eax
je 0x2a0d
cmpl $0x1, %eax
jne 0x2ab4
addq $0x28, %rbx
movq %r14, %rdi
movq -0x30(%rbp), %rsi
movq %rbx, %rdx
callq 0x1e9f
jmp 0x2a1d
movq 0x28(%rbx), %rdx
movq %r14, %rdi
movq -0x30(%rbp), %rsi
callq 0x1d46
movq %r14, %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
leaq -0x70(%rbp), %rcx
callq 0x2398
movq %rax, -0x68(%rbp)
movq %r14, %rdi
callq 0x233e
movq -0x70(%rbp), %rbx
leaq (%rbx,%r12), %r14
movq -0x38(%rbp), %rdi
movq %r14, %rsi
callq 0x11d0
movq %rax, -0x38(%rbp)
addq %rax, %r12
movq -0x68(%rbp), %r13
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x1190
movq %r13, %rdi
callq 0x1030
incl %r15d
movq %r14, %r12
movq -0x58(%rbp), %r13
movq -0x88(%rbp), %rbx
jmp 0x29b9
movq -0x30(%rbp), %rdi
callq 0x1030
movq -0x48(%rbp), %rdi
callq 0x1030
movq -0x78(%rbp), %rsp
movq -0x38(%rbp), %rdx
movq %r12, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3c5f(%rip), %rdi      # 0x671a
xorl %eax, %eax
callq 0x10e0
leaq 0x3c20(%rip), %rdi      # 0x66e9
movq -0x40(%rbp), %rsi
xorl %eax, %eax
callq 0x10e0
leaq 0x3c6b(%rip), %rdi      # 0x6746
leaq 0x375c(%rip), %rsi      # 0x623e
leaq 0x3b26(%rip), %rcx      # 0x660f
movl $0x297, %edx            # imm = 0x297
callq 0x1110
leaq 0x3ade(%rip), %rdi      # 0x65d8
leaq 0x373d(%rip), %rsi      # 0x623e
leaq 0x3b07(%rip), %rcx      # 0x660f
movl $0x263, %edx            # imm = 0x263
callq 0x1110
leaq 0x3b5e(%rip), %rdi      # 0x6677
leaq 0x371e(%rip), %rsi      # 0x623e
leaq 0x3ae8(%rip), %rcx      # 0x660f
movl $0x264, %edx            # imm = 0x264
callq 0x1110
leaq 0x3b76(%rip), %rdi      # 0x66ae
leaq 0x36ff(%rip), %rsi      # 0x623e
leaq 0x3ac9(%rip), %rcx      # 0x660f
movl $0x265, %edx            # imm = 0x265
callq 0x1110
leaq 0x40d5(%rip), %rdi      # 0x6c2c
callq 0x1070
leaq 0x3b86(%rip), %rdi      # 0x66e9
movq -0x40(%rbp), %rsi
xorl %eax, %eax
callq 0x10e0
leaq 0x3b7e(%rip), %rdi      # 0x66f3
leaq 0x36c2(%rip), %rsi      # 0x623e
leaq 0x3a8c(%rip), %rcx      # 0x660f
movl $0x27b, %edx            # imm = 0x27B
callq 0x1110
leaq 0x3b6d(%rip), %rdi      # 0x6701
leaq 0x36a3(%rip), %rsi      # 0x623e
leaq 0x3a6d(%rip), %rcx      # 0x660f
movl $0x27c, %edx            # imm = 0x27C
callq 0x1110