#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
char *expand_dollar_with_args(char const **, int, char **);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncat(char *, char const *, size_t);
char *xmalloc(int);
char *xrealloc(char *, size_t);
char *__expand_string(const char **str, bool (*is_end)(char c), int argc,
                      char *argv[]) {
  const char *in, *p;
  char *expansion, *out;
  size_t in_len, out_len;
  out = xmalloc(1);
  *out = 0;
  out_len = 1;
  p = in = *str;
  while (1) {
    if (*p == '$') {
      in_len = p - in;
      p++;
      expansion = expand_dollar_with_args(&p, argc, argv);
      out_len += in_len + strlen(expansion);
      out = xrealloc(out, out_len);
      strncat(out, in, in_len);
      strcat(out, expansion);
      free(expansion);
      in = p;
      continue;
    }
    if (is_end(*p))
      break;
    p++;
  }
  in_len = p - in;
  out_len += in_len;
  out = xrealloc(out, out_len);
  strncat(out, in, in_len);
  *str = p;
  return out;
}


x86 assembly (clang, -O0):
<__expand_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    $0x1,%edi
call   1060 <xmalloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x1,-0x50(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x24,%eax
jne    125a <__expand_string+0xea>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
lea    -0x30(%rbp),%rdi
call   1080 <expand_dollar_with_args@plt>
mov    %rax,-0x38(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
add    %rcx,%rax
add    -0x50(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x50(%rbp),%rsi
call   1050 <xrealloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1070 <strncat@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11b3 <__expand_string+0x43>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl (%rcx),%edi
call   *%rax
cmp    $0x0,%eax
je     1275 <__expand_string+0x105>
jmp    1286 <__expand_string+0x116>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11b3 <__expand_string+0x43>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
add    -0x50(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x50(%rbp),%rsi
call   1050 <xrealloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1070 <strncat@plt>
mov    -0x30(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__expand_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,0x20(%rsp)
mov    %edx,0x14(%rsp)
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    $0x1,%edi
call   1060 <xmalloc@plt>
mov    %rax,%r13
movb   $0x0,(%rax)
mov    %rbp,0x18(%rsp)
mov    0x0(%rbp),%r14
mov    %r14,0x8(%rsp)
mov    $0x1,%r12d
jmp    1227 <__expand_string+0xb7>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rax,%r15
sub    %r14,%r15
add    $0x1,%rax
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rdi
mov    0x14(%rsp),%esi
mov    0x20(%rsp),%rdx
call   1080 <expand_dollar_with_args@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <strlen@plt>
add    %r15,%r12
add    %rax,%r12
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <xrealloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <strncat@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
call   1090 <strcat@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%r14
mov    (%r14),%cl
mov    %r14,%rax
cmp    $0x24,%cl
je     11c0 <__expand_string+0x50>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl %cl,%edi
call   *%rbx
mov    0x8(%rsp),%rbp
test   %eax,%eax
jne    1265 <__expand_string+0xf5>
lea    0x1(%rbp),%rax
mov    %rax,0x8(%rsp)
movzbl 0x1(%rbp),%ecx
cmp    $0x24,%cl
jne    1240 <__expand_string+0xd0>
jmp    11c0 <__expand_string+0x50>
sub    %r14,%rbp
add    %rbp,%r12
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <xrealloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1070 <strncat@plt>
mov    0x8(%rsp),%rax
mov    0x18(%rsp),%rcx
mov    %rax,(%rcx)
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @__expand_string(i8** %0, i32 (i8)* %1, i32 %2, i8** %3) {
%5 = alloca i8**
%6 = alloca i32 (i8)*
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i64
store i8** %0, i8*** %5
store i32 (i8)* %1, i32 (i8)** %6
store i32 %2, i32* %7
store i8** %3, i8*** %8
%15 = call i8* @xmalloc(i32 1)
store i8* %15, i8** %12
%16 = load i8*, i8** %12
store i8 0, i8* %16
store i64 1, i64* %14
%17 = load i8**, i8*** %5
%18 = load i8*, i8** %17
store i8* %18, i8** %9
store i8* %18, i8** %10
br label %19
19: ; preds = %4, %24, %61
%20 = load i8*, i8** %10
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 36
br i1 %23, label %24, label %54
24: ; preds = %19
%25 = load i8*, i8** %10
%26 = load i8*, i8** %9
%27 = ptrtoint i8* %25 to i64
%28 = ptrtoint i8* %26 to i64
%29 = sub i64 %27, %28
store i64 %29, i64* %13
%30 = load i8*, i8** %10
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %10
%32 = load i32, i32* %7
%33 = load i8**, i8*** %8
%34 = call i8* @expand_dollar_with_args(i8** %10, i32 %32, i8** %33)
store i8* %34, i8** %11
%35 = load i64, i64* %13
%36 = load i8*, i8** %11
%37 = call i64 @strlen(i8* %36)
%38 = add i64 %35, %37
%39 = load i64, i64* %14
%40 = add i64 %39, %38
store i64 %40, i64* %14
%41 = load i8*, i8** %12
%42 = load i64, i64* %14
%43 = call i8* @xrealloc(i8* %41, i64 %42)
store i8* %43, i8** %12
%44 = load i8*, i8** %12
%45 = load i8*, i8** %9
%46 = load i64, i64* %13
%47 = call i32 @strncat(i8* %44, i8* %45, i64 %46)
%48 = load i8*, i8** %12
%49 = load i8*, i8** %11
%50 = call i32 @strcat(i8* %48, i8* %49)
%51 = load i8*, i8** %11
%52 = call i32 @free(i8* %51)
%53 = load i8*, i8** %10
store i8* %53, i8** %9
br label %19
54: ; preds = %19
%55 = load i32 (i8)*, i32 (i8)** %6
%56 = load i8*, i8** %10
%57 = load i8, i8* %56
%58 = call i32 %55(i8 signext %57)
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %54
br label %64
61: ; preds = %54
%62 = load i8*, i8** %10
%63 = getelementptr inbounds i8, i8* %62, i32 1
store i8* %63, i8** %10
br label %19
64: ; preds = %60
%65 = load i8*, i8** %10
%66 = load i8*, i8** %9
%67 = ptrtoint i8* %65 to i64
%68 = ptrtoint i8* %66 to i64
%69 = sub i64 %67, %68
store i64 %69, i64* %13
%70 = load i64, i64* %13
%71 = load i64, i64* %14
%72 = add i64 %71, %70
store i64 %72, i64* %14
%73 = load i8*, i8** %12
%74 = load i64, i64* %14
%75 = call i8* @xrealloc(i8* %73, i64 %74)
store i8* %75, i8** %12
%76 = load i8*, i8** %12
%77 = load i8*, i8** %9
%78 = load i64, i64* %13
%79 = call i32 @strncat(i8* %76, i8* %77, i64 %78)
%80 = load i8*, i8** %10
%81 = load i8**, i8*** %5
store i8* %80, i8** %81
%82 = load i8*, i8** %12
ret i8* %82
}

LLVM IR (-O2):
define i8* @__expand_string(i8** nocapture %0, i32 (i8)* nocapture %1, i32 %2, i8** %3) {
%5 = alloca i8*
%6 = bitcast i8** %5 to i8*
%7 = tail call i8* @xmalloc(i32 1)
store i8 0, i8* %7
%8 = load i8*, i8** %0
store i8* %8, i8** %5
br label %9
9: ; preds = %17, %4
%10 = phi i8* [ %31, %17 ], [ %8, %4 ]
%11 = phi i8* [ %27, %17 ], [ %7, %4 ]
%12 = phi i64 [ %26, %17 ], [ 1, %4 ]
%13 = load i8, i8* %10
%14 = icmp eq i8 %13, 36
br i1 %14, label %17, label %32
15: ; preds = %37
%16 = getelementptr inbounds i8, i8* %36, i64 1
br label %17
17: ; preds = %15, %9
%18 = phi i8* [ %10, %9 ], [ %16, %15 ]
%19 = ptrtoint i8* %18 to i64
%20 = ptrtoint i8* %10 to i64
%21 = sub i64 %19, %20
%22 = getelementptr inbounds i8, i8* %18, i64 1
store i8* %22, i8** %5
%23 = call i8* @expand_dollar_with_args(i8** %5, i32 %2, i8** %3)
%24 = call i64 @strlen(i8* dereferenceable(1) %23)
%25 = add i64 %21, %12
%26 = add i64 %25, %24
%27 = call i8* @xrealloc(i8* %11, i64 %26)
%28 = call i32 @strncat(i8* %27, i8* %10, i64 %21)
%29 = call i32 @strcat(i8* %27, i8* %23)
%30 = call i32 @free(i8* %23)
%31 = load i8*, i8** %5
br label %9
32: ; preds = %9, %37
%33 = phi i8 [ %39, %37 ], [ %13, %9 ]
%34 = call i32 %1(i8 signext %33)
%35 = icmp eq i32 %34, 0
%36 = load i8*, i8** %5
br i1 %35, label %37, label %41
37: ; preds = %32
%38 = getelementptr inbounds i8, i8* %36, i64 1
store i8* %38, i8** %5
%39 = load i8, i8* %38
%40 = icmp eq i8 %39, 36
br i1 %40, label %15, label %32
41: ; preds = %32
%42 = ptrtoint i8* %36 to i64
%43 = ptrtoint i8* %10 to i64
%44 = sub i64 %42, %43
%45 = add i64 %44, %12
%46 = call i8* @xrealloc(i8* %11, i64 %45)
%47 = call i32 @strncat(i8* %46, i8* %10, i64 %44)
%48 = load i8*, i8** %5
store i8* %48, i8** %0
ret i8* %46
}