#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 */
scalar_t__ allowed_env_var_name(char const);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
char *calloc(int, size_t);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char const *, int, ...);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *expand_environment(const char *input, const char *src_name,
                         int src_line_no) {
  char *result = malloc((128 * 1024));
  char *out = result;
  const char *in = input;
  while (*in != '\0') {
    if (out >= result + (128 * 1024) - 1) {
      goto too_long;
    }
    if (*in != '$') {
      *out++ = *in++;
      continue;
    }
    in++;
    const char *env_start = in;
    while (allowed_env_var_name(*in)) {
      in++;
    }
    size_t env_len = in - env_start;
    char *env_name = calloc(1, env_len + 1);
    assert(env_name != NULL);
    strncpy(env_name, env_start, env_len);
    const char *value = getenv(env_name);
    if (value == NULL || strlen(value) == 0) {
      printf("%s:%d: undefined environment variable \"%s\"\n", src_name,
             src_line_no, env_name);
      exit(1);
    }
    free(env_name);
    if (out + strlen(value) >= result + (128 * 1024) - 1) {
      goto too_long;
    }
    strcpy(out, value);
    out += strlen(value);
  }
  *out = '\0';
  return result;
too_long:
  printf("%s:%d: Expansion is longer than %d bytes\n", src_name, src_line_no,
         (128 * 1024));
  free(result);
  exit(1);
}


