#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*/ free(char *);
char *getenv(char *);
scalar_t__ path_is_exec(char const *);
int /*<<< orphan*/ *strchr(char const *, char);
char *string_format(char *, char const *, char const *);
char *strsep(char **, char *);
char *xxstrdup(char const *);
char *path_which(const char *exec) {
  if (!exec)
    return NULL;
  if (strchr(exec, '/') != NULL) {
    if (path_is_exec(exec))
      return xxstrdup(exec);
    return NULL;
  }
  const char *path_org = getenv("PATH");
  if (!path_org)
    return NULL;
  char *path = xxstrdup(path_org);
  int found = 0;
  char *candidate = NULL;
  const char *d;
  char *path_init = path;
  while (!found && (d = strsep(&path, ":")) != NULL) {
    if (*d == '\0')
      d = ".";
    candidate = string_format("%s/%s", d, exec);
    if (path_is_exec(candidate)) {
      found = 1;
    } else {
      free(candidate);
    }
  }
  free(path_init);
  if (found) {
    return candidate;
  } else {
    return NULL;
  }
}


x86 assembly (clang, -O0):
<path_which>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1194 <path_which+0x24>
movq   $0x0,-0x8(%rbp)
jmp    12ef <path_which+0x17f>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
cmp    $0x0,%rax
je     11de <path_which+0x6e>
mov    -0x10(%rbp),%rdi
call   1030 <path_is_exec@plt>
cmp    $0x0,%rax
je     11d1 <path_which+0x61>
mov    -0x10(%rbp),%rdi
call   1090 <xxstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    12ef <path_which+0x17f>
movq   $0x0,-0x8(%rbp)
jmp    12ef <path_which+0x17f>
lea    0xe1b(%rip),%rdi
call   1040 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1206 <path_which+0x96>
movq   $0x0,-0x8(%rbp)
jmp    12ef <path_which+0x17f>
mov    -0x18(%rbp),%rdi
call   1090 <xxstrdup@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x24(%rbp)
mov    %al,-0x41(%rbp)
jne    1257 <path_which+0xe7>
lea    -0x20(%rbp),%rdi
lea    0xdc1(%rip),%rsi
call   1080 <strsep@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    1267 <path_which+0xf7>
jmp    12c7 <path_which+0x157>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1282 <path_which+0x112>
lea    0xd89(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xd78(%rip),%rdi
call   1070 <string_format@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <path_is_exec@plt>
cmp    $0x0,%rax
je     12b9 <path_which+0x149>
movl   $0x1,-0x24(%rbp)
jmp    12c2 <path_which+0x152>
mov    -0x30(%rbp),%rdi
call   1050 <free@plt>
jmp    122a <path_which+0xba>
mov    -0x40(%rbp),%rdi
call   1050 <free@plt>
cmpl   $0x0,-0x24(%rbp)
je     12e7 <path_which+0x177>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ef <path_which+0x17f>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_which>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdi,%rdi
je     1266 <path_which+0xf6>
mov    %rdi,%r12
mov    $0x2f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11c0 <path_which+0x50>
mov    %r12,%rdi
call   1030 <path_is_exec@plt>
test   %rax,%rax
je     1266 <path_which+0xf6>
mov    %r12,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <xxstrdup@plt>
lea    0xe39(%rip),%rdi
call   1040 <getenv@plt>
test   %rax,%rax
je     1266 <path_which+0xf6>
mov    %rax,%rdi
call   1090 <xxstrdup@plt>
mov    %rax,%rbp
mov    %rax,0x10(%rsp)
lea    0xe19(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1080 <strsep@plt>
test   %rax,%rax
je     127a <path_which+0x10a>
mov    %rbp,0x8(%rsp)
lea    0xe00(%rip),%r14
lea    0xdfb(%rip),%r15
lea    0xdf0(%rip),%r13
lea    0x10(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
cmpb   $0x0,(%rax)
cmove  %r14,%rax
mov    %r15,%rdi
mov    %rax,%rsi
mov    %r12,%rdx
call   1070 <string_format@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <path_is_exec@plt>
test   %rax,%rax
jne    125f <path_which+0xef>
mov    %rbx,%rdi
call   1050 <free@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1080 <strsep@plt>
test   %rax,%rax
jne    1220 <path_which+0xb0>
xor    %ebx,%ebx
mov    0x8(%rsp),%rbp
jmp    127c <path_which+0x10c>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
mov    %rbp,%rdi
call   1050 <free@plt>
jmp    1268 <path_which+0xf8>

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

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [6 x i8] c"%s/%s\00"
define i8* @path_which(i8* %0) {
%2 = alloca i8*
%3 = icmp eq i8* %0, null
br i1 %3, label %35, label %4
4: ; preds = %1
%5 = tail call i32* @strchr(i8* %0, i8 signext 47)
%6 = icmp eq i32* %5, null
br i1 %6, label %12, label %7
7: ; preds = %4
%8 = tail call i64 @path_is_exec(i8* %0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %35, label %10
10: ; preds = %7
%11 = tail call i8* @xxstrdup(i8* %0)
br label %35
12: ; preds = %4
%13 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i8* %13, null
br i1 %14, label %35, label %15
15: ; preds = %12
%16 = bitcast i8** %2 to i8*
%17 = tail call i8* @xxstrdup(i8* %13)
store i8* %17, i8** %2
%18 = call i8* @strsep(i8** %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %32, label %20
20: ; preds = %15, %28
%21 = phi i8* [ %30, %28 ], [ %18, %15 ]
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 0
%24 = select i1 %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %21
%25 = call i8* @string_format(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %24, i8* %0)
%26 = call i64 @path_is_exec(i8* %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %20
%29 = call i32 @free(i8* %25)
%30 = call i8* @strsep(i8** %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %20
32: ; preds = %28, %20, %15
%33 = phi i8* [ null, %15 ], [ null, %28 ], [ %25, %20 ]
%34 = call i32 @free(i8* %17)
br label %35
35: ; preds = %32, %12, %7, %1, %10
%36 = phi i8* [ %11, %10 ], [ null, %1 ], [ null, %7 ], [ %33, %32 ], [ null, %12 ]
ret i8* %36
}