// File: /workspace/github_c_makefile_star2_O3/qpfiffer[P]38-Moths/src/greshunkel.c
// Function: interpolate_line_subcontext
static line
_interpolate_line_subcontext(const greshunkel_ctext *ctext, const line current_line, const struct compiled_regex *all_regex) {
	/* Matches context-based variables, which are dictionary-like. */
	line interpolated_line = {0};
	line new_line_to_add = {0};
	const line *operating_line = &current_line;

	match_t cvar_match[3];
	while (regexec_2_0_beta(&all_regex->c_cvar_regex, operating_line->data, 3, cvar_match) == 0) {
		const match_t inner_match = cvar_match[1];
		const match_t subname_match = cvar_match[2];
		assert(inner_match.rm_so != -1 && inner_match.rm_eo != -1);
		assert(subname_match.rm_so != -1 && subname_match.rm_eo != -1);

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

		/* So here we search for a sub context instead of a string. */
		if ((tuple = find_needle(ctext, just_match_str, 1)) && tuple->type == GSHKL_SUBCTEXT) {
			const greshunkel_ctext *sub_ctext = tuple->value.sub_ctext;

			char just_subname_match_str[subname_match.len + 1];
			just_subname_match_str[subname_match.len] = '\0';
			strncpy(just_subname_match_str, subname_match.start, subname_match.len);
			/* So now we need to:
			 * 1. Loop through the subcontext's values searching for subname_match
			 * 2. Inteprolate using that value.
			 */

			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(just_subname_match_str) ?
						strlen(_name) : strlen(just_subname_match_str);

				if (strncmp(_name, just_subname_match_str, larger) == 0) {
					/* TODO: Only works on strings. */
					assert(sub_tuple->type == GSHKL_STR);
					vishnu(&new_line_to_add, cvar_match[0], sub_tuple->value.str, operating_line);
					goto done;
				}
			}

			/* Blow up if we had a variable that wasn't in the context. */
			printf("Did not match a sub-context variable that needed to be matched, or variable was None/NULL/0).\n");
			printf("Line: %s\n", operating_line->data);
			assert(1 == 0);
		} else {
			/* Blow up if we had a variable that wasn't in the context. */
			printf("Did not match a cvariable that needed to be matched.\n");
			printf("Line: %s\n", operating_line->data);
			assert(tuple != NULL);
			assert(tuple->type == GSHKL_SUBCTEXT);
		}

done:
		free(interpolated_line.data);
		interpolated_line.size = new_line_to_add.size;
		interpolated_line.data = new_line_to_add.data;
		new_line_to_add.size = 0;
		new_line_to_add.data = NULL;
		operating_line = &interpolated_line;

		/* Set the next regex check after this one. */
		memset(cvar_match, 0, sizeof(cvar_match));
	}

	return (*operating_line);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rcx, %r15
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %r12
movq %rsi, -0xd0(%rbp)
movq %rdx, -0xc8(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movaps %xmm0, -0x40(%rbp)
addq $0x40, %r15
leaq -0xc0(%rbp), %rcx
movl $0x3, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x3a75
testl %eax, %eax
jne 0x3e49
leaq -0xd0(%rbp), %rdx
xorl %ecx, %ecx
movq %r15, -0x58(%rbp)
movq %r12, -0x60(%rbp)
cmpl $-0x1, -0xa8(%rbp)
je 0x3eff
cmpl $-0x1, -0xa4(%rbp)
je 0x3eff
movq %rdx, -0x30(%rbp)
movq %rcx, -0x78(%rbp)
cmpl $-0x1, -0x90(%rbp)
je 0x3ee0
cmpl $-0x1, -0x8c(%rbp)
je 0x3ee0
movq %rsp, -0x70(%rbp)
movq -0xa0(%rbp), %rdx
movq -0x98(%rbp), %rsi
movq -0x88(%rbp), %r13
movq -0x80(%rbp), %rbx
movq %rsp, %r14
leaq 0x10(%rdx), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movb $0x0, (%rdx,%r14)
movq %r14, %rdi
callq 0x1050
movq %r12, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x3b22
testq %rax, %rax
je 0x3e9f
movq %rax, %r14
cmpl $0x2, 0x20(%rax)
jne 0x3d6a
movq %rsp, -0x68(%rbp)
movq 0x28(%r14), %r15
movq %rsp, %r14
leaq 0x10(%r13), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movb $0x0, (%r13,%r14)
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x1050
movq (%r15), %rdi
cmpq $0x0, 0x10(%rdi)
je 0x3e5e
xorl %ebx, %ebx
movl %ebx, %esi
callq 0x494b
movq %rax, %r13
movq %rax, %rdi
callq 0x10c0
movq %rax, %r12
movq %r14, %rdi
callq 0x10c0
cmpq %rax, %r12
cmovbeq %rax, %r12
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x1060
testl %eax, %eax
je 0x3da4
incl %ebx
movq (%r15), %rdi
cmpq %rbx, 0x10(%rdi)
ja 0x3d24
jmp 0x3e5e
leaq 0x2eef(%rip), %rdi      # 0x6c60
callq 0x1070
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x2964(%rip), %rdi      # 0x66e9
xorl %eax, %eax
callq 0x10e0
cmpl $0x2, 0x20(%r14)
movl $0x0, %r13d
movl $0x0, %r14d
je 0x3de3
jmp 0x3f1e
cmpl $0x1, 0x20(%r13)
movq -0x60(%rbp), %r12
jne 0x3f3d
addq $0x28, %r13
movl -0xc0(%rbp), %esi
movl -0xbc(%rbp), %edx
leaq -0x40(%rbp), %rdi
movq %r13, %rcx
movq -0x30(%rbp), %r8
callq 0x3f5c
movq -0x68(%rbp), %rsp
movq -0x40(%rbp), %r14
movq -0x38(%rbp), %r13
movq -0x58(%rbp), %r15
movq -0x78(%rbp), %rdi
callq 0x1030
movq %r14, -0x50(%rbp)
movq %r13, -0x48(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movaps %xmm0, -0xc0(%rbp)
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xa0(%rbp)
movaps %xmm0, -0x90(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x70(%rbp), %rsp
movl $0x3, %edx
movq %r15, %rdi
movq %r13, %rsi
leaq -0xc0(%rbp), %rcx
callq 0x3a75
leaq -0x50(%rbp), %rdx
movq %r13, %rcx
testl %eax, %eax
je 0x3c4a
movq %r14, %rax
movq %r13, %rdx
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2e30(%rip), %rdi      # 0x6c95
callq 0x1070
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x2870(%rip), %rdi      # 0x66e9
xorl %eax, %eax
callq 0x10e0
leaq 0x28bf(%rip), %rdi      # 0x6746
leaq 0x23b0(%rip), %rsi      # 0x623e
leaq 0x2bdc(%rip), %rcx      # 0x6a71
movl $0x1d7, %edx            # imm = 0x1D7
callq 0x1110
leaq 0x2dba(%rip), %rdi      # 0x6c60
callq 0x1070
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x282f(%rip), %rdi      # 0x66e9
xorl %eax, %eax
callq 0x10e0
leaq 0x282b(%rip), %rdi      # 0x66f3
leaq 0x236f(%rip), %rsi      # 0x623e
leaq 0x2b9b(%rip), %rcx      # 0x6a71
movl $0x1dc, %edx            # imm = 0x1DC
callq 0x1110
leaq 0x2bf1(%rip), %rdi      # 0x6ad8
leaq 0x2350(%rip), %rsi      # 0x623e
leaq 0x2b7c(%rip), %rcx      # 0x6a71
movl $0x1b2, %edx            # imm = 0x1B2
callq 0x1110
leaq 0x2b38(%rip), %rdi      # 0x6a3e
leaq 0x2331(%rip), %rsi      # 0x623e
leaq 0x2b5d(%rip), %rcx      # 0x6a71
movl $0x1b1, %edx            # imm = 0x1B1
callq 0x1110
leaq 0x2c07(%rip), %rdi      # 0x6b2c
leaq 0x2312(%rip), %rsi      # 0x623e
leaq 0x2b3e(%rip), %rcx      # 0x6a71
movl $0x1dd, %edx            # imm = 0x1DD
callq 0x1110
leaq 0x2bcb(%rip), %rdi      # 0x6b0f
leaq 0x22f3(%rip), %rsi      # 0x623e
leaq 0x2b1f(%rip), %rcx      # 0x6a71
movl $0x1ce, %edx            # imm = 0x1CE
callq 0x1110