#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 */
int /*<<< orphan*/ free(char *);
char *getenv(char *);
char *malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strstr(char *, char *);
char *expand_envstr(char *path) {
  char *srcptr, *envptr, *tmpptr, *dup, *envname;
  int len;
  srcptr = strstr(path, "$(");
  if (!srcptr)
    return path;
  len = strlen(path);
  if (!len)
    return path;
  dup = malloc(len + 1);
  if (!dup)
    return path;
  strcpy(dup, path);
  envptr = strstr(dup, "$(") + 2;
  tmpptr = strchr(envptr, ')');
  if (!tmpptr) {
    free(dup);
    return path;
  }
  *tmpptr = 0;
  *srcptr = 0;
  envname = getenv(envptr);
  if (envname)
    strcat(srcptr, envname);
  strcat(srcptr, tmpptr + 1);
  free(dup);
  return expand_envstr(path);
}


x86 assembly (clang, -O0):
<expand_envstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   10b0 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c8 <expand_envstr+0x38>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12c4 <expand_envstr+0x134>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    11eb <expand_envstr+0x5b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12c4 <expand_envstr+0x134>
mov    -0x3c(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1212 <expand_envstr+0x82>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12c4 <expand_envstr+0x134>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x30(%rbp),%rdi
lea    0xdd6(%rip),%rsi
call   10b0 <strstr@plt>
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x29,%esi
call   1070 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    126a <expand_envstr+0xda>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12c4 <expand_envstr+0x134>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     129d <expand_envstr+0x10d>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
call   1090 <strcat@plt>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <expand_envstr@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_envstr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
lea    0xe6b(%rip),%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     125f <expand_envstr+0xdf>
mov    %rax,%r12
lea    0xe53(%rip),%r14
jmp    11de <expand_envstr+0x5e>
nop
add    $0x1,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
mov    %r13,%rdi
call   1040 <free@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   10a0 <strstr@plt>
mov    %rax,%r12
test   %rax,%rax
je     125f <expand_envstr+0xdf>
mov    %r15,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
je     125f <expand_envstr+0xdf>
add    $0x1,%eax
mov    %eax,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     125f <expand_envstr+0xdf>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r15,%rsi
call   1050 <strcpy@plt>
mov    %r13,%rdi
mov    %r14,%rsi
call   10a0 <strstr@plt>
mov    %rax,%rbp
add    $0x2,%rbp
mov    %rbp,%rdi
mov    $0x29,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     1257 <expand_envstr+0xd7>
mov    %rax,%rbx
movb   $0x0,(%rax)
movb   $0x0,(%r12)
mov    %rbp,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11b0 <expand_envstr+0x30>
mov    %r12,%rdi
mov    %rax,%rsi
call   1090 <strcat@plt>
jmp    11b0 <expand_envstr+0x30>
mov    %r13,%rdi
call   1040 <free@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"$(\00"
define i8* @expand_envstr(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = call i8* @strstr(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %1
%15 = load i8*, i8** %3
store i8* %15, i8** %2
br label %66
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp ne i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = load i8*, i8** %3
store i8* %22, i8** %2
br label %66
23: ; preds = %16
%24 = load i32, i32* %9
%25 = add nsw i32 %24, 1
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** %7
%27 = load i8*, i8** %7
%28 = icmp ne i8* %27, null
br i1 %28, label %31, label %29
29: ; preds = %23
%30 = load i8*, i8** %3
store i8* %30, i8** %2
br label %66
31: ; preds = %23
%32 = load i8*, i8** %7
%33 = load i8*, i8** %3
%34 = call i32 @strcpy(i8* %32, i8* %33)
%35 = load i8*, i8** %7
%36 = call i8* @strstr(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%37 = getelementptr inbounds i8, i8* %36, i64 2
store i8* %37, i8** %5
%38 = load i8*, i8** %5
%39 = call i8* @strchr(i8* %38, i8 signext 41)
store i8* %39, i8** %6
%40 = load i8*, i8** %6
%41 = icmp ne i8* %40, null
br i1 %41, label %46, label %42
42: ; preds = %31
%43 = load i8*, i8** %7
%44 = call i32 @free(i8* %43)
%45 = load i8*, i8** %3
store i8* %45, i8** %2
br label %66
46: ; preds = %31
%47 = load i8*, i8** %6
store i8 0, i8* %47
%48 = load i8*, i8** %4
store i8 0, i8* %48
%49 = load i8*, i8** %5
%50 = call i8* @getenv(i8* %49)
store i8* %50, i8** %8
%51 = load i8*, i8** %8
%52 = icmp ne i8* %51, null
br i1 %52, label %53, label %57
53: ; preds = %46
%54 = load i8*, i8** %4
%55 = load i8*, i8** %8
%56 = call i32 @strcat(i8* %54, i8* %55)
br label %57
57: ; preds = %53, %46
%58 = load i8*, i8** %4
%59 = load i8*, i8** %6
%60 = getelementptr inbounds i8, i8* %59, i64 1
%61 = call i32 @strcat(i8* %58, i8* %60)
%62 = load i8*, i8** %7
%63 = call i32 @free(i8* %62)
%64 = load i8*, i8** %3
%65 = call i8* @expand_envstr(i8* %64)
store i8* %65, i8** %2
br label %66
66: ; preds = %57, %42, %29, %21, %14
%67 = load i8*, i8** %2
ret i8* %67
}

LLVM IR (-O2):
@.str = [3 x i8] c"$(\00"
define i8* @expand_envstr(i8* %0) {
%2 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %31, label %4
4: ; preds = %1, %25
%5 = phi i8* [ %29, %25 ], [ %2, %1 ]
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %31, label %8
8: ; preds = %4
%9 = add nsw i32 %6, 1
%10 = tail call i8* @malloc(i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %31, label %12
12: ; preds = %8
%13 = tail call i32 @strcpy(i8* %10, i8* %0)
%14 = tail call i8* @strstr(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%15 = getelementptr inbounds i8, i8* %14, i64 2
%16 = tail call i8* @strchr(i8* %15, i8 signext 41)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = tail call i32 @free(i8* %10)
br label %31
20: ; preds = %12
store i8 0, i8* %16
store i8 0, i8* %5
%21 = tail call i8* @getenv(i8* %15)
%22 = icmp eq i8* %21, null
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 @strcat(i8* %5, i8* %21)
br label %25
25: ; preds = %23, %20
%26 = getelementptr inbounds i8, i8* %16, i64 1
%27 = tail call i32 @strcat(i8* %5, i8* %26)
%28 = tail call i32 @free(i8* %10)
%29 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %4
31: ; preds = %25, %4, %8, %1, %18
ret i8* %0
}