#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 */
int /*<<< orphan*/ R_OK;
int access(char *, int /*<<< orphan*/);
int asprintf(char **, char *, char *, char *);
char *expand_environment_variable(char *);
int /*<<< orphan*/ free(char *);
char *strchr(char *, char);
char *strdup(char *);
char *search_file(char *name, char *pathes) {
  char *dup;
  char *p = NULL;
  char *s;
  char *rp;
  char *sname;
  int err;
  dup = strdup(pathes);
  s = dup;
  do {
    p = strchr(s, ':');
    if (p != NULL) {
      p[0] = 0;
    }
    rp = expand_environment_variable(s);
    err = asprintf(&sname, "%s/%s", rp, name);
    free(rp);
    err = access(sname, R_OK);
    if (err == 0)
      return sname;
    free(sname);
    s = p + 1;
  } while (p != NULL);
  free(dup);
  return NULL;
}


x86 assembly (clang, -O0):
<search_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11b1 <search_file+0x51>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
call   1060 <expand_environment_variable@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    -0x40(%rbp),%rdi
lea    0xe2f(%rip),%rsi
call   1050 <asprintf@plt>
mov    %eax,-0x44(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    120e <search_file+0xae>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    123f <search_file+0xdf>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118d <search_file+0x2d>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r13
mov    %rsi,%rdi
call   1080 <strdup@plt>
mov    %rax,%rbp
lea    0xe7d(%rip),%r15
lea    0x8(%rsp),%r12
mov    0x2e51(%rip),%r14
mov    %rax,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbp,%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11b8 <search_file+0x58>
movb   $0x0,(%rbx)
mov    %rbp,%rdi
call   1060 <expand_environment_variable@plt>
mov    %rax,%rbp
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
mov    %r13,%rcx
call   1050 <asprintf@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
mov    (%r14),%esi
call   1070 <access@plt>
mov    0x8(%rsp),%rdi
test   %eax,%eax
je     120c <search_file+0xac>
call   1030 <free@plt>
lea    0x1(%rbx),%rbp
test   %rbx,%rbx
jne    11a0 <search_file+0x40>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
xor    %edi,%edi
mov    %rdi,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@R_OK = global i32 0
define i8* @search_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %7
%12 = load i8*, i8** %5
%13 = call i8* @strdup(i8* %12)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
store i8* %14, i8** %8
br label %15
15: ; preds = %43, %2
%16 = load i8*, i8** %8
%17 = call i8* @strchr(i8* %16, i8 signext 58)
store i8* %17, i8** %7
%18 = load i8*, i8** %7
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i8*, i8** %7
%22 = getelementptr inbounds i8, i8* %21, i64 0
store i8 0, i8* %22
br label %23
23: ; preds = %20, %15
%24 = load i8*, i8** %8
%25 = call i8* @expand_environment_variable(i8* %24)
store i8* %25, i8** %9
%26 = load i8*, i8** %9
%27 = load i8*, i8** %4
%28 = call i32 @asprintf(i8** %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %27)
store i32 %28, i32* %11
%29 = load i8*, i8** %9
%30 = call i32 @free(i8* %29)
%31 = load i8*, i8** %10
%32 = load i32, i32* @R_OK
%33 = call i32 @access(i8* %31, i32 %32)
store i32 %33, i32* %11
%34 = load i32, i32* %11
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %23
%37 = load i8*, i8** %10
store i8* %37, i8** %3
br label %49
38: ; preds = %23
%39 = load i8*, i8** %10
%40 = call i32 @free(i8* %39)
%41 = load i8*, i8** %7
%42 = getelementptr inbounds i8, i8* %41, i64 1
store i8* %42, i8** %8
br label %43
43: ; preds = %38
%44 = load i8*, i8** %7
%45 = icmp ne i8* %44, null
br i1 %45, label %15, label %46
46: ; preds = %43
%47 = load i8*, i8** %6
%48 = call i32 @free(i8* %47)
store i8* null, i8** %3
br label %49
49: ; preds = %46, %36
%50 = load i8*, i8** %3
ret i8* %50
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@R_OK = global i32 0
define i8* @search_file(i8* %0, i8* nocapture %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = tail call i8* @strdup(i8* %1)
br label %6
6: ; preds = %20, %2
%7 = phi i8* [ %5, %2 ], [ %22, %20 ]
%8 = call i8* @strchr(i8* dereferenceable(1) %7, i8 signext 58)
%9 = icmp eq i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %6
store i8 0, i8* %8
br label %11
11: ; preds = %10, %6
%12 = call i8* @expand_environment_variable(i8* %7)
%13 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %12, i8* %0)
%14 = call i32 @free(i8* %12)
%15 = load i8*, i8** %3
%16 = load i32, i32* @R_OK
%17 = call i32 @access(i8* %15, i32 %16)
%18 = icmp eq i32 %17, 0
%19 = load i8*, i8** %3
br i1 %18, label %25, label %20
20: ; preds = %11
%21 = call i32 @free(i8* %19)
%22 = getelementptr inbounds i8, i8* %8, i64 1
br i1 %9, label %23, label %6
23: ; preds = %20
%24 = call i32 @free(i8* %5)
br label %25
25: ; preds = %11, %23
%26 = phi i8* [ null, %23 ], [ %19, %11 ]
ret i8* %26
}