#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 strlen(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 - strlen(expanded));
      strncat(expanded, end + 1, PATH_MAX - strlen(expanded));
      strncpy(new_location, expanded, PATH_MAX);
      new_location[PATH_MAX] = 0;
    } else
      break;
  }
  return new_location;
}


x86 assembly (clang, -O0):
<cpio_replace_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e7d(%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    0xe31(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12e6 <cpio_replace_env+0x186>
mov    -0x28(%rbp),%rdi
mov    $0x7d,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    12dc <cpio_replace_env+0x17c>
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   1070 <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   1070 <strncat@plt>
mov    -0x38(%rbp),%rdi
call   1030 <getenv@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,-0x58(%rbp)
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
call   1050 <strlen@plt>
mov    -0x5c(%rbp),%edx
mov    -0x58(%rbp),%rsi
mov    -0x40(%rbp),%rdi
sub    %eax,%edx
call   1070 <strncat@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
call   1050 <strlen@plt>
mov    -0x4c(%rbp),%edx
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%rdi
sub    %eax,%edx
call   1070 <strncat@plt>
mov    -0x40(%rbp),%rsi
mov    -0x8(%rbp),%rdi
mov    0x2d32(%rip),%rax
mov    (%rax),%edx
call   1040 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    0x2d20(%rip),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12e1 <cpio_replace_env+0x181>
jmp    12e6 <cpio_replace_env+0x186>
jmp    11c4 <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
sub    $0x18,%rsp
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rsp,%rcx
sub    %rax,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
lea    0xe5a(%rip),%rsi
mov    %rdi,-0x30(%rbp)
call   1080 <strstr@plt>
test   %rax,%rax
je     1292 <cpio_replace_env+0x132>
mov    %rax,%r12
mov    -0x40(%rbp),%r13
nop
mov    %r12,%rdi
mov    $0x7d,%esi
call   1060 <strchr@plt>
cmp    %rax,%r12
jae    1292 <cpio_replace_env+0x132>
mov    %rax,%r14
movb   $0x0,0x0(%r13)
mov    -0x38(%rbp),%rbx
movb   $0x0,(%rbx)
lea    0x2(%r12),%rsi
mov    %r14d,%edx
sub    %r12d,%edx
add    $0xfffffffe,%edx
mov    %rbx,%rdi
call   1070 <strncat@plt>
mov    -0x30(%rbp),%rsi
sub    %esi,%r12d
mov    %r13,%rdi
mov    %r12d,%edx
call   1070 <strncat@plt>
mov    %rbx,%rdi
call   1030 <getenv@plt>
mov    %rax,%r12
mov    0x2dd1(%rip),%rax
mov    (%rax),%r15d
mov    %rax,%rbx
mov    %r13,%rdi
call   1050 <strlen@plt>
sub    %eax,%r15d
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1070 <strncat@plt>
add    $0x1,%r14
mov    %rbx,%r15
mov    (%rbx),%ebx
mov    %r13,%rdi
call   1050 <strlen@plt>
sub    %eax,%ebx
mov    %r13,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1070 <strncat@plt>
mov    (%r15),%edx
mov    -0x30(%rbp),%rbx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1040 <strncpy@plt>
movslq (%r15),%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
lea    0xd7f(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11c0 <cpio_replace_env+0x60>
mov    -0x30(%rbp),%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 = %65, %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 %66
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 %64
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 @strlen(i8* %12)
%49 = sub nsw i32 %47, %48
%50 = call i32 @strncat(i8* %12, i8* %46, i32 %49)
%51 = load i8*, i8** %7
%52 = getelementptr inbounds i8, i8* %51, i64 1
%53 = load i32, i32* @PATH_MAX
%54 = call i32 @strlen(i8* %12)
%55 = sub nsw i32 %53, %54
%56 = call i32 @strncat(i8* %12, i8* %52, i32 %55)
%57 = load i8*, i8** %2
%58 = load i32, i32* @PATH_MAX
%59 = call i32 @strncpy(i8* %57, i8* %12, i32 %58)
%60 = load i8*, i8** %2
%61 = load i32, i32* @PATH_MAX
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
store i8 0, i8* %63
br label %65
64: ; preds = %21
br label %66
65: ; preds = %27
br label %17
66: ; preds = %64, %17
%67 = load i8*, i8** %2
%68 = load i8*, i8** %3
ret i8* %67
}

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