#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 PATH_MAX;
int /*<<< orphan*/ X_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
char *strchr(char *, char);
char *strdup(char *);
char *find_rvm_path(void) {
  char *path = getenv("PATH");
  if (path == NULL)
    return NULL;
  path = strdup(path);
  char *str = path;
  char *dir = NULL;
  char buffer[PATH_MAX];
  do {
    dir = strchr(str, ':');
    if (dir != NULL)
      dir[0] = 0;
    sprintf(buffer, "%s/rvm", str);
    if (access(buffer, X_OK) == 0) {
      free(path);
      return (strdup(buffer));
    }
    str = dir + 1;
  } while (dir != NULL);
  free(path);
  return NULL;
}


x86 assembly (clang, -O0):
<find_rvm_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xe91(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1190 <find_rvm_path+0x30>
movq   $0x0,-0x8(%rbp)
jmp    1285 <find_rvm_path+0x125>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11fd <find_rvm_path+0x9d>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdf9(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x40(%rbp),%rdi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jne    124f <find_rvm_path+0xef>
mov    -0x10(%rbp),%rdi
call   1040 <free@plt>
mov    -0x40(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    127e <find_rvm_path+0x11e>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d9 <find_rvm_path+0x79>
mov    -0x10(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_rvm_path>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe88(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     121e <find_rvm_path+0xbe>
mov    %rax,%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
mov    %rsp,-0x38(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    0xe4f(%rip),%r15
mov    0x2e23(%rip),%r14
mov    %rbx,-0x30(%rbp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,%r13
test   %rax,%rax
je     11ea <find_rvm_path+0x8a>
movb   $0x0,0x0(%r13)
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1070 <sprintf@plt>
mov    (%r14),%esi
mov    %r12,%rdi
call   1060 <access@plt>
test   %rax,%rax
je     1222 <find_rvm_path+0xc2>
lea    0x1(%r13),%rbx
test   %r13,%r13
jne    11d0 <find_rvm_path+0x70>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
xor    %eax,%eax
jmp    1233 <find_rvm_path+0xd3>
xor    %eax,%eax
jmp    1237 <find_rvm_path+0xd7>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    %r12,%rdi
call   1080 <strdup@plt>
mov    -0x38(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@PATH_MAX = global i32 0
@.str.1 = [7 x i8] c"%s/rvm\00"
@X_OK = global i32 0
define noalias i8* @find_rvm_path() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %28, label %3
3: ; preds = %0
%4 = tail call i8* @strdup(i8* %1)
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
br label %9
9: ; preds = %22, %3
%10 = phi i8* [ %4, %3 ], [ %23, %22 ]
%11 = tail call i8* @strchr(i8* dereferenceable(1) %10, i8 signext 58)
%12 = icmp eq i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %9
store i8 0, i8* %11
br label %14
14: ; preds = %13, %9
%15 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%16 = load i32, i32* @X_OK
%17 = call i64 @access(i8* %8, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = tail call i32 @free(i8* %4)
%21 = call i8* @strdup(i8* %8)
br label %26
22: ; preds = %14
%23 = getelementptr inbounds i8, i8* %11, i64 1
br i1 %12, label %24, label %9
24: ; preds = %22
%25 = tail call i32 @free(i8* %4)
br label %26
26: ; preds = %24, %19
%27 = phi i8* [ %21, %19 ], [ null, %24 ]
tail call void @llvm.stackrestore(i8* %7)
br label %28
28: ; preds = %0, %26
%29 = phi i8* [ %27, %26 ], [ null, %0 ]
ret i8* %29
}