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


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    1324 <my_searchenv+0x184>
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1200 <my_searchenv+0x60>
jmp    1324 <my_searchenv+0x184>
mov    -0x28(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
lea    0xde0(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     131b <my_searchenv+0x17b>
movq   $0x0,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x54(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x54(%rbp),%edi
add    %eax,%edi
add    $0x2,%edi
call   1090 <malloc@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    127a <my_searchenv+0xda>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
jmp    1324 <my_searchenv+0x184>
mov    -0x50(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1060 <strcpy@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x50(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x60(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     12c1 <my_searchenv+0x121>
mov    -0x50(%rbp),%rdi
lea    0xd46(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10b0 <strcat@plt>
mov    -0x50(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%eax
jne    1316 <my_searchenv+0x176>
mov    -0x18(%rbp),%rdi
mov    -0x50(%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    1324 <my_searchenv+0x184>
jmp    1215 <my_searchenv+0x75>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
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    %ecx,%ebx
mov    %rdx,%r13
mov    %rdi,%rbp
movb   $0x0,(%rdx)
cmpb   $0x2f,(%rdi)
jne    11d9 <my_searchenv+0x39>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <strncpy@plt>
mov    %rsi,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     12e3 <my_searchenv+0x143>
mov    %ebx,0xc(%rsp)
mov    %rax,%rdi
call   10c0 <strdup@plt>
lea    0xe03(%rip),%rsi
mov    %rax,(%rsp)
mov    %rax,%rdi
call   10a0 <strtok@plt>
test   %rax,%rax
je     12a9 <my_searchenv+0x109>
mov    %rax,%r14
lea    0x10(%rsp),%r12
nopw   0x0(%rax,%rax,1)
mov    %r14,%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     12da <my_searchenv+0x13a>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <strcpy@plt>
mov    %r15,%rdi
call   1070 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%r15,1)
je     1275 <my_searchenv+0xd5>
mov    %r15,%rdi
lea    0xd92(%rip),%rsi
call   10b0 <strcat@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
call   10b0 <strcat@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1080 <stat@plt>
test   %eax,%eax
je     12c0 <my_searchenv+0x120>
xor    %edi,%edi
lea    0xd68(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1220 <my_searchenv+0x80>
mov    (%rsp),%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <free@plt>
movslq 0xc(%rsp),%rbx
lea    -0x1(%rbx),%rdx
mov    %r13,%rdi
mov    %r15,%rsi
call   1050 <strncpy@plt>
movb   $0x0,-0x1(%r13,%rbx,1)
mov    (%rsp),%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 i8*
%13 = alloca %struct.stat
%14 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%15 = load i8*, i8** %7
store i8 0, i8* %15
%16 = load i8*, i8** %5
%17 = getelementptr inbounds i8, i8* %16, i64 0
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 47
br i1 %20, label %21, label %26
21: ; preds = %4
%22 = load i8*, i8** %7
%23 = load i8*, i8** %5
%24 = load i32, i32* %8
%25 = call i32 @strncpy(i8* %22, i8* %23, i32 %24)
br label %93
26: ; preds = %4
%27 = load i8*, i8** %6
%28 = call i8* @getenv(i8* %27)
store i8* %28, i8** %9
%29 = load i8*, i8** %9
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %32
31: ; preds = %26
br label %93
32: ; preds = %26
%33 = load i8*, i8** %9
%34 = call i8* @strdup(i8* %33)
store i8* %34, i8** %10
%35 = load i8*, i8** %10
store i8* %35, i8** %12
br label %36
36: ; preds = %89, %32
%37 = load i8*, i8** %12
%38 = call i8* @strtok(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %38, i8** %11
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %90
40: ; preds = %36
store i8* null, i8** %12
%41 = load i8*, i8** %11
%42 = call i32 @strlen(i8* %41)
%43 = load i8*, i8** %5
%44 = call i32 @strlen(i8* %43)
%45 = add nsw i32 %42, %44
%46 = add nsw i32 %45, 2
%47 = call i8* @malloc(i32 %46)
store i8* %47, i8** %14
%48 = load i8*, i8** %14
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %53
50: ; preds = %40
%51 = load i8*, i8** %10
%52 = call i32 @free(i8* %51)
br label %93
53: ; preds = %40
%54 = load i8*, i8** %14
%55 = load i8*, i8** %11
%56 = call i32 @strcpy(i8* %54, i8* %55)
%57 = load i8*, i8** %14
%58 = load i8*, i8** %14
%59 = call i32 @strlen(i8* %58)
%60 = sub nsw i32 %59, 1
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %57, i64 %61
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = icmp ne i32 %64, 47
br i1 %65, label %66, label %69
66: ; preds = %53
%67 = load i8*, i8** %14
%68 = call i32 @strcat(i8* %67, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %69
69: ; preds = %66, %53
%70 = load i8*, i8** %14
%71 = load i8*, i8** %5
%72 = call i32 @strcat(i8* %70, i8* %71)
%73 = load i8*, i8** %14
%74 = call i32 @stat(i8* %73, %struct.stat* %13)
%75 = icmp ne i32 %74, 0
br i1 %75, label %89, label %76
76: ; preds = %69
%77 = load i8*, i8** %7
%78 = load i8*, i8** %14
%79 = load i32, i32* %8
%80 = sub nsw i32 %79, 1
%81 = call i32 @strncpy(i8* %77, i8* %78, i32 %80)
%82 = load i8*, i8** %7
%83 = load i32, i32* %8
%84 = sub nsw i32 %83, 1
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i8, i8* %82, i64 %85
store i8 0, i8* %86
%87 = load i8*, i8** %10
%88 = call i32 @free(i8* %87)
br label %93
89: ; preds = %69
br label %36
90: ; preds = %36
%91 = load i8*, i8** %10
%92 = call i32 @free(i8* %91)
br label %93
93: ; preds = %90, %76, %50, %31, %21
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 %54
10: ; preds = %4
%11 = tail call i8* @getenv(i8* %1)
%12 = icmp eq i8* %11, null
br i1 %12, label %54, 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 %54
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 %54
49: ; preds = %39
%50 = tail call i8* @strtok(i8* null, 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
%53 = tail call i32 @free(i8* %14)
br label %54
54: ; preds = %27, %43, %10, %52, %8
ret void
}