#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;
  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);
      strncat(expanded, getenv(env_var), 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    $0x40,%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,-0x40(%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,-0x38(%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     1295 <cpio_replace_env+0x145>
mov    -0x28(%rbp),%rdi
mov    $0x7d,%esi
call   1050 <strchr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    128b <cpio_replace_env+0x13b>
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rdi
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    -0x40(%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    -0x38(%rbp),%rdi
call   1030 <getenv@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rsi
mov    0x2da1(%rip),%rax
mov    (%rax),%edx
call   1060 <strncat@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    $0x1,%rsi
mov    0x2d87(%rip),%rax
mov    (%rax),%edx
call   1060 <strncat@plt>
mov    -0x40(%rbp),%rsi
mov    -0x8(%rbp),%rdi
mov    0x2d71(%rip),%rax
mov    (%rax),%edx
call   1040 <strncpy@plt>
jmp    1290 <cpio_replace_env+0x140>
jmp    1295 <cpio_replace_env+0x145>
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     123b <cpio_replace_env+0xeb>
mov    %rax,%rbx
nop
mov    %rbx,%rdi
mov    $0x7d,%esi
call   1050 <strchr@plt>
cmp    %rax,%rbx
jae    123b <cpio_replace_env+0xeb>
mov    %rax,%r12
movb   $0x0,0x0(%r13)
movb   $0x0,(%r15)
lea    0x2(%rbx),%rsi
mov    %r12d,%edx
sub    %ebx,%edx
add    $0xfffffffe,%edx
mov    %r15,%rdi
call   1060 <strncat@plt>
sub    %r14d,%ebx
mov    %r13,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1060 <strncat@plt>
mov    %r15,%rdi
call   1030 <getenv@plt>
mov    0x2dfb(%rip),%rbx
mov    (%rbx),%edx
mov    %r13,%rdi
mov    %rax,%rsi
call   1060 <strncat@plt>
add    $0x1,%r12
mov    (%rbx),%edx
mov    %r13,%rdi
mov    %r12,%rsi
call   1060 <strncat@plt>
mov    (%rbx),%edx
mov    %r14,%rdi
mov    %r13,%rsi
call   1040 <strncpy@plt>
mov    %r14,%rdi
lea    0xdd6(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a0 <cpio_replace_env+0x50>
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*
store i8* %0, i8** %2
%8 = load i32, i32* @PATH_MAX
%9 = add nsw i32 %8, 1
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %3
%12 = alloca i8, i64 %10
store i64 %10, i64* %4
%13 = load i32, i32* @PATH_MAX
%14 = add nsw i32 %13, 1
%15 = zext i32 %14 to i64
%16 = alloca i8, i64 %15
store i64 %15, i64* %5
store i8* null, i8** %6
br label %17
17: ; preds = %57, %1
%18 = load i8*, i8** %2
%19 = call i8* @strstr(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %19, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %58
21: ; preds = %17
%22 = load i8*, i8** %6
%23 = call i8* @strchr(i8* %22, i8 signext 125)
store i8* %23, i8** %7
%24 = load i8*, i8** %6
%25 = load i8*, i8** %7
%26 = icmp ult i8* %24, %25
br i1 %26, label %27, label %56
27: ; preds = %21
store i8 0, i8* %12
store i8 0, i8* %16
%28 = load i8*, i8** %6
%29 = getelementptr inbounds i8, i8* %28, i64 2
%30 = load i8*, i8** %7
%31 = load i8*, i8** %6
%32 = ptrtoint i8* %30 to i64
%33 = ptrtoint i8* %31 to i64
%34 = sub i64 %32, %33
%35 = sub nsw i64 %34, 2
%36 = trunc i64 %35 to i32
%37 = call i32 @strncat(i8* %16, i8* %29, i32 %36)
%38 = load i8*, i8** %2
%39 = load i8*, i8** %6
%40 = load i8*, i8** %2
%41 = ptrtoint i8* %39 to i64
%42 = ptrtoint i8* %40 to i64
%43 = sub i64 %41, %42
%44 = trunc i64 %43 to i32
%45 = call i32 @strncat(i8* %12, i8* %38, i32 %44)
%46 = call i8* @getenv(i8* %16)
%47 = load i32, i32* @PATH_MAX
%48 = call i32 @strncat(i8* %12, i8* %46, i32 %47)
%49 = load i8*, i8** %7
%50 = getelementptr inbounds i8, i8* %49, i64 1
%51 = load i32, i32* @PATH_MAX
%52 = call i32 @strncat(i8* %12, i8* %50, i32 %51)
%53 = load i8*, i8** %2
%54 = load i32, i32* @PATH_MAX
%55 = call i32 @strncpy(i8* %53, i8* %12, i32 %54)
br label %57
56: ; preds = %21
br label %58
57: ; preds = %27
br label %17
58: ; preds = %56, %17
%59 = load i8*, i8** %2
%60 = load i8*, i8** %3
ret i8* %59
}

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 %36, label %9
9: ; preds = %1
%10 = ptrtoint i8* %0 to i64
br label %11
11: ; preds = %9, %15
%12 = phi i8* [ %7, %9 ], [ %34, %15 ]
%13 = call i8* @strchr(i8* %12, i8 signext 125)
%14 = icmp ult i8* %12, %13
br i1 %14, label %15, label %36
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 = load i32, i32* @PATH_MAX
%28 = call i32 @strncat(i8* %5, i8* %26, i32 %27)
%29 = getelementptr inbounds i8, i8* %13, i64 1
%30 = load i32, i32* @PATH_MAX
%31 = call i32 @strncat(i8* %5, i8* %29, i32 %30)
%32 = load i32, i32* @PATH_MAX
%33 = call i32 @strncpy(i8* %0, i8* %5, i32 %32)
%34 = call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %11
36: ; preds = %15, %11, %1
ret i8* %0
}