// File: /workspace/github_c_makefile_star2_O2/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, %rbx
movq %rdi, %r15
andq $0x0, (%rsi)
movq (%rdi), %r14
pushq $0x24
popq %rsi
movq %r14, %rdi
callq 0x2130
testq %rax, %rax
je 0x4c07
movq %rax, %r12
movb 0x1(%rax), %bpl
testb %bpl, %bpl
je 0x4c07
movq %rbx, 0x18(%rsp)
movq %r15, 0x20(%rsp)
leaq 0x8(%rsp), %rax
movq %rax, (%rax)
movq %rax, 0x8(%rax)
leaq 0x30ac(%rip), %r15      # 0x7b10
movb 0x24(%r15), %al
movb %al, 0x7(%rsp)
movq %r12, %rsi
subq %r14, %rsi
jbe 0x4a86
movq %r14, %rdi
leaq 0x8(%rsp), %rdx
callq 0x4c7e
movb 0x1(%r12), %bpl
leaq 0x1(%r12), %r13
leaq 0x2(%r12), %rdi
xorl %eax, %eax
cmpb $0x7b, %bpl
setne %cl
cmoveq %rdi, %r13
testb $0x8, 0x7(%rsp)
je 0x4c5f
movb %cl, %al
negq %rax
movzbl 0x2(%r12,%rax), %eax
movzbl (%r15,%rax), %eax
testb $0xf, %al
je 0x4b55
testb $0x8, %al
movl $0x0, %ecx
pushq $0x7
popq %rdx
cmovel %edx, %ecx
testb $0x2, %al
pushq $0x2
popq %rax
cmovnel %eax, %ecx
xorl %r14d, %r14d
movzbl 0x1(%r13,%r14), %eax
movzbl (%r15,%rax), %edx
incq %r14
testl %edx, %ecx
jne 0x4ad8
cmpb $0x7b, %bpl
jne 0x4af7
movq %r12, %rbx
cmpb $0x7d, %al
jne 0x4b5e
movq %r13, %rdi
movq %r14, %rsi
callq 0x47e4
testq %rax, %rax
je 0x4b3f
movq %rax, %r12
movq %rax, %rdi
callq 0x2100
movq %r12, %rdi
movq %rax, %rsi
callq 0x6152
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 (%r14,%r13), %rax
cmpb $0x7b, %bpl
leaq 0x1(%r13,%r14), %rdi
cmovneq %rax, %rdi
movq %rdi, %rbx
jmp 0x4b5e
incq %r13
movq %r13, %rdi
movq %r12, %rbx
pushq $0x24
popq %rsi
callq 0x2130
testq %rax, %rax
movq %rbx, %r14
je 0x4b7e
movq %rax, %r12
movb 0x1(%rax), %bpl
testb %bpl, %bpl
jne 0x4a6c
cmpb $0x0, (%r14)
movq 0x20(%rsp), %r15
je 0x4ba0
movq 0x8(%r15), %rsi
subq %r14, %rsi
addq (%r15), %rsi
leaq 0x8(%rsp), %rdx
movq %r14, %rdi
callq 0x4c7e
xorl %r14d, %r14d
leaq 0x8(%rsp), %rcx
movq %rcx, %rax
movq 0x18(%rsp), %rbx
movq (%rax), %rax
cmpq %rcx, %rax
je 0x4bbe
addq -0x10(%rax), %r14
jmp 0x4bb0
testq %r14, %r14
je 0x4be7
movl 0x10(%r15), %ebp
movq %r15, %rdi
callq 0x6193
testb $0x1, %bpl
jne 0x4c19
leaq 0x8(%rsp), %rdi
callq 0x61e1
movq %rax, %r15
movl %ebp, 0x10(%rax)
jmp 0x4c00
leaq 0x8(%rsp), %rdi
callq 0x61ae
andq $0x0, 0x8(%r15)
movq (%r15), %rax
movb $0x0, (%rax)
movl 0x10(%r15), %ebp
orl $0x8, %ebp
movl %ebp, 0x10(%r15)
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x60df
movq %rax, (%rbx)
leaq 0x8(%rsp), %rax
xorl %ecx, %ecx
movq %rax, %rdx
movq (%rdx), %rdx
cmpq %rax, %rdx
je 0x4bd5
testb $0x1, -0x7(%rdx)
jne 0x4c45
movq -0x10(%rdx), %rdi
addq %rdi, %rcx
jmp 0x4c2e
xorl %esi, %esi
movq -0x10(%rdx), %rdi
cmpq %rdi, %rsi
jae 0x4c40
movq (%rbx), %rdi
addq %rcx, %rdi
movb $0x1, (%rsi,%rdi)
incq %rsi
jmp 0x4c47
leaq 0x2c40(%rip), %rdi      # 0x78a6
leaq 0x2c69(%rip), %rsi      # 0x78d6
leaq 0x2c6f(%rip), %rcx      # 0x78e3
movl $0x205, %edx            # imm = 0x205
callq 0x2160