#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*/ LOG_ERR;
scalar_t__ file_exists(char const *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *strcat(char *, char *);
char *strdup(char const *);
int strlen(char const *);
char *strncat(char *, char const *, int);
char *strncpy(char *, char *, int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char const *);
char *expand_executable_w_path(const char *executable, char **allowed_paths) {
  ;
  if (file_exists(executable)) {
    return strdup(executable);
  }
  if (!allowed_paths) {
    return NULL;
  } else {
    char **path = allowed_paths;
    int elen = strlen(executable);
    while (*path) {
      char *newpath;
      int tlen = strlen(*path);
      newpath = malloc((elen + tlen + 2) * sizeof(char));
      memset(newpath, 0, (elen + tlen + 2) * sizeof(char));
      newpath = strncpy(newpath, *path, tlen);
      if (*(*path + tlen - 1) != '/') {
        newpath = strcat(newpath, "/");
        ;
      }
      newpath = strncat(newpath, executable, elen);
      if (file_exists(newpath)) {
        ;
        return newpath;
      }
      free(newpath);
      path++;
    }
  }
  syslog(LOG_ERR, "the requested executable %s is not found\n", executable);
  return NULL;
}


x86 assembly (clang, -O0):
<expand_executable_w_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <file_exists@plt>
cmp    $0x0,%rax
je     11d5 <expand_executable_w_path+0x35>
mov    -0x10(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    130c <expand_executable_w_path+0x16c>
cmpq   $0x0,-0x18(%rbp)
jne    11ed <expand_executable_w_path+0x4d>
movq   $0x0,-0x8(%rbp)
jmp    130c <expand_executable_w_path+0x16c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     12e6 <expand_executable_w_path+0x146>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%eax
add    -0x34(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%eax
add    -0x34(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x34(%rbp),%edx
call   1040 <strncpy@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movslq -0x34(%rbp),%rcx
movsbl -0x1(%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1298 <expand_executable_w_path+0xf8>
mov    -0x30(%rbp),%rdi
lea    0xd71(%rip),%rsi
call   10b0 <strcat@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1080 <strncat@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <file_exists@plt>
cmp    $0x0,%rax
je     12cc <expand_executable_w_path+0x12c>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    130c <expand_executable_w_path+0x16c>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    1201 <expand_executable_w_path+0x61>
jmp    12eb <expand_executable_w_path+0x14b>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd03(%rip),%rsi
call   10a0 <syslog@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_executable_w_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r13
mov    %rdi,%r14
call   1050 <file_exists@plt>
test   %rax,%rax
je     11c4 <expand_executable_w_path+0x34>
mov    %r14,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <strdup@plt>
test   %r13,%r13
je     12a6 <expand_executable_w_path+0x116>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %eax,0xc(%rsp)
mov    0x0(%r13),%rdi
test   %rdi,%rdi
je     128e <expand_executable_w_path+0xfe>
mov    0xc(%rsp),%eax
add    $0x2,%eax
mov    %rax,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
call   1060 <strlen@plt>
mov    %eax,%r15d
mov    0x10(%rsp),%rax
lea    (%rax,%r15,1),%ebx
mov    %ebx,%edi
call   1090 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1070 <memset@plt>
mov    0x0(%r13),%rsi
mov    %rbp,%rdi
mov    %r15d,%edx
call   1040 <strncpy@plt>
mov    %rax,%r12
mov    0x0(%r13),%rax
movslq %r15d,%rcx
cmpb   $0x2f,-0x1(%rax,%rcx,1)
je     1256 <expand_executable_w_path+0xc6>
mov    %r12,%rdi
call   1060 <strlen@plt>
movw   $0x2f,(%r12,%rax,1)
mov    %r12,%rdi
mov    %r14,%rsi
mov    0xc(%rsp),%edx
call   1080 <strncat@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <file_exists@plt>
test   %rax,%rax
jne    12a8 <expand_executable_w_path+0x118>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    0x8(%r13),%rdi
add    $0x8,%r13
test   %rdi,%rdi
jne    1200 <expand_executable_w_path+0x70>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
lea    0xd62(%rip),%rsi
mov    %r14,%rdx
call   10a0 <syslog@plt>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@LOG_ERR = global i32 0
@.str.1 = [42 x i8] c"the requested executable %s is not found\0A\00"
define i8* @expand_executable_w_path(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
%10 = load i8*, i8** %4
%11 = call i64 @file_exists(i8* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i8* @strdup(i8* %14)
store i8* %15, i8** %3
br label %86
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = icmp ne i8** %17, null
br i1 %18, label %20, label %19
19: ; preds = %16
store i8* null, i8** %3
br label %86
20: ; preds = %16
%21 = load i8**, i8*** %5
store i8** %21, i8*** %6
%22 = load i8*, i8** %4
%23 = call i32 @strlen(i8* %22)
store i32 %23, i32* %7
br label %24
24: ; preds = %76, %20
%25 = load i8**, i8*** %6
%26 = load i8*, i8** %25
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %81
28: ; preds = %24
%29 = load i8**, i8*** %6
%30 = load i8*, i8** %29
%31 = call i32 @strlen(i8* %30)
store i32 %31, i32* %9
%32 = load i32, i32* %7
%33 = load i32, i32* %9
%34 = add nsw i32 %32, %33
%35 = add nsw i32 %34, 2
%36 = sext i32 %35 to i64
%37 = mul i64 %36, 1
%38 = trunc i64 %37 to i32
%39 = call i8* @malloc(i32 %38)
store i8* %39, i8** %8
%40 = load i8*, i8** %8
%41 = load i32, i32* %7
%42 = load i32, i32* %9
%43 = add nsw i32 %41, %42
%44 = add nsw i32 %43, 2
%45 = sext i32 %44 to i64
%46 = mul i64 %45, 1
%47 = trunc i64 %46 to i32
%48 = call i32 @memset(i8* %40, i32 0, i32 %47)
%49 = load i8*, i8** %8
%50 = load i8**, i8*** %6
%51 = load i8*, i8** %50
%52 = load i32, i32* %9
%53 = call i8* @strncpy(i8* %49, i8* %51, i32 %52)
store i8* %53, i8** %8
%54 = load i8**, i8*** %6
%55 = load i8*, i8** %54
%56 = load i32, i32* %9
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
%59 = getelementptr inbounds i8, i8* %58, i64 -1
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp ne i32 %61, 47
br i1 %62, label %63, label %66
63: ; preds = %28
%64 = load i8*, i8** %8
%65 = call i8* @strcat(i8* %64, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %65, i8** %8
br label %66
66: ; preds = %63, %28
%67 = load i8*, i8** %8
%68 = load i8*, i8** %4
%69 = load i32, i32* %7
%70 = call i8* @strncat(i8* %67, i8* %68, i32 %69)
store i8* %70, i8** %8
%71 = load i8*, i8** %8
%72 = call i64 @file_exists(i8* %71)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %66
%75 = load i8*, i8** %8
store i8* %75, i8** %3
br label %86
76: ; preds = %66
%77 = load i8*, i8** %8
%78 = call i32 @free(i8* %77)
%79 = load i8**, i8*** %6
%80 = getelementptr inbounds i8*, i8** %79, i32 1
store i8** %80, i8*** %6
br label %24
81: ; preds = %24
br label %82
82: ; preds = %81
%83 = load i32, i32* @LOG_ERR
%84 = load i8*, i8** %4
%85 = call i32 @syslog(i32 %83, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %84)
store i8* null, i8** %3
br label %86
86: ; preds = %82, %74, %19, %13
%87 = load i8*, i8** %3
ret i8* %87
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str.1 = [42 x i8] c"the requested executable %s is not found\0A\00"
define i8* @expand_executable_w_path(i8* %0, i8** %1) {
%3 = tail call i64 @file_exists(i8* %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i8* @strdup(i8* %0)
br label %46
7: ; preds = %2
%8 = icmp eq i8** %1, null
br i1 %8, label %46, label %9
9: ; preds = %7
%10 = tail call i32 @strlen(i8* %0)
%11 = load i8*, i8** %1
%12 = icmp eq i8* %11, null
br i1 %12, label %43, label %13
13: ; preds = %9
%14 = add i32 %10, 2
br label %15
15: ; preds = %13, %38
%16 = phi i8* [ %11, %13 ], [ %41, %38 ]
%17 = phi i8** [ %1, %13 ], [ %40, %38 ]
%18 = tail call i32 @strlen(i8* %16)
%19 = add i32 %14, %18
%20 = tail call i8* @malloc(i32 %19)
%21 = tail call i32 @memset(i8* %20, i32 0, i32 %19)
%22 = load i8*, i8** %17
%23 = tail call i8* @strncpy(i8* %20, i8* %22, i32 %18)
%24 = load i8*, i8** %17
%25 = sext i32 %18 to i64
%26 = getelementptr inbounds i8, i8* %24, i64 -1
%27 = getelementptr inbounds i8, i8* %26, i64 %25
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 47
br i1 %29, label %34, label %30
30: ; preds = %15
%31 = tail call i64 bitcast (i32 (i8*)* @strlen to i64 (i8*)*)(i8* %23)
%32 = getelementptr i8, i8* %23, i64 %31
%33 = bitcast i8* %32 to i16*
store i16 47, i16* %33
br label %34
34: ; preds = %30, %15
%35 = tail call i8* @strncat(i8* %23, i8* %0, i32 %10)
%36 = tail call i64 @file_exists(i8* %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %46
38: ; preds = %34
%39 = tail call i32 @free(i8* %35)
%40 = getelementptr inbounds i8*, i8** %17, i64 1
%41 = load i8*, i8** %40
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %15
43: ; preds = %38, %9
%44 = load i32, i32* @LOG_ERR
%45 = tail call i32 @syslog(i32 %44, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %46
46: ; preds = %34, %7, %43, %5
%47 = phi i8* [ %6, %5 ], [ null, %43 ], [ null, %7 ], [ %35, %34 ]
ret i8* %47
}