// File: /workspace/github_c_makefile_star2_O2/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, %r12
movq %rdi, %r13
leaq -0xd0(%rbp), %rbx
movq %rsi, (%rbx)
movq %rdx, 0x8(%rbx)
xorps %xmm0, %xmm0
leaq -0x50(%rbp), %rax
movaps %xmm0, (%rax)
leaq -0x40(%rbp), %rax
movaps %xmm0, (%rax)
addq $0x40, %r15
xorl %r14d, %r14d
movq %r15, -0x60(%rbp)
movq %rdi, -0x58(%rbp)
movq %r15, %rdi
movq %r12, %rsi
pushq $0x3
popq %rdx
leaq -0xc0(%rbp), %rcx
callq 0x354a
testl %eax, %eax
jne 0x3a0c
cmpl $-0x1, -0xa8(%rbp)
je 0x3a63
cmpl $-0x1, -0xa4(%rbp)
je 0x3a63
movq %r14, -0x78(%rbp)
movq %rbx, -0x30(%rbp)
cmpl $-0x1, -0x90(%rbp)
je 0x3ac3
cmpl $-0x1, -0x8c(%rbp)
je 0x3ac3
movq %rsp, -0x70(%rbp)
movq -0xa0(%rbp), %rdx
movq -0x98(%rbp), %rsi
movq -0x88(%rbp), %r14
movq -0x80(%rbp), %rbx
movq %rsp, %r12
leaq 0x10(%rdx), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movb $0x0, (%rdx,%r12)
movq %r12, %rdi
callq 0x1050
movq %r13, %rdi
movq %r12, %rsi
pushq $0x1
popq %rdx
callq 0x35f0
testq %rax, %rax
je 0x3a82
movq %rax, %r12
cmpl $0x2, 0x20(%rax)
jne 0x398a
movq %rsp, -0x68(%rbp)
movq 0x28(%r12), %r15
movq %rsp, %r12
leaq 0x10(%r14), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movb $0x0, (%r14,%r12)
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1050
xorl %ebx, %ebx
movl %ebx, %eax
movq (%r15), %rdi
cmpq %rax, 0x10(%rdi)
jbe 0x3a22
movl %ebx, %esi
callq 0x44dc
movq %rax, %r14
movq %rax, %rdi
callq 0x10c0
movq %rax, %r13
movq %r12, %rdi
callq 0x10c0
cmpq %rax, %r13
cmovbeq %rax, %r13
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x1060
incl %ebx
testl %eax, %eax
jne 0x38ef
cmpl $0x1, 0x20(%r14)
jne 0x3b01
addq $0x28, %r14
subq $0x20, %rsp
movq -0xb0(%rbp), %rax
movq %rax, 0x10(%rsp)
movaps -0xc0(%rbp), %xmm0
movups %xmm0, (%rsp)
leaq -0x40(%rbp), %rdi
movq %r14, %rsi
movq -0x30(%rbp), %rdx
callq 0x3b20
addq $0x20, %rsp
movq -0x68(%rbp), %rsp
movq -0x40(%rbp), %rbx
movq -0x38(%rbp), %r12
movq -0x60(%rbp), %r15
movq -0x58(%rbp), %r13
jmp 0x39c3
leaq 0x32cf(%rip), %rdi      # 0x6c60
callq 0x1070
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x2d44(%rip), %rdi      # 0x66e9
xorl %eax, %eax
callq 0x10e0
cmpl $0x2, 0x20(%r12)
movl $0x0, %r12d
movl $0x0, %ebx
jne 0x3ae2
movq -0x78(%rbp), %rdi
callq 0x1030
movq %rbx, -0x50(%rbp)
movq %r12, -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)
andq $0x0, -0x80(%rbp)
movq -0x70(%rbp), %rsp
movq %r12, %r14
leaq -0x50(%rbp), %rbx
jmp 0x3808
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x326c(%rip), %rdi      # 0x6c95
callq 0x1070
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x2cac(%rip), %rdi      # 0x66e9
xorl %eax, %eax
callq 0x10e0
leaq 0x2cfb(%rip), %rdi      # 0x6746
leaq 0x27ec(%rip), %rsi      # 0x623e
leaq 0x3018(%rip), %rcx      # 0x6a71
movl $0x1d7, %edx            # imm = 0x1D7
callq 0x1110
leaq 0x2fd4(%rip), %rdi      # 0x6a3e
leaq 0x27cd(%rip), %rsi      # 0x623e
leaq 0x2ff9(%rip), %rcx      # 0x6a71
movl $0x1b1, %edx            # imm = 0x1B1
callq 0x1110
leaq 0x31d7(%rip), %rdi      # 0x6c60
callq 0x1070
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x2c4c(%rip), %rdi      # 0x66e9
xorl %eax, %eax
callq 0x10e0
leaq 0x2c48(%rip), %rdi      # 0x66f3
leaq 0x278c(%rip), %rsi      # 0x623e
leaq 0x2fb8(%rip), %rcx      # 0x6a71
movl $0x1dc, %edx            # imm = 0x1DC
callq 0x1110
leaq 0x300e(%rip), %rdi      # 0x6ad8
leaq 0x276d(%rip), %rsi      # 0x623e
leaq 0x2f99(%rip), %rcx      # 0x6a71
movl $0x1b2, %edx            # imm = 0x1B2
callq 0x1110
leaq 0x3043(%rip), %rdi      # 0x6b2c
leaq 0x274e(%rip), %rsi      # 0x623e
leaq 0x2f7a(%rip), %rcx      # 0x6a71
movl $0x1dd, %edx            # imm = 0x1DD
callq 0x1110
leaq 0x3007(%rip), %rdi      # 0x6b0f
leaq 0x272f(%rip), %rsi      # 0x623e
leaq 0x2f5b(%rip), %rcx      # 0x6a71
movl $0x1ce, %edx            # imm = 0x1CE
callq 0x1110