#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(char *, char *);
void my_searchenv(char *name, char *envname, char *hitfile, int hitlen) {
  char *env;
  char *envcopy;
  char *cp;
  *hitfile = 0;
  if (name[0] == '/') {
    strncpy(hitfile, name, hitlen);
    return;
  }
  env = getenv(envname);
  if (env == NULL)
    return;
  envcopy = strdup(env);
  while ((cp = strtok(envcopy, ":")) != 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;
    }
  }
}


x86 assembly (clang, -O0):
<my_searchenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%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_searchenv+0x43>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <strncpy@plt>
jmp    130b <my_searchenv+0x16b>
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1200 <my_searchenv+0x60>
jmp    130b <my_searchenv+0x16b>
mov    -0x28(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xde8(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     130b <my_searchenv+0x16b>
mov    -0x38(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x4c(%rbp),%edi
add    %eax,%edi
add    $0x2,%edi
call   1090 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    126a <my_searchenv+0xca>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
jmp    130b <my_searchenv+0x16b>
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1060 <strcpy@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     12b1 <my_searchenv+0x111>
mov    -0x48(%rbp),%rdi
lea    0xd56(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x48(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10b0 <strcat@plt>
mov    -0x48(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%eax
jne    1306 <my_searchenv+0x166>
mov    -0x18(%rbp),%rdi
mov    -0x48(%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    130b <my_searchenv+0x16b>
jmp    120d <my_searchenv+0x6d>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_searchenv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rdi,%rbp
movb   $0x0,(%rdx)
cmpb   $0x2f,(%rdi)
jne    11d7 <my_searchenv+0x37>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %ecx,%edx
add    $0x18,%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
je     12d0 <my_searchenv+0x130>
mov    %rax,%rdi
call   10c0 <strdup@plt>
mov    %rax,%r12
lea    0xe02(%rip),%rsi
mov    %rax,%rdi
call   10a0 <strtok@plt>
test   %rax,%rax
je     12d0 <my_searchenv+0x130>
mov    %rax,%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
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   1090 <malloc@plt>
test   %rax,%rax
je     12c8 <my_searchenv+0x128>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r13,%rsi
call   1060 <strcpy@plt>
mov    %r14,%rdi
call   1070 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%r14,1)
je     1275 <my_searchenv+0xd5>
mov    %r14,%rdi
lea    0xd92(%rip),%rsi
call   10b0 <strcat@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
call   10b0 <strcat@plt>
mov    %r14,%rdi
lea    0x10(%rsp),%rsi
call   1080 <stat@plt>
test   %eax,%eax
je     12ae <my_searchenv+0x10e>
mov    %r12,%rdi
lea    0xd65(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,%r13
test   %rax,%rax
jne    1220 <my_searchenv+0x80>
jmp    12d0 <my_searchenv+0x130>
movslq 0xc(%rsp),%rbx
lea    -0x1(%rbx),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <strncpy@plt>
movb   $0x0,-0x1(%r15,%rbx,1)
mov    %r12,%rdi
call   1040 <free@plt>
add    $0x18,%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"
define void @my_searchenv(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 %struct.stat
%13 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%14 = load i8*, i8** %7
store i8 0, i8* %14
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i64 0
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 47
br i1 %19, label %20, label %25
20: ; preds = %4
%21 = load i8*, i8** %7
%22 = load i8*, i8** %5
%23 = load i32, i32* %8
%24 = call i32 @strncpy(i8* %21, i8* %22, i32 %23)
br label %88
25: ; preds = %4
%26 = load i8*, i8** %6
%27 = call i8* @getenv(i8* %26)
store i8* %27, i8** %9
%28 = load i8*, i8** %9
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %31
30: ; preds = %25
br label %88
31: ; preds = %25
%32 = load i8*, i8** %9
%33 = call i8* @strdup(i8* %32)
store i8* %33, i8** %10
br label %34
34: ; preds = %87, %31
%35 = load i8*, i8** %10
%36 = call i8* @strtok(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %36, i8** %11
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %88
38: ; preds = %34
%39 = load i8*, i8** %11
%40 = call i32 @strlen(i8* %39)
%41 = load i8*, i8** %5
%42 = call i32 @strlen(i8* %41)
%43 = add nsw i32 %40, %42
%44 = add nsw i32 %43, 2
%45 = call i8* @malloc(i32 %44)
store i8* %45, i8** %13
%46 = load i8*, i8** %13
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %51
48: ; preds = %38
%49 = load i8*, i8** %10
%50 = call i32 @free(i8* %49)
br label %88
51: ; preds = %38
%52 = load i8*, i8** %13
%53 = load i8*, i8** %11
%54 = call i32 @strcpy(i8* %52, i8* %53)
%55 = load i8*, i8** %13
%56 = load i8*, i8** %13
%57 = call i32 @strlen(i8* %56)
%58 = sub nsw i32 %57, 1
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %55, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp ne i32 %62, 47
br i1 %63, label %64, label %67
64: ; preds = %51
%65 = load i8*, i8** %13
%66 = call i32 @strcat(i8* %65, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %67
67: ; preds = %64, %51
%68 = load i8*, i8** %13
%69 = load i8*, i8** %5
%70 = call i32 @strcat(i8* %68, i8* %69)
%71 = load i8*, i8** %13
%72 = call i32 @stat(i8* %71, %struct.stat* %12)
%73 = icmp ne i32 %72, 0
br i1 %73, label %87, label %74
74: ; preds = %67
%75 = load i8*, i8** %7
%76 = load i8*, i8** %13
%77 = load i32, i32* %8
%78 = sub nsw i32 %77, 1
%79 = call i32 @strncpy(i8* %75, i8* %76, i32 %78)
%80 = load i8*, i8** %7
%81 = load i32, i32* %8
%82 = sub nsw i32 %81, 1
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds i8, i8* %80, i64 %83
store i8 0, i8* %84
%85 = load i8*, i8** %10
%86 = call i32 @free(i8* %85)
br label %88
87: ; preds = %67
br label %34
88: ; preds = %20, %30, %48, %74, %34
ret void
}

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