#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 */
typedef int /*<<< orphan*/ varname;

/* Variables and functions */
int /*<<< orphan*/ buffer_appendf(char **, int *, char *, ...);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
scalar_t__ is_variable_character(char const);
size_t strlen(char const *);
char *str_expand_envs(const char *in) {
  size_t inlen = strlen(in);
  size_t inpos = 0;
  char *out = NULL;
  int outpos = 0;
  while (inpos < inlen) {
    if (in[inpos] != '$') {
      buffer_appendf(&out, &outpos, "%c", in[inpos]);
      inpos++;
      continue;
    } else {
      inpos++;
      char *varname = NULL;
      int varnamepos = 0;
      while (varnamepos < sizeof(varname) && inpos < inlen &&
             is_variable_character(in[inpos])) {
        buffer_appendf(&varname, &varnamepos, "%c", in[inpos]);
        inpos++;
      }
      char *env = getenv(varname);
      if (env)
        buffer_appendf(&out, &outpos, "%s", env);
      free(varname);
    }
  }
  return out;
}


x86 assembly (clang, -O0):
<str_expand_envs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    12b5 <str_expand_envs+0x165>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x24,%eax
je     11d6 <str_expand_envs+0x86>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
lea    -0x20(%rbp),%rdi
lea    -0x24(%rbp),%rsi
lea    0xe42(%rip),%rdx
mov    $0x0,%al
call   1070 <buffer_appendf@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1180 <str_expand_envs+0x30>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movslq -0x34(%rbp),%rcx
xor    %eax,%eax
cmp    $0x8,%rcx
mov    %al,-0x41(%rbp)
jae    1232 <str_expand_envs+0xe2>
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x41(%rbp)
jae    1232 <str_expand_envs+0xe2>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1050 <is_variable_character@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    1242 <str_expand_envs+0xf2>
jmp    1275 <str_expand_envs+0x125>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
lea    -0x30(%rbp),%rdi
lea    -0x34(%rbp),%rsi
lea    0xda3(%rip),%rdx
mov    $0x0,%al
call   1070 <buffer_appendf@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11f1 <str_expand_envs+0xa1>
mov    -0x30(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12a7 <str_expand_envs+0x157>
mov    -0x40(%rbp),%rcx
lea    -0x20(%rbp),%rdi
lea    -0x24(%rbp),%rsi
lea    0xd63(%rip),%rdx
mov    $0x0,%al
call   1070 <buffer_appendf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
jmp    1180 <str_expand_envs+0x30>
mov    -0x20(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_expand_envs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
call   1060 <strlen@plt>
movq   $0x0,0x10(%rsp)
movl   $0x0,0x4(%rsp)
test   %rax,%rax
je     127c <str_expand_envs+0x12c>
mov    %rax,%r14
lea    0xe76(%rip),%r15
lea    0x8(%rsp),%rbp
mov    %rsp,%r13
xor    %r12d,%r12d
jmp    11c1 <str_expand_envs+0x71>
nopw   0x0(%rax,%rax,1)
lea    0x10(%rsp),%rdi
lea    0x4(%rsp),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <buffer_appendf@plt>
add    $0x1,%r12
cmp    %r14,%r12
jae    1275 <str_expand_envs+0x125>
movsbl (%rbx,%r12,1),%ecx
cmp    $0x24,%ecx
jne    11a0 <str_expand_envs+0x50>
movq   $0x0,0x8(%rsp)
movl   $0x0,(%rsp)
add    $0x1,%r12
cmp    %r14,%r12
jae    1230 <str_expand_envs+0xe0>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl (%rbx,%r12,1),%edi
call   1050 <is_variable_character@plt>
test   %rax,%rax
je     1223 <str_expand_envs+0xd3>
movsbl (%rbx,%r12,1),%ecx
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <buffer_appendf@plt>
add    $0x1,%r12
cmpl   $0x7,(%rsp)
ja     1223 <str_expand_envs+0xd3>
cmp    %r14,%r12
jb     11f0 <str_expand_envs+0xa0>
mov    0x8(%rsp),%rbp
jmp    1232 <str_expand_envs+0xe2>
nopw   0x0(%rax,%rax,1)
xor    %ebp,%ebp
mov    %rbp,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     125f <str_expand_envs+0x10f>
lea    0x10(%rsp),%rdi
lea    0x4(%rsp),%rsi
lea    0xdb3(%rip),%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <buffer_appendf@plt>
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <free@plt>
lea    0x8(%rsp),%rbp
cmp    %r14,%r12
jb     11c1 <str_expand_envs+0x71>
mov    0x10(%rsp),%rax
jmp    127e <str_expand_envs+0x12e>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%c\00"
@.str.1 = [3 x i8] c"%s\00"
define i8* @str_expand_envs(i8* nocapture %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%8 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%9 = icmp eq i64 %6, 0
br i1 %9, label %59, label %10
10: ; preds = %1
%11 = bitcast i8** %4 to i8*
%12 = bitcast i32* %5 to i8*
br label %13
13: ; preds = %10, %22
%14 = phi i64 [ 0, %10 ], [ %23, %22 ]
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 36
br i1 %17, label %25, label %18
18: ; preds = %13
%19 = sext i8 %16 to i32
%20 = call i32 (i8**, i32*, i8*, ...) @buffer_appendf(i8** %2, i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = add nuw i64 %14, 1
br label %22
22: ; preds = %18, %54
%23 = phi i64 [ %21, %18 ], [ %48, %54 ]
%24 = icmp ult i64 %23, %6
br i1 %24, label %13, label %57
25: ; preds = %13
store i8* null, i8** %4
store i32 0, i32* %5
%26 = add nuw i64 %14, 1
%27 = icmp ult i64 %26, %6
br i1 %27, label %28, label %46
28: ; preds = %25, %34
%29 = phi i64 [ %38, %34 ], [ %26, %25 ]
%30 = getelementptr inbounds i8, i8* %0, i64 %29
%31 = load i8, i8* %30
%32 = call i64 @is_variable_character(i8 signext %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %43, label %34
34: ; preds = %28
%35 = load i8, i8* %30
%36 = sext i8 %35 to i32
%37 = call i32 (i8**, i32*, i8*, ...) @buffer_appendf(i8** %4, i32* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %36)
%38 = add nuw i64 %29, 1
%39 = load i32, i32* %5
%40 = icmp ult i32 %39, 8
%41 = icmp ult i64 %38, %6
%42 = select i1 %40, i1 %41, i1 false
br i1 %42, label %28, label %43
43: ; preds = %34, %28
%44 = phi i64 [ %29, %28 ], [ %38, %34 ]
%45 = load i8*, i8** %4
br label %46
46: ; preds = %43, %25
%47 = phi i8* [ null, %25 ], [ %45, %43 ]
%48 = phi i64 [ %26, %25 ], [ %44, %43 ]
%49 = call i8* @getenv(i8* %47)
%50 = icmp eq i8* %49, null
br i1 %50, label %54, label %51
51: ; preds = %46
%52 = call i32 (i8**, i32*, i8*, ...) @buffer_appendf(i8** %2, i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %49)
%53 = load i8*, i8** %4
br label %54
54: ; preds = %51, %46
%55 = phi i8* [ %53, %51 ], [ %47, %46 ]
%56 = call i32 @free(i8* %55)
br label %22
57: ; preds = %22
%58 = load i8*, i8** %2
br label %59
59: ; preds = %57, %1
%60 = phi i8* [ %58, %57 ], [ null, %1 ]
ret i8* %60
}