x86 assembly (clang, -O0):
<expand_environment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x20000,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1387 <expand_environment+0x1d7>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    $0x20000,%rcx
add    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jb     1212 <expand_environment+0x62>
jmp    1398 <expand_environment+0x1e8>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x24,%eax
je     1249 <expand_environment+0x99>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
mov    (%rax),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
jmp    11e1 <expand_environment+0x31>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%edi
call   1080 <allowed_env_var_name@plt>
cmp    $0x0,%rax
je     1284 <expand_environment+0xd4>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    125d <expand_environment+0xad>
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rsi
add    $0x1,%rsi
mov    $0x1,%edi
call   10a0 <calloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10b0 <assert@plt>
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1050 <strncpy@plt>
mov    -0x48(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12f6 <expand_environment+0x146>
mov    -0x50(%rbp),%rdi
call   1070 <strlen@plt>
cmp    $0x0,%eax
jne    1319 <expand_environment+0x169>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x48(%rbp),%rcx
lea    0xcf8(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x48(%rbp),%rdi
call   1040 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    -0x20(%rbp),%rcx
add    $0x20000,%rcx
add    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jb     135c <expand_environment+0x1ac>
jmp    1398 <expand_environment+0x1e8>
mov    -0x28(%rbp),%rdi
mov    -0x50(%rbp),%rsi
call   1060 <strcpy@plt>
mov    -0x50(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    11e1 <expand_environment+0x31>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xc86(%rip),%rdi
mov    $0x20000,%ecx
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<expand_environment>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,0xc(%rsp)
mov    %rsi,0x10(%rsp)
mov    %rdi,%rbx
mov    $0x20000,%edi
call   10c0 <malloc@plt>
mov    %rax,%r15
mov    (%rbx),%al
mov    %r15,%r13
test   %al,%al
je     12f3 <expand_environment+0x143>
xor    %r12d,%r12d
mov    %r15,%r13
mov    %r15,0x18(%rsp)
jmp    121a <expand_environment+0x6a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r12
mov    %al,0x0(%r13)
mov    %r14,%rbx
lea    (%r15,%r12,1),%r13
mov    (%r14),%al
test   %al,%al
je     12f3 <expand_environment+0x143>
cmp    $0x1fffe,%r12
jg     130a <expand_environment+0x15a>
lea    0x1(%rbx),%r14
cmp    $0x24,%al
jne    1200 <expand_environment+0x50>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl 0x1(%rbx,%rbp,1),%edi
call   1080 <allowed_env_var_name@plt>
add    $0x1,%rbp
test   %rax,%rax
jne    1240 <expand_environment+0x90>
lea    -0x1(%rbp),%rax
mov    %rax,0x20(%rsp)
mov    $0x1,%edi
mov    %rbp,%rsi
call   10a0 <calloc@plt>
mov    %rax,%r15
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   10b0 <assert@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    0x20(%rsp),%rdx
call   1050 <strncpy@plt>
mov    %r15,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1338 <expand_environment+0x188>
mov    %rax,%r14
mov    %rax,%rdi
call   1070 <strlen@plt>
test   %eax,%eax
je     1338 <expand_environment+0x188>
mov    %r15,%rdi
call   1040 <free@plt>
mov    %r14,%rdi
call   1070 <strlen@plt>
cltq
add    %r12,%rax
cmp    $0x1fffe,%rax
mov    0x18(%rsp),%r15
jg     130a <expand_environment+0x15a>
add    %rbp,%rbx
mov    %r13,%rdi
mov    %r14,%rsi
call   1060 <strcpy@plt>
mov    %r14,%rdi
call   1070 <strlen@plt>
cltq
add    %rax,%r12
mov    %rbx,%r14
jmp    120b <expand_environment+0x5b>
movb   $0x0,0x0(%r13)
mov    %r15,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd1b(%rip),%rdi
mov    0x10(%rsp),%rsi
mov    0xc(%rsp),%edx
mov    $0x20000,%ecx
xor    %eax,%eax
call   1090 <printf@plt>
mov    %r15,%rdi
call   1040 <free@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xcc1(%rip),%rdi
mov    0x10(%rsp),%rsi
mov    0xc(%rsp),%edx
mov    %r15,%rcx
xor    %eax,%eax
call   1090 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [44 x i8] c"%s:%d: undefined environment variable \22%s\22\0A\00"
@.str.1 = [42 x i8] c"%s:%d: Expansion is longer than %d bytes\0A\00"
define i8* @expand_environment(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%14 = call i8* @malloc(i32 131072)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
store i8* %15, i8** %8
%16 = load i8*, i8** %4
store i8* %16, i8** %9
br label %17
17: ; preds = %96, %34, %3
%18 = load i8*, i8** %9
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %105
22: ; preds = %17
%23 = load i8*, i8** %8
%24 = load i8*, i8** %7
%25 = getelementptr inbounds i8, i8* %24, i64 131072
%26 = getelementptr inbounds i8, i8* %25, i64 -1
%27 = icmp uge i8* %23, %26
br i1 %27, label %28, label %29
28: ; preds = %22
br label %108
29: ; preds = %22
%30 = load i8*, i8** %9
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 36
br i1 %33, label %34, label %40
34: ; preds = %29
%35 = load i8*, i8** %9
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %9
%37 = load i8, i8* %35
%38 = load i8*, i8** %8
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %8
store i8 %37, i8* %38
br label %17
40: ; preds = %29
%41 = load i8*, i8** %9
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %9
%43 = load i8*, i8** %9
store i8* %43, i8** %10
br label %44
44: ; preds = %49, %40
%45 = load i8*, i8** %9
%46 = load i8, i8* %45
%47 = call i64 @allowed_env_var_name(i8 signext %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = load i8*, i8** %9
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %9
br label %44
52: ; preds = %44
%53 = load i8*, i8** %9
%54 = load i8*, i8** %10
%55 = ptrtoint i8* %53 to i64
%56 = ptrtoint i8* %54 to i64
%57 = sub i64 %55, %56
store i64 %57, i64* %11
%58 = load i64, i64* %11
%59 = add i64 %58, 1
%60 = call i8* @calloc(i32 1, i64 %59)
store i8* %60, i8** %12
%61 = load i8*, i8** %12
%62 = icmp ne i8* %61, null
%63 = zext i1 %62 to i32
%64 = call i32 @assert(i32 %63)
%65 = load i8*, i8** %12
%66 = load i8*, i8** %10
%67 = load i64, i64* %11
%68 = call i32 @strncpy(i8* %65, i8* %66, i64 %67)
%69 = load i8*, i8** %12
%70 = call i8* @getenv(i8* %69)
store i8* %70, i8** %13
%71 = load i8*, i8** %13
%72 = icmp eq i8* %71, null
br i1 %72, label %77, label %73
73: ; preds = %52
%74 = load i8*, i8** %13
%75 = call i32 @strlen(i8* %74)
%76 = icmp eq i32 %75, 0
br i1 %76, label %77, label %83
77: ; preds = %73, %52
%78 = load i8*, i8** %5
%79 = load i32, i32* %6
%80 = load i8*, i8** %12
%81 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %78, i32 %79, i8* %80)
%82 = call i32 @exit(i32 1)
unreachable
83: ; preds = %73
%84 = load i8*, i8** %12
%85 = call i32 @free(i8* %84)
%86 = load i8*, i8** %8
%87 = load i8*, i8** %13
%88 = call i32 @strlen(i8* %87)
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds i8, i8* %86, i64 %89
%91 = load i8*, i8** %7
%92 = getelementptr inbounds i8, i8* %91, i64 131072
%93 = getelementptr inbounds i8, i8* %92, i64 -1
%94 = icmp uge i8* %90, %93
br i1 %94, label %95, label %96
95: ; preds = %83
br label %108
96: ; preds = %83
%97 = load i8*, i8** %8
%98 = load i8*, i8** %13
%99 = call i32 @strcpy(i8* %97, i8* %98)
%100 = load i8*, i8** %13
%101 = call i32 @strlen(i8* %100)
%102 = load i8*, i8** %8
%103 = sext i32 %101 to i64
%104 = getelementptr inbounds i8, i8* %102, i64 %103
store i8* %104, i8** %8
br label %17
105: ; preds = %17
%106 = load i8*, i8** %8
store i8 0, i8* %106
%107 = load i8*, i8** %7
ret i8* %107
108: ; preds = %95, %28
%109 = load i8*, i8** %5
%110 = load i32, i32* %6
%111 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %109, i32 %110, i32 131072)
%112 = load i8*, i8** %7
%113 = call i32 @free(i8* %112)
%114 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@.str = [44 x i8] c"%s:%d: undefined environment variable \22%s\22\0A\00"
@.str.1 = [42 x i8] c"%s:%d: Expansion is longer than %d bytes\0A\00"
define i8* @expand_environment(i8* %0, i8* %1, i32 %2) {
%4 = tail call dereferenceable_or_null(131072) i8* @malloc(i32 131072)
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %59, label %7
7: ; preds = %3, %18
%8 = phi i8 [ %22, %18 ], [ %5, %3 ]
%9 = phi i8* [ %21, %18 ], [ %4, %3 ]
%10 = phi i64 [ %20, %18 ], [ 0, %3 ]
%11 = phi i8* [ %19, %18 ], [ %0, %3 ]
%12 = icmp slt i64 %10, 131071
br i1 %12, label %13, label %61
13: ; preds = %7
%14 = icmp eq i8 %8, 36
%15 = getelementptr inbounds i8, i8* %11, i64 1
br i1 %14, label %24, label %16
16: ; preds = %13
%17 = add nsw i64 %10, 1
store i8 %8, i8* %9
br label %18
18: ; preds = %16, %54
%19 = phi i8* [ %15, %16 ], [ %25, %54 ]
%20 = phi i64 [ %17, %16 ], [ %58, %54 ]
%21 = getelementptr inbounds i8, i8* %4, i64 %20
%22 = load i8, i8* %19
%23 = icmp eq i8 %22, 0
br i1 %23, label %59, label %7
24: ; preds = %13, %24
%25 = phi i8* [ %29, %24 ], [ %15, %13 ]
%26 = load i8, i8* %25
%27 = tail call i64 @allowed_env_var_name(i8 signext %26)
%28 = icmp eq i64 %27, 0
%29 = getelementptr inbounds i8, i8* %25, i64 1
br i1 %28, label %30, label %24
30: ; preds = %24
%31 = ptrtoint i8* %25 to i64
%32 = ptrtoint i8* %15 to i64
%33 = sub i64 %31, %32
%34 = add i64 %33, 1
%35 = tail call i8* @calloc(i32 1, i64 %34)
%36 = icmp ne i8* %35, null
%37 = zext i1 %36 to i32
%38 = tail call i32 @assert(i32 %37)
%39 = tail call i32 @strncpy(i8* %35, i8* %15, i64 %33)
%40 = tail call i8* @getenv(i8* %35)
%41 = icmp eq i8* %40, null
br i1 %41, label %45, label %42
42: ; preds = %30
%43 = tail call i32 @strlen(i8* %40)
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42, %30
%46 = tail call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %2, i8* %35)
%47 = tail call i32 @exit(i32 1)
unreachable
48: ; preds = %42
%49 = tail call i32 @free(i8* %35)
%50 = tail call i32 @strlen(i8* %40)
%51 = sext i32 %50 to i64
%52 = add nsw i64 %10, %51
%53 = icmp slt i64 %52, 131071
br i1 %53, label %54, label %61
54: ; preds = %48
%55 = tail call i32 @strcpy(i8* %9, i8* %40)
%56 = tail call i32 @strlen(i8* %40)
%57 = sext i32 %56 to i64
%58 = add nsw i64 %10, %57
br label %18
59: ; preds = %18, %3
%60 = phi i8* [ %4, %3 ], [ %21, %18 ]
store i8 0, i8* %60
ret i8* %4
61: ; preds = %48, %7
%62 = tail call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %2, i32 131072)
%63 = tail call i32 @free(i8* %4)
%64 = tail call i32 @exit(i32 1)
unreachable
}