// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq $0x0, (%rsi)
movq (%rdi), %r13
movq %r13, %rdi
movl $0x24, %esi
callq 0x2130
testq %rax, %rax
je 0x538f
movq %rax, %r12
cmpb $0x0, 0x1(%rax)
je 0x538f
movq %r14, 0x20(%rsp)
leaq 0x10(%rsp), %rax
movq %rax, (%rax)
movq %rax, 0x8(%rax)
leaq 0x299a(%rip), %r14      # 0x7b10
movb 0x24(%r14), %al
movb %al, 0xf(%rsp)
movq %r12, %rsi
subq %r13, %rsi
jbe 0x51ac
movq %r13, %rdi
callq 0x67fa
leaq 0x18(%rax), %rcx
movq 0x18(%rsp), %rdx
movq %rcx, 0x18(%rsp)
leaq 0x10(%rsp), %rsi
movq %rsi, 0x18(%rax)
movq %rdx, 0x20(%rax)
movq %rcx, (%rdx)
leaq 0x1(%r12), %rdx
movb 0x1(%r12), %bpl
leaq 0x2(%r12), %r15
xorl %eax, %eax
cmpb $0x7b, %bpl
setne %cl
cmovneq %rdx, %r15
testb $0x8, 0xf(%rsp)
je 0x53a1
movb %cl, %al
negq %rax
movzbl 0x2(%r12,%rax), %eax
movzbl (%r14,%rax), %ecx
testb $0xf, %cl
je 0x5229
testb $0x8, %cl
movl $0x0, %eax
movl $0x7, %edx
cmovel %edx, %eax
testb $0x2, %cl
movl $0x2, %ecx
cmovnel %ecx, %eax
xorl %r12d, %r12d
movzbl 0x1(%r15,%r12), %ecx
movzbl (%r14,%rcx), %edx
incq %r12
testl %edx, %eax
jne 0x5206
cmpb $0x7b, %bpl
jne 0x5231
cmpb $0x7d, %cl
je 0x5231
leaq -0x2(%r15), %r13
jmp 0x528d
incq %r15
movq %r12, %r13
jmp 0x528d
movq %r15, %rdi
movq %r12, %rsi
callq 0x4eda
testq %rax, %rax
je 0x5279
movq %rax, %r13
movq %rax, %rdi
callq 0x2100
movq %r13, %rdi
movq %rax, %rsi
callq 0x67fa
leaq 0x18(%rax), %rcx
movq 0x18(%rsp), %rdx
movq %rcx, 0x18(%rsp)
leaq 0x10(%rsp), %rsi
movq %rsi, 0x18(%rax)
movq %rdx, 0x20(%rax)
movq %rcx, (%rdx)
orb $0x1, 0x11(%rax)
leaq (%r15,%r12), %rax
cmpb $0x7b, %bpl
leaq 0x1(%r15,%r12), %r15
cmovneq %rax, %r15
movq %r15, %r13
movq %r15, %rdi
movl $0x24, %esi
callq 0x2130
testq %rax, %rax
je 0x52ac
movq %rax, %r12
cmpb $0x0, 0x1(%rax)
jne 0x517e
cmpb $0x0, (%r13)
leaq 0x10(%rsp), %r14
je 0x52e8
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rsi
subq %r13, %rsi
addq (%rax), %rsi
movq %r13, %rdi
callq 0x67fa
leaq 0x18(%rax), %rcx
movq 0x18(%rsp), %rdx
movq %rcx, 0x18(%rsp)
movq %r14, 0x18(%rax)
movq %rdx, 0x20(%rax)
movq %rcx, (%rdx)
movq 0x10(%rsp), %rax
xorl %r15d, %r15d
cmpq %r14, %rax
je 0x52fe
addq -0x10(%rax), %r15
movq (%rax), %rax
jmp 0x52f0
testq %r15, %r15
je 0x536d
movq 0x20(%rsp), %rdi
movl 0x10(%rdi), %ebp
callq 0x6870
testb $0x1, %bpl
je 0x535b
movq %r15, %rdi
callq 0x6778
movq %rax, (%rbx)
movq 0x10(%rsp), %rax
cmpq %r14, %rax
je 0x535b
xorl %ecx, %ecx
testb $0x1, -0x7(%rax)
je 0x534f
cmpq $0x0, -0x10(%rax)
je 0x534f
xorl %edx, %edx
movq (%rbx), %rsi
addq %rcx, %rsi
movb $0x1, (%rdx,%rsi)
incq %rdx
cmpq -0x10(%rax), %rdx
jb 0x533c
addq -0x10(%rax), %rcx
movq (%rax), %rax
cmpq %r14, %rax
jne 0x532d
leaq 0x10(%rsp), %rdi
callq 0x68cc
movq %rax, %r14
movl %ebp, 0x10(%rax)
jmp 0x538a
leaq 0x10(%rsp), %rdi
callq 0x688b
movq 0x20(%rsp), %r14
movq $0x0, 0x8(%r14)
movq (%r14), %rax
movb $0x0, (%rax)
orb $0x8, 0x10(%r14)
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x24fe(%rip), %rdi      # 0x78a6
leaq 0x2527(%rip), %rsi      # 0x78d6
leaq 0x252d(%rip), %rcx      # 0x78e3
movl $0x205, %edx            # imm = 0x205
callq 0x2160