// File: /workspace/github_c_makefile_star2_O0/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
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
leaq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1110
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
movl $0x60, %edx
callq 0x1110
movq -0x30(%rbp), %rdi
addq $0x80, %rdi
movq -0x20(%rbp), %rsi
leaq -0x90(%rbp), %rcx
movl $0x4, %edx
callq 0x4610
cmpl $0x0, %eax
jne 0x3552
movq -0x78(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpl $-0x1, -0xc0(%rbp)
je 0x3139
cmpl $-0x1, -0xbc(%rbp)
je 0x3139
jmp 0x3158
leaq 0x54cb(%rip), %rdi      # 0x860b
leaq 0x50e6(%rip), %rsi      # 0x822d
movl $0x263, %edx            # imm = 0x263
leaq 0x54ef(%rip), %rcx      # 0x8642
callq 0x1100
cmpl $-0x1, -0xa8(%rbp)
je 0x316c
cmpl $-0x1, -0xa4(%rbp)
je 0x316c
jmp 0x318b
leaq 0x5537(%rip), %rdi      # 0x86aa
leaq 0x50b3(%rip), %rsi      # 0x822d
movl $0x264, %edx            # imm = 0x264
leaq 0x54bc(%rip), %rcx      # 0x8642
callq 0x1100
cmpl $-0x1, -0xd8(%rbp)
je 0x319f
cmpl $-0x1, -0xd4(%rbp)
je 0x319f
jmp 0x31be
leaq 0x553b(%rip), %rdi      # 0x86e1
leaq 0x5080(%rip), %rsi      # 0x822d
movl $0x265, %edx            # imm = 0x265
leaq 0x5489(%rip), %rcx      # 0x8642
callq 0x1100
movq $0x0, -0xe0(%rbp)
movq $0x0, -0xe8(%rbp)
movq -0xc8(%rbp), %rdi
leaq 0x552e(%rip), %rsi      # 0x8710
callq 0x11f0
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movl -0xd8(%rbp), %ecx
cmpq %rcx, %rax
je 0x3237
movq -0xe0(%rbp), %rax
movl %eax, -0xd4(%rbp)
movl -0xd4(%rbp), %eax
subl -0xd8(%rbp), %eax
cltq
movq %rax, -0xd0(%rbp)
movslq -0xd4(%rbp), %rcx
addq $0xb, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0xb8(%rbp), %rcx
movq %rcx, %rax
incq %rax
movq %rsp, %rdx
movq %rdx, -0xf8(%rbp)
addq $0x10, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x158(%rbp)
movq %rdi, %rsp
movq %rax, -0x100(%rbp)
movq -0xb8(%rbp), %rax
movb $0x0, (%rdi,%rax)
movq -0xb0(%rbp), %rsi
movq -0xb8(%rbp), %rdx
callq 0x1050
movq -0x158(%rbp), %rsi
movq -0x18(%rbp), %rdi
movl $0x1, %edx
callq 0x4740
movq %rax, -0xf0(%rbp)
cmpq $0x0, %rax
je 0x32cc
movq -0xf0(%rbp), %rax
cmpl $0x0, 0x20(%rax)
je 0x3347
leaq 0x5449(%rip), %rdi      # 0x871c
movb $0x0, %al
callq 0x10d0
movq -0x20(%rbp), %rsi
leaq 0x546c(%rip), %rdi      # 0x8751
movb $0x0, %al
callq 0x10d0
cmpq $0x0, -0xf0(%rbp)
je 0x32f8
jmp 0x3317
leaq 0x545c(%rip), %rdi      # 0x875b
leaq 0x4f27(%rip), %rsi      # 0x822d
movl $0x27b, %edx            # imm = 0x27B
leaq 0x5330(%rip), %rcx      # 0x8642
callq 0x1100
movq -0xf0(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jne 0x3326
jmp 0x3345
leaq 0x543c(%rip), %rdi      # 0x8769
leaq 0x4ef9(%rip), %rsi      # 0x822d
movl $0x27c, %edx            # imm = 0x27C
leaq 0x5302(%rip), %rcx      # 0x8642
callq 0x1100
jmp 0x3347
movq -0xc8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0x1140
movq %rax, -0x108(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0xf0(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x118(%rbp)
movq -0x98(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0x1140
movq %rax, -0x120(%rbp)
movl $0x0, -0x124(%rbp)
movl -0x124(%rbp), %eax
movq -0x118(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x3530
movq -0x118(%rbp), %rdi
movl -0x124(%rbp), %esi
callq 0x60b0
movq %rax, -0x130(%rbp)
leaq -0x140(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1110
movq -0x18(%rbp), %rdi
callq 0x4880
movq %rax, -0x148(%rbp)
movq -0x130(%rbp), %rax
cmpl $0x1, 0x20(%rax)
jne 0x3425
movq -0x148(%rbp), %rdi
movq -0x120(%rbp), %rsi
movq -0x130(%rbp), %rdx
addq $0x28, %rdx
callq 0x2390
jmp 0x3493
movq -0x130(%rbp), %rax
cmpl $0x2, 0x20(%rax)
jne 0x3452
movq -0x148(%rbp), %rdi
movq -0x120(%rbp), %rsi
movq -0x130(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x2180
jmp 0x3491
leaq 0x5329(%rip), %rdi      # 0x8782
movb $0x0, %al
callq 0x10d0
movq -0x20(%rbp), %rsi
leaq 0x52e6(%rip), %rdi      # 0x8751
movb $0x0, %al
callq 0x10d0
leaq 0x5335(%rip), %rdi      # 0x87ae
leaq 0x4dad(%rip), %rsi      # 0x822d
movl $0x297, %edx            # imm = 0x297
leaq 0x51b6(%rip), %rcx      # 0x8642
callq 0x1100
jmp 0x3493
movq -0x148(%rbp), %rdi
movq -0x108(%rbp), %rsi
movq -0x110(%rbp), %rdx
leaq -0x140(%rbp), %rcx
callq 0x2a60
movq %rax, -0x138(%rbp)
movq -0x148(%rbp), %rdi
callq 0x2970
movq -0x10(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x140(%rbp), %rax
addq -0x10(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x11c0
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rdi
addq -0x150(%rbp), %rdi
movq -0x138(%rbp), %rsi
movq -0x140(%rbp), %rdx
callq 0x1180
movq -0x138(%rbp), %rdi
callq 0x1030
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
jmp 0x33a5
movq -0x120(%rbp), %rdi
callq 0x1030
movq -0x108(%rbp), %rdi
callq 0x1030
movq -0xf8(%rbp), %rax
movq %rax, %rsp
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rdx
movq %rbp, %rsp
popq %rbp
retq
nop