#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*/);
char *getenv(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
char *strdup(char *);
char *find_rvm_home(void) {
  char *home = getenv("HOME");
  char buffer[PATH_MAX];
  if (home == NULL)
    return NULL;
  sprintf(buffer, "%s/.rvm/bin/rvm", home);
  if (access(buffer, X_OK) == 0)
    return strdup(buffer);
  return NULL;
}


x86 assembly (clang, -O0):
<find_rvm_home>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xeb1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a3 <find_rvm_home+0x63>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    11fb <find_rvm_home+0xbb>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe53(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x30(%rbp),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
call   1040 <access@plt>
cmp    $0x0,%rax
jne    11ec <find_rvm_home+0xac>
mov    -0x30(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    11fb <find_rvm_home+0xbb>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_rvm_home>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
push   %rax
lea    0xeb3(%rip),%rdi
call   1030 <getenv@plt>
mov    0x2e97(%rip),%rcx
mov    (%rcx),%ecx
mov    %rsp,%rbx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
test   %rax,%rax
je     1199 <find_rvm_home+0x59>
lea    0xe8d(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1050 <sprintf@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <access@plt>
test   %rax,%rax
je     119d <find_rvm_home+0x5d>
xor    %eax,%eax
jmp    11a5 <find_rvm_home+0x65>
mov    %rbx,%rdi
call   1060 <strdup@plt>
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@PATH_MAX = global i32 0
@.str.1 = [16 x i8] c"%s/.rvm/bin/rvm\00"
@X_OK = global i32 0
define i8* @find_rvm_home() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** %2
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %3
%10 = alloca i8, i64 %8
store i64 %8, i64* %4
%11 = load i8*, i8** %2
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %0
store i8* null, i8** %1
store i32 1, i32* %5
br label %23
14: ; preds = %0
%15 = load i8*, i8** %2
%16 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @X_OK
%18 = call i64 @access(i8* %10, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i8* @strdup(i8* %10)
store i8* %21, i8** %1
store i32 1, i32* %5
br label %23
22: ; preds = %14
store i8* null, i8** %1
store i32 1, i32* %5
br label %23
23: ; preds = %22, %20, %13
%24 = load i8*, i8** %3
%25 = load i8*, i8** %1
ret i8* %25
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@PATH_MAX = global i32 0
@.str.1 = [16 x i8] c"%s/.rvm/bin/rvm\00"
@X_OK = global i32 0
define noalias i8* @find_rvm_home() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = icmp eq i8* %1, null
br i1 %5, label %13, label %6
6: ; preds = %0
%7 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%8 = load i32, i32* @X_OK
%9 = call i64 @access(i8* %4, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = call i8* @strdup(i8* %4)
br label %13
13: ; preds = %6, %0, %11
%14 = phi i8* [ %12, %11 ], [ null, %0 ], [ null, %6 ]
ret i8* %14
}