#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*/ X_OK;
int /*<<< orphan*/ _Exit(int);
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *getVariable(char *);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strcspn(char const *, char *);
size_t strlen(char const *);
int /*<<< orphan*/ warn(char *);
const char *getExecutablePath(const char *command) {
  size_t commandLength = strlen(command);
  const char *path = getVariable("PATH");
  if (!path)
    return NULL;
  while (*path) {
    size_t length = strcspn(path, ":");
    char *buffer = malloc(commandLength + length + 2);
    if (!buffer) {
      warn("malloc");
      _Exit(126);
    }
    memcpy(buffer, path, length);
    buffer[length] = '/';
    memcpy(buffer + length + 1, command, commandLength);
    buffer[commandLength + length + 1] = '\0';
    if (access(buffer, X_OK) == 0) {
      return buffer;
    }
    free(buffer);
    path += length + 1;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<getExecutablePath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x18(%rbp)
lea    0xe50(%rip),%rdi
call   10a0 <getVariable@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d1 <getExecutablePath+0x41>
movq   $0x0,-0x8(%rbp)
jmp    12c3 <getExecutablePath+0x133>
jmp    11d6 <getExecutablePath+0x46>
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     12bb <getExecutablePath+0x12b>
mov    -0x20(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   1060 <strcspn@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
add    -0x28(%rbp),%rdi
add    $0x2,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    122d <getExecutablePath+0x9d>
lea    0xde9(%rip),%rdi
call   1040 <warn@plt>
mov    $0x7e,%edi
call   10b0 <_Exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
add    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%rcx
add    -0x28(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%rax
jne    129d <getExecutablePath+0x10d>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12c3 <getExecutablePath+0x133>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11d6 <getExecutablePath+0x46>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getExecutablePath>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1050 <strlen@plt>
mov    %rax,%r15
lea    0xe53(%rip),%rdi
call   10a0 <getVariable@plt>
test   %rax,%rax
je     1255 <getExecutablePath+0xc5>
mov    %rax,%r13
cmpb   $0x0,(%rax)
je     1255 <getExecutablePath+0xc5>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
lea    0xe2b(%rip),%rsi
call   1060 <strcspn@plt>
mov    %rax,%rbp
lea    (%rax,%r15,1),%rdi
add    $0x2,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1269 <getExecutablePath+0xd9>
mov    %rax,%rbx
lea    (%r15,%rbp,1),%r12
mov    %rax,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1070 <memcpy@plt>
movb   $0x2f,(%rbx,%rbp,1)
lea    (%rbx,%rbp,1),%rdi
add    $0x1,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <memcpy@plt>
movb   $0x0,0x1(%r12,%rbx,1)
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <access@plt>
test   %rax,%rax
je     1257 <getExecutablePath+0xc7>
mov    %rbx,%rdi
call   1030 <free@plt>
cmpb   $0x0,0x1(%rbp,%r13,1)
lea    0x1(%rbp,%r13,1),%r13
jne    11d0 <getExecutablePath+0x40>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd97(%rip),%rdi
call   1040 <warn@plt>
mov    $0x7e,%edi
call   10b0 <_Exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [7 x i8] c"malloc\00"
@X_OK = global i32 0
define i8* @getExecutablePath(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %4
%10 = call i8* @getVariable(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %1
store i8* null, i8** %2
br label %67
14: ; preds = %1
br label %15
15: ; preds = %59, %14
%16 = load i8*, i8** %5
%17 = load i8, i8* %16
%18 = icmp ne i8 %17, 0
br i1 %18, label %19, label %66
19: ; preds = %15
%20 = load i8*, i8** %5
%21 = call i64 @strcspn(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i64 %21, i64* %6
%22 = load i64, i64* %4
%23 = load i64, i64* %6
%24 = add i64 %22, %23
%25 = add i64 %24, 2
%26 = call i8* @malloc(i64 %25)
store i8* %26, i8** %7
%27 = load i8*, i8** %7
%28 = icmp ne i8* %27, null
br i1 %28, label %32, label %29
29: ; preds = %19
%30 = call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @_Exit(i32 126)
unreachable
32: ; preds = %19
%33 = load i8*, i8** %7
%34 = load i8*, i8** %5
%35 = load i64, i64* %6
%36 = call i32 @memcpy(i8* %33, i8* %34, i64 %35)
%37 = load i8*, i8** %7
%38 = load i64, i64* %6
%39 = getelementptr inbounds i8, i8* %37, i64 %38
store i8 47, i8* %39
%40 = load i8*, i8** %7
%41 = load i64, i64* %6
%42 = getelementptr inbounds i8, i8* %40, i64 %41
%43 = getelementptr inbounds i8, i8* %42, i64 1
%44 = load i8*, i8** %3
%45 = load i64, i64* %4
%46 = call i32 @memcpy(i8* %43, i8* %44, i64 %45)
%47 = load i8*, i8** %7
%48 = load i64, i64* %4
%49 = load i64, i64* %6
%50 = add i64 %48, %49
%51 = add i64 %50, 1
%52 = getelementptr inbounds i8, i8* %47, i64 %51
store i8 0, i8* %52
%53 = load i8*, i8** %7
%54 = load i32, i32* @X_OK
%55 = call i64 @access(i8* %53, i32 %54)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %32
%58 = load i8*, i8** %7
store i8* %58, i8** %2
br label %67
59: ; preds = %32
%60 = load i8*, i8** %7
%61 = call i32 @free(i8* %60)
%62 = load i64, i64* %6
%63 = add i64 %62, 1
%64 = load i8*, i8** %5
%65 = getelementptr inbounds i8, i8* %64, i64 %63
store i8* %65, i8** %5
br label %15
66: ; preds = %15
store i8* null, i8** %2
br label %67
67: ; preds = %66, %57, %13
%68 = load i8*, i8** %2
ret i8* %68
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [7 x i8] c"malloc\00"
@X_OK = global i32 0
define i8* @getExecutablePath(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = tail call i8* @getVariable(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %34, label %5
5: ; preds = %1
%6 = load i8, i8* %3
%7 = icmp eq i8 %6, 0
br i1 %7, label %34, label %8
8: ; preds = %5, %28
%9 = phi i8* [ %31, %28 ], [ %3, %5 ]
%10 = tail call i64 @strcspn(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = add i64 %10, %2
%12 = add i64 %11, 2
%13 = tail call i8* @malloc(i64 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = tail call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @_Exit(i32 126)
unreachable
18: ; preds = %8
%19 = tail call i32 @memcpy(i8* %13, i8* %9, i64 %10)
%20 = getelementptr inbounds i8, i8* %13, i64 %10
store i8 47, i8* %20
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = tail call i32 @memcpy(i8* %21, i8* %0, i64 %2)
%23 = add i64 %11, 1
%24 = getelementptr inbounds i8, i8* %13, i64 %23
store i8 0, i8* %24
%25 = load i32, i32* @X_OK
%26 = tail call i64 @access(i8* %13, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %34, label %28
28: ; preds = %18
%29 = tail call i32 @free(i8* %13)
%30 = add i64 %10, 1
%31 = getelementptr inbounds i8, i8* %9, i64 %30
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 0
br i1 %33, label %34, label %8
34: ; preds = %28, %18, %5, %1
%35 = phi i8* [ null, %1 ], [ null, %5 ], [ null, %28 ], [ %13, %18 ]
ret i8* %35
}