#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*/ free(char *);
int /*<<< orphan*/ getenv(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strdup(int /*<<< orphan*/);
int strlen(char *);
char *strtok(char *, char *);
int findCommand(char *path, const char *cmd) {
  const char *scan = cmd;
  while (*scan != 0) {
    if (*scan == '/') {
      strcpy(path, cmd);
      return 0;
    };
    scan++;
  };
  char *search = strdup(getenv("PATH"));
  char *token = strtok(search, ":");
  do {
    strcpy(path, token);
    if (path[strlen(path) - 1] != '/')
      strcat(path, "/");
    strcat(path, cmd);
    struct stat st;
    if (stat(path, &st) == 0) {
      free(search);
      return 0;
    };
    token = strtok(NULL, ":");
  } while (token != NULL);
  free(search);
  return -1;
}


x86 assembly (clang, -O0):
<findCommand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11e2 <findCommand+0x62>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11d1 <findCommand+0x51>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    12bc <findCommand+0x13c>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1198 <findCommand+0x18>
lea    0xe17(%rip),%rdi
call   1030 <getenv@plt>
mov    %eax,%edi
call   10a0 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe01(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1254 <findCommand+0xd4>
mov    -0x10(%rbp),%rdi
lea    0xdb8(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    128d <findCommand+0x10d>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12bc <findCommand+0x13c>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd6d(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    120d <findCommand+0x8d>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findCommand>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl (%rax),%ecx
test   %cl,%cl
je     11b6 <findCommand+0x36>
cmp    $0x2f,%cl
je     1250 <findCommand+0xd0>
add    $0x1,%rax
jmp    11a0 <findCommand+0x20>
lea    0xe43(%rip),%rdi
call   1030 <getenv@plt>
mov    %eax,%edi
call   10a0 <strdup@plt>
mov    %rax,%r15
lea    0xe32(%rip),%r12
mov    %rax,%rdi
mov    %r12,%rsi
call   1080 <strtok@plt>
mov    %rsp,%r13
lea    0xe1f(%rip),%rbp
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rax,%rsi
call   1050 <strcpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
je     1217 <findCommand+0x97>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1070 <stat@plt>
test   %rax,%rax
je     125d <findCommand+0xdd>
xor    %edi,%edi
mov    %r12,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
jne    11f0 <findCommand+0x70>
mov    %r15,%rdi
call   1040 <free@plt>
mov    $0xffffffff,%eax
jmp    1267 <findCommand+0xe7>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strcpy@plt>
jmp    1265 <findCommand+0xe5>
mov    %r15,%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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