#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 asprintf(char **, char *, char *, char *, ...);
char *getenv(char *);
char *strchr(char *, char);
char *strdup(char *);
char *strtok_r(char *, char *, char **);
char *expand_environment_variable(char *s) {
  char *p;
  char *r;
  char *pre;
  char *post;
  char *var;
  char a = '\0';
  int ret;
  if ((p = strchr(s, '$')) == NULL)
    return strdup(s);
  if (s[0] == '$') {
    pre = &a;
    p = strtok_r(s + 1, "/\0", &r);
  } else {
    pre = strtok_r(s, "$\0", &r);
    p = strtok_r(NULL, "/\0", &r);
  }
  var = getenv(p);
  post = strtok_r(NULL, "\0", &r);
  if (post == NULL)
    ret = asprintf(&p, "%s%s", pre, var);
  else
    ret = asprintf(&p, "%s%s/%s", pre, var, post);
  if (ret == -1) {
    p = strdup("error foo");
  }
  return p;
}


x86 assembly (clang, -O0):
<expand_environment_variable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movb   $0x0,-0x39(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x24,%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    118e <expand_environment_variable+0x3e>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    128c <expand_environment_variable+0x13c>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x24,%eax
jne    11c7 <expand_environment_variable+0x77>
lea    -0x39(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
lea    0xe4b(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x18(%rbp)
jmp    11f7 <expand_environment_variable+0xa7>
mov    -0x10(%rbp),%rdi
lea    0xe31(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe16(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdf7(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1249 <expand_environment_variable+0xf9>
mov    -0x28(%rbp),%rdx
mov    -0x38(%rbp),%rcx
lea    -0x18(%rbp),%rdi
lea    0xdce(%rip),%rsi
mov    $0x0,%al
call   1060 <asprintf@plt>
mov    %eax,-0x40(%rbp)
jmp    126a <expand_environment_variable+0x11a>
mov    -0x28(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x30(%rbp),%r8
lea    -0x18(%rbp),%rdi
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1060 <asprintf@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    1284 <expand_environment_variable+0x134>
lea    0xd9a(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_environment_variable>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
movb   $0x0,0xf(%rsp)
mov    $0x24,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1197 <expand_environment_variable+0x47>
cmpb   $0x24,(%rbx)
jne    11a8 <expand_environment_variable+0x58>
add    $0x1,%rbx
lea    0xe80(%rip),%rsi
lea    0x18(%rsp),%rdx
mov    %rbx,%rdi
call   1050 <strtok_r@plt>
mov    %rax,%rdi
lea    0xf(%rsp),%r14
jmp    11d6 <expand_environment_variable+0x86>
mov    %rbx,%rdi
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1070 <strdup@plt>
lea    0xe54(%rip),%rsi
lea    0x18(%rsp),%r15
mov    %rbx,%rdi
mov    %r15,%rdx
call   1050 <strtok_r@plt>
mov    %rax,%r14
lea    0xe37(%rip),%rsi
xor    %edi,%edi
mov    %r15,%rdx
call   1050 <strtok_r@plt>
mov    %rax,%rdi
mov    %rdi,0x10(%rsp)
call   1030 <getenv@plt>
mov    %rax,%rbx
lea    0xe1c(%rip),%rsi
lea    0x18(%rsp),%rdx
xor    %edi,%edi
call   1050 <strtok_r@plt>
test   %rax,%rax
je     1223 <expand_environment_variable+0xd3>
lea    0xe0b(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
je     1241 <expand_environment_variable+0xf1>
mov    0x10(%rsp),%rax
jmp    124d <expand_environment_variable+0xfd>
lea    0xdde(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %r14,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    121c <expand_environment_variable+0xcc>
lea    0xdcd(%rip),%rdi
call   1070 <strdup@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"/\00\00"
@.str.1 = [3 x i8] c"$\00\00"
@.str.2 = [2 x i8] zeroinitializer
@.str.3 = [5 x i8] c"%s%s\00"
@.str.4 = [8 x i8] c"%s%s/%s\00"
@.str.5 = [10 x i8] c"error foo\00"
define i8* @expand_environment_variable(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 i8
%10 = alloca i32
store i8* %0, i8** %3
store i8 0, i8* %9
%11 = load i8*, i8** %3
%12 = call i8* @strchr(i8* %11, i8 signext 36)
store i8* %12, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call i8* @strdup(i8* %15)
store i8* %16, i8** %2
br label %53
17: ; preds = %1
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i64 0
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 36
br i1 %22, label %23, label %27
23: ; preds = %17
store i8* %9, i8** %6
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i64 1
%26 = call i8* @strtok_r(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %5)
store i8* %26, i8** %4
br label %31
27: ; preds = %17
%28 = load i8*, i8** %3
%29 = call i8* @strtok_r(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %5)
store i8* %29, i8** %6
%30 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %5)
store i8* %30, i8** %4
br label %31
31: ; preds = %27, %23
%32 = load i8*, i8** %4
%33 = call i8* @getenv(i8* %32)
store i8* %33, i8** %8
%34 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %5)
store i8* %34, i8** %7
%35 = load i8*, i8** %7
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %41
37: ; preds = %31
%38 = load i8*, i8** %6
%39 = load i8*, i8** %8
%40 = call i32 (i8**, i8*, i8*, i8*, ...) @asprintf(i8** %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %38, i8* %39)
store i32 %40, i32* %10
br label %46
41: ; preds = %31
%42 = load i8*, i8** %6
%43 = load i8*, i8** %8
%44 = load i8*, i8** %7
%45 = call i32 (i8**, i8*, i8*, i8*, ...) @asprintf(i8** %4, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %42, i8* %43, i8* %44)
store i32 %45, i32* %10
br label %46
46: ; preds = %41, %37
%47 = load i32, i32* %10
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i8* @strdup(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
store i8* %50, i8** %4
br label %51
51: ; preds = %49, %46
%52 = load i8*, i8** %4
store i8* %52, i8** %2
br label %53
53: ; preds = %51, %14
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
@.str = [3 x i8] c"/\00\00"
@.str.1 = [3 x i8] c"$\00\00"
@.str.2 = [2 x i8] zeroinitializer
@.str.3 = [5 x i8] c"%s%s\00"
@.str.4 = [8 x i8] c"%s%s/%s\00"
@.str.5 = [10 x i8] c"error foo\00"
define i8* @expand_environment_variable(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8
%5 = bitcast i8** %2 to i8*
%6 = bitcast i8** %3 to i8*
store i8 0, i8* %4
%7 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 36)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i8* @strdup(i8* %0)
br label %37
11: ; preds = %1
%12 = load i8, i8* %0
%13 = icmp eq i8 %12, 36
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = getelementptr inbounds i8, i8* %0, i64 1
%16 = call i8* @strtok_r(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %3)
br label %20
17: ; preds = %11
%18 = call i8* @strtok_r(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %3)
%19 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %3)
br label %20
20: ; preds = %17, %14
%21 = phi i8* [ %19, %17 ], [ %16, %14 ]
%22 = phi i8* [ %18, %17 ], [ %4, %14 ]
store i8* %21, i8** %2
%23 = call i8* @getenv(i8* %21)
%24 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %3)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 (i8**, i8*, i8*, i8*, ...) @asprintf(i8** %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %22, i8* %23)
br label %30
28: ; preds = %20
%29 = call i32 (i8**, i8*, i8*, i8*, ...) @asprintf(i8** %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %22, i8* %23, i8* %24)
br label %30
30: ; preds = %28, %26
%31 = phi i32 [ %27, %26 ], [ %29, %28 ]
%32 = icmp eq i32 %31, -1
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = load i8*, i8** %2
br label %37
35: ; preds = %30
%36 = call dereferenceable_or_null(10) i8* @strdup(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
br label %37
37: ; preds = %35, %33, %9
%38 = phi i8* [ %10, %9 ], [ %34, %33 ], [ %36, %35 ]
ret i8* %38
}