#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();
int /*<<< orphan*/ ppm_error(char *);
char *gethome(void) {
  char *home;
  struct passwd *pwd;
  home = getenv("HOME");
  if (home)
    return home;
  pwd = getpwuid(getuid());
  if (!pwd) {
    ppm_error("failed to find home directory");
    return NULL;
  }
  return pwd->pw_dir;
}


x86 assembly (clang, -O0):
<gethome>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1170 <gethome+0x30>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b1 <gethome+0x71>
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a6 <gethome+0x66>
lea    0xe71(%rip),%rdi
call   1060 <ppm_error@plt>
movq   $0x0,-0x8(%rbp)
jmp    11b1 <gethome+0x71>
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):
<gethome>:
push   %rax
lea    0xeb8(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1154 <gethome+0x14>
pop    %rcx
ret
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     116c <gethome+0x2c>
mov    (%rax),%rax
pop    %rcx
ret
lea    0xe92(%rip),%rdi
call   1060 <ppm_error@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
@.str.1 = [30 x i8] c"failed to find home directory\00"
define i8* @gethome() {
%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 %20
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 %16, label %14
14: ; preds = %9
%15 = call i32 @ppm_error(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %1
br label %20
16: ; preds = %9
%17 = load %struct.passwd*, %struct.passwd** %3
%18 = getelementptr inbounds %struct.passwd, %struct.passwd* %17, i32 0, i32 0
%19 = load i8*, i8** %18
store i8* %19, i8** %1
br label %20
20: ; preds = %16, %14, %7
%21 = load i8*, i8** %1
ret i8* %21
}

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