// File: /workspace/github_c_makefile_star2_O0/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
subq $0x150, %rsp            # imm = 0x150
movq %rsi, -0x20(%rbp)
movq %rdx, -0x18(%rbp)
movq %rdi, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1110
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1110
leaq -0x20(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rdi
addq $0x40, %rdi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0xa0(%rbp), %rcx
movl $0x3, %edx
callq 0x4610
cmpl $0x0, %eax
jne 0x50f2
movq -0x88(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0xc0(%rbp)
cmpl $-0x1, -0xb8(%rbp)
je 0x4d26
cmpl $-0x1, -0xb4(%rbp)
je 0x4d26
jmp 0x4d45
leaq 0x3d79(%rip), %rdi      # 0x8aa6
leaq 0x34f9(%rip), %rsi      # 0x822d
movl $0x1b1, %edx            # imm = 0x1B1
leaq 0x3d99(%rip), %rcx      # 0x8ad9
callq 0x1100
cmpl $-0x1, -0xd0(%rbp)
je 0x4d59
cmpl $-0x1, -0xcc(%rbp)
je 0x4d59
jmp 0x4d78
leaq 0x3de0(%rip), %rdi      # 0x8b40
leaq 0x34c6(%rip), %rsi      # 0x822d
movl $0x1b2, %edx            # imm = 0x1B2
leaq 0x3d66(%rip), %rcx      # 0x8ad9
callq 0x1100
movq -0xb0(%rbp), %rcx
movq %rcx, %rax
incq %rax
movq %rsp, %rdx
movq %rdx, -0xe0(%rbp)
addq $0x10, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x130(%rbp)
movq %rdi, %rsp
movq %rax, -0xe8(%rbp)
movq -0xb0(%rbp), %rax
movb $0x0, (%rdi,%rax)
movq -0xa8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
callq 0x1050
movq -0x130(%rbp), %rsi
movq -0x28(%rbp), %rdi
movl $0x1, %edx
callq 0x4740
movq %rax, -0xd8(%rbp)
cmpq $0x0, %rax
je 0x501e
movq -0xd8(%rbp), %rax
cmpl $0x2, 0x20(%rax)
jne 0x501e
movq -0xd8(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0xf0(%rbp)
movq -0xc8(%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, -0x138(%rbp)
movq %rdi, %rsp
movq %rax, -0x100(%rbp)
movq -0xc8(%rbp), %rax
movb $0x0, (%rdi,%rax)
movq -0xc0(%rbp), %rsi
movq -0xc8(%rbp), %rdx
callq 0x1050
movl $0x0, -0x104(%rbp)
movl -0x104(%rbp), %eax
movq -0xf0(%rbp), %rcx
movq (%rcx), %rcx
cmpq 0x10(%rcx), %rax
jae 0x4fdb
movq -0xf0(%rbp), %rax
movq (%rax), %rdi
movl -0x104(%rbp), %esi
callq 0x60b0
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x118(%rbp), %rdi
callq 0x10b0
movq -0x138(%rbp), %rdi
movq %rax, -0x140(%rbp)
callq 0x10b0
movq %rax, %rcx
movq -0x140(%rbp), %rax
cmpq %rcx, %rax
jbe 0x4efa
movq -0x118(%rbp), %rdi
callq 0x10b0
movq %rax, -0x148(%rbp)
jmp 0x4f0d
movq -0x138(%rbp), %rdi
callq 0x10b0
movq %rax, -0x148(%rbp)
movq -0x138(%rbp), %rsi
movq -0x148(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x118(%rbp), %rdi
movq -0x120(%rbp), %rdx
callq 0x1060
cmpl $0x0, %eax
jne 0x4fc5
movq -0x110(%rbp), %rax
cmpl $0x1, 0x20(%rax)
jne 0x4f4d
jmp 0x4f6c
leaq 0x3c23(%rip), %rdi      # 0x8b77
leaq 0x32d2(%rip), %rsi      # 0x822d
movl $0x1ce, %edx            # imm = 0x1CE
leaq 0x3b72(%rip), %rcx      # 0x8ad9
callq 0x1100
leaq -0xa0(%rbp), %rax
movq -0x110(%rbp), %rsi
addq $0x28, %rsi
movq -0x58(%rbp), %rdx
leaq -0x50(%rbp), %rdi
subq $0x20, %rsp
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x5110
addq $0x20, %rsp
movl $0x7, -0x124(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, %rsp
jmp 0x509f
jmp 0x4fc7
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
jmp 0x4e73
leaq 0x3bb2(%rip), %rdi      # 0x8b94
movb $0x0, %al
callq 0x10d0
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x3759(%rip), %rdi      # 0x8751
movb $0x0, %al
callq 0x10d0
leaq 0x37a8(%rip), %rdi      # 0x87ae
leaq 0x3220(%rip), %rsi      # 0x822d
movl $0x1d7, %edx            # imm = 0x1D7
leaq 0x3ac0(%rip), %rcx      # 0x8ad9
callq 0x1100
leaq 0x3bce(%rip), %rdi      # 0x8bf3
movb $0x0, %al
callq 0x10d0
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x3716(%rip), %rdi      # 0x8751
movb $0x0, %al
callq 0x10d0
cmpq $0x0, -0xd8(%rbp)
je 0x504e
jmp 0x506d
leaq 0x3706(%rip), %rdi      # 0x875b
leaq 0x31d1(%rip), %rsi      # 0x822d
movl $0x1dc, %edx            # imm = 0x1DC
leaq 0x3a71(%rip), %rcx      # 0x8ad9
callq 0x1100
movq -0xd8(%rbp), %rax
cmpl $0x2, 0x20(%rax)
jne 0x507c
jmp 0x509b
leaq 0x3ba6(%rip), %rdi      # 0x8c29
leaq 0x31a3(%rip), %rsi      # 0x822d
movl $0x1dd, %edx            # imm = 0x1DD
leaq 0x3a43(%rip), %rcx      # 0x8ad9
callq 0x1100
jmp 0x509d
jmp 0x509f
movq -0x38(%rbp), %rdi
callq 0x1030
movq -0x50(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
leaq -0x40(%rbp), %rax
movq %rax, -0x58(%rbp)
leaq -0xa0(%rbp), %rdi
xorl %esi, %esi
movl $0x48, %edx
callq 0x1110
movq -0xe0(%rbp), %rax
movq %rax, %rsp
jmp 0x4ca3
movq -0x58(%rbp), %rax
movups (%rax), %xmm0
movaps %xmm0, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rdx
movq %rbp, %rsp
popq %rbp
retq
nopw (%rax,%rax)