#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 */
typedef int /*<<< orphan*/ cmd;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ X_OK;
int /*<<< orphan*/ _CS_PATH;
int /*<<< orphan*/ access(char *, int /*<<< orphan*/);
int confstr(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
char *malloc(int);
int /*<<< orphan*/ strcat(char *, char const *);
char *strchrnul(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *which_program(const char *program_name) {
  size_t prog_len = strlen(program_name);
  char *env_path = getenv("PATH");
  char *search_path = env_path;
  char *program_path = NULL;
  if (!search_path) {
    int n = confstr(_CS_PATH, NULL, 0);
    search_path = malloc(n);
    confstr(_CS_PATH, search_path, n);
  }
  char *path = search_path;
  do {
    char cmd[PATH_MAX];
    char *p = strchrnul(path, ':');
    size_t path_len = p - path;
    path += path_len;
    if (path_len + prog_len + 1 > sizeof(cmd))
      continue;
    if (path_len) {
      strncpy(cmd, path - path_len, path_len);
      cmd[path_len] = '/';
      cmd[path_len + 1] = 0;
    } else {
      strcpy(cmd, "./");
    }
    strcat(cmd, program_name);
    if (!access(cmd, X_OK))
      program_path = strdup(cmd);
  } while (!program_path && *path++ == ':');
  if (!env_path)
    free(search_path);
  return program_path;
}


x86 assembly (clang, -O0):
<which_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strlen@plt>
mov    %rax,-0x10(%rbp)
lea    0xe2d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    122f <which_program+0x7f>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <confstr@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   10a0 <confstr@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x68(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
mov    $0x3a,%esi
call   1070 <strchrnul@plt>
mov    -0x68(%rbp),%rcx
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    -0x38(%rbp),%rdx
sub    %rdx,%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x58(%rbp),%rax
add    -0x10(%rbp),%rax
add    $0x1,%rax
cmp    %rcx,%rax
jbe    12b9 <which_program+0x109>
movl   $0x3,-0x5c(%rbp)
jmp    1344 <which_program+0x194>
cmpq   $0x0,-0x58(%rbp)
je     12f8 <which_program+0x148>
mov    -0x70(%rbp),%rdi
mov    -0x38(%rbp),%rsi
xor    %eax,%eax
sub    -0x58(%rbp),%rax
add    %rax,%rsi
mov    -0x58(%rbp),%rdx
call   1050 <strncpy@plt>
mov    -0x70(%rbp),%rax
mov    -0x58(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x58(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
jmp    1308 <which_program+0x158>
mov    -0x70(%rbp),%rdi
lea    0xd02(%rip),%rsi
call   1060 <strcpy@plt>
mov    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10c0 <strcat@plt>
mov    -0x70(%rbp),%rdi
mov    0x2cb8(%rip),%rax
mov    (%rax),%esi
call   10b0 <access@plt>
cmp    $0x0,%eax
jne    133d <which_program+0x18d>
mov    -0x70(%rbp),%rdi
call   10d0 <strdup@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x5c(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x5c(%rbp),%eax
test   %eax,%eax
je     1360 <which_program+0x1b0>
jmp    135b <which_program+0x1ab>
jmp    1365 <which_program+0x1b5>
jmp    1365 <which_program+0x1b5>
xor    %eax,%eax
cmpq   $0x0,-0x28(%rbp)
mov    %al,-0x71(%rbp)
jne    1390 <which_program+0x1e0>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movsbl (%rax),%eax
cmp    $0x3a,%eax
sete   %al
mov    %al,-0x71(%rbp)
mov    -0x71(%rbp),%al
test   $0x1,%al
jne    1237 <which_program+0x87>
cmpq   $0x0,-0x18(%rbp)
jne    13af <which_program+0x1ff>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<which_program>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,-0x40(%rbp)
call   1080 <strlen@plt>
mov    %rax,-0x48(%rbp)
lea    0xe2b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r12
mov    %rax,-0x38(%rbp)
test   %rax,%rax
jne    1214 <which_program+0x64>
mov    0x2df3(%rip),%r14
mov    (%r14),%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <confstr@plt>
mov    %eax,%r15d
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    (%r14),%edi
mov    %rax,%rsi
mov    %r15d,%edx
call   10a0 <confstr@plt>
mov    %r12,-0x30(%rbp)
jmp    1233 <which_program+0x83>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rsp
mov    %rcx,%r12
add    $0x1,%r12
cmpb   $0x3a,(%rcx)
jne    12e9 <which_program+0x139>
mov    %rsp,%r13
mov    0x2db3(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%r14
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    %r12,%rdi
mov    $0x3a,%esi
call   1070 <strchrnul@plt>
mov    %rax,%rcx
mov    %rax,%rbx
sub    %r12,%rbx
mov    -0x48(%rbp),%rax
add    %rbx,%rax
add    $0x1,%rax
cmp    %r15,%rax
ja     1220 <which_program+0x70>
mov    %rcx,%r15
mov    %r14,%rdi
test   %rbx,%rbx
je     1296 <which_program+0xe6>
mov    %r12,%rsi
mov    %rbx,%rdx
call   1050 <strncpy@plt>
movw   $0x2f,(%r14,%rbx,1)
jmp    12a2 <which_program+0xf2>
lea    0xd68(%rip),%rsi
call   1060 <strcpy@plt>
mov    %r14,%rdi
mov    -0x40(%rbp),%rsi
call   10c0 <strcat@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10b0 <access@plt>
test   %eax,%eax
mov    %r15,%rcx
jne    1220 <which_program+0x70>
mov    %r14,%rdi
mov    %rcx,%rbx
call   10d0 <strdup@plt>
mov    %rbx,%rcx
mov    %r13,%rsp
test   %rax,%rax
je     1223 <which_program+0x73>
mov    %rax,%rbx
jmp    12eb <which_program+0x13b>
xor    %ebx,%ebx
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rdi
test   %rax,%rax
jne    12fd <which_program+0x14d>
call   1040 <free@plt>
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@_CS_PATH = global i32 0
@PATH_MAX = global i32 0
@.str.1 = [3 x i8] c"./\00"
@X_OK = global i32 0
define i8* @which_program(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store i8* %0, i8** %2
%14 = load i8*, i8** %2
%15 = call i64 @strlen(i8* %14)
store i64 %15, i64* %3
%16 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %16, i8** %4
%17 = load i8*, i8** %4
store i8* %17, i8** %5
store i8* null, i8** %6
%18 = load i8*, i8** %5
%19 = icmp ne i8* %18, null
br i1 %19, label %29, label %20
20: ; preds = %1
%21 = load i32, i32* @_CS_PATH
%22 = call i32 @confstr(i32 %21, i8* null, i32 0)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = call i8* @malloc(i32 %23)
store i8* %24, i8** %5
%25 = load i32, i32* @_CS_PATH
%26 = load i8*, i8** %5
%27 = load i32, i32* %7
%28 = call i32 @confstr(i32 %25, i8* %26, i32 %27)
br label %29
29: ; preds = %20, %1
%30 = load i8*, i8** %5
store i8* %30, i8** %8
br label %31
31: ; preds = %91, %29
%32 = load i32, i32* @PATH_MAX
%33 = zext i32 %32 to i64
%34 = call i8* @llvm.stacksave()
store i8* %34, i8** %9
%35 = alloca i8, i64 %33
store i64 %33, i64* %10
%36 = load i8*, i8** %8
%37 = call i8* @strchrnul(i8* %36, i8 signext 58)
store i8* %37, i8** %11
%38 = load i8*, i8** %11
%39 = load i8*, i8** %8
%40 = ptrtoint i8* %38 to i64
%41 = ptrtoint i8* %39 to i64
%42 = sub i64 %40, %41
store i64 %42, i64* %12
%43 = load i64, i64* %12
%44 = load i8*, i8** %8
%45 = getelementptr inbounds i8, i8* %44, i64 %43
store i8* %45, i8** %8
%46 = load i64, i64* %12
%47 = load i64, i64* %3
%48 = add i64 %46, %47
%49 = add i64 %48, 1
%50 = icmp ugt i64 %49, %33
br i1 %50, label %51, label %52
51: ; preds = %31
store i32 3, i32* %13
br label %78
52: ; preds = %31
%53 = load i64, i64* %12
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %67
55: ; preds = %52
%56 = load i8*, i8** %8
%57 = load i64, i64* %12
%58 = sub i64 0, %57
%59 = getelementptr inbounds i8, i8* %56, i64 %58
%60 = load i64, i64* %12
%61 = call i32 @strncpy(i8* %35, i8* %59, i64 %60)
%62 = load i64, i64* %12
%63 = getelementptr inbounds i8, i8* %35, i64 %62
store i8 47, i8* %63
%64 = load i64, i64* %12
%65 = add i64 %64, 1
%66 = getelementptr inbounds i8, i8* %35, i64 %65
store i8 0, i8* %66
br label %69
67: ; preds = %52
%68 = call i32 @strcpy(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %69
69: ; preds = %67, %55
%70 = load i8*, i8** %2
%71 = call i32 @strcat(i8* %35, i8* %70)
%72 = load i32, i32* @X_OK
%73 = call i32 @access(i8* %35, i32 %72)
%74 = icmp ne i32 %73, 0
br i1 %74, label %77, label %75
75: ; preds = %69
%76 = call i8* @strdup(i8* %35)
store i8* %76, i8** %6
br label %77
77: ; preds = %75, %69
store i32 0, i32* %13
br label %78
78: ; preds = %77, %51
%79 = load i8*, i8** %9
%80 = load i32, i32* %13
switch i32 %80, label %101 [
i32 0, label %81
i32 3, label %82
]
81: ; preds = %78
br label %82
82: ; preds = %81, %78
%83 = load i8*, i8** %6
%84 = icmp ne i8* %83, null
br i1 %84, label %91, label %85
85: ; preds = %82
%86 = load i8*, i8** %8
%87 = getelementptr inbounds i8, i8* %86, i32 1
store i8* %87, i8** %8
%88 = load i8, i8* %86
%89 = sext i8 %88 to i32
%90 = icmp eq i32 %89, 58
br label %91
91: ; preds = %85, %82
%92 = phi i1 [ false, %82 ], [ %90, %85 ]
br i1 %92, label %31, label %93
93: ; preds = %91
%94 = load i8*, i8** %4
%95 = icmp ne i8* %94, null
br i1 %95, label %99, label %96
96: ; preds = %93
%97 = load i8*, i8** %5
%98 = call i32 @free(i8* %97)
br label %99
99: ; preds = %96, %93
%100 = load i8*, i8** %6
ret i8* %100
101: ; preds = %78
unreachable
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@_CS_PATH = global i32 0
@PATH_MAX = global i32 0
@.str.1 = [3 x i8] c"./\00"
@X_OK = global i32 0
define i8* @which_program(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %11
5: ; preds = %1
%6 = load i32, i32* @_CS_PATH
%7 = tail call i32 @confstr(i32 %6, i8* null, i32 0)
%8 = tail call i8* @malloc(i32 %7)
%9 = load i32, i32* @_CS_PATH
%10 = tail call i32 @confstr(i32 %9, i8* %8, i32 %7)
br label %11
11: ; preds = %5, %1
%12 = phi i8* [ %3, %1 ], [ %8, %5 ]
br label %13
13: ; preds = %44, %11
%14 = phi i8* [ %12, %11 ], [ %45, %44 ]
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
%18 = alloca i8, i64 %16
%19 = call i8* @strchrnul(i8* %14, i8 signext 58)
%20 = ptrtoint i8* %19 to i64
%21 = ptrtoint i8* %14 to i64
%22 = sub i64 %20, %21
%23 = getelementptr inbounds i8, i8* %14, i64 %22
%24 = add i64 %22, 1
%25 = add i64 %24, %2
%26 = icmp ugt i64 %25, %16
br i1 %26, label %40, label %27
27: ; preds = %13
%28 = icmp eq i64 %22, 0
br i1 %28, label %33, label %29
29: ; preds = %27
%30 = call i32 @strncpy(i8* %18, i8* %14, i64 %22)
%31 = getelementptr inbounds i8, i8* %18, i64 %22
store i8 47, i8* %31
%32 = getelementptr inbounds i8, i8* %18, i64 %24
store i8 0, i8* %32
br label %35
33: ; preds = %27
%34 = call i32 @strcpy(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
%36 = call i32 @strcat(i8* %18, i8* %0)
%37 = load i32, i32* @X_OK
%38 = call i32 @access(i8* %18, i32 %37)
%39 = icmp eq i32 %38, 0
br i1 %39, label %41, label %40
40: ; preds = %13, %35
br label %44
41: ; preds = %35
%42 = call i8* @strdup(i8* %18)
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %48
44: ; preds = %40, %41
%45 = getelementptr inbounds i8, i8* %23, i64 1
%46 = load i8, i8* %23
%47 = icmp eq i8 %46, 58
br i1 %47, label %13, label %48
48: ; preds = %41, %44
%49 = phi i8* [ %42, %41 ], [ null, %44 ]
br i1 %4, label %50, label %52
50: ; preds = %48
%51 = call i32 @free(i8* %12)
br label %52
52: ; preds = %50, %48
ret i8* %49
}