#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*/ X_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char const *);
char *strdup(char const *);
int strlen(char const *);
char *strtok(char *, char *);
char *which_path(const char *name, const char *_path) {
  char *path = strdup(_path);
  if (NULL == path)
    return NULL;
  char *tok = strtok(path, ":");
  while (tok) {
    int len = strlen(tok) + 2 + strlen(name);
    char *file = malloc(len);
    if (!file) {
      free(path);
      return NULL;
    }
    sprintf(file, "%s/%s", tok, name);
    if (0 == access(file, X_OK)) {
      free(path);
      return file;
    }
    tok = strtok(NULL, ":");
    free(file);
  }
  free(path);
  return NULL;
}


x86 assembly (clang, -O0):
<which_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    11a6 <which_path+0x36>
movq   $0x0,-0x8(%rbp)
jmp    1295 <which_path+0x125>
mov    -0x20(%rbp),%rdi
lea    0xe4f(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1284 <which_path+0x114>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x2,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1214 <which_path+0xa4>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1295 <which_path+0x125>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xddb(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x38(%rbp),%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1262 <which_path+0xf2>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1295 <which_path+0x125>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd93(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    11ba <which_path+0x4a>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<which_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r13
mov    %rsi,%rdi
call   1090 <strdup@plt>
test   %rax,%rax
je     122b <which_path+0xbb>
mov    %rax,%r15
lea    0xe67(%rip),%rsi
mov    %rax,%rdi
call   1070 <strtok@plt>
test   %rax,%rax
je     122f <which_path+0xbf>
mov    %rax,%rbp
xor    %eax,%eax
mov    %rax,(%rsp)
mov    0x2e2e(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     123b <which_path+0xcb>
mov    %rax,%r14
mov    %rax,%rdi
lea    0xe13(%rip),%rsi
mov    %rbp,%rdx
mov    %r13,%rcx
call   1080 <sprintf@plt>
mov    (%r12),%esi
mov    %r14,%rdi
call   1060 <access@plt>
test   %rax,%rax
je     1237 <which_path+0xc7>
xor    %edi,%edi
lea    0xdec(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,%rbp
mov    %r14,%rdi
call   1030 <free@plt>
test   %rbp,%rbp
jne    11c0 <which_path+0x50>
jmp    123b <which_path+0xcb>
xor    %eax,%eax
jmp    1247 <which_path+0xd7>
xor    %eax,%eax
mov    %rax,(%rsp)
jmp    123b <which_path+0xcb>
mov    %r14,(%rsp)
mov    %r15,%rdi
call   1030 <free@plt>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c":\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@X_OK = global i32 0
define noalias i8* @which_path(i8* %0, i8* nocapture %1) {
%3 = tail call i8* @strdup(i8* %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %28, label %5
5: ; preds = %2
%6 = tail call i8* @strtok(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %25, label %8
8: ; preds = %5, %21
%9 = phi i8* [ %22, %21 ], [ %6, %5 ]
%10 = tail call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 2
%12 = tail call i32 @strlen(i8* %0)
%13 = add nsw i32 %11, %12
%14 = tail call i8* @malloc(i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %25, label %16
16: ; preds = %8
%17 = tail call i32 @sprintf(i8* %14, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %9, i8* %0)
%18 = load i32, i32* @X_OK
%19 = tail call i64 @access(i8* %14, i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %16
%22 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%23 = tail call i32 @free(i8* %14)
%24 = icmp eq i8* %22, null
br i1 %24, label %25, label %8
25: ; preds = %21, %16, %8, %5
%26 = phi i8* [ null, %5 ], [ null, %8 ], [ %14, %16 ], [ null, %21 ]
%27 = tail call i32 @free(i8* %3)
br label %28
28: ; preds = %25, %2
%29 = phi i8* [ null, %2 ], [ %26, %25 ]
ret i8* %29
}