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

/* Variables and functions */
int /*<<< orphan*/ die(char *, ...);
scalar_t__ errno;
char *getenv(char *);
scalar_t__ geteuid();
struct passwd *getpwuid(scalar_t__);
char *strdup(int /*<<< orphan*/);
char *lookup_home(void) {
  char *home;
  struct passwd *p;
  uid_t euid;
  if ((home = getenv("HOME")) != NULL)
    return home;
  euid = geteuid();
  errno = 0;
  if ((p = getpwuid(euid)) == NULL) {
    if (errno != 0)
      die("lookup_home: getpwuid %lu:", (unsigned long)euid);
    else
      die("lookup_home: no uid %lu in /etc/passwd", (unsigned long)euid);
  }
  if ((home = strdup(p->pw_dir)) == NULL)
    die("lookup_home: out of memory");
  return home;
}


x86 assembly (clang, -O0):
<lookup_home>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     117f <lookup_home+0x2f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121d <lookup_home+0xcd>
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e4f(%rip),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11ee <lookup_home+0x9e>
mov    0x2e2a(%rip),%rax
cmpq   $0x0,(%rax)
je     11d7 <lookup_home+0x87>
mov    -0x20(%rbp),%rsi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1060 <die@plt>
jmp    11e9 <lookup_home+0x99>
mov    -0x20(%rbp),%rsi
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1060 <die@plt>
jmp    11ee <lookup_home+0x9e>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1070 <strdup@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1215 <lookup_home+0xc5>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1060 <die@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookup_home>:
push   %r15
push   %r14
push   %rbx
lea    0xea4(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d0 <lookup_home+0x80>
xor    %eax,%eax
call   1050 <geteuid@plt>
mov    %rax,%r14
mov    0x2e66(%rip),%r15
movq   $0x0,(%r15)
mov    %rax,%rdi
call   1040 <getpwuid@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b1 <lookup_home+0x61>
cmpq   $0x0,(%r15)
lea    0xe84(%rip),%rax
lea    0xe62(%rip),%rdi
cmove  %rax,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <die@plt>
mov    (%rbx),%edi
call   1070 <strdup@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d0 <lookup_home+0x80>
lea    0xe80(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <die@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
@.str = [5 x i8] c"HOME\00"
@errno = global i64 0
@.str.1 = [27 x i8] c"lookup_home: getpwuid %lu:\00"
@.str.2 = [39 x i8] c"lookup_home: no uid %lu in /etc/passwd\00"
@.str.3 = [27 x i8] c"lookup_home: out of memory\00"
define i8* @lookup_home() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca %struct.passwd*
%4 = alloca i64
%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 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 %34
9: ; preds = %0
%10 = call i64 (...) @geteuid()
store i64 %10, i64* %4
store i64 0, i64* @errno
%11 = load i64, i64* %4
%12 = call %struct.passwd* @getpwuid(i64 %11)
store %struct.passwd* %12, %struct.passwd** %3
%13 = icmp eq %struct.passwd* %12, null
br i1 %13, label %14, label %24
14: ; preds = %9
%15 = load i64, i64* @errno
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i64, i64* %4
%19 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %18)
br label %23
20: ; preds = %14
%21 = load i64, i64* %4
%22 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i64 %21)
br label %23
23: ; preds = %20, %17
br label %24
24: ; preds = %23, %9
%25 = load %struct.passwd*, %struct.passwd** %3
%26 = getelementptr inbounds %struct.passwd, %struct.passwd* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i8* @strdup(i32 %27)
store i8* %28, i8** %2
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %24
%33 = load i8*, i8** %2
store i8* %33, i8** %1
br label %34
34: ; preds = %32, %7
%35 = load i8*, i8** %1
ret i8* %35
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@.str = [5 x i8] c"HOME\00"
@errno = global i64 0
@.str.1 = [27 x i8] c"lookup_home: getpwuid %lu:\00"
@.str.2 = [39 x i8] c"lookup_home: no uid %lu in /etc/passwd\00"
@.str.3 = [27 x i8] c"lookup_home: out of memory\00"
define i8* @lookup_home() {
%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 %19
3: ; preds = %0
%4 = tail call i64 (...) @geteuid()
store i64 0, i64* @errno
%5 = tail call %struct.passwd* @getpwuid(i64 %4)
%6 = icmp eq %struct.passwd* %5, null
br i1 %6, label %7, label %12
7: ; preds = %3
%8 = load i64, i64* @errno
%9 = icmp eq i64 %8, 0
%10 = select i1 %9, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0)
%11 = tail call i32 (i8*, ...) @die(i8* %10, i64 %4)
br label %12
12: ; preds = %7, %3
%13 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i8* @strdup(i32 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %12, %17, %0
%20 = phi i8* [ %1, %0 ], [ null, %17 ], [ %15, %12 ]
ret i8* %20
}