#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 dirent {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *getenv(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strcat(char *, char *);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
scalar_t__ strlen(char *);
char *strtok(char *, char *);
char *find_command_path(char *command) {
  char *paths = getenv("PATH");
  char *path_dir = strtok(paths, ":");
  while (path_dir != NULL) {
    DIR *dir = opendir(path_dir);
    if (dir != NULL) {
      struct dirent *entry = NULL;
      while ((entry = readdir(dir)) != NULL) {
        if (strcmp(entry->d_name, command) == 0) {
          char *path_found = malloc(strlen(path_dir) + strlen(command) + 2);
          path_found = strcat(path_found, path_dir);
          path_found = strcat(path_found, "/");
          path_found = strcat(path_found, command);
          return path_found;
        }
      }
    } else {
      fprintf(stderr, "cannot open dir : %s", path_dir);
    }
    path_dir = strtok(NULL, ":");
  }
  return NULL;
}


x86 assembly (clang, -O0):
<find_command_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe5d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12c4 <find_command_path+0x134>
mov    -0x20(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1292 <find_command_path+0x102>
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     128d <find_command_path+0xfd>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1288 <find_command_path+0xf8>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10b0 <strcat@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xda6(%rip),%rsi
call   10b0 <strcat@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <strcat@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12cc <find_command_path+0x13c>
jmp    11eb <find_command_path+0x5b>
jmp    12ab <find_command_path+0x11b>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd63(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd4f(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    11c0 <find_command_path+0x30>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_command_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xe5b(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe54(%rip),%rsi
mov    %rax,%rdi
call   10a0 <strtok@plt>
test   %rax,%rax
je     1282 <find_command_path+0xf2>
mov    %rax,%r15
lea    0xe3b(%rip),%r12
lea    0xe32(%rip),%r13
xor    %r14d,%r14d
mov    %r15,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1210 <find_command_path+0x80>
mov    %rax,%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1224 <find_command_path+0x94>
mov    (%rax),%edi
mov    %rbx,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11f0 <find_command_path+0x60>
jmp    1238 <find_command_path+0xa8>
xchg   %ax,%ax
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
mov    %r15,%rdx
call   1070 <fprintf@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   10a0 <strtok@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11d6 <find_command_path+0x46>
jmp    1285 <find_command_path+0xf5>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    (%rax,%rbp,1),%rdi
add    $0x2,%rdi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
call   10b0 <strcat@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
movw   $0x2f,(%r14,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   10b0 <strcat@plt>
jmp    1285 <find_command_path+0xf5>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@stderr = global i32 0
@.str.3 = [21 x i8] c"cannot open dir : %s\00"
define i8* @find_command_path(i8* %0) {
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%3 = tail call i8* @strtok(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %34, label %5
5: ; preds = %1, %31
%6 = phi i8* [ %32, %31 ], [ %3, %1 ]
%7 = tail call i32* @opendir(i8* %6)
%8 = icmp eq i32* %7, null
br i1 %8, label %28, label %9
9: ; preds = %5, %12
%10 = tail call %struct.dirent* @readdir(i32* %7)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %31, label %12
12: ; preds = %9
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i64 @strcmp(i32 %14, i8* %0)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %9
17: ; preds = %12
%18 = tail call i64 @strlen(i8* %6)
%19 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%20 = add i64 %18, 2
%21 = add i64 %20, %19
%22 = tail call i8* @malloc(i64 %21)
%23 = tail call i8* @strcat(i8* dereferenceable(1) %22, i8* %6)
%24 = tail call i64 @strlen(i8* dereferenceable(1) %22)
%25 = getelementptr i8, i8* %22, i64 %24
%26 = bitcast i8* %25 to i16*
store i16 47, i16* %26
%27 = tail call i8* @strcat(i8* dereferenceable(1) %22, i8* dereferenceable(1) %0)
br label %34
28: ; preds = %5
%29 = load i32, i32* @stderr
%30 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %6)
br label %31
31: ; preds = %9, %28
%32 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %5
34: ; preds = %31, %1, %17
%35 = phi i8* [ %22, %17 ], [ null, %1 ], [ null, %31 ]
ret i8* %35
}