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

/* Variables and functions */
struct passwd *getpwnam(char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *GetHomeDir(char *dest) {
  uid_t uid;
  extern char *getenv();
  extern uid_t getuid();
  extern struct passwd *getpwuid(), *getpwnam();
  struct passwd *pw;
  register char *ptr;
  if ((ptr = getenv("HOME")) != NULL) {
    (void)strcpy(dest, ptr);
  } else {
    if ((ptr = getenv("USER")) != NULL) {
      pw = getpwnam(ptr);
    } else {
      uid = getuid();
      pw = getpwuid(uid);
    }
    if (pw)
      (void)strcpy(dest, pw->pw_dir);
    else
      *dest = '\0';
  }
  return dest;
}


x86 assembly (clang, -O0):
<GetHomeDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     118a <GetHomeDir+0x3a>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    11fc <GetHomeDir+0xac>
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11b8 <GetHomeDir+0x68>
mov    -0x20(%rbp),%rdi
call   1070 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
jmp    11d0 <GetHomeDir+0x80>
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x0,%al
call   1050 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11f0 <GetHomeDir+0xa0>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strcpy@plt>
jmp    11f7 <GetHomeDir+0xa7>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
jmp    11fc <GetHomeDir+0xac>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetHomeDir>:
push   %rbx
mov    %rdi,%rbx
lea    0xea5(%rip),%rdi
xor    %eax,%eax
call   1030 <getenv@plt>
test   %rax,%rax
je     1174 <GetHomeDir+0x24>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
jmp    11b9 <GetHomeDir+0x69>
lea    0xe8a(%rip),%rdi
xor    %eax,%eax
call   1030 <getenv@plt>
test   %rax,%rax
je     11a1 <GetHomeDir+0x51>
mov    %rax,%rdi
call   1070 <getpwnam@plt>
test   %rax,%rax
je     11b6 <GetHomeDir+0x66>
mov    (%rax),%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
jmp    11b9 <GetHomeDir+0x69>
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %eax,%edi
xor    %eax,%eax
call   1050 <getpwuid@plt>
test   %rax,%rax
jne    1194 <GetHomeDir+0x44>
movb   $0x0,(%rbx)
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
@.str.1 = [5 x i8] c"USER\00"
define i8* @GetHomeDir(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.passwd*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = call i8* (i8*, ...) bitcast (i8* (...)* @getenv to i8* (i8*, ...)*)(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** %5
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i8*, i8** %2
%10 = load i8*, i8** %5
%11 = call i32 @strcpy(i8* %9, i8* %10)
br label %34
12: ; preds = %1
%13 = call i8* (i8*, ...) bitcast (i8* (...)* @getenv to i8* (i8*, ...)*)(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %13, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i8*, i8** %5
%17 = call %struct.passwd* @getpwnam(i8* %16)
store %struct.passwd* %17, %struct.passwd** %4
br label %22
18: ; preds = %12
%19 = call i32 (...) @getuid()
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = call %struct.passwd* (i32, ...) bitcast (%struct.passwd* (...)* @getpwuid to %struct.passwd* (i32, ...)*)(i32 %20)
store %struct.passwd* %21, %struct.passwd** %4
br label %22
22: ; preds = %18, %15
%23 = load %struct.passwd*, %struct.passwd** %4
%24 = icmp ne %struct.passwd* %23, null
br i1 %24, label %25, label %31
25: ; preds = %22
%26 = load i8*, i8** %2
%27 = load %struct.passwd*, %struct.passwd** %4
%28 = getelementptr inbounds %struct.passwd, %struct.passwd* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i32 @strcpy(i8* %26, i8* %29)
br label %33
31: ; preds = %22
%32 = load i8*, i8** %2
store i8 0, i8* %32
br label %33
33: ; preds = %31, %25
br label %34
34: ; preds = %33, %8
%35 = load i8*, i8** %2
ret i8* %35
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
@.str.1 = [5 x i8] c"USER\00"
define i8* @GetHomeDir(i8* returned %0) {
%2 = tail call i8* (i8*, ...) bitcast (i8* (...)* @getenv to i8* (i8*, ...)*)(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %6, label %4
4: ; preds = %1
%5 = tail call i32 @strcpy(i8* %0, i8* %2)
br label %22
6: ; preds = %1
%7 = tail call i8* (i8*, ...) bitcast (i8* (...)* @getenv to i8* (i8*, ...)*)(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call %struct.passwd* @getpwnam(i8* %7)
br label %14
11: ; preds = %6
%12 = tail call i32 (...) @getuid()
%13 = tail call %struct.passwd* (i32, ...) bitcast (%struct.passwd* (...)* @getpwuid to %struct.passwd* (i32, ...)*)(i32 %12)
br label %14
14: ; preds = %11, %9
%15 = phi %struct.passwd* [ %10, %9 ], [ %13, %11 ]
%16 = icmp eq %struct.passwd* %15, null
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = getelementptr inbounds %struct.passwd, %struct.passwd* %15, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = tail call i32 @strcpy(i8* %0, i8* %19)
br label %22
21: ; preds = %14
store i8 0, i8* %0
br label %22
22: ; preds = %17, %21, %4
ret i8* %0
}