#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;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *get_word(char *, int *, char *, char *);
int /*<<< orphan*/ my_strlen(char *);
char *strcatrealloc(char *, char *, int /*<<< orphan*/);
char *test_path(char *cmd, char *path, int *ok, char *sys_path) {
  int s;
  s = 0;
  if ((path = strcatrealloc(path, cmd, my_strlen(cmd))) == NULL)
    return (NULL);
  while ((*ok = access(path, X_OK)) != 0 && sys_path[s] != '\0') {
    if (path != NULL)
      free(path);
    path = NULL;
    if ((path = get_word(sys_path, &s, ":", path)) == NULL)
      return (NULL);
    if ((path = strcatrealloc(path, cmd, my_strlen(cmd))) == NULL)
      return (NULL);
  }
  return (path);
}


x86 assembly (clang, -O0):
<test_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <my_strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1050 <strcatrealloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b2 <test_path+0x62>
movq   $0x0,-0x8(%rbp)
jmp    12a3 <test_path+0x153>
jmp    11b7 <test_path+0x67>
mov    -0x18(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x41(%rbp)
je     11f4 <test_path+0xa4>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    1204 <test_path+0xb4>
jmp    129b <test_path+0x14b>
cmpq   $0x0,-0x18(%rbp)
je     1218 <test_path+0xc8>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rcx
lea    -0x2c(%rbp),%rsi
lea    0xdcd(%rip),%rdx
call   1060 <get_word@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1253 <test_path+0x103>
movq   $0x0,-0x8(%rbp)
jmp    12a3 <test_path+0x153>
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <my_strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1050 <strcatrealloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1296 <test_path+0x146>
movq   $0x0,-0x8(%rbp)
jmp    12a3 <test_path+0x153>
jmp    11b7 <test_path+0x67>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%rbx
movl   $0x0,0xc(%rsp)
call   1030 <my_strlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <strcatrealloc@plt>
test   %rax,%rax
je     1204 <test_path+0xb4>
mov    %rax,%r12
mov    0x2e55(%rip),%r13
xor    %eax,%eax
mov    %rax,0x10(%rsp)
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13),%esi
mov    %r12,%rdi
call   1070 <access@plt>
mov    %eax,(%r14)
test   %eax,%eax
je     120d <test_path+0xbd>
movslq 0xc(%rsp),%rax
cmpb   $0x0,(%r15,%rax,1)
je     120d <test_path+0xbd>
mov    %r12,%rdi
call   1040 <free@plt>
mov    %r15,%rdi
lea    0xc(%rsp),%rsi
lea    0xe2a(%rip),%rdx
xor    %ecx,%ecx
call   1060 <get_word@plt>
test   %rax,%rax
je     1212 <test_path+0xc2>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <my_strlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <strcatrealloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11a0 <test_path+0x50>
jmp    1212 <test_path+0xc2>
xor    %eax,%eax
mov    %rax,0x10(%rsp)
jmp    1212 <test_path+0xc2>
mov    %r12,0x10(%rsp)
mov    0x10(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@X_OK = global i32 0
@.str = [2 x i8] c":\00"
define i8* @test_path(i8* %0, i8* %1, i32* nocapture writeonly %2, i8* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%7 = tail call i32 @my_strlen(i8* %0)
%8 = tail call i8* @strcatrealloc(i8* %1, i8* %0, i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %29, label %10
10: ; preds = %4, %25
%11 = phi i8* [ %27, %25 ], [ %8, %4 ]
%12 = load i32, i32* @X_OK
%13 = call i32 @access(i8* %11, i32 %12)
store i32 %13, i32* %2
%14 = icmp eq i32 %13, 0
br i1 %14, label %29, label %15
15: ; preds = %10
%16 = load i32, i32* %5
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %3, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %15
%22 = call i32 @free(i8* %11)
%23 = call i8* @get_word(i8* %3, i32* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* null)
%24 = icmp eq i8* %23, null
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = call i32 @my_strlen(i8* %0)
%27 = call i8* @strcatrealloc(i8* %23, i8* %0, i32 %26)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %10
29: ; preds = %15, %10, %25, %21, %4
%30 = phi i8* [ null, %4 ], [ %11, %15 ], [ %11, %10 ], [ null, %25 ], [ null, %21 ]
ret i8* %30
}