#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 */
char *getenv(char *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *, ...);
int stat(char const *, struct stat *);
char *strtok_r(char *, char *, char **);
char *which(char *outbuf, size_t bufsize, const char *filename) {
  struct stat statbuf;
  char *paths, *saveptr = NULL;
  if (filename && '/' == *filename) {
    int err = stat(filename, &statbuf);
    if (!err) {
      snprintf(outbuf, bufsize, "%s", filename);
      return outbuf;
    }
    return NULL;
  }
  for (paths = getenv("PATH");; paths = NULL) {
    char *path = strtok_r(paths, ":", &saveptr);
    int err;
    if (path == NULL)
      return NULL;
    snprintf(outbuf, bufsize, "%s/%s", path, filename);
    err = stat(outbuf, &statbuf);
    if (!err)
      return outbuf;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<which>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11c9 <which+0x89>
mov    -0x20(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x2f,%eax
cmp    %ecx,%eax
jne    11c9 <which+0x89>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    11bc <which+0x7c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
lea    0xe62(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1260 <which+0x120>
movq   $0x0,-0x8(%rbp)
jmp    1260 <which+0x120>
lea    0xe30(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe21(%rip),%rsi
lea    -0x38(%rbp),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1209 <which+0xc9>
movq   $0x0,-0x8(%rbp)
jmp    1260 <which+0x120>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x48(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xde7(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jne    124e <which+0x10e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1260 <which+0x120>
jmp    1253 <which+0x113>
movq   $0x0,-0x30(%rbp)
jmp    11d9 <which+0x99>
mov    -0x8(%rbp),%rax
add    $0x50,%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    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
movq   $0x0,0x10(%rsp)
test   %rdx,%rdx
je     1180 <which+0x40>
cmpb   $0x2f,(%r14)
jne    1180 <which+0x40>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1060 <stat@plt>
test   %eax,%eax
je     11e0 <which+0xa0>
xor    %ebx,%ebx
jmp    11f7 <which+0xb7>
lea    0xe79(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
lea    0x10(%rsp),%r13
lea    0xe6c(%rip),%rbp
lea    0x8(%rsp),%r12
lea    0xe5e(%rip),%rsi
mov    %r13,%rdx
call   1050 <strtok_r@plt>
test   %rax,%rax
je     117c <which+0x3c>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
mov    %rax,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1060 <stat@plt>
mov    $0x0,%edi
test   %eax,%eax
jne    11a0 <which+0x60>
jmp    11f7 <which+0xb7>
lea    0xe23(%rip),%rdx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rcx
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [3 x i8] c"%s\00"
@.str.1 = [5 x i8] c"PATH\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [6 x i8] c"%s/%s\00"
define i8* @which(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca %struct.stat
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i8* null, i8** %10
%14 = load i8*, i8** %7
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %33
16: ; preds = %3
%17 = load i8*, i8** %7
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 47, %19
br i1 %20, label %21, label %33
21: ; preds = %16
%22 = load i8*, i8** %7
%23 = call i32 @stat(i8* %22, %struct.stat* %8)
store i32 %23, i32* %11
%24 = load i32, i32* %11
%25 = icmp ne i32 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %21
%27 = load i8*, i8** %5
%28 = load i64, i64* %6
%29 = load i8*, i8** %7
%30 = call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %27, i64 %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %5
store i8* %31, i8** %4
br label %55
32: ; preds = %21
store i8* null, i8** %4
br label %55
33: ; preds = %16, %3
%34 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %34, i8** %9
br label %35
35: ; preds = %54, %33
%36 = load i8*, i8** %9
%37 = call i8* @strtok_r(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %10)
store i8* %37, i8** %12
%38 = load i8*, i8** %12
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %41
40: ; preds = %35
store i8* null, i8** %4
br label %55
41: ; preds = %35
%42 = load i8*, i8** %5
%43 = load i64, i64* %6
%44 = load i8*, i8** %12
%45 = load i8*, i8** %7
%46 = call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %42, i64 %43, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %44, i8* %45)
%47 = load i8*, i8** %5
%48 = call i32 @stat(i8* %47, %struct.stat* %8)
store i32 %48, i32* %13
%49 = load i32, i32* %13
%50 = icmp ne i32 %49, 0
br i1 %50, label %53, label %51
51: ; preds = %41
%52 = load i8*, i8** %5
store i8* %52, i8** %4
br label %55
53: ; preds = %41
br label %54
54: ; preds = %53
store i8* null, i8** %9
br label %35
55: ; preds = %51, %40, %32, %26
%56 = load i8*, i8** %4
ret i8* %56
}

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