#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 /*<<< orphan*/ PATH_MAX;
char *getenv(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
char *strstr(char *, char *);
char *cpio_replace_env(char *new_location) {
  char expanded[PATH_MAX + 1];
  char *start, *end, *var;
  while ((start = strstr(new_location, "${")) &&
         (end = strchr(start + 2, '}'))) {
    *start = *end = 0;
    var = getenv(start + 2);
    snprintf(expanded, sizeof expanded, "%s%s%s", new_location, var ? var : "",
             end + 1);
    strcpy(new_location, expanded);
  }
  return new_location;
}


x86 assembly (clang, -O0):
<cpio_replace_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x41(%rbp)
je     11d5 <cpio_replace_env+0x85>
mov    -0x20(%rbp),%rdi
add    $0x2,%rdi
mov    $0x7d,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    11e5 <cpio_replace_env+0x95>
jmp    1273 <cpio_replace_env+0x123>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
add    $0x2,%rdi
call   1030 <getenv@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,-0x30(%rbp)
mov    %eax,-0x54(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     122e <cpio_replace_env+0xde>
mov    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
jmp    123e <cpio_replace_env+0xee>
lea    0xdd4(%rip),%rax
mov    %rax,-0x60(%rbp)
jmp    123e <cpio_replace_env+0xee>
mov    -0x38(%rbp),%rdi
mov    -0x50(%rbp),%rcx
mov    -0x54(%rbp),%esi
mov    -0x60(%rbp),%r8
mov    -0x28(%rbp),%r9
add    $0x1,%r9
lea    0xda7(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x38(%rbp),%rsi
mov    -0x8(%rbp),%rdi
call   1040 <strcpy@plt>
jmp    118f <cpio_replace_env+0x3f>
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,%rbx
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%r15
mov    %rax,-0x30(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    0xe77(%rip),%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     121a <cpio_replace_env+0xca>
mov    %rax,%r12
nopw   0x0(%rax,%rax,1)
lea    0x2(%r12),%r14
mov    %r14,%rdi
mov    $0x7d,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     121a <cpio_replace_env+0xca>
mov    %rax,%r13
movb   $0x0,(%rax)
movb   $0x0,(%r12)
mov    %r14,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe35(%rip),%rcx
cmove  %rcx,%rax
add    $0x1,%r13
mov    %r15,%rdi
mov    -0x30(%rbp),%rsi
lea    0xe19(%rip),%rdx
mov    %rbx,%rcx
mov    %rax,%r8
mov    %r13,%r9
call   1060 <snprintf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
lea    0xdf3(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11a0 <cpio_replace_env+0x50>
mov    %rbx,%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"
@.str.1 = [7 x i8] c"%s%s%s\00"
@.str.2 = [1 x i8] zeroinitializer
define i8* @cpio_replace_env(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%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
br label %13
13: ; preds = %37, %1
%14 = load i8*, i8** %2
%15 = call i8* @strstr(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %15, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = load i8*, i8** %5
%19 = getelementptr inbounds i8, i8* %18, i64 2
%20 = call i8* @strchr(i8* %19, i8 signext 125)
store i8* %20, i8** %6
%21 = icmp ne i8* %20, null
br label %22
22: ; preds = %17, %13
%23 = phi i1 [ false, %13 ], [ %21, %17 ]
br i1 %23, label %24, label %44
24: ; preds = %22
%25 = load i8*, i8** %6
store i8 0, i8* %25
%26 = load i8*, i8** %5
store i8 0, i8* %26
%27 = load i8*, i8** %5
%28 = getelementptr inbounds i8, i8* %27, i64 2
%29 = call i8* @getenv(i8* %28)
store i8* %29, i8** %7
%30 = trunc i64 %10 to i32
%31 = load i8*, i8** %2
%32 = load i8*, i8** %7
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %36
34: ; preds = %24
%35 = load i8*, i8** %7
br label %37
36: ; preds = %24
br label %37
37: ; preds = %36, %34
%38 = phi i8* [ %35, %34 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), %36 ]
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i64 1
%41 = call i32 @snprintf(i8* %12, i32 %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %31, i8* %38, i8* %40)
%42 = load i8*, i8** %2
%43 = call i32 @strcpy(i8* %42, i8* %12)
br label %13
44: ; preds = %22
%45 = load i8*, i8** %2
%46 = load i8*, i8** %3
ret i8* %45
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [3 x i8] c"${\00"
@.str.1 = [7 x i8] c"%s%s%s\00"
@.str.2 = [1 x i8] zeroinitializer
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 = call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %22, label %8
8: ; preds = %1, %13
%9 = phi i8* [ %20, %13 ], [ %6, %1 ]
%10 = getelementptr inbounds i8, i8* %9, i64 2
%11 = call i8* @strchr(i8* %10, i8 signext 125)
%12 = icmp eq i8* %11, null
br i1 %12, label %22, label %13
13: ; preds = %8
store i8 0, i8* %11
store i8 0, i8* %9
%14 = call i8* @getenv(i8* %10)
%15 = icmp eq i8* %14, null
%16 = select i1 %15, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8* %14
%17 = getelementptr inbounds i8, i8* %11, i64 1
%18 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %16, i8* %17)
%19 = call i32 @strcpy(i8* %0, i8* %5)
%20 = call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %8
22: ; preds = %8, %13, %1
ret i8* %0
}