#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__ asprintf(char **, char *, char const *, char *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int /*<<< orphan*/ printf(char *);
int setenv(char *, char const *, int);
int prepend_path(const char *extra) {
  char *oldpath, *newpath;
  int r;
  if (extra == NULL)
    return 0;
  oldpath = getenv("PATH");
  if (oldpath == NULL)
    return setenv("PATH", extra, 1);
  if (asprintf(&newpath, "%s:%s", extra, oldpath) < 0) {
    printf("TESTSUITE: "
           "ERR: "
           "failed to allocate memory to new PATH\n");
    return -1;
  }
  r = setenv("PATH", newpath, 1);
  free(newpath);
  return r;
}


x86 assembly (clang, -O0):
<prepend_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1173 <prepend_path+0x23>
movl   $0x0,-0x4(%rbp)
jmp    120c <prepend_path+0xbc>
lea    0xe86(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ab <prepend_path+0x5b>
mov    -0x10(%rbp),%rsi
lea    0xe67(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    %eax,-0x4(%rbp)
jmp    120c <prepend_path+0xbc>
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x20(%rbp),%rdi
lea    0xe47(%rip),%rsi
call   1070 <asprintf@plt>
cmp    $0x0,%rax
jge    11e5 <prepend_path+0x95>
lea    0xe37(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <prepend_path+0xbc>
mov    -0x20(%rbp),%rsi
lea    0xe10(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepend_path>:
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     11ae <prepend_path+0x5e>
mov    %rdi,%rbx
lea    0xed3(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11b2 <prepend_path+0x62>
lea    0xe8b(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1070 <asprintf@plt>
test   %rax,%rax
js     11cb <prepend_path+0x7b>
mov    0x8(%rsp),%rsi
lea    0xea1(%rip),%rdi
mov    $0x1,%edx
call   1060 <setenv@plt>
mov    %eax,%ebx
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
jmp    11dc <prepend_path+0x8c>
xor    %ebx,%ebx
jmp    11dc <prepend_path+0x8c>
lea    0xe7e(%rip),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
add    $0x10,%rsp
pop    %rbx
jmp    1060 <setenv@plt>
lea    0xe34(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [6 x i8] c"%s:%s\00"
@.str.2 = [55 x i8] c"TESTSUITE: ERR: failed to allocate memory to new PATH\0A\00"
define i32 @prepend_path(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 0, i32* %2
br label %30
10: ; preds = %1
%11 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %15, i32 1)
store i32 %16, i32* %2
br label %30
17: ; preds = %10
%18 = load i8*, i8** %3
%19 = load i8*, i8** %4
%20 = call i64 @asprintf(i8** %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %18, i8* %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %30
24: ; preds = %17
%25 = load i8*, i8** %5
%26 = call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %25, i32 1)
store i32 %26, i32* %6
%27 = load i8*, i8** %5
%28 = call i32 @free(i8* %27)
%29 = load i32, i32* %6
store i32 %29, i32* %2
br label %30
30: ; preds = %24, %22, %14, %9
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [6 x i8] c"%s:%s\00"
@str = [54 x i8] c"TESTSUITE: ERR: failed to allocate memory to new PATH\00"
define i32 @prepend_path(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = icmp eq i8* %0, null
br i1 %4, label %20, label %5
5: ; preds = %1
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0, i32 1)
br label %20
10: ; preds = %5
%11 = call i64 @asprintf(i8** %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %6)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str, i64 0, i64 0))
br label %20
15: ; preds = %10
%16 = load i8*, i8** %2
%17 = call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %16, i32 1)
%18 = load i8*, i8** %2
%19 = call i32 @free(i8* %18)
br label %20
20: ; preds = %1, %15, %13, %8
%21 = phi i32 [ %9, %8 ], [ -1, %13 ], [ %17, %15 ], [ 0, %1 ]
ret i32 %21
}