#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 */
struct passwd {
  char *pw_dir;
};

/* Variables and functions */
int R_OK;
int W_OK;
int X_OK;
scalar_t__ access(char *, int);
char *getenv(char *);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
char *get_home_dir(void) {
  struct passwd *pwd;
  char *home_dir = NULL;
  if ((home_dir = getenv("HOME")) == NULL) {
    if ((pwd = getpwuid(getuid())))
      home_dir = pwd->pw_dir;
    else
      return NULL;
  }
  if (access(home_dir, R_OK | W_OK | X_OK) < 0) {
    perror("access");
    printf("User doesn't have RWX permissions on home directory %s\n",
           home_dir);
    return NULL;
  }
  return home_dir;
}


x86 assembly (clang, -O0):
<get_home_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x18(%rbp)
lea    0xe89(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11c8 <get_home_dir+0x68>
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11b6 <get_home_dir+0x56>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11c3 <get_home_dir+0x63>
movq   $0x0,-0x8(%rbp)
jmp    1227 <get_home_dir+0xc7>
jmp    11c8 <get_home_dir+0x68>
mov    -0x18(%rbp),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
mov    0x2e0c(%rip),%rax
or     (%rax),%esi
mov    0x2dfb(%rip),%rax
or     (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
jge    121f <get_home_dir+0xbf>
lea    0xe08(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%rsi
lea    0xdff(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1227 <get_home_dir+0xc7>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_home_dir>:
push   %rbx
lea    0xe98(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    118d <get_home_dir+0x2d>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     11b5 <get_home_dir+0x55>
mov    (%rax),%rbx
mov    0x2e3c(%rip),%rax
mov    0x2e4d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <access@plt>
test   %rax,%rax
js     11ba <get_home_dir+0x5a>
mov    %rbx,%rax
pop    %rbx
ret
lea    0xe44(%rip),%rdi
call   1080 <perror@plt>
lea    0xe3f(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
xor    %ebx,%ebx
jmp    11b5 <get_home_dir+0x55>

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
@R_OK = global i32 0
@W_OK = global i32 0
@X_OK = global i32 0
@.str.1 = [7 x i8] c"access\00"
@.str.2 = [56 x i8] c"User doesn't have RWX permissions on home directory %s\0A\00"
define i8* @get_home_dir() {
%1 = alloca i8*
%2 = alloca %struct.passwd*
%3 = alloca i8*
store i8* null, i8** %3
%4 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %3
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %16
6: ; preds = %0
%7 = call i32 (...) @getuid()
%8 = call %struct.passwd* @getpwuid(i32 %7)
store %struct.passwd* %8, %struct.passwd** %2
%9 = icmp ne %struct.passwd* %8, null
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = load %struct.passwd*, %struct.passwd** %2
%12 = getelementptr inbounds %struct.passwd, %struct.passwd* %11, i32 0, i32 0
%13 = load i8*, i8** %12
store i8* %13, i8** %3
br label %15
14: ; preds = %6
store i8* null, i8** %1
br label %31
15: ; preds = %10
br label %16
16: ; preds = %15, %0
%17 = load i8*, i8** %3
%18 = load i32, i32* @R_OK
%19 = load i32, i32* @W_OK
%20 = or i32 %18, %19
%21 = load i32, i32* @X_OK
%22 = or i32 %20, %21
%23 = call i64 @access(i8* %17, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %16
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%27 = load i8*, i8** %3
%28 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i8* %27)
store i8* null, i8** %1
br label %31
29: ; preds = %16
%30 = load i8*, i8** %3
store i8* %30, i8** %1
br label %31
31: ; preds = %29, %25, %14
%32 = load i8*, i8** %1
ret i8* %32
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
@R_OK = global i32 0
@W_OK = global i32 0
@X_OK = global i32 0
@.str.1 = [7 x i8] c"access\00"
@.str.2 = [56 x i8] c"User doesn't have RWX permissions on home directory %s\0A\00"
define i8* @get_home_dir() {
%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 %3, label %10
3: ; preds = %0
%4 = tail call i32 (...) @getuid()
%5 = tail call %struct.passwd* @getpwuid(i32 %4)
%6 = icmp eq %struct.passwd* %5, null
br i1 %6, label %22, label %7
7: ; preds = %3
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 0
%9 = load i8*, i8** %8
br label %10
10: ; preds = %7, %0
%11 = phi i8* [ %9, %7 ], [ %1, %0 ]
%12 = load i32, i32* @R_OK
%13 = load i32, i32* @W_OK
%14 = or i32 %13, %12
%15 = load i32, i32* @X_OK
%16 = or i32 %14, %15
%17 = tail call i64 @access(i8* %11, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %10
%20 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i8* %11)
br label %22
22: ; preds = %10, %3, %19
%23 = phi i8* [ null, %19 ], [ null, %3 ], [ %11, %10 ]
ret i8* %23
}