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


x86 assembly (clang, -O0):
<user_home_dir>:
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 <user_home_dir+0x30>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1195 <user_home_dir+0x65>
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118a <user_home_dir+0x5a>
movq   $0x0,-0x8(%rbp)
jmp    1195 <user_home_dir+0x65>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<user_home_dir>:
push   %rbx
lea    0xec8(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    115d <user_home_dir+0x2d>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     115d <user_home_dir+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* @user_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 %7, label %9
7: ; preds = %0
%8 = load i8*, i8** %2
store i8* %8, i8** %1
br label %19
9: ; preds = %0
%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 ne %struct.passwd* %12, null
br i1 %13, label %15, label %14
14: ; preds = %9
store i8* null, i8** %1
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
store i8* %18, i8** %1
br label %19
19: ; preds = %15, %14, %7
%20 = load i8*, i8** %1
ret i8* %20
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
define i8* @user_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 %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
}