#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 /*<<< orphan*/ free(struct passwd *);
char *getenv(char *);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
char *__get_home_dir(void) {
  char *dir;
  struct passwd *pw;
  dir = getenv("HOME");
  if (!dir) {
    pw = getpwuid(getuid());
    if (!pw) {
      return NULL;
    }
    dir = pw->pw_dir;
    free(pw);
  }
  return dir;
}


x86 assembly (clang, -O0):
<__get_home_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a1 <__get_home_dir+0x61>
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118d <__get_home_dir+0x4d>
movq   $0x0,-0x8(%rbp)
jmp    11a9 <__get_home_dir+0x69>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x10(%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    0xeb8(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1175 <__get_home_dir+0x35>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
test   %rax,%rax
je     1175 <__get_home_dir+0x35>
mov    (%rax),%rbx
mov    %rax,%rdi
call   1040 <free@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
define i8* @__get_home_dir() {
%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 %19, label %7
7: ; preds = %0
%8 = call i32 (...) @getuid()
%9 = call %struct.passwd* @getpwuid(i32 %8)
store %struct.passwd* %9, %struct.passwd** %3
%10 = load %struct.passwd*, %struct.passwd** %3
%11 = icmp ne %struct.passwd* %10, null
br i1 %11, label %13, label %12
12: ; preds = %7
store i8* null, i8** %1
br label %21
13: ; preds = %7
%14 = load %struct.passwd*, %struct.passwd** %3
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i32 0, i32 0
%16 = load i8*, i8** %15
store i8* %16, i8** %2
%17 = load %struct.passwd*, %struct.passwd** %3
%18 = call i32 @free(%struct.passwd* %17)
br label %19
19: ; preds = %13, %0
%20 = load i8*, i8** %2
store i8* %20, i8** %1
br label %21
21: ; preds = %19, %12
%22 = load i8*, i8** %1
ret i8* %22
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\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 %11
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 %11, label %7
7: ; preds = %3
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 0
%9 = load i8*, i8** %8
%10 = tail call i32 @free(%struct.passwd* %5)
br label %11
11: ; preds = %0, %7, %3
%12 = phi i8* [ null, %3 ], [ %1, %0 ], [ %9, %7 ]
ret i8* %12
}