#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*/ access(char *, int /*<<< orphan*/);
char *alloca(int);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char const *);
char *strchr(char const *, char);
int /*<<< orphan*/ strcpy(char *, char *);
char const *strdup(char const *);
int strlen(char const *);
const char *findProgramPath(const char *argv0) {
  char *path = getenv("PATH");
  char *pathbuf;
  char *start, *chptr;
  char *buf;
  if (argv0 == NULL)
    return NULL;
  if (strchr(argv0, '/'))
    return strdup(argv0);
  if (path == NULL)
    return NULL;
  start = pathbuf = alloca(strlen(path) + 1);
  buf = malloc(strlen(path) + strlen(argv0) + sizeof("/"));
  if (buf == NULL)
    return NULL;
  strcpy(pathbuf, path);
  chptr = NULL;
  do {
    if ((chptr = strchr(start, ':')))
      *chptr = '\0';
    sprintf(buf, "%s/%s", start, argv0);
    if (!access(buf, X_OK))
      return buf;
    if (chptr)
      start = chptr + 1;
    else
      start = NULL;
  } while (start && *start);
  free(buf);
  return NULL;
}


x86 assembly (clang, -O0):
<findProgramPath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe4d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d4 <findProgramPath+0x34>
movq   $0x0,-0x8(%rbp)
jmp    1353 <findProgramPath+0x1b3>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
cmp    $0x0,%rax
je     11fe <findProgramPath+0x5e>
mov    -0x10(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1353 <findProgramPath+0x1b3>
cmpq   $0x0,-0x18(%rbp)
jne    1216 <findProgramPath+0x76>
movq   $0x0,-0x8(%rbp)
jmp    1353 <findProgramPath+0x1b3>
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1080 <alloca@plt>
mov    %rax,-0x20(%rbp)
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
cltq
add    $0x2,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1276 <findProgramPath+0xd6>
movq   $0x0,-0x8(%rbp)
jmp    1353 <findProgramPath+0x1b3>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcpy@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     12ae <findProgramPath+0x10e>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xd44(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x38(%rbp),%rdi
mov    0x2d17(%rip),%rax
mov    (%rax),%esi
call   10a0 <access@plt>
cmp    $0x0,%eax
jne    12ee <findProgramPath+0x14e>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1353 <findProgramPath+0x1b3>
cmpq   $0x0,-0x30(%rbp)
je     130a <findProgramPath+0x16a>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1312 <findProgramPath+0x172>
movq   $0x0,-0x28(%rbp)
jmp    1317 <findProgramPath+0x177>
xor    %eax,%eax
cmpq   $0x0,-0x28(%rbp)
mov    %al,-0x3d(%rbp)
je     1337 <findProgramPath+0x197>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    128b <findProgramPath+0xeb>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findProgramPath>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe4b(%rip),%rdi
call   1030 <getenv@plt>
test   %r14,%r14
je     12a1 <findProgramPath+0x101>
mov    %rax,%r15
mov    %r14,%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     11ee <findProgramPath+0x4e>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10c0 <strdup@plt>
test   %r15,%r15
je     12a1 <findProgramPath+0x101>
mov    %r15,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edi
call   1080 <alloca@plt>
mov    %rax,%r13
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     12a1 <findProgramPath+0x101>
mov    %rax,%r12
mov    %r13,%rdi
mov    %r15,%rsi
call   1050 <strcpy@plt>
lea    0xdc4(%rip),%r15
mov    0x2da0(%rip),%rbp
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1268 <findProgramPath+0xc8>
movb   $0x0,(%rbx)
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
mov    %r14,%rcx
call   10b0 <sprintf@plt>
mov    0x0(%rbp),%esi
mov    %r12,%rdi
call   10a0 <access@plt>
test   %eax,%eax
je     12a4 <findProgramPath+0x104>
test   %rbx,%rbx
je     1299 <findProgramPath+0xf9>
add    $0x1,%rbx
cmpb   $0x0,(%rbx)
mov    %rbx,%r13
jne    1250 <findProgramPath+0xb0>
mov    %r12,%rdi
call   1040 <free@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@X_OK = global i32 0
define noalias i8* @findProgramPath(i8* %0) {
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %0, null
br i1 %3, label %40, label %4
4: ; preds = %1
%5 = tail call i8* @strchr(i8* %0, i8 signext 47)
%6 = icmp eq i8* %5, null
br i1 %6, label %9, label %7
7: ; preds = %4
%8 = tail call i8* @strdup(i8* %0)
br label %40
9: ; preds = %4
%10 = icmp eq i8* %2, null
br i1 %10, label %40, label %11
11: ; preds = %9
%12 = tail call i32 @strlen(i8* %2)
%13 = add nsw i32 %12, 1
%14 = tail call i8* @alloca(i32 %13)
%15 = tail call i32 @strlen(i8* %2)
%16 = tail call i32 @strlen(i8* %0)
%17 = add i32 %15, 2
%18 = add i32 %17, %16
%19 = tail call i8* @malloc(i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %40, label %21
21: ; preds = %11
%22 = tail call i32 @strcpy(i8* %14, i8* %2)
br label %23
23: ; preds = %35, %21
%24 = phi i8* [ %14, %21 ], [ %34, %35 ]
%25 = tail call i8* @strchr(i8* dereferenceable(1) %24, i8 signext 58)
%26 = icmp eq i8* %25, null
br i1 %26, label %28, label %27
27: ; preds = %23
store i8 0, i8* %25
br label %28
28: ; preds = %27, %23
%29 = tail call i32 @sprintf(i8* %19, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %0)
%30 = load i32, i32* @X_OK
%31 = tail call i32 @access(i8* %19, i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %40, label %33
33: ; preds = %28
%34 = getelementptr inbounds i8, i8* %25, i64 1
br i1 %26, label %38, label %35
35: ; preds = %33
%36 = load i8, i8* %34
%37 = icmp eq i8 %36, 0
br i1 %37, label %38, label %23
38: ; preds = %33, %35
%39 = tail call i32 @free(i8* %19)
br label %40
40: ; preds = %28, %11, %9, %1, %38, %7
%41 = phi i8* [ %8, %7 ], [ null, %38 ], [ null, %1 ], [ null, %9 ], [ null, %11 ], [ %19, %28 ]
ret i8* %41
}