#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*/ *fopen(char *, char const *);
int /*<<< orphan*/ free(char *);
char *getenv(char const *);
int /*<<< orphan*/ strcat(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char const *);
char *strrchr(char *, char);
char *strtok(char *, char *);
FILE *search_path_fopen(char *filename, const char *pathvar, const char *mode) {
  const char *envpointer;
  char *basename = NULL, *directory, *pathvalue = NULL;
  FILE *fp = NULL;
  fp = fopen(filename, mode);
  if (fp == NULL) {
    envpointer = getenv(pathvar);
    if (envpointer != NULL) {
      pathvalue = strdup(envpointer);
      basename = strrchr(filename, '/');
      basename = (basename != NULL) ? strdup(basename + 1) : strdup(filename);
      if (basename == NULL)
        return NULL;
      directory = strtok(pathvalue, ": ");
      while (directory != NULL) {
        strcat(strcat(strcpy(filename, directory), "/"), basename);
        fp = fopen(filename, mode);
        if (fp != NULL)
          break;
        directory = strtok(NULL, ": ");
      }
    }
  }
  if (basename != NULL)
    free(basename);
  if (pathvalue != NULL)
    free(pathvalue);
  return fp;
}


x86 assembly (clang, -O0):
<search_path_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12d6 <search_path_fopen+0x156>
mov    -0x18(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12d1 <search_path_fopen+0x151>
mov    -0x28(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1220 <search_path_fopen+0xa0>
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x50(%rbp)
jmp    122d <search_path_fopen+0xad>
mov    -0x10(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    124d <search_path_fopen+0xcd>
movq   $0x0,-0x8(%rbp)
jmp    1306 <search_path_fopen+0x186>
mov    -0x40(%rbp),%rdi
lea    0xda8(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12cc <search_path_fopen+0x14c>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1050 <strcpy@plt>
mov    %eax,%edi
lea    0xd81(%rip),%rsi
call   1090 <strcat@plt>
mov    %eax,%edi
mov    -0x30(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12b3 <search_path_fopen+0x133>
jmp    12cc <search_path_fopen+0x14c>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd42(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x38(%rbp)
jmp    1261 <search_path_fopen+0xe1>
jmp    12d1 <search_path_fopen+0x151>
jmp    12d6 <search_path_fopen+0x156>
cmpq   $0x0,-0x30(%rbp)
je     12ea <search_path_fopen+0x16a>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
cmpq   $0x0,-0x40(%rbp)
je     12fe <search_path_fopen+0x17e>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_path_fopen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %rdx,%rsi
call   1070 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1283 <search_path_fopen+0x103>
mov    %rbp,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1260 <search_path_fopen+0xe0>
mov    %rax,%rdi
call   10a0 <strdup@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11df <search_path_fopen+0x5f>
add    $0x1,%rax
mov    %rax,%rdi
jmp    11e2 <search_path_fopen+0x62>
mov    %rbx,%rdi
call   10a0 <strdup@plt>
mov    %rax,%r13
test   %rax,%rax
je     1260 <search_path_fopen+0xe0>
lea    0xe0a(%rip),%rsi
mov    %rbp,%rdi
call   1080 <strtok@plt>
test   %rax,%rax
mov    %rbp,(%rsp)
je     1265 <search_path_fopen+0xe5>
lea    0xdf5(%rip),%rbp
lea    0xdeb(%rip),%r15
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rax,%rsi
call   1050 <strcpy@plt>
mov    %eax,%edi
mov    %rbp,%rsi
call   1090 <strcat@plt>
mov    %eax,%edi
mov    %r13,%rsi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <fopen@plt>
test   %rax,%rax
jne    126a <search_path_fopen+0xea>
xor    %edi,%edi
mov    %r15,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
jne    1220 <search_path_fopen+0xa0>
jmp    126d <search_path_fopen+0xed>
xor    %r12d,%r12d
jmp    1283 <search_path_fopen+0x103>
xor    %r12d,%r12d
jmp    126d <search_path_fopen+0xed>
mov    %rax,%r12
mov    %r13,%rdi
call   1040 <free@plt>
mov    (%rsp),%rdi
test   %rdi,%rdi
je     1283 <search_path_fopen+0x103>
call   1040 <free@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c": \00"
@.str.1 = [2 x i8] c"/\00"
define i32* @search_path_fopen(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* null, i8** %9
store i8* null, i8** %11
store i32* null, i32** %12
%13 = load i8*, i8** %5
%14 = load i8*, i8** %7
%15 = call i32* @fopen(i8* %13, i8* %14)
store i32* %15, i32** %12
%16 = load i32*, i32** %12
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %65
18: ; preds = %3
%19 = load i8*, i8** %6
%20 = call i8* @getenv(i8* %19)
store i8* %20, i8** %8
%21 = load i8*, i8** %8
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %64
23: ; preds = %18
%24 = load i8*, i8** %8
%25 = call i8* @strdup(i8* %24)
store i8* %25, i8** %11
%26 = load i8*, i8** %5
%27 = call i8* @strrchr(i8* %26, i8 signext 47)
store i8* %27, i8** %9
%28 = load i8*, i8** %9
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %34
30: ; preds = %23
%31 = load i8*, i8** %9
%32 = getelementptr inbounds i8, i8* %31, i64 1
%33 = call i8* @strdup(i8* %32)
br label %37
34: ; preds = %23
%35 = load i8*, i8** %5
%36 = call i8* @strdup(i8* %35)
br label %37
37: ; preds = %34, %30
%38 = phi i8* [ %33, %30 ], [ %36, %34 ]
store i8* %38, i8** %9
%39 = load i8*, i8** %9
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %42
41: ; preds = %37
store i32* null, i32** %4
br label %79
42: ; preds = %37
%43 = load i8*, i8** %11
%44 = call i8* @strtok(i8* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %44, i8** %10
br label %45
45: ; preds = %61, %42
%46 = load i8*, i8** %10
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %63
48: ; preds = %45
%49 = load i8*, i8** %5
%50 = load i8*, i8** %10
%51 = call i32 @strcpy(i8* %49, i8* %50)
%52 = call i32 @strcat(i32 %51, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%53 = load i8*, i8** %9
%54 = call i32 @strcat(i32 %52, i8* %53)
%55 = load i8*, i8** %5
%56 = load i8*, i8** %7
%57 = call i32* @fopen(i8* %55, i8* %56)
store i32* %57, i32** %12
%58 = load i32*, i32** %12
%59 = icmp ne i32* %58, null
br i1 %59, label %60, label %61
60: ; preds = %48
br label %63
61: ; preds = %48
%62 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %62, i8** %10
br label %45
63: ; preds = %60, %45
br label %64
64: ; preds = %63, %18
br label %65
65: ; preds = %64, %3
%66 = load i8*, i8** %9
%67 = icmp ne i8* %66, null
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = load i8*, i8** %9
%70 = call i32 @free(i8* %69)
br label %71
71: ; preds = %68, %65
%72 = load i8*, i8** %11
%73 = icmp ne i8* %72, null
br i1 %73, label %74, label %77
74: ; preds = %71
%75 = load i8*, i8** %11
%76 = call i32 @free(i8* %75)
br label %77
77: ; preds = %74, %71
%78 = load i32*, i32** %12
store i32* %78, i32** %4
br label %79
79: ; preds = %77, %41
%80 = load i32*, i32** %4
ret i32* %80
}

LLVM IR (-O2):
@.str = [3 x i8] c": \00"
@.str.1 = [2 x i8] c"/\00"
define noalias i32* @search_path_fopen(i8* %0, i8* nocapture %1, i8* nocapture %2) {
%4 = tail call i32* @fopen(i8* %0, i8* %2)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %40
6: ; preds = %3
%7 = tail call i8* @getenv(i8* %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %40, label %9
9: ; preds = %6
%10 = tail call i8* @strdup(i8* %7)
%11 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%12 = icmp eq i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %9
%14 = getelementptr inbounds i8, i8* %11, i64 1
%15 = tail call i8* @strdup(i8* %14)
br label %18
16: ; preds = %9
%17 = tail call i8* @strdup(i8* %0)
br label %18
18: ; preds = %16, %13
%19 = phi i8* [ %15, %13 ], [ %17, %16 ]
%20 = icmp eq i8* %19, null
br i1 %20, label %40, label %21
21: ; preds = %18
%22 = tail call i8* @strtok(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%23 = icmp eq i8* %22, null
br i1 %23, label %34, label %24
24: ; preds = %21, %31
%25 = phi i8* [ %32, %31 ], [ %22, %21 ]
%26 = tail call i32 @strcpy(i8* %0, i8* %25)
%27 = tail call i32 @strcat(i32 %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%28 = tail call i32 @strcat(i32 %27, i8* %19)
%29 = tail call i32* @fopen(i8* %0, i8* %2)
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %24
34: ; preds = %24, %31, %21
%35 = phi i32* [ null, %21 ], [ null, %31 ], [ %29, %24 ]
%36 = tail call i32 @free(i8* %19)
%37 = icmp eq i8* %10, null
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = tail call i32 @free(i8* %10)
br label %40
40: ; preds = %6, %3, %34, %38, %18
%41 = phi i32* [ null, %18 ], [ %35, %38 ], [ %35, %34 ], [ %4, %3 ], [ null, %6 ]
ret i32* %41
}