#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 *getenv(char *);
int /*<<< orphan*/ geteuid();
struct passwd *getpwuid(int /*<<< orphan*/);
char *strdup(int /*<<< orphan*/);
const char *detect_home(void) {
  static const char *home = NULL;
  if (!home) {
    struct passwd *pwent;
    pwent = getpwuid(geteuid());
    if (pwent) {
      home = strdup(pwent->pw_dir);
    }
  }
  if (!home) {
    home = getenv("HOME");
  }
  if (!home) {
    home = "";
  }
  return home;
}


x86 assembly (clang, -O0):
<detect_home>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
cmpq   $0x0,0x2ef8(%rip)
jne    118a <detect_home+0x4a>
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1185 <detect_home+0x45>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1060 <strdup@plt>
mov    %rax,0x2ec3(%rip)
jmp    118a <detect_home+0x4a>
cmpq   $0x0,0x2eb6(%rip)
jne    11ab <detect_home+0x6b>
lea    0xe61(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x2e9d(%rip)
cmpq   $0x0,0x2e95(%rip)
jne    11c7 <detect_home+0x87>
lea    0xe44(%rip),%rax
mov    %rax,0x2e81(%rip)
mov    0x2e7a(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detect_home>:
push   %rax
mov    0x2f00(%rip),%rax
test   %rax,%rax
je     114f <detect_home+0xf>
pop    %rcx
ret
xor    %eax,%eax
call   1050 <geteuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     1177 <detect_home+0x37>
mov    (%rax),%edi
call   1060 <strdup@plt>
mov    %rax,0x2ed8(%rip)
test   %rax,%rax
jne    114d <detect_home+0xd>
jmp    1183 <detect_home+0x43>
mov    0x2eca(%rip),%rax
test   %rax,%rax
jne    114d <detect_home+0xd>
lea    0xe76(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rcx
test   %rax,%rax
lea    0xe68(%rip),%rax
cmovne %rcx,%rax
mov    %rax,0x2ea1(%rip)
pop    %rcx
ret

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

LLVM IR (-O2):
%struct.passwd = type { i32 }
@detect_home.home = internal unnamed_addr global i8* null
@.str = [5 x i8] c"HOME\00"
@.str.1 = [1 x i8] zeroinitializer
define i8* @detect_home() {
%1 = load i8*, i8** @detect_home.home
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %21
3: ; preds = %0
%4 = tail call i32 (...) @geteuid()
%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 i32, i32* %8
%10 = tail call i8* @strdup(i32 %9)
store i8* %10, i8** @detect_home.home
br label %13
11: ; preds = %3
%12 = load i8*, i8** @detect_home.home
br label %13
13: ; preds = %11, %7
%14 = phi i8* [ %12, %11 ], [ %10, %7 ]
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i8* %17, null
%19 = select i1 %18, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* %17
store i8* %19, i8** @detect_home.home
%20 = select i1 %18, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* %17
br label %21
21: ; preds = %16, %13, %0
%22 = phi i8* [ %1, %0 ], [ %14, %13 ], [ %20, %16 ]
ret i8* %22
}