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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ free(char *);
char *get_current_dir_name();
int /*<<< orphan*/ getenv(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, ...);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(int /*<<< orphan*/);
scalar_t__ strncmp(char const *, char *, int);
scalar_t__ strstr(char const *, char *);
char *strtok(char *, char *);
bool command_found(const char *command, char *cmdpath) {
  int ret = false;
  char *path = strdup(getenv("PATH"));
  struct stat sb;
  char fpath[PATH_MAX];
  char *cdn;
  cdn = get_current_dir_name();
  if (strncmp(command, "./", 2) == 0) {
    snprintf(fpath, sizeof(fpath), "%s/%s", cdn, command + 2);
    if (stat(fpath, &sb) == 0)
      ret = true;
  } else if (strncmp(command, "/", 1) == 0) {
    snprintf(fpath, sizeof(fpath), "%s", command);
    if (stat(fpath, &sb) == 0)
      ret = true;
  } else if (strstr(command, "/")) {
    snprintf(fpath, sizeof(fpath), "%s/%s", cdn, command);
    if (stat(fpath, &sb) == 0)
      ret = true;
  } else {
    char *token = strtok(path, ":");
    while (token != NULL) {
      snprintf(fpath, sizeof(fpath), "%s/%s", token, command);
      if (stat(fpath, &sb) == 0) {
        ret = true;
        break;
      }
      token = strtok(NULL, ":");
    }
  }
  free(path);
  free(cdn);
  strcpy(cmdpath, fpath);
  return ret;
}


