// File: /workspace/github_c_makefile_star2_O2/qpfiffer[P]38-Moths/src/greshunkel.c
// Function: interpolate_conditionals
static line
_interpolate_conditionals(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}};
	if (regexec_2_0_beta(&all_regex->c_conditional_inverse_regex, buf, 3, match) == 0) {
		const match_t conditional_variable = match[1];
		match_t conditional_meat = match[2];
		/* Make sure they were matched: */
		assert(conditional_variable.rm_so != -1 && conditional_variable.rm_eo != -1);
		assert(conditional_meat.rm_so != -1 && conditional_meat.rm_eo != -1);

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

		/* We found a conditioanl. */
		*num_read = conditional_meat.rm_eo + strlen("xXx ENDLESS xXx");

		char just_match_str[conditional_variable.len + 1];
		just_match_str[conditional_variable.len] = '\0';
		strncpy(just_match_str, conditional_variable.start, conditional_variable.len);

		/* TODO: Support a boolean type. */
		/*if (!((tuple = find_needle(ctext, just_match_str, 1)) && tuple->type == GSHKL_BOOL)) { */
		const greshunkel_tuple *tuple = NULL;
		char *sub_ctext_match = NULL;
		int should_render = 0;
		if ((tuple = find_needle(ctext, just_match_str, 1))) {
			if (_is_truthy(tuple)) {
				should_render = 1;
			}
		} else if ((sub_ctext_match = strchr(just_match_str, '.')) != NULL) {
			/* TODO: Check sub-context. */
			char sub_context_name[MAX_GSHKL_STR_SIZE] = {0};
			char sub_item_name[MAX_GSHKL_STR_SIZE] = {0};
			memcpy(sub_context_name, just_match_str, sub_ctext_match - just_match_str);
			memcpy(sub_item_name, sub_ctext_match + strlen("."), conditional_variable.len - strlen(sub_context_name) + strlen("."));

			if ((tuple = find_needle(ctext, sub_context_name, 1)) && tuple->type == GSHKL_SUBCTEXT) {
				const greshunkel_ctext *sub_ctext = tuple->value.sub_ctext;

				unsigned int i;
				for (i = 0; i < sub_ctext->values->count; i++) {
					const greshunkel_tuple *sub_tuple = vector_get(sub_ctext->values, i);
					const char *_name = sub_tuple->name;
					const size_t larger = strlen(_name) > strlen(sub_item_name) ?
							strlen(_name) : strlen(sub_item_name);

					if (strncmp(_name, sub_item_name, larger) == 0 && _is_falsey(sub_tuple)) {
							should_render = 1;
					}
				}
			}
		}

		if (should_render) {
			line to_render_line;
			to_render_line.data = strndup(conditional_meat.start, conditional_meat.len);
			to_render_line.size = conditional_meat.len;

			line rendered_piece = {0};

			/* Recurse contexts until my fucking mind melts. */
			greshunkel_ctext *_temp_ctext = _gshkl_init_child_context(ctext);
			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;
			if (old_size <= 0) {
				to_return.data = calloc(1, to_return.size);
			} else {
				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(to_render_line.data);
		}
	} else if (regexec_2_0_beta(&all_regex->c_conditional_regex, buf, 3, match) == 0) {
		const match_t conditional_variable = match[1];
		match_t conditional_meat = match[2];

		assert(conditional_variable.rm_so != -1 && conditional_variable.rm_eo != -1);
		assert(conditional_meat.rm_so != -1 && conditional_meat.rm_eo != -1);

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

		/* We found a conditioanl. */
		*num_read = conditional_meat.rm_eo + strlen("xXx ENDLESS xXx");

		char just_match_str[conditional_variable.len + 1];
		just_match_str[conditional_variable.len] = '\0';
		strncpy(just_match_str, conditional_variable.start, conditional_variable.len);

		/* TODO: Support a boolean type. */
		/*if (!((tuple = find_needle(ctext, just_match_str, 1)) && tuple->type == GSHKL_BOOL)) */
		const greshunkel_tuple *tuple = NULL;
		char *sub_ctext_match = NULL;
		int should_render = 0;
		if (!(tuple = find_needle(ctext, just_match_str, 1))) {
			if ((sub_ctext_match = strchr(just_match_str, '.')) != NULL) {
				/* TODO: Check sub-context. */
				char sub_context_name[MAX_GSHKL_STR_SIZE] = {0};
				char sub_item_name[MAX_GSHKL_STR_SIZE] = {0};
				memcpy(sub_context_name, just_match_str, sub_ctext_match - just_match_str);
				memcpy(sub_item_name, sub_ctext_match + strlen("."), conditional_variable.len - strlen(sub_context_name) + strlen("."));

				if ((tuple = find_needle(ctext, sub_context_name, 1)) && tuple->type == GSHKL_SUBCTEXT) {
					const greshunkel_ctext *sub_ctext = tuple->value.sub_ctext;

					unsigned int i;
					for (i = 0; i < sub_ctext->values->count; i++) {
						const greshunkel_tuple *sub_tuple = vector_get(sub_ctext->values, i);
						const char *_name = sub_tuple->name;
						const size_t larger = strlen(_name) > strlen(sub_item_name) ?
								strlen(_name) : strlen(sub_item_name);

						if (strncmp(_name, sub_item_name, larger) == 0 &&
							_is_truthy(sub_tuple)) {
								should_render = 1;
						}
					}
				}
			} else {
				should_render = 1;
			}
		} else {
			if (_is_falsey(tuple)) {
				should_render = 1;
			} else if ((sub_ctext_match = strchr(just_match_str, '.')) != NULL) {
				/* TODO: Check sub-context. */
				char sub_context_name[MAX_GSHKL_STR_SIZE] = {0};
				char sub_item_name[MAX_GSHKL_STR_SIZE] = {0};
				memcpy(sub_context_name, just_match_str, sub_ctext_match - just_match_str);
				memcpy(sub_item_name, sub_ctext_match + strlen("."), conditional_variable.len - strlen(sub_context_name) + strlen("."));

				if ((tuple = find_needle(ctext, sub_context_name, 1)) && tuple->type == GSHKL_SUBCTEXT) {
					const greshunkel_ctext *sub_ctext = tuple->value.sub_ctext;

					unsigned int i;
					for (i = 0; i < sub_ctext->values->count; i++) {
						const greshunkel_tuple *sub_tuple = vector_get(sub_ctext->values, i);
						const char *_name = sub_tuple->name;
						const size_t larger = strlen(_name) > strlen(sub_item_name) ?
								strlen(_name) : strlen(sub_item_name);

						if (strncmp(_name, sub_item_name, larger) == 0 &&
							_is_truthy(sub_tuple)) {
								should_render = 1;
						}
					}
				}
			}
		}

		if (should_render) {
			line to_render_line;
			to_render_line.data = strndup(conditional_meat.start, conditional_meat.len);
			to_render_line.size = conditional_meat.len;

			/* TODO: This should use gshkl_render. See _interpolate_loop */
			line rendered_piece = _interpolate_line(ctext, to_render_line, all_regex);

			const size_t old_size = to_return.size;
			to_return.size += rendered_piece.size;
			if (old_size <= 0) {
				to_return.data = calloc(1, to_return.size);
			} else {
				to_return.data = realloc(to_return.data, to_return.size);
			}

			memcpy(to_return.data + old_size, rendered_piece.data, rendered_piece.size);
			if (rendered_piece.data != to_render_line.data)
				free(rendered_piece.data);
			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 $0x498, %rsp            # imm = 0x498
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, -0x40(%rbp)
andq $0x0, (%rdx)
xorps %xmm0, %xmm0
leaq -0xc0(%rbp), %rcx
movaps %xmm0, 0x50(%rcx)
movaps %xmm0, 0x40(%rcx)
movaps %xmm0, 0x30(%rcx)
movaps %xmm0, 0x20(%rcx)
movaps %xmm0, 0x10(%rcx)
movaps %xmm0, (%rcx)
leaq 0x1c0(%r14), %rdi
pushq $0x3
popq %rdx
callq 0x354a
testl %eax, %eax
je 0x2e26
leaq 0x180(%r14), %rdi
pushq $0x3
popq %rdx
leaq -0xc0(%rbp), %rcx
movq %r13, %rsi
callq 0x354a
xorl %r15d, %r15d
movl $0x0, %ebx
testl %eax, %eax
jne 0x3237
movq %r14, -0x50(%rbp)
cmpl $-0x1, -0xa8(%rbp)
je 0x328a
cmpl $-0x1, -0xa4(%rbp)
je 0x328a
movl -0x90(%rbp), %ebx
cmpl $-0x1, %ebx
je 0x32a9
movl -0x8c(%rbp), %r14d
cmpl $-0x1, %r14d
je 0x32a9
movq -0xa0(%rbp), %r15
movq -0x98(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x80(%rbp), %rdi
leaq 0x3bff(%rip), %rsi      # 0x6897
movq %rdi, -0x58(%rbp)
callq 0x1200
movq %rsp, -0x38(%rbp)
subq %r13, %rax
movl %eax, %ecx
subl %ebx, %ecx
cmpq %rbx, %rax
movslq %ecx, %rcx
cmovnel %eax, %r14d
cmoveq -0x48(%rbp), %rcx
movq %rcx, -0x48(%rbp)
movslq %r14d, %rax
addq $0xf, %rax
movq %rax, (%r12)
leaq 0x1(%r15), %r12
movq %rsp, %rbx
leaq 0x10(%r15), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movb $0x0, (%r15,%rbx)
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq %r15, %rdx
callq 0x1050
pushq $0x1
popq %rdx
movq -0x40(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x35f0
testq %rax, %rax
je 0x30af
movq %rax, %rdi
callq 0x3737
testl %eax, %eax
jne 0x31b4
pushq $0x2e
popq %rsi
movq %rbx, %rdi
callq 0x10d0
xorl %r15d, %r15d
testq %rax, %rax
je 0x3215
movq %rax, %r14
leaq -0x4c0(%rbp), %r13
movl $0x200, %edx            # imm = 0x200
movq %r13, %rdi
xorl %esi, %esi
callq 0x1120
leaq -0x2c0(%rbp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x1120
movq %r14, %rdx
subq %rbx, %rdx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1190
incq %r14
movq %r13, %rdi
callq 0x10c0
subq %rax, %r12
leaq -0x2c0(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x1190
pushq $0x1
popq %rdx
movq -0x40(%rbp), %rdi
movq %r13, %rsi
callq 0x35f0
testq %rax, %rax
je 0x31ab
cmpl $0x2, 0x20(%rax)
jne 0x31ab
movq 0x28(%rax), %rax
movq %rax, -0x30(%rbp)
xorl %r15d, %r15d
xorl %ebx, %ebx
movl %ebx, %eax
movq -0x30(%rbp), %rcx
movq (%rcx), %rdi
cmpq %rax, 0x10(%rdi)
jbe 0x31ab
movl %ebx, %esi
callq 0x44dc
movq %rax, %r14
movq %rax, %rdi
callq 0x10c0
movq %rax, %r13
leaq -0x2c0(%rbp), %r12
movq %r12, %rdi
callq 0x10c0
cmpq %rax, %r13
cmovbeq %rax, %r13
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x1060
testl %eax, %eax
jne 0x2e22
movq %r14, %rdi
callq 0x36a2
testl %eax, %eax
pushq $0x1
popq %rax
cmovnel %eax, %r15d
incl %ebx
jmp 0x2dc1
cmpl $-0x1, -0xa8(%rbp)
je 0x324c
cmpl $-0x1, -0xa4(%rbp)
je 0x324c
movq %r12, -0x38(%rbp)
movl -0x90(%rbp), %r14d
cmpl $-0x1, %r14d
je 0x326b
movl -0x8c(%rbp), %r15d
cmpl $-0x1, %r15d
je 0x326b
movq -0xa0(%rbp), %r12
movq -0x98(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x88(%rbp), %rbx
movq -0x80(%rbp), %rdi
leaq 0x3a0d(%rip), %rsi      # 0x6897
movq %rdi, -0x48(%rbp)
callq 0x1200
subq %r13, %rax
movl %eax, %ecx
subl %r14d, %ecx
cmpq %r14, %rax
movslq %ecx, %rcx
cmovnel %eax, %r15d
cmoveq %rbx, %rcx
movq %r12, %rbx
movq %rcx, -0x50(%rbp)
movq %rsp, %r13
movslq %r15d, %rax
addq $0xf, %rax
movq -0x38(%rbp), %rcx
movq %rax, (%rcx)
movq %rsp, %r12
leaq 0x10(%rbx), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movb $0x0, (%rbx,%r12)
movq %r12, %rdi
movq -0x30(%rbp), %rsi
movq %rbx, %rdx
callq 0x1050
pushq $0x1
popq %rdx
movq -0x40(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x35f0
testq %rax, %rax
je 0x2f9f
movq %rax, %rdi
callq 0x36a2
testl %eax, %eax
je 0x322f
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %r14
movq %r14, %rsi
callq 0x1150
movq %r15, %r12
movq %rax, %r15
movq %rax, -0x38(%rbp)
leaq -0x4c0(%rbp), %rax
andq $0x0, (%rax)
callq 0x1cb0
movq %rax, %rbx
movq %r12, 0x10(%rax)
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rdx
leaq -0x4c0(%rbp), %r14
movq %r14, %rcx
callq 0x2398
movq %rax, 0x8(%r14)
movq %rbx, %rdi
callq 0x233e
movq (%r14), %rbx
pushq $0x1
popq %rdi
movq %rbx, %rsi
callq 0x1160
movq %rax, %r15
movq 0x8(%r14), %r12
movq %rax, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1190
movq %r12, %rdi
callq 0x1030
movq -0x38(%rbp), %rdi
callq 0x1030
jmp 0x3234
movq %r13, -0x38(%rbp)
pushq $0x2e
popq %rsi
movq %r12, %rdi
callq 0x10d0
testq %rax, %rax
je 0x3212
movq %rax, %r13
incq %rbx
leaq -0x4c0(%rbp), %r14
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
xorl %esi, %esi
callq 0x1120
leaq -0x2c0(%rbp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x1120
movq %r13, %rdx
subq %r12, %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0x1190
incq %r13
movq %r14, %rdi
callq 0x10c0
subq %rax, %rbx
leaq -0x2c0(%rbp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x1190
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x35f0
testq %rax, %rax
je 0x3212
cmpl $0x2, 0x20(%rax)
jne 0x3212
movq 0x28(%rax), %r15
xorl %r12d, %r12d
leaq -0x2c0(%rbp), %r13
movl $0x0, -0x30(%rbp)
movl %r12d, %eax
movq (%r15), %rdi
cmpq %rax, 0x10(%rdi)
jbe 0x321d
movl %r12d, %esi
callq 0x44dc
movq %rax, %r14
movq %rax, %rdi
callq 0x10c0
movq %rax, %rbx
movq %r13, %rdi
callq 0x10c0
cmpq %rax, %rbx
cmovbeq %rax, %rbx
movq %r14, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x1060
testl %eax, %eax
jne 0x30aa
movq %r14, %rdi
callq 0x3737
testl %eax, %eax
pushq $0x1
popq %rax
movl -0x30(%rbp), %ecx
cmovnel %eax, %ecx
movl %ecx, -0x30(%rbp)
incl %r12d
jmp 0x304d
pushq $0x2e
popq %rsi
movq %rbx, %rdi
callq 0x10d0
testq %rax, %rax
je 0x31b4
movq %rax, %r14
leaq -0x4c0(%rbp), %rdi
xorl %r15d, %r15d
movl $0x200, %edx            # imm = 0x200
movq %rdi, %r13
xorl %esi, %esi
callq 0x1120
leaq -0x2c0(%rbp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x1120
movq %r14, %rdx
subq %rbx, %rdx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1190
incq %r14
movq %r13, %rdi
callq 0x10c0
subq %rax, %r12
leaq -0x2c0(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x1190
pushq $0x1
popq %rdx
movq -0x40(%rbp), %rdi
movq %r13, %rsi
callq 0x35f0
testq %rax, %rax
je 0x31ab
cmpl $0x2, 0x20(%rax)
jne 0x31ab
movq 0x28(%rax), %rax
movq %rax, -0x30(%rbp)
xorl %r15d, %r15d
xorl %ebx, %ebx
movl %ebx, %eax
movq -0x30(%rbp), %rcx
movq (%rcx), %rdi
cmpq %rax, 0x10(%rdi)
jbe 0x31ab
movl %ebx, %esi
callq 0x44dc
movq %rax, %r14
movq %rax, %rdi
callq 0x10c0
movq %rax, %r13
leaq -0x2c0(%rbp), %r12
movq %r12, %rdi
callq 0x10c0
cmpq %rax, %r13
cmovbeq %rax, %r13
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x1060
testl %eax, %eax
jne 0x31a7
movq %r14, %rdi
callq 0x36a2
testl %eax, %eax
pushq $0x1
popq %rax
cmovnel %eax, %r15d
incl %ebx
jmp 0x314a
testl %r15d, %r15d
movq -0x40(%rbp), %r13
je 0x3212
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %rbx
movq %rbx, %rsi
callq 0x1150
movq %rax, %r14
movq %r13, %rdi
movq %rbx, %rsi
movq %rax, %rdx
movq -0x50(%rbp), %rcx
callq 0x32c8
movq %rax, %rbx
movq %rdx, %r12
pushq $0x1
popq %rdi
movq %rax, %rsi
callq 0x1160
movq %rax, %r15
movq %rax, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1190
cmpq %r14, %r12
je 0x3208
movq %r12, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
jmp 0x3217
xorl %r15d, %r15d
xorl %ebx, %ebx
movq -0x38(%rbp), %r13
jmp 0x3234
cmpl $0x0, -0x30(%rbp)
movq -0x40(%rbp), %r15
movq -0x38(%rbp), %r13
jne 0x2f11
xorl %r15d, %r15d
xorl %ebx, %ebx
movq %r13, %rsp
movq %rbx, %rax
movq %r15, %rdx
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3552(%rip), %rdi      # 0x67a5
leaq 0x2fe4(%rip), %rsi      # 0x623e
leaq 0x3589(%rip), %rcx      # 0x67ea
movl $0x2f4, %edx            # imm = 0x2F4
callq 0x1110
leaq 0x35e8(%rip), %rdi      # 0x685a
leaq 0x2fc5(%rip), %rsi      # 0x623e
leaq 0x356a(%rip), %rcx      # 0x67ea
movl $0x2f5, %edx            # imm = 0x2F5
callq 0x1110
leaq 0x3514(%rip), %rdi      # 0x67a5
leaq 0x2fa6(%rip), %rsi      # 0x623e
leaq 0x354b(%rip), %rcx      # 0x67ea
movl $0x344, %edx            # imm = 0x344
callq 0x1110
leaq 0x35aa(%rip), %rdi      # 0x685a
leaq 0x2f87(%rip), %rsi      # 0x623e
leaq 0x352c(%rip), %rcx      # 0x67ea
movl $0x345, %edx            # imm = 0x345
callq 0x1110