#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*/ access(char *, int);
char *alloca(scalar_t__);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char *, char const *);
char *strchr(char const *, char);
int /*<<< orphan*/ strcpy(char *, char *);
char const *strdup(char const *);
scalar_t__ strlen(char const *);
const char *findProgramPath(const char *argv0) {
  char *path = getenv("PATH");
  char *pathbuf;
  char *start, *chptr;
  char *buf;
  if (strchr(argv0, '/'))
    return strdup(argv0);
  if (!path)
    return NULL;
  start = pathbuf = alloca(strlen(path) + 1);
  buf = malloc(strlen(path) + strlen(argv0) + 2);
  strcpy(pathbuf, path);
  chptr = NULL;
  do {
    if ((chptr = strchr(start, ':')))
      *chptr = '\0';
    sprintf(buf, "%s/%s", start, argv0);
    if (!access(buf, 1))
      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    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe4d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
cmp    $0x0,%rax
je     11e6 <findProgramPath+0x46>
mov    -0x10(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    131e <findProgramPath+0x17e>
cmpq   $0x0,-0x18(%rbp)
jne    11fe <findProgramPath+0x5e>
movq   $0x0,-0x8(%rbp)
jmp    131e <findProgramPath+0x17e>
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1080 <alloca@plt>
mov    %rax,-0x20(%rbp)
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x40(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
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     127d <findProgramPath+0xdd>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xd75(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x38(%rbp),%rdi
mov    $0x1,%esi
call   10a0 <access@plt>
cmp    $0x0,%eax
jne    12b9 <findProgramPath+0x119>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    131e <findProgramPath+0x17e>
cmpq   $0x0,-0x30(%rbp)
je     12d5 <findProgramPath+0x135>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    12dd <findProgramPath+0x13d>
movq   $0x0,-0x28(%rbp)
jmp    12e2 <findProgramPath+0x142>
xor    %eax,%eax
cmpq   $0x0,-0x28(%rbp)
mov    %al,-0x41(%rbp)
je     1302 <findProgramPath+0x162>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    125a <findProgramPath+0xba>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findProgramPath>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
lea    0xe4d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r12
mov    %r14,%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     11de <findProgramPath+0x3e>
mov    %r14,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    10c0 <strdup@plt>
test   %r12,%r12
je     1283 <findProgramPath+0xe3>
mov    %r12,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdi
call   1080 <alloca@plt>
mov    %rax,%r13
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1090 <malloc@plt>
mov    %rax,%r15
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <strcpy@plt>
lea    0xdd5(%rip),%r12
mov    %r13,%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1248 <findProgramPath+0xa8>
movb   $0x0,(%rbx)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %r14,%rcx
call   10b0 <sprintf@plt>
mov    %r15,%rdi
mov    $0x1,%esi
call   10a0 <access@plt>
test   %eax,%eax
je     1286 <findProgramPath+0xe6>
test   %rbx,%rbx
je     127b <findProgramPath+0xdb>
add    $0x1,%rbx
cmpb   $0x0,(%rbx)
mov    %rbx,%r13
jne    1230 <findProgramPath+0x90>
mov    %r15,%rdi
call   1040 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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