#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 */
typedef int /*<<< orphan*/ uid_t;
struct passwd {
  char const *pw_dir;
};

/* Variables and functions */
char *getenv(char *);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
const char *get_homedir(void) {
  const char *homedir;
  if ((homedir = getenv("HOME")) == NULL) {
    uid_t uid = getuid();
    struct passwd *pwd = getpwuid(uid);
    if (pwd == NULL)
      return NULL;
    return pwd->pw_dir;
  }
  return homedir;
}


x86 assembly (clang, -O0):
<get_homedir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xec1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1190 <get_homedir+0x60>
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1180 <get_homedir+0x50>
movq   $0x0,-0x8(%rbp)
jmp    1198 <get_homedir+0x68>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1198 <get_homedir+0x68>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_homedir>:
push   %rbx
lea    0xec8(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    115d <get_homedir+0x2d>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     115d <get_homedir+0x2d>
mov    (%rax),%rbx
mov    %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
define i8* @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
%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 = %0, %7, %3
%11 = phi i8* [ %9, %7 ], [ null, %3 ], [ %1, %0 ]
ret i8* %11
}