#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 */

/* Variables and functions */
scalar_t__ check_file_in_dir(char const *, char *, char *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
scalar_t__ malloc(scalar_t__);
char *strchr(char *, char const);
char *strdup(char *);
scalar_t__ strlen(char const *);
char *search_in_path(const char *exename) {
  const char dirsepchar = ':';
  char *syspath = strdup(getenv("PATH"));
  char *buff = (char *)malloc(strlen(syspath) + strlen(exename) + 2);
  char *dir = syspath;
  char *dirsep = strchr(dir, dirsepchar);
  char *path = 0;
  while (dirsep) {
    *dirsep = 0;
    if (check_file_in_dir(exename, dir, buff)) {
      path = strdup(dir);
      free(syspath);
      free(buff);
      return path;
    }
    dir = dirsep + 1;
    dirsep = strchr(dir, dirsepchar);
  }
  if (check_file_in_dir(exename, dir, buff)) {
    path = strdup(dir);
  }
  free(syspath);
  free(buff);
  return path;
}


x86 assembly (clang, -O0):
<search_in_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movb   $0x3a,-0x11(%rbp)
lea    0xe79(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1260 <search_in_path+0xf0>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1080 <check_file_in_dir@plt>
cmp    $0x0,%rax
je     123d <search_in_path+0xcd>
mov    -0x30(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12a2 <search_in_path+0x132>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,-0x38(%rbp)
jmp    11e4 <search_in_path+0x74>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1080 <check_file_in_dir@plt>
cmp    $0x0,%rax
je     1288 <search_in_path+0x118>
mov    -0x30(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_in_path>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r12
lea    0xe7d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1090 <strdup@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %r14,%rbx
test   %rax,%rax
je     1214 <search_in_path+0xa4>
mov    %rax,%r13
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movb   $0x0,0x0(%r13)
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1080 <check_file_in_dir@plt>
test   %rax,%rax
jne    1227 <search_in_path+0xb7>
mov    %r13,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11e0 <search_in_path+0x70>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1080 <check_file_in_dir@plt>
test   %rax,%rax
je     1234 <search_in_path+0xc4>
mov    %rbx,%rdi
call   1090 <strdup@plt>
mov    %rax,%rbx
jmp    1236 <search_in_path+0xc6>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1040 <free@plt>
mov    %r15,%rdi
call   1040 <free@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
define noalias i8* @search_in_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* @strdup(i8* %2)
%4 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = add i64 %4, 2
%7 = add i64 %6, %5
%8 = tail call i64 @malloc(i64 %7)
%9 = inttoptr i64 %8 to i8*
%10 = tail call i8* @strchr(i8* dereferenceable(1) %3, i8 signext 58)
%11 = icmp eq i8* %10, null
br i1 %11, label %21, label %12
12: ; preds = %1, %17
%13 = phi i8* [ %19, %17 ], [ %10, %1 ]
%14 = phi i8* [ %18, %17 ], [ %3, %1 ]
store i8 0, i8* %13
%15 = tail call i64 @check_file_in_dir(i8* %0, i8* %14, i8* %9)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %12
%18 = getelementptr inbounds i8, i8* %13, i64 1
%19 = tail call i8* @strchr(i8* %18, i8 signext 58)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %12
21: ; preds = %17, %1
%22 = phi i8* [ %3, %1 ], [ %18, %17 ]
%23 = tail call i64 @check_file_in_dir(i8* %0, i8* %22, i8* %9)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %12, %21
%26 = phi i8* [ %22, %21 ], [ %14, %12 ]
%27 = tail call i8* @strdup(i8* %26)
br label %28
28: ; preds = %25, %21
%29 = phi i8* [ null, %21 ], [ %27, %25 ]
%30 = tail call i32 @free(i8* %3)
%31 = tail call i32 @free(i8* %9)
ret i8* %29
}