#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 PATH_MAX;
char *getenv(char *);
char *strchr(char *, char);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
char *cpio_replace_env(char *new_location) {
  char expanded[PATH_MAX + 1];
  char env_var[PATH_MAX + 1];
  char *start;
  char *end;
  char *env_val;
  for (start = NULL; (start = strstr(new_location, "${"));) {
    end = strchr(start, '}');
    if (start < end) {
      *env_var = *expanded = '\0';
      strncat(env_var, start + 2, end - start - 2);
      strncat(expanded, new_location, start - new_location);
      env_val = getenv(env_var);
      if (NULL != env_val)
        strncat(expanded, env_val, PATH_MAX);
      strncat(expanded, end + 1, PATH_MAX);
      strncpy(new_location, expanded, PATH_MAX);
    } else
      break;
  }
  return new_location;
}


x86 assembly (clang, -O0):
<cpio_replace_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %rsp,%rdx
mov    %rdx,-0x10(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x48(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x18(%rbp)
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12a6 <cpio_replace_env+0x156>
mov    -0x28(%rbp),%rdi
mov    $0x7d,%esi
call   1050 <strchr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    129c <cpio_replace_env+0x14c>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax)
movb   $0x0,(%rdi)
mov    -0x28(%rbp),%rsi
add    $0x2,%rsi
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
sub    $0x2,%rax
mov    %eax,%edx
call   1060 <strncat@plt>
mov    -0x48(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1060 <strncat@plt>
mov    -0x40(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmp    -0x38(%rbp),%rax
je     1267 <cpio_replace_env+0x117>
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2d90(%rip),%rax
mov    (%rax),%edx
call   1060 <strncat@plt>
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    $0x1,%rsi
mov    0x2d76(%rip),%rax
mov    (%rax),%edx
call   1060 <strncat@plt>
mov    -0x48(%rbp),%rsi
mov    -0x8(%rbp),%rdi
mov    0x2d60(%rip),%rax
mov    (%rax),%edx
call   1040 <strncpy@plt>
jmp    12a1 <cpio_replace_env+0x151>
jmp    12a6 <cpio_replace_env+0x156>
jmp    11b4 <cpio_replace_env+0x64>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpio_replace_env>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    %rsp,%r15
sub    %rax,%r15
mov    %r15,%rsp
lea    0xe72(%rip),%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     1259 <cpio_replace_env+0x109>
mov    %rax,%r12
jmp    11e9 <cpio_replace_env+0x99>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
mov    (%r12),%edx
mov    %r13,%rdi
mov    %rbx,%rsi
call   1060 <strncat@plt>
mov    (%r12),%edx
mov    %r14,%rdi
mov    %r13,%rsi
call   1040 <strncpy@plt>
mov    %r14,%rdi
lea    0xe24(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,%r12
test   %rax,%rax
je     1259 <cpio_replace_env+0x109>
mov    %r12,%rdi
mov    $0x7d,%esi
call   1050 <strchr@plt>
cmp    %rax,%r12
jae    1259 <cpio_replace_env+0x109>
mov    %rax,%rbx
movb   $0x0,0x0(%r13)
movb   $0x0,(%r15)
lea    0x2(%r12),%rsi
mov    %ebx,%edx
sub    %r12d,%edx
add    $0xfffffffe,%edx
mov    %r15,%rdi
call   1060 <strncat@plt>
sub    %r14d,%r12d
mov    %r13,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1060 <strncat@plt>
mov    %r15,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
mov    0x2db1(%rip),%r12
je     11b0 <cpio_replace_env+0x60>
mov    (%r12),%edx
mov    %r13,%rdi
mov    %rax,%rsi
call   1060 <strncat@plt>
jmp    11b0 <cpio_replace_env+0x60>
mov    %r14,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [3 x i8] c"${\00"
define i8* @cpio_replace_env(i8* returned %0) {
%2 = load i32, i32* @PATH_MAX
%3 = add nsw i32 %2, 1
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = alloca i8, i64 %4
%7 = call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %39, label %9
9: ; preds = %1
%10 = ptrtoint i8* %0 to i64
br label %11
11: ; preds = %9, %31
%12 = phi i8* [ %7, %9 ], [ %37, %31 ]
%13 = call i8* @strchr(i8* %12, i8 signext 125)
%14 = icmp ult i8* %12, %13
br i1 %14, label %15, label %39
15: ; preds = %11
store i8 0, i8* %5
store i8 0, i8* %6
%16 = getelementptr inbounds i8, i8* %12, i64 2
%17 = ptrtoint i8* %13 to i64
%18 = ptrtoint i8* %12 to i64
%19 = sub i64 %17, %18
%20 = trunc i64 %19 to i32
%21 = add i32 %20, -2
%22 = call i32 @strncat(i8* %6, i8* %16, i32 %21)
%23 = sub i64 %18, %10
%24 = trunc i64 %23 to i32
%25 = call i32 @strncat(i8* %5, i8* %0, i32 %24)
%26 = call i8* @getenv(i8* %6)
%27 = icmp eq i8* %26, null
br i1 %27, label %31, label %28
28: ; preds = %15
%29 = load i32, i32* @PATH_MAX
%30 = call i32 @strncat(i8* %5, i8* %26, i32 %29)
br label %31
31: ; preds = %28, %15
%32 = getelementptr inbounds i8, i8* %13, i64 1
%33 = load i32, i32* @PATH_MAX
%34 = call i32 @strncat(i8* %5, i8* %32, i32 %33)
%35 = load i32, i32* @PATH_MAX
%36 = call i32 @strncpy(i8* %0, i8* %5, i32 %35)
%37 = call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %11
39: ; preds = %31, %11, %1
ret i8* %0
}