#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*/ F_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
char *calloc(int, size_t);
int /*<<< orphan*/ free(char *);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
int walker(size_t len, const char *name, void *ud) {
  const char **paths = ud;
  const char *argv0 = paths[0];
  size_t alen = strlen(argv0);
  char *buf = calloc(1, len + alen + 1 + 1);
  strncpy(buf, name, len);
  buf[len] = '/';
  strncpy(&buf[len + 1], argv0, alen);
  if (access(buf, F_OK) == 0) {
    paths[1] = buf;
    return 1;
  }
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<walker>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rsi
add    -0x38(%rbp),%rsi
add    $0x1,%rsi
add    $0x1,%rsi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rax
add    $0x1,%rax
add    %rax,%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x40(%rbp),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
jne    120f <walker+0xbf>
mov    -0x40(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,0x8(%rax)
movl   $0x1,-0x4(%rbp)
jmp    121f <walker+0xcf>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<walker>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%rbp
mov    %rdx,(%rsp)
mov    (%rdx),%r13
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %rax,%r14
lea    (%rax,%rbp,1),%rsi
add    $0x2,%rsi
mov    $0x1,%r15d
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1040 <strncpy@plt>
movb   $0x2f,(%rbx,%rbp,1)
lea    (%rbx,%rbp,1),%rdi
add    $0x1,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
call   1040 <strncpy@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <access@plt>
test   %rax,%rax
je     11d6 <walker+0x86>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
jmp    11de <walker+0x8e>
mov    (%rsp),%rax
mov    %rbx,0x8(%rax)
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@F_OK = global i32 0
define i32 @walker(i64 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8**
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
store i64 %0, i64* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%12 = load i8*, i8** %7
%13 = bitcast i8* %12 to i8**
store i8** %13, i8*** %8
%14 = load i8**, i8*** %8
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
store i8* %16, i8** %9
%17 = load i8*, i8** %9
%18 = call i64 @strlen(i8* %17)
store i64 %18, i64* %10
%19 = load i64, i64* %5
%20 = load i64, i64* %10
%21 = add i64 %19, %20
%22 = add i64 %21, 1
%23 = add i64 %22, 1
%24 = call i8* @calloc(i32 1, i64 %23)
store i8* %24, i8** %11
%25 = load i8*, i8** %11
%26 = load i8*, i8** %6
%27 = load i64, i64* %5
%28 = call i32 @strncpy(i8* %25, i8* %26, i64 %27)
%29 = load i8*, i8** %11
%30 = load i64, i64* %5
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8 47, i8* %31
%32 = load i8*, i8** %11
%33 = load i64, i64* %5
%34 = add i64 %33, 1
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8*, i8** %9
%37 = load i64, i64* %10
%38 = call i32 @strncpy(i8* %35, i8* %36, i64 %37)
%39 = load i8*, i8** %11
%40 = load i32, i32* @F_OK
%41 = call i64 @access(i8* %39, i32 %40)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %3
%44 = load i8*, i8** %11
%45 = load i8**, i8*** %8
%46 = getelementptr inbounds i8*, i8** %45, i64 1
store i8* %44, i8** %46
store i32 1, i32* %4
br label %50
47: ; preds = %3
%48 = load i8*, i8** %11
%49 = call i32 @free(i8* %48)
store i32 0, i32* %4
br label %50
50: ; preds = %47, %43
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
@F_OK = global i32 0
define i32 @walker(i64 %0, i8* %1, i8* nocapture %2) {
%4 = bitcast i8* %2 to i8**
%5 = load i8*, i8** %4
%6 = tail call i64 @strlen(i8* dereferenceable(1) %5)
%7 = add i64 %0, 2
%8 = add i64 %7, %6
%9 = tail call i8* @calloc(i32 1, i64 %8)
%10 = tail call i32 @strncpy(i8* %9, i8* %1, i64 %0)
%11 = getelementptr inbounds i8, i8* %9, i64 %0
store i8 47, i8* %11
%12 = add i64 %0, 1
%13 = getelementptr inbounds i8, i8* %9, i64 %12
%14 = tail call i32 @strncpy(i8* %13, i8* %5, i64 %6)
%15 = load i32, i32* @F_OK
%16 = tail call i64 @access(i8* %9, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = getelementptr inbounds i8, i8* %2, i64 8
%20 = bitcast i8* %19 to i8**
store i8* %9, i8** %20
br label %23
21: ; preds = %3
%22 = tail call i32 @free(i8* %9)
br label %23
23: ; preds = %21, %18
%24 = phi i32 [ 1, %18 ], [ 0, %21 ]
ret i32 %24
}