#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 const *pw_dir;
};

/* Variables and functions */
scalar_t__ errno;
char *getenv(char *);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
const char *pe_utils_get_homedir(void) {
  const char *homedir = getenv("HOME");
  if (homedir != NULL)
    return homedir;
  errno = 0;
  struct passwd *pwd = getpwuid(getuid());
  return pwd == NULL ? NULL : pwd->pw_dir;
}


x86 assembly (clang, -O0):
<pe_utils_get_homedir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xec1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1160 <pe_utils_get_homedir+0x30>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a9 <pe_utils_get_homedir+0x79>
mov    0x2e79(%rip),%rax
movq   $0x0,(%rax)
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1196 <pe_utils_get_homedir+0x66>
xor    %eax,%eax
mov    %rax,-0x20(%rbp)
jmp    11a1 <pe_utils_get_homedir+0x71>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pe_utils_get_homedir>:
push   %rbx
lea    0xec8(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    116b <pe_utils_get_homedir+0x3b>
mov    0x2e94(%rip),%rax
movq   $0x0,(%rax)
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     116b <pe_utils_get_homedir+0x3b>
mov    (%rax),%rbx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
@errno = global i64 0
define i8* @pe_utils_get_homedir() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca %struct.passwd*
%4 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %2
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = load i8*, i8** %2
store i8* %8, i8** %1
br label %21
9: ; preds = %0
store i64 0, i64* @errno
%10 = call i32 (...) @getuid()
%11 = call %struct.passwd* @getpwuid(i32 %10)
store %struct.passwd* %11, %struct.passwd** %3
%12 = load %struct.passwd*, %struct.passwd** %3
%13 = icmp eq %struct.passwd* %12, null
br i1 %13, label %14, label %15
14: ; preds = %9
br label %19
15: ; preds = %9
%16 = load %struct.passwd*, %struct.passwd** %3
%17 = getelementptr inbounds %struct.passwd, %struct.passwd* %16, i32 0, i32 0
%18 = load i8*, i8** %17
br label %19
19: ; preds = %15, %14
%20 = phi i8* [ null, %14 ], [ %18, %15 ]
store i8* %20, i8** %1
br label %21
21: ; preds = %19, %7
%22 = load i8*, i8** %1
ret i8* %22
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
@errno = global i64 0
define i8* @pe_utils_get_homedir() {
%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
store i64 0, i64* @errno
%4 = tail call i32 (...) @getuid()
%5 = tail call %struct.passwd* @getpwuid(i32 %4)
%6 = icmp eq %struct.passwd* %5, null
br i1 %6, label %10, 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, %3, %0
%11 = phi i8* [ %1, %0 ], [ %9, %7 ], [ null, %3 ]
ret i8* %11
}