#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ asprintf(char **, char *, char *, char const *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
scalar_t__ stat(char *, struct stat *);
char *strdup(char const *);
char *strtok(char *, char *);
char *which(const char *cmd) {
  char *file = NULL;
  char *token = NULL;
  char *tmp = NULL;
  struct stat buf;
  const char *path = NULL;
  path = getenv("PATH");
  if (!path)
    goto out;
  tmp = strdup(path);
  if (!tmp)
    goto out;
  token = strtok(tmp, ":");
  while (token != NULL) {
    asprintf(&file, "%s/%s", token, cmd);
    if (stat(file, &buf) == 0)
      break;
    token = strtok(NULL, ":");
    free(file);
    file = NULL;
  }
out:
  free(tmp);
  tmp = NULL;
  return file;
}


x86 assembly (clang, -O0):
<which>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
lea    0xe6d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ac <which+0x4c>
jmp    124b <which+0xeb>
mov    -0x30(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c9 <which+0x69>
jmp    124b <which+0xeb>
mov    -0x20(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1246 <which+0xe6>
mov    -0x18(%rbp),%rdx
mov    -0x8(%rbp),%rcx
lea    -0x10(%rbp),%rdi
lea    0xe0c(%rip),%rsi
call   1060 <asprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    121c <which+0xbc>
jmp    1246 <which+0xe6>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdde(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x10(%rbp)
jmp    11dd <which+0x7d>
jmp    124b <which+0xeb>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<which>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
movq   $0x0,(%rsp)
lea    0xe80(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     122e <which+0xce>
mov    %rax,%rdi
call   1080 <strdup@plt>
test   %rax,%rax
je     122e <which+0xce>
mov    %rax,%rbp
lea    0xe5c(%rip),%rsi
mov    %rax,%rdi
call   1070 <strtok@plt>
test   %rax,%rax
je     122a <which+0xca>
mov    %rax,%r14
mov    %rbp,0x8(%rsp)
lea    0xe42(%rip),%r12
mov    %rsp,%r13
lea    0x10(%rsp),%rbp
lea    0xe31(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1060 <asprintf@plt>
mov    (%rsp),%rdi
mov    %rbp,%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     124c <which+0xec>
xor    %edi,%edi
mov    %r15,%rsi
call   1070 <strtok@plt>
mov    %rax,%r14
mov    (%rsp),%rdi
call   1040 <free@plt>
movq   $0x0,(%rsp)
test   %r14,%r14
jne    11e0 <which+0x80>
mov    0x8(%rsp),%rbp
xor    %ebx,%ebx
jmp    1232 <which+0xd2>
xor    %ebx,%ebx
xor    %ebp,%ebp
mov    %rbp,%rdi
call   1040 <free@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rbx
mov    0x8(%rsp),%rbp
jmp    1232 <which+0xd2>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i8* @which(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i8*
store i8* %0, i8** %2
store i8* null, i8** %3
store i8* null, i8** %4
store i8* null, i8** %5
store i8* null, i8** %7
%8 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %7
%9 = load i8*, i8** %7
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %1
br label %37
12: ; preds = %1
%13 = load i8*, i8** %7
%14 = call i8* @strdup(i8* %13)
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %12
br label %37
18: ; preds = %12
%19 = load i8*, i8** %5
%20 = call i8* @strtok(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %20, i8** %4
br label %21
21: ; preds = %32, %18
%22 = load i8*, i8** %4
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %36
24: ; preds = %21
%25 = load i8*, i8** %4
%26 = load i8*, i8** %2
%27 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %25, i8* %26)
%28 = load i8*, i8** %3
%29 = call i64 @stat(i8* %28, %struct.stat* %6)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %24
br label %36
32: ; preds = %24
%33 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %33, i8** %4
%34 = load i8*, i8** %3
%35 = call i32 @free(i8* %34)
store i8* null, i8** %3
br label %21
36: ; preds = %31, %21
br label %37
37: ; preds = %36, %17, %11
%38 = load i8*, i8** %5
%39 = call i32 @free(i8* %38)
store i8* null, i8** %5
%40 = load i8*, i8** %3
ret i8* %40
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i8* @which(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%5 = bitcast %struct.stat* %3 to i8*
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %27, label %8
8: ; preds = %1
%9 = tail call i8* @strdup(i8* %6)
%10 = icmp eq i8* %9, null
br i1 %10, label %27, label %11
11: ; preds = %8
%12 = tail call i8* @strtok(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %27, label %14
14: ; preds = %11, %22
%15 = phi i8* [ %23, %22 ], [ %12, %11 ]
%16 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %15, i8* %0)
%17 = load i8*, i8** %2
%18 = call i64 @stat(i8* %17, %struct.stat* %3)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = load i8*, i8** %2
br label %27
22: ; preds = %14
%23 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = load i8*, i8** %2
%25 = call i32 @free(i8* %24)
store i8* null, i8** %2
%26 = icmp eq i8* %23, null
br i1 %26, label %27, label %14
27: ; preds = %22, %20, %11, %8, %1
%28 = phi i8* [ null, %8 ], [ null, %1 ], [ null, %11 ], [ %21, %20 ], [ null, %22 ]
%29 = phi i8* [ null, %8 ], [ null, %1 ], [ %9, %11 ], [ %9, %20 ], [ %9, %22 ]
%30 = call i32 @free(i8* %29)
ret i8* %28
}