// File: /workspace/github_c_makefile_star2_O3/ebiggers[P]mysh/mysh_param.c
// Function: do_param_expansion
struct string *
do_param_expansion(struct string *s, unsigned char **param_char_map)
{
	const char *var_begin;
	const char *dollar_sign;
	const char *var_end;
	const char *scan_next;
	unsigned char mask;
	unsigned char char_type;
	struct string *tmp;
	size_t len;
	struct list_head string_list;
	bool bracketed;

	*param_char_map = NULL;
	var_end = s->chars;
	dollar_sign = strchr(var_end, '$');
	if (!dollar_sign || *(dollar_sign + 1) == '\0') {
		/* No parameter expansion to be done.
		 * Return the original string. */
		return s;
	}

	/* As a result of doing the expansions, we will produce a list of
	 * strings in the list @string_list, each of which will correspond to
	 * either literal characters or to characters that were produced as a
	 * result of a parameter expansion.  These strings are later joined
	 * together to produce the returned, expanded string. */
	INIT_LIST_HEAD(&string_list);
	do {
		/* Append the literal characters (if any) from after the end of
		 * the previous variable (or the beginning of the string) up
		 * until the dollar sign. */
		if (dollar_sign > var_end)
			append_string(var_end, dollar_sign - var_end, &string_list);

		/* Parse the parameter name.  There are several cases; for
		 * example, the parameter may be specified as ${foo} rather than
		 * $foo.  Parameters beginning with a number are positional
		 * parameters, so they are parsed up until the first non-digit
		 * or closing '}'.  Any invalid sequences, such as the missing
		 * closing '}', are just output literally. */
		var_begin = dollar_sign + 1;
		if (*var_begin == '{') {
			bracketed = true;
			var_begin++;
		} else
			bracketed = false;
		var_end = var_begin;
		char_type = shell_char_type(*var_end);
		mysh_assert(shell_char_type('$') & SHELL_PARAM_SPECIAL_CHAR);
		if (char_type & (SHELL_NORMAL_PARAM_CHAR |
				 SHELL_PARAM_SPECIAL_CHAR))
		{
			if (char_type & SHELL_PARAM_NUMERIC_CHAR) {
				/* positional parameter */
				mask = SHELL_PARAM_NUMERIC_CHAR;
			} else if (char_type & SHELL_PARAM_SPECIAL_CHAR) {
				/* special parameter */
				mask = 0;
			} else {
				/* regular parameter */
				mask = SHELL_NORMAL_PARAM_CHAR;
			}
			do {
				var_end++;
			} while (shell_char_type(*var_end) & mask);
			if (bracketed && *var_end != '}') {
				/* Missing closing brace */

				/* Set var_end to point to dollar sign so it's
				 * output literally */
				var_end = var_begin - 2;
				/* Start scanning at the ? in ${?.  So,
				 * "${$HOME" will expand $HOME. */
				scan_next = var_begin;
			} else {
				/* The parameter name (or special character, or
				 * positional parameter) begins at var_begin and
				 * continues for (var_end - var_begin)
				 * characters.  Look it up and append it to the
				 * string list. */
				append_param(var_begin, var_end - var_begin,
					     &string_list);
				if (bracketed)
					++var_end;
				scan_next = var_end;
			}
		} else {
			scan_next = var_end + 1;
			var_end = dollar_sign;
		}
	} while ((dollar_sign = strchr(scan_next, '$')) && *(dollar_sign + 1) != '\0');

	/* Append any remaining literal characters */
	if (*var_end)
		append_string(var_end, s->len - (var_end - s->chars), &string_list);

	/* Sum the total length of the expanded string */
	len = 0;
	list_for_each_entry(tmp, &string_list, list)
		len += tmp->len;
	if (len == 0) {
		/* The expansions produced the empty string.  Return the
		 * original string.  */
		free_string_list(&string_list);
		s->len = 0;
		s->chars[0] = '\0';
	} else {
		/* The expansions produced one or more strings, which now need
		 * to be joined.  The original string that was passed in must be
		 * freed. */
		int flags;
		size_t pos;

		flags = s->flags;
		free_string(s);
		if (flags & STRING_FLAG_UNQUOTED) {
			*param_char_map = xzalloc(len);
			pos = 0;
			list_for_each_entry(tmp, &string_list, list) {
				if (tmp->flags & STRING_FLAG_WAS_PARAM) {
					size_t i;
					for (i = 0; i < tmp->len; i++)
						(*param_char_map)[pos + i] = 1;
				}
				pos += tmp->len;

			}
		}
		s = join_strings(&string_list);
		s->flags = flags;
	}
	s->flags |= STRING_FLAG_PARAM_EXPANDED;
	return s;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movq %rdi, %r12
movq $0x0, (%rsi)
movq (%rdi), %r13
movq %r13, %rdi
movl $0x24, %esi
callq 0x2130
testq %rax, %rax
je 0x52a6
movq %rax, %r15
movb 0x1(%rax), %bl
testb %bl, %bl
je 0x52a6
movq %r12, 0x18(%rsp)
movq %r14, 0x20(%rsp)
leaq 0x8(%rsp), %rax
movq %rax, (%rax)
movq %rax, 0x8(%rax)
leaq 0x2a97(%rip), %r14      # 0x7b10
movb 0x24(%r14), %al
movb %al, 0x7(%rsp)
movq %r15, %rsi
subq %r13, %rsi
jbe 0x50b3
movq %r13, %rdi
callq 0x65e6
leaq 0x18(%rax), %rcx
movq 0x10(%rsp), %rdx
movq %rcx, 0x10(%rsp)
leaq 0x8(%rsp), %rsi
movq %rsi, 0x18(%rax)
movq %rdx, 0x20(%rax)
movq %rcx, (%rdx)
movb 0x1(%r15), %bl
leaq 0x1(%r15), %r12
leaq 0x2(%r15), %rdi
xorl %eax, %eax
cmpb $0x7b, %bl
setne %cl
cmoveq %rdi, %r12
testb $0x8, 0x7(%rsp)
je 0x52b8
movb %cl, %al
negq %rax
movzbl 0x2(%r15,%rax), %eax
movzbl (%r14,%rax), %eax
testb $0xf, %al
je 0x5180
testb $0x8, %al
movl $0x0, %ecx
movl $0x7, %edx
cmovel %edx, %ecx
testb $0x2, %al
movl $0x2, %eax
cmovnel %eax, %ecx
xorl %ebp, %ebp
movzbl 0x1(%r12,%rbp), %eax
movzbl (%r14,%rax), %edx
incq %rbp
testl %edx, %ecx
jne 0x5105
cmpb $0x7b, %bl
jne 0x5123
movq %r15, %r13
cmpb $0x7d, %al
jne 0x5189
movq %r12, %rdi
movq %rbp, %rsi
callq 0x4dd1
testq %rax, %rax
je 0x516b
movq %rax, %r15
movq %rax, %rdi
callq 0x2100
movq %r15, %rdi
movq %rax, %rsi
callq 0x65e6
leaq 0x18(%rax), %rcx
movq 0x10(%rsp), %rdx
movq %rcx, 0x10(%rsp)
leaq 0x8(%rsp), %rsi
movq %rsi, 0x18(%rax)
movq %rdx, 0x20(%rax)
movq %rcx, (%rdx)
orb $0x1, 0x11(%rax)
leaq (%r12,%rbp), %rax
cmpb $0x7b, %bl
leaq 0x1(%r12,%rbp), %rdi
cmovneq %rax, %rdi
movq %rdi, %r13
jmp 0x5189
incq %r12
movq %r12, %rdi
movq %r15, %r13
movl $0x24, %esi
callq 0x2130
testq %rax, %rax
je 0x51a6
movq %rax, %r15
movb 0x1(%rax), %bl
testb %bl, %bl
jne 0x5081
cmpb $0x0, (%r13)
leaq 0x8(%rsp), %r12
je 0x51e2
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rsi
subq %r13, %rsi
addq (%rax), %rsi
movq %r13, %rdi
callq 0x65e6
leaq 0x18(%rax), %rcx
movq 0x10(%rsp), %rdx
movq %rcx, 0x10(%rsp)
movq %r12, 0x18(%rax)
movq %rdx, 0x20(%rax)
movq %rcx, (%rdx)
movq 0x8(%rsp), %rax
cmpq %r12, %rax
movq 0x20(%rsp), %r14
je 0x527a
xorl %r15d, %r15d
addq -0x10(%rax), %r15
movq (%rax), %rax
cmpq %r12, %rax
jne 0x51f8
testq %r15, %r15
je 0x527a
movq 0x18(%rsp), %rdi
movl 0x10(%rdi), %ebx
callq 0x665c
testb $0x1, %bl
je 0x5268
movq %r15, %rdi
callq 0x6564
movq %rax, (%r14)
movq 0x8(%rsp), %rax
cmpq %r12, %rax
je 0x5268
xorl %ecx, %ecx
testb $0x1, -0x7(%rax)
movq -0x10(%rax), %rdx
je 0x525d
testq %rdx, %rdx
je 0x525b
xorl %esi, %esi
movq (%r14), %rdx
addq %rcx, %rdx
movb $0x1, (%rsi,%rdx)
incq %rsi
movq -0x10(%rax), %rdx
cmpq %rdx, %rsi
jb 0x5243
jmp 0x525d
xorl %edx, %edx
addq %rdx, %rcx
movq (%rax), %rax
cmpq %r12, %rax
jne 0x5232
leaq 0x8(%rsp), %rdi
callq 0x66b8
movq %rax, %r12
movl %ebx, 0x10(%rax)
jmp 0x529e
leaq 0x8(%rsp), %rdi
callq 0x6677
movq 0x18(%rsp), %r12
movq $0x0, 0x8(%r12)
movq (%r12), %rax
movb $0x0, (%rax)
movl 0x10(%r12), %ebx
orl $0x8, %ebx
movl %ebx, 0x10(%r12)
movq %r12, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x25e7(%rip), %rdi      # 0x78a6
leaq 0x2610(%rip), %rsi      # 0x78d6
leaq 0x2616(%rip), %rcx      # 0x78e3
movl $0x205, %edx            # imm = 0x205
callq 0x2160