#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 *);
char *getenv(char *);
char *malloc(int);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strtok_r(char *, char *, char **);
void my_searchenv2(char *name, char *envname, char *hitfile, int hitlen) {
  char *env;
  char *envcopy;
  char *envcopy2;
  char *cp;
  char *last;
  *hitfile = 0;
  if (name[0] == '/') {
    strncpy(hitfile, name, hitlen);
    return;
  }
  env = getenv(envname);
  if (env == NULL) {
    env = ".";
  }
  envcopy = strdup(env);
  envcopy2 = envcopy;
  while ((cp = strtok_r(envcopy2, ":", &last)) != NULL) {
    struct stat info;
    char *concat = malloc(strlen(cp) + strlen(name) + 2);
    if (concat == NULL) {
      free(envcopy);
      return;
    }
    strcpy(concat, cp);
    if (concat[strlen(concat) - 1] != '/')
      strcat(concat, "/");
    strcat(concat, name);
    if (!stat(concat, &info)) {
      strncpy(hitfile, concat, hitlen - 1);
      hitfile[hitlen - 1] = 0;
      free(envcopy);
      return;
    }
    envcopy2 = NULL;
  }
  free(envcopy);
}


x86 assembly (clang, -O0):
<my_searchenv2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11e3 <my_searchenv2+0x43>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <strncpy@plt>
jmp    132e <my_searchenv2+0x18e>
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1206 <my_searchenv2+0x66>
lea    0xdfe(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xddc(%rip),%rsi
lea    -0x48(%rbp),%rdx
call   1080 <strtok_r@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     1325 <my_searchenv2+0x185>
mov    -0x40(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x5c(%rbp),%edi
add    %eax,%edi
add    $0x2,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    127c <my_searchenv2+0xdc>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
jmp    132e <my_searchenv2+0x18e>
mov    -0x58(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1060 <strcpy@plt>
mov    -0x58(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x58(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x68(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     12c3 <my_searchenv2+0x123>
mov    -0x58(%rbp),%rdi
lea    0xd46(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x58(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10b0 <strcat@plt>
mov    -0x58(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%eax
jne    1318 <my_searchenv2+0x178>
mov    -0x18(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
call   1050 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
jmp    132e <my_searchenv2+0x18e>
movq   $0x0,-0x38(%rbp)
jmp    121b <my_searchenv2+0x7b>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_searchenv2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r12
mov    %rdi,%rbp
movb   $0x0,(%rdx)
cmpb   $0x2f,(%rdi)
jne    11d7 <my_searchenv2+0x37>
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %ecx,%edx
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <strncpy@plt>
mov    %ecx,0xc(%rsp)
mov    %rsi,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe13(%rip),%rdi
cmovne %rax,%rdi
call   10c0 <strdup@plt>
lea    0xe05(%rip),%rsi
lea    0x20(%rsp),%rdx
mov    %rax,0x10(%rsp)
mov    %rax,%rdi
call   1080 <strtok_r@plt>
test   %rax,%rax
je     12ca <my_searchenv2+0x12a>
mov    %rax,%r13
lea    0x20(%rsp),%r14
mov    %r13,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1070 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     12ca <my_searchenv2+0x12a>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r13,%rsi
call   1060 <strcpy@plt>
mov    %r15,%rdi
call   1070 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%r15,1)
je     1275 <my_searchenv2+0xd5>
mov    %r15,%rdi
lea    0xd94(%rip),%rsi
call   10b0 <strcat@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
call   10b0 <strcat@plt>
mov    %r15,%rdi
lea    0x18(%rsp),%rsi
call   1090 <stat@plt>
test   %eax,%eax
je     12b0 <my_searchenv2+0x110>
xor    %edi,%edi
lea    0xd68(%rip),%rsi
mov    %r14,%rdx
call   1080 <strtok_r@plt>
mov    %rax,%r13
test   %rax,%rax
jne    1220 <my_searchenv2+0x80>
jmp    12ca <my_searchenv2+0x12a>
movslq 0xc(%rsp),%rbx
lea    -0x1(%rbx),%rdx
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <strncpy@plt>
movb   $0x0,-0x1(%r12,%rbx,1)
mov    0x10(%rsp),%rdi
call   1040 <free@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [2 x i8] c"/\00"
define void @my_searchenv2(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca %struct.stat
%15 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%16 = load i8*, i8** %7
store i8 0, i8* %16
%17 = load i8*, i8** %5
%18 = getelementptr inbounds i8, i8* %17, i64 0
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 47
br i1 %21, label %22, label %27
22: ; preds = %4
%23 = load i8*, i8** %7
%24 = load i8*, i8** %5
%25 = load i32, i32* %8
%26 = call i32 @strncpy(i8* %23, i8* %24, i32 %25)
br label %94
27: ; preds = %4
%28 = load i8*, i8** %6
%29 = call i8* @getenv(i8* %28)
store i8* %29, i8** %9
%30 = load i8*, i8** %9
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %27
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %9
br label %33
33: ; preds = %32, %27
%34 = load i8*, i8** %9
%35 = call i8* @strdup(i8* %34)
store i8* %35, i8** %10
%36 = load i8*, i8** %10
store i8* %36, i8** %11
br label %37
37: ; preds = %90, %33
%38 = load i8*, i8** %11
%39 = call i8* @strtok_r(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %13)
store i8* %39, i8** %12
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %91
41: ; preds = %37
%42 = load i8*, i8** %12
%43 = call i32 @strlen(i8* %42)
%44 = load i8*, i8** %5
%45 = call i32 @strlen(i8* %44)
%46 = add nsw i32 %43, %45
%47 = add nsw i32 %46, 2
%48 = call i8* @malloc(i32 %47)
store i8* %48, i8** %15
%49 = load i8*, i8** %15
%50 = icmp eq i8* %49, null
br i1 %50, label %51, label %54
51: ; preds = %41
%52 = load i8*, i8** %10
%53 = call i32 @free(i8* %52)
br label %94
54: ; preds = %41
%55 = load i8*, i8** %15
%56 = load i8*, i8** %12
%57 = call i32 @strcpy(i8* %55, i8* %56)
%58 = load i8*, i8** %15
%59 = load i8*, i8** %15
%60 = call i32 @strlen(i8* %59)
%61 = sub nsw i32 %60, 1
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %58, i64 %62
%64 = load i8, i8* %63
%65 = sext i8 %64 to i32
%66 = icmp ne i32 %65, 47
br i1 %66, label %67, label %70
67: ; preds = %54
%68 = load i8*, i8** %15
%69 = call i32 @strcat(i8* %68, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %70
70: ; preds = %67, %54
%71 = load i8*, i8** %15
%72 = load i8*, i8** %5
%73 = call i32 @strcat(i8* %71, i8* %72)
%74 = load i8*, i8** %15
%75 = call i32 @stat(i8* %74, %struct.stat* %14)
%76 = icmp ne i32 %75, 0
br i1 %76, label %90, label %77
77: ; preds = %70
%78 = load i8*, i8** %7
%79 = load i8*, i8** %15
%80 = load i32, i32* %8
%81 = sub nsw i32 %80, 1
%82 = call i32 @strncpy(i8* %78, i8* %79, i32 %81)
%83 = load i8*, i8** %7
%84 = load i32, i32* %8
%85 = sub nsw i32 %84, 1
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i8, i8* %83, i64 %86
store i8 0, i8* %87
%88 = load i8*, i8** %10
%89 = call i32 @free(i8* %88)
br label %94
90: ; preds = %70
store i8* null, i8** %11
br label %37
91: ; preds = %37
%92 = load i8*, i8** %10
%93 = call i32 @free(i8* %92)
br label %94
94: ; preds = %91, %77, %51, %22
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [2 x i8] c"/\00"
define void @my_searchenv2(i8* %0, i8* nocapture %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = bitcast i8** %5 to i8*
store i8 0, i8* %2
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 47
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 @strncpy(i8* %2, i8* %0, i32 %3)
br label %55
12: ; preds = %4
%13 = tail call i8* @getenv(i8* %1)
%14 = icmp eq i8* %13, null
%15 = select i1 %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %13
%16 = tail call i8* @strdup(i8* %15)
%17 = call i8* @strtok_r(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %5)
%18 = icmp eq i8* %17, null
br i1 %18, label %53, label %19
19: ; preds = %12
%20 = bitcast %struct.stat* %6 to i8*
br label %21
21: ; preds = %19, %50
%22 = phi i8* [ %17, %19 ], [ %51, %50 ]
%23 = call i32 @strlen(i8* %22)
%24 = call i32 @strlen(i8* %0)
%25 = add i32 %23, 2
%26 = add i32 %25, %24
%27 = call i8* @malloc(i32 %26)
%28 = icmp eq i8* %27, null
br i1 %28, label %48, label %29
29: ; preds = %21
%30 = call i32 @strcpy(i8* %27, i8* %22)
%31 = call i32 @strlen(i8* %27)
%32 = add nsw i32 %31, -1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %27, i64 %33
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 47
br i1 %36, label %39, label %37
37: ; preds = %29
%38 = call i32 @strcat(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %29
%40 = call i32 @strcat(i8* %27, i8* %0)
%41 = call i32 @stat(i8* %27, %struct.stat* %6)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %39
%44 = add nsw i32 %3, -1
%45 = call i32 @strncpy(i8* %2, i8* %27, i32 %44)
%46 = sext i32 %44 to i64
%47 = getelementptr inbounds i8, i8* %2, i64 %46
store i8 0, i8* %47
br label %48
48: ; preds = %21, %43
%49 = call i32 @free(i8* %16)
br label %55
50: ; preds = %39
%51 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %5)
%52 = icmp eq i8* %51, null
br i1 %52, label %53, label %21
53: ; preds = %50, %12
%54 = call i32 @free(i8* %16)
br label %55
55: ; preds = %48, %53, %10
ret void
}