#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 {
  int /*<<< orphan*/ pw_dir;
};

/* Variables and functions */
char *getStringCopy(int /*<<< orphan*/);
char *getenv(char *);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
char *getHomeDir() {
  static char *dir = NULL;
  if (dir == NULL) {
    if ((dir = getenv("HOME")) == NULL) {
      struct passwd *pwd;
      if ((pwd = getpwuid(getuid())) != NULL)
        dir = getStringCopy(pwd->pw_dir);
      else
        dir = ".";
    }
  }
  return dir;
}


x86 assembly (clang, -O0):
<getHomeDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
cmpq   $0x0,0x2ef8(%rip)
jne    11be <getHomeDir+0x7e>
lea    0xea3(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x2edf(%rip)
cmp    $0x0,%rax
jne    11b9 <getHomeDir+0x79>
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     11a6 <getHomeDir+0x66>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1050 <getStringCopy@plt>
mov    %rax,0x2ea7(%rip)
jmp    11b4 <getHomeDir+0x74>
lea    0xe58(%rip),%rax
mov    %rax,0x2e94(%rip)
jmp    11b9 <getHomeDir+0x79>
jmp    11be <getHomeDir+0x7e>
mov    0x2e83(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getHomeDir>:
push   %rax
mov    0x2f00(%rip),%rax
test   %rax,%rax
je     114f <getHomeDir+0xf>
pop    %rcx
ret
lea    0xeaa(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x2ee6(%rip)
test   %rax,%rax
jne    114d <getHomeDir+0xd>
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     1183 <getHomeDir+0x43>
mov    (%rax),%edi
call   1050 <getStringCopy@plt>
jmp    118a <getHomeDir+0x4a>
lea    0xe7b(%rip),%rax
mov    %rax,0x2eb7(%rip)
pop    %rcx
ret

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

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