x86 assembly (clang, -O0):
<command_found>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe42(%rip),%rdi
call   1030 <getenv@plt>
mov    %eax,%edi
call   10b0 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1090 <get_current_dir_name@plt>
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xdf1(%rip),%rsi
mov    $0x2,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    126f <command_found+0xcf>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %eax,%esi
mov    -0x40(%rbp),%rcx
mov    -0x8(%rbp),%r8
add    $0x2,%r8
lea    0xdc3(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x50(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    126a <command_found+0xca>
movl   $0x1,-0x14(%rbp)
jmp    13b4 <command_found+0x214>
mov    -0x8(%rbp),%rdi
lea    0xd8c(%rip),%rsi
mov    $0x1,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    12cd <command_found+0x12d>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
lea    0xd68(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x50(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    12c8 <command_found+0x128>
movl   $0x1,-0x14(%rbp)
jmp    13af <command_found+0x20f>
mov    -0x8(%rbp),%rdi
lea    0xd2e(%rip),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%rax
je     132a <command_found+0x18a>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %eax,%esi
mov    -0x40(%rbp),%rcx
mov    -0x8(%rbp),%r8
lea    0xd08(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x50(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    1325 <command_found+0x185>
movl   $0x1,-0x14(%rbp)
jmp    13aa <command_found+0x20a>
mov    -0x20(%rbp),%rdi
lea    0xcd9(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     13a5 <command_found+0x205>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %eax,%esi
mov    -0x48(%rbp),%rcx
mov    -0x8(%rbp),%r8
lea    0xca6(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x50(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    138c <command_found+0x1ec>
movl   $0x1,-0x14(%rbp)
jmp    13a5 <command_found+0x205>
xor    %eax,%eax
mov    %eax,%edi
lea    0xc77(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x48(%rbp)
jmp    133e <command_found+0x19e>
jmp    13aa <command_found+0x20a>
jmp    13af <command_found+0x20f>
jmp    13b4 <command_found+0x214>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    -0x50(%rbp),%rsi
mov    -0x10(%rbp),%rdi
call   1060 <strcpy@plt>
mov    -0x14(%rbp),%eax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<command_found>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,-0x48(%rbp)
mov    %rdi,%r13
lea    0xe41(%rip),%rdi
call   1030 <getenv@plt>
mov    %eax,%edi
call   10b0 <strdup@plt>
mov    %rax,-0x38(%rbp)
mov    %rsp,%r15
mov    0x2e17(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %eax,%eax
call   1090 <get_current_dir_name@plt>
mov    %rax,%r12
lea    0xe04(%rip),%rsi
mov    %r13,%rdi
mov    $0x2,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
mov    %r15,-0x50(%rbp)
mov    %r12,-0x40(%rbp)
je     125f <command_found+0xbf>
lea    0xde4(%rip),%rsi
mov    %r13,%rdi
mov    $0x1,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
je     127f <command_found+0xdf>
lea    0xdcb(%rip),%rsi
mov    %r13,%rdi
call   10c0 <strstr@plt>
test   %rax,%rax
je     12e0 <command_found+0x140>
lea    0xdb5(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    -0x40(%rbp),%rcx
jmp    1273 <command_found+0xd3>
add    $0x2,%r13
lea    0xd9e(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r12,%rcx
mov    %r13,%r8
xor    %eax,%eax
call   1070 <snprintf@plt>
jmp    1296 <command_found+0xf6>
lea    0xd85(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r13,%rcx
xor    %eax,%eax
call   1070 <snprintf@plt>
lea    -0x30(%rbp),%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
xor    %r15d,%r15d
test   %rax,%rax
sete   %r15b
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    -0x48(%rbp),%rdi
mov    %rbx,%rsi
call   1060 <strcpy@plt>
mov    -0x50(%rbp),%rsp
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd27(%rip),%rsi
mov    -0x38(%rbp),%rdi
call   10a0 <strtok@plt>
test   %rax,%rax
je     1342 <command_found+0x1a2>
lea    0xd0c(%rip),%r12
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r12,%rdx
mov    %rax,%rcx
mov    %r13,%r8
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    %rbx,%rdi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
je     134a <command_found+0x1aa>
xor    %r15d,%r15d
xor    %edi,%edi
lea    0xcdb(%rip),%rsi
call   10a0 <strtok@plt>
test   %rax,%rax
jne    1300 <command_found+0x160>
jmp    12ac <command_found+0x10c>
xor    %r15d,%r15d
jmp    12ac <command_found+0x10c>
mov    $0x1,%r15d
jmp    12ac <command_found+0x10c>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"PATH\00"
@PATH_MAX = global i32 0
@.str.1 = [3 x i8] c"./\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [2 x i8] c":\00"
define i32 @command_found(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.stat
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
%12 = call i32 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = call i8* @strdup(i32 %12)
store i8* %13, i8** %6
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca i8, i64 %15
store i64 %15, i64* %9
%18 = call i8* (...) @get_current_dir_name()
store i8* %18, i8** %10
%19 = load i8*, i8** %3
%20 = call i64 @strncmp(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %32
22: ; preds = %2
%23 = trunc i64 %15 to i32
%24 = load i8*, i8** %10
%25 = load i8*, i8** %3
%26 = getelementptr inbounds i8, i8* %25, i64 2
%27 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %17, i32 %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %24, i8* %26)
%28 = call i64 @stat(i8* %17, %struct.stat* %7)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %22
store i32 1, i32* %5
br label %31
31: ; preds = %30, %22
br label %76
32: ; preds = %2
%33 = load i8*, i8** %3
%34 = call i64 @strncmp(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %44
36: ; preds = %32
%37 = trunc i64 %15 to i32
%38 = load i8*, i8** %3
%39 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %17, i32 %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %38)
%40 = call i64 @stat(i8* %17, %struct.stat* %7)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36
store i32 1, i32* %5
br label %43
43: ; preds = %42, %36
br label %75
44: ; preds = %32
%45 = load i8*, i8** %3
%46 = call i64 @strstr(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %57
48: ; preds = %44
%49 = trunc i64 %15 to i32
%50 = load i8*, i8** %10
%51 = load i8*, i8** %3
%52 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %17, i32 %49, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %50, i8* %51)
%53 = call i64 @stat(i8* %17, %struct.stat* %7)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %48
store i32 1, i32* %5
br label %56
56: ; preds = %55, %48
br label %74
57: ; preds = %44
%58 = load i8*, i8** %6
%59 = call i8* @strtok(i8* %58, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i8* %59, i8** %11
br label %60
60: ; preds = %71, %57
%61 = load i8*, i8** %11
%62 = icmp ne i8* %61, null
br i1 %62, label %63, label %73
63: ; preds = %60
%64 = trunc i64 %15 to i32
%65 = load i8*, i8** %11
%66 = load i8*, i8** %3
%67 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %17, i32 %64, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %65, i8* %66)
%68 = call i64 @stat(i8* %17, %struct.stat* %7)
%69 = icmp eq i64 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %63
store i32 1, i32* %5
br label %73
71: ; preds = %63
%72 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i8* %72, i8** %11
br label %60
73: ; preds = %70, %60
br label %74
74: ; preds = %73, %56
br label %75
75: ; preds = %74, %43
br label %76
76: ; preds = %75, %31
%77 = load i8*, i8** %6
%78 = call i32 @free(i8* %77)
%79 = load i8*, i8** %10
%80 = call i32 @free(i8* %79)
%81 = load i8*, i8** %4
%82 = call i32 @strcpy(i8* %81, i8* %17)
%83 = load i32, i32* %5
%84 = load i8*, i8** %8
ret i32 %83
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"PATH\00"
@PATH_MAX = global i32 0
@.str.1 = [3 x i8] c"./\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [2 x i8] c":\00"
define i32 @command_found(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = tail call i32 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = tail call i8* @strdup(i32 %4)
%6 = bitcast %struct.stat* %3 to i8*
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = tail call i8* (...) @get_current_dir_name()
%12 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = getelementptr inbounds i8, i8* %0, i64 2
%16 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %10, i32 %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %11, i8* %15)
br label %38
17: ; preds = %2
%18 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %10, i32 %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %38
22: ; preds = %17
%23 = tail call i64 @strstr(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %10, i32 %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %11, i8* %0)
br label %38
27: ; preds = %22
%28 = tail call i8* @strtok(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%29 = icmp eq i8* %28, null
br i1 %29, label %42, label %30
30: ; preds = %27, %35
%31 = phi i8* [ %36, %35 ], [ %28, %27 ]
%32 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %10, i32 %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %31, i8* %0)
%33 = call i64 @stat(i8* %10, %struct.stat* %3)
%34 = icmp eq i64 %33, 0
br i1 %34, label %42, label %35
35: ; preds = %30
%36 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%37 = icmp eq i8* %36, null
br i1 %37, label %42, label %30
38: ; preds = %14, %20, %25
%39 = call i64 @stat(i8* %10, %struct.stat* %3)
%40 = icmp eq i64 %39, 0
%41 = zext i1 %40 to i32
br label %42
42: ; preds = %30, %35, %38, %27
%43 = phi i32 [ 0, %27 ], [ %41, %38 ], [ 1, %30 ], [ 0, %35 ]
%44 = call i32 @free(i8* %5)
%45 = call i32 @free(i8* %11)
%46 = call i32 @strcpy(i8* %1, i8* %10)
ret i32 %43
}