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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *getenv(char *);
char *strcat(char *, char const *);
scalar_t__ strcmp(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ *strcpy(char *, char *);
char *strtok(char *, char *);
char *search_cmd_in_path(const char *command) {
  if (((command == NULL) || ((char)command == 0)))
    return NULL;
  char PATH[4096];
  static char cmd[1024] = "";
  FILE *cmd_file;
  if (strcpy(PATH, getenv("PATH")) == NULL) {
    return NULL;
  }
  strcpy(cmd, strtok(PATH, ":"));
  strcat(strcat(cmd, "/"), command);
  if ((cmd_file = fopen(cmd, "r"))) {
    fclose(cmd_file);
    return cmd;
  }
  char *p;
  while (strcmp(strcpy(cmd, ((p = strtok(NULL, ":")) != NULL) ? p : ""), "") !=
         0) {
    strcat(strcat(cmd, "/"), command);
    if ((cmd_file = fopen(cmd, "r"))) {
      fclose(cmd_file);
      return cmd;
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<search_cmd_in_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119a <search_cmd_in_path+0x2a>
mov    -0x10(%rbp),%rax
movsbl %al,%eax
cmp    $0x0,%eax
jne    11a7 <search_cmd_in_path+0x37>
movq   $0x0,-0x8(%rbp)
jmp    134c <search_cmd_in_path+0x1dc>
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1028(%rbp)
lea    0xe44(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x1028(%rbp),%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
cmp    $0x0,%rax
jne    11e7 <search_cmd_in_path+0x77>
movq   $0x0,-0x8(%rbp)
jmp    134c <search_cmd_in_path+0x1dc>
lea    -0x1010(%rbp),%rdi
lea    0xe10(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,%rsi
lea    0x2e6c(%rip),%rdi
call   1040 <strcpy@plt>
lea    0x2e60(%rip),%rdi
lea    0xdf0(%rip),%rsi
call   1090 <strcat@plt>
mov    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcat@plt>
lea    0x2e41(%rip),%rdi
lea    0xdd3(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
je     1268 <search_cmd_in_path+0xf8>
mov    -0x1018(%rbp),%rdi
call   1050 <fclose@plt>
lea    0x2e11(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    134c <search_cmd_in_path+0x1dc>
jmp    126d <search_cmd_in_path+0xfd>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd8d(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x1020(%rbp)
cmp    $0x0,%rax
je     12a1 <search_cmd_in_path+0x131>
mov    -0x1020(%rbp),%rax
mov    %rax,-0x1030(%rbp)
jmp    12b4 <search_cmd_in_path+0x144>
lea    0xd60(%rip),%rax
mov    %rax,-0x1030(%rbp)
jmp    12b4 <search_cmd_in_path+0x144>
mov    -0x1030(%rbp),%rsi
lea    0x2dae(%rip),%rdi
call   1040 <strcpy@plt>
mov    %rax,%rdi
lea    0xd37(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1344 <search_cmd_in_path+0x1d4>
lea    0x2d89(%rip),%rdi
lea    0xd19(%rip),%rsi
call   1090 <strcat@plt>
mov    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcat@plt>
lea    0x2d6a(%rip),%rdi
lea    0xcfc(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
je     133f <search_cmd_in_path+0x1cf>
mov    -0x1018(%rbp),%rdi
call   1050 <fclose@plt>
lea    0x2d3a(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    134c <search_cmd_in_path+0x1dc>
jmp    126d <search_cmd_in_path+0xfd>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_cmd_in_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rdi,%rbx
test   %bl,%bl
je     1296 <search_cmd_in_path+0x116>
lea    0xe5d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rsp,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
test   %rax,%rax
je     1296 <search_cmd_in_path+0x116>
lea    0xe42(%rip),%rsi
mov    %rsp,%rdi
call   1090 <strtok@plt>
lea    0x2e9e(%rip),%r15
mov    %r15,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
movw   $0x2f,(%rax,%r15,1)
mov    %r15,%rdi
mov    %rbx,%rsi
call   10a0 <strcat@plt>
lea    0xe09(%rip),%rsi
mov    %r15,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
jne    128c <search_cmd_in_path+0x10c>
lea    0xdef(%rip),%r12
xor    %r15d,%r15d
lea    0xde6(%rip),%r13
lea    0x2e49(%rip),%rbp
lea    0xdd9(%rip),%r14
xchg   %ax,%ax
xor    %edi,%edi
mov    %r12,%rsi
call   1090 <strtok@plt>
test   %rax,%rax
cmove  %r13,%rax
mov    %rbp,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
mov    %rax,%rdi
mov    %r13,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1299 <search_cmd_in_path+0x119>
mov    %rbp,%rdi
call   1060 <strlen@plt>
movw   $0x2f,(%rax,%rbp,1)
mov    %rbp,%rdi
mov    %rbx,%rsi
call   10a0 <strcat@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1230 <search_cmd_in_path+0xb0>
lea    0x2de4(%rip),%r15
mov    %rax,%rdi
call   1050 <fclose@plt>
jmp    1299 <search_cmd_in_path+0x119>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@search_cmd_in_path.cmd = internal global [1024 x i8] zeroinitializer
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [1 x i8] zeroinitializer
define i8* @search_cmd_in_path(i8* %0) {
%2 = alloca [4096 x i8]
%3 = ptrtoint i8* %0 to i64
%4 = and i64 %3, 255
%5 = icmp eq i64 %4, 0
br i1 %5, label %39, label %6
6: ; preds = %1
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = call i32* @strcpy(i8* %7, i8* %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %37, label %11
11: ; preds = %6
%12 = call i8* @strtok(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32* @strcpy(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @search_cmd_in_path.cmd, i64 0, i64 0), i8* %12)
%14 = call i64 @strlen(i8* dereferenceable(1) getelementptr inbounds ([1024 x i8], [1024 x i8]* @search_cmd_in_path.cmd, i64 0, i64 0))
%15 = getelementptr [1024 x i8], [1024 x i8]* @search_cmd_in_path.cmd, i64 0, i64 %14
%16 = bitcast i8* %15 to i16*
store i16 47, i16* %16
%17 = call i8* @strcat(i8* dereferenceable(1) getelementptr inbounds ([1024 x i8], [1024 x i8]* @search_cmd_in_path.cmd, i64 0, i64 0), i8* %0)
%18 = call i32* @fopen(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @search_cmd_in_path.cmd, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %34
20: ; preds = %11, %27
%21 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i8* %21, null
%23 = select i1 %22, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i8* %21
%24 = call i32* @strcpy(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @search_cmd_in_path.cmd, i64 0, i64 0), i8* %23)
%25 = call i64 @strcmp(i32* %24, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %37, label %27
27: ; preds = %20
%28 = call i64 @strlen(i8* dereferenceable(1) getelementptr inbounds ([1024 x i8], [1024 x i8]* @search_cmd_in_path.cmd, i64 0, i64 0))
%29 = getelementptr [1024 x i8], [1024 x i8]* @search_cmd_in_path.cmd, i64 0, i64 %28
%30 = bitcast i8* %29 to i16*
store i16 47, i16* %30
%31 = call i8* @strcat(i8* dereferenceable(1) getelementptr inbounds ([1024 x i8], [1024 x i8]* @search_cmd_in_path.cmd, i64 0, i64 0), i8* %0)
%32 = call i32* @fopen(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @search_cmd_in_path.cmd, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i32* %32, null
br i1 %33, label %20, label %34
34: ; preds = %27, %11
%35 = phi i32* [ %18, %11 ], [ %32, %27 ]
%36 = call i32 @fclose(i32* %35)
br label %37
37: ; preds = %20, %34, %6
%38 = phi i8* [ null, %6 ], [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @search_cmd_in_path.cmd, i64 0, i64 0), %34 ], [ null, %20 ]
br label %39
39: ; preds = %1, %37
%40 = phi i8* [ %38, %37 ], [ null, %1 ]
ret i8* %40
}