// File: /workspace/github_c_makefile_star2_O1/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 $0xb8, %rsp
movq %rcx, %r13
movq %rdx, %rax
movq %rdi, %r12
movq %rsi, -0x58(%rbp)
movq %rdx, -0x50(%rbp)
xorl %ecx, %ecx
movq %rcx, -0x48(%rbp)
movq %rcx, -0x40(%rbp)
addq $0x40, %r13
leaq -0xe0(%rbp), %rcx
movl $0x3, %edx
movq %r13, %rdi
movq %rax, %rsi
callq 0x3b54
testl %eax, %eax
je 0x3d53
leaq -0x58(%rbp), %rcx
leaq -0x50(%rbp), %rdx
jmp 0x3ff9
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %r8
xorl %esi, %esi
movq %r13, -0x60(%rbp)
movq %r12, -0x88(%rbp)
cmpl $-0x1, -0xc8(%rbp)
je 0x40ad
cmpl $-0x1, -0xc4(%rbp)
je 0x40ad
movq %r8, -0x38(%rbp)
movq %rdi, -0x30(%rbp)
movq %rsi, -0x98(%rbp)
cmpl $-0x1, -0xb0(%rbp)
je 0x408e
cmpl $-0x1, -0xac(%rbp)
je 0x408e
movq %rsp, -0x90(%rbp)
movq -0xc0(%rbp), %rdx
movq -0xb8(%rbp), %rsi
movq -0xa8(%rbp), %r15
movq -0xa0(%rbp), %r14
movq %rsp, %rbx
leaq 0x10(%rdx), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movb $0x0, (%rdx,%rbx)
movq %rbx, %rdi
callq 0x1050
movq %r12, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x3c01
testq %rax, %rax
je 0x404e
movq %rax, %rbx
cmpl $0x2, 0x20(%rax)
jne 0x3e98
movq %rsp, -0x80(%rbp)
movq 0x28(%rbx), %r12
movq %rsp, %r13
leaq 0x10(%r15), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
movb $0x0, (%r15,%r13)
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1050
movq (%r12), %rdi
cmpq $0x0, 0x10(%rdi)
je 0x400e
xorl %r14d, %r14d
movl %r14d, %esi
callq 0x4baf
movq %rax, %r15
movq %rax, %rdi
callq 0x10c0
movq %rax, %rbx
movq %r13, %rdi
callq 0x10c0
cmpq %rax, %rbx
cmovbeq %rax, %rbx
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x1060
testl %eax, %eax
je 0x3ed4
incl %r14d
movq (%r12), %rdi
cmpq %r14, 0x10(%rdi)
ja 0x3e4f
jmp 0x400e
leaq 0x2dc1(%rip), %rdi      # 0x6c60
callq 0x1070
movq -0x30(%rbp), %rax
movq (%rax), %rsi
leaq 0x2837(%rip), %rdi      # 0x66e9
xorl %eax, %eax
callq 0x10e0
cmpl $0x2, 0x20(%rbx)
movl $0x0, %r14d
movl $0x0, %r12d
je 0x3f7d
jmp 0x40cc
cmpl $0x1, 0x20(%r15)
jne 0x40eb
addq $0x28, %r15
movslq -0xe0(%rbp), %r13
movslq -0xdc(%rbp), %rbx
movq %rbx, -0x78(%rbp)
movq %r15, %rdi
callq 0x10c0
movq %rax, %rcx
movq %rax, -0x70(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
subq %rbx, %rax
movq %rax, -0x38(%rbp)
leaq (%rcx,%r13), %r14
movq %r13, -0x68(%rbp)
leaq (%rax,%r14), %rsi
incq %rsi
addq %rax, %r14
movl $0x1, %edi
callq 0x1160
movq %rax, %r12
movq -0x30(%rbp), %rax
movq (%rax), %rbx
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x1190
movq %r12, %r13
addq -0x68(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq -0x70(%rbp), %r15
movq %r15, %rdx
callq 0x1190
addq %r15, %r13
addq -0x78(%rbp), %rbx
movq %r13, %rdi
movq %rbx, %rsi
movq -0x38(%rbp), %rdx
callq 0x1190
movq -0x80(%rbp), %rsp
movq -0x60(%rbp), %r13
movq -0x98(%rbp), %rdi
callq 0x1030
movq %r14, -0x48(%rbp)
movq %r12, -0x40(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movaps %xmm0, -0xb0(%rbp)
movq $0x0, -0xa0(%rbp)
movq -0x90(%rbp), %rsp
movl $0x3, %edx
movq %r13, %rdi
movq %r12, %rsi
leaq -0xe0(%rbp), %rcx
callq 0x3b54
leaq -0x48(%rbp), %rcx
movq %rcx, %r8
leaq -0x40(%rbp), %rdx
movq %rdx, %rdi
movq %r12, %rsi
testl %eax, %eax
movq -0x88(%rbp), %r12
je 0x3d68
movq (%rcx), %rax
movq (%rdx), %rdx
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2c80(%rip), %rdi      # 0x6c95
callq 0x1070
movq -0x30(%rbp), %rax
movq (%rax), %rsi
leaq 0x26c1(%rip), %rdi      # 0x66e9
xorl %eax, %eax
callq 0x10e0
leaq 0x2710(%rip), %rdi      # 0x6746
leaq 0x2201(%rip), %rsi      # 0x623e
leaq 0x2a2d(%rip), %rcx      # 0x6a71
movl $0x1d7, %edx            # imm = 0x1D7
callq 0x1110
leaq 0x2c0b(%rip), %rdi      # 0x6c60
callq 0x1070
movq -0x30(%rbp), %rax
movq (%rax), %rsi
leaq 0x2681(%rip), %rdi      # 0x66e9
xorl %eax, %eax
callq 0x10e0
leaq 0x267d(%rip), %rdi      # 0x66f3
leaq 0x21c1(%rip), %rsi      # 0x623e
leaq 0x29ed(%rip), %rcx      # 0x6a71
movl $0x1dc, %edx            # imm = 0x1DC
callq 0x1110
leaq 0x2a43(%rip), %rdi      # 0x6ad8
leaq 0x21a2(%rip), %rsi      # 0x623e
leaq 0x29ce(%rip), %rcx      # 0x6a71
movl $0x1b2, %edx            # imm = 0x1B2
callq 0x1110
leaq 0x298a(%rip), %rdi      # 0x6a3e
leaq 0x2183(%rip), %rsi      # 0x623e
leaq 0x29af(%rip), %rcx      # 0x6a71
movl $0x1b1, %edx            # imm = 0x1B1
callq 0x1110
leaq 0x2a59(%rip), %rdi      # 0x6b2c
leaq 0x2164(%rip), %rsi      # 0x623e
leaq 0x2990(%rip), %rcx      # 0x6a71
movl $0x1dd, %edx            # imm = 0x1DD
callq 0x1110
leaq 0x2a1d(%rip), %rdi      # 0x6b0f
leaq 0x2145(%rip), %rsi      # 0x623e
leaq 0x2971(%rip), %rcx      # 0x6a71
movl $0x1ce, %edx            # imm = 0x1CE
callq 0x1110