#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 */
char *getenv(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char const *);
scalar_t__ strncmp(char *, char const *, int);
const char *prepend_destdir(const char *pathname) {
  char *p = NULL;
  char *destdir = NULL;
  char *wrapdir = NULL;
  int destlen = 0;
  if (pathname[0] != '/')
    return NULL;
  if (!destdir) {
    destdir = getenv("DESTDIR");
    if (!destdir) {
      return NULL;
    }
    destlen = strlen(destdir);
  }
  wrapdir = getenv("WRAPDIR");
  if (wrapdir) {
    int wlen, plen;
    wlen = strlen(wrapdir);
    plen = strlen(pathname);
    if (plen < wlen)
      return NULL;
    if (strncmp(wrapdir, pathname, wlen))
      return NULL;
  }
  p = (char *)malloc(strlen(pathname) + destlen + 1);
  strcpy(p, destdir);
  strcat(p, pathname);
  return p;
}


x86 assembly (clang, -O0):
<prepend_destdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11a8 <prepend_destdir+0x48>
movq   $0x0,-0x8(%rbp)
jmp    129b <prepend_destdir+0x13b>
cmpq   $0x0,-0x20(%rbp)
jne    11e7 <prepend_destdir+0x87>
lea    0xe46(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11db <prepend_destdir+0x7b>
movq   $0x0,-0x8(%rbp)
jmp    129b <prepend_destdir+0x13b>
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x2c(%rbp)
lea    0xe1a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     125f <prepend_destdir+0xff>
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    1233 <prepend_destdir+0xd3>
movq   $0x0,-0x8(%rbp)
jmp    129b <prepend_destdir+0x13b>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     125a <prepend_destdir+0xfa>
movq   $0x0,-0x8(%rbp)
jmp    129b <prepend_destdir+0x13b>
jmp    125f <prepend_destdir+0xff>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%edi
add    -0x2c(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepend_destdir>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmpb   $0x2f,(%rdi)
jne    11cb <prepend_destdir+0x6b>
mov    %rdi,%r12
lea    0xe89(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11cb <prepend_destdir+0x6b>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %eax,%r15d
lea    0xe72(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11d9 <prepend_destdir+0x79>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1060 <strlen@plt>
cmp    %ebp,%eax
jl     11cb <prepend_destdir+0x6b>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11d9 <prepend_destdir+0x79>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
call   1060 <strlen@plt>
lea    (%r15,%rax,1),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <strcpy@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <strcat@plt>
jmp    11cd <prepend_destdir+0x6d>

LLVM IR (-O0):
@.str = [8 x i8] c"DESTDIR\00"
@.str.1 = [8 x i8] c"WRAPDIR\00"
define i8* @prepend_destdir(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %4
store i8* null, i8** %5
store i8* null, i8** %6
store i32 0, i32* %7
%10 = load i8*, i8** %3
%11 = getelementptr inbounds i8, i8* %10, i64 0
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 47
br i1 %14, label %15, label %16
15: ; preds = %1
store i8* null, i8** %2
br label %63
16: ; preds = %1
%17 = load i8*, i8** %5
%18 = icmp ne i8* %17, null
br i1 %18, label %27, label %19
19: ; preds = %16
%20 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* %20, i8** %5
%21 = load i8*, i8** %5
%22 = icmp ne i8* %21, null
br i1 %22, label %24, label %23
23: ; preds = %19
store i8* null, i8** %2
br label %63
24: ; preds = %19
%25 = load i8*, i8** %5
%26 = call i32 @strlen(i8* %25)
store i32 %26, i32* %7
br label %27
27: ; preds = %24, %16
%28 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %48
31: ; preds = %27
%32 = load i8*, i8** %6
%33 = call i32 @strlen(i8* %32)
store i32 %33, i32* %8
%34 = load i8*, i8** %3
%35 = call i32 @strlen(i8* %34)
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = load i32, i32* %8
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %31
store i8* null, i8** %2
br label %63
40: ; preds = %31
%41 = load i8*, i8** %6
%42 = load i8*, i8** %3
%43 = load i32, i32* %8
%44 = call i64 @strncmp(i8* %41, i8* %42, i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %40
store i8* null, i8** %2
br label %63
47: ; preds = %40
br label %48
48: ; preds = %47, %27
%49 = load i8*, i8** %3
%50 = call i32 @strlen(i8* %49)
%51 = load i32, i32* %7
%52 = add nsw i32 %50, %51
%53 = add nsw i32 %52, 1
%54 = call i64 @malloc(i32 %53)
%55 = inttoptr i64 %54 to i8*
store i8* %55, i8** %4
%56 = load i8*, i8** %4
%57 = load i8*, i8** %5
%58 = call i32 @strcpy(i8* %56, i8* %57)
%59 = load i8*, i8** %4
%60 = load i8*, i8** %3
%61 = call i32 @strcat(i8* %59, i8* %60)
%62 = load i8*, i8** %4
store i8* %62, i8** %2
br label %63
63: ; preds = %48, %46, %39, %23, %15
%64 = load i8*, i8** %2
ret i8* %64
}

LLVM IR (-O2):
@.str = [8 x i8] c"DESTDIR\00"
@.str.1 = [8 x i8] c"WRAPDIR\00"
define i8* @prepend_destdir(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 47
br i1 %3, label %4, label %26
4: ; preds = %1
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %26, label %7
7: ; preds = %4
%8 = tail call i32 @strlen(i8* %5)
%9 = tail call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %18, label %11
11: ; preds = %7
%12 = tail call i32 @strlen(i8* %9)
%13 = tail call i32 @strlen(i8* %0)
%14 = icmp slt i32 %13, %12
br i1 %14, label %26, label %15
15: ; preds = %11
%16 = tail call i64 @strncmp(i8* %9, i8* %0, i32 %12)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %15, %7
%19 = tail call i32 @strlen(i8* %0)
%20 = add i32 %8, 1
%21 = add i32 %20, %19
%22 = tail call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
%24 = tail call i32 @strcpy(i8* %23, i8* %5)
%25 = tail call i32 @strcat(i8* %23, i8* %0)
br label %26
26: ; preds = %11, %4, %1, %15, %18
%27 = phi i8* [ %23, %18 ], [ null, %15 ], [ null, %1 ], [ null, %4 ], [ null, %11 ]
ret i8* %27
}