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


x86 assembly (clang, -O0):
<get_home_folder>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movq   $0x0,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11f8 <get_home_folder+0xa8>
lea    0xe86(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     11a6 <get_home_folder+0x56>
lea    0xe70(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1050 <am_strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11f3 <get_home_folder+0xa3>
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11de <get_home_folder+0x8e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <am_strdup@plt>
mov    %rax,-0x8(%rbp)
mov    $0x0,%al
call   1070 <endpwent@plt>
jmp    11ee <get_home_folder+0x9e>
lea    0xe1f(%rip),%rdi
call   1050 <am_strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11f3 <get_home_folder+0xa3>
jmp    11f8 <get_home_folder+0xa8>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_home_folder>:
push   %rbx
lea    0xea8(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     116b <get_home_folder+0x1b>
mov    %rax,%rdi
pop    %rbx
jmp    1050 <am_strdup@plt>
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     1195 <get_home_folder+0x45>
mov    (%rax),%rdi
call   1050 <am_strdup@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <endpwent@plt>
mov    %rbx,%rax
pop    %rbx
ret
lea    0xe68(%rip),%rdi
pop    %rbx
jmp    1050 <am_strdup@plt>

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

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
@.str.1 = [1 x i8] zeroinitializer
define i8* @get_home_folder() {
%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 %5, label %3
3: ; preds = %0
%4 = tail call i8* @am_strdup(i8* %1)
br label %16
5: ; preds = %0
%6 = tail call i32 (...) @getuid()
%7 = tail call %struct.passwd* @getpwuid(i32 %6)
%8 = icmp eq %struct.passwd* %7, null
br i1 %8, label %14, label %9
9: ; preds = %5
%10 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i64 0, i32 0
%11 = load i8*, i8** %10
%12 = tail call i8* @am_strdup(i8* %11)
%13 = tail call i32 (...) @endpwent()
br label %16
14: ; preds = %5
%15 = tail call i8* @am_strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %3, %14, %9
%17 = phi i8* [ %4, %3 ], [ %12, %9 ], [ %15, %14 ]
ret i8* %17
}