#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 */

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EPERM;
scalar_t__ EROFS;
scalar_t__ errno;
int /*<<< orphan*/ getcwd(char *, int);
char *getenv(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
char *sys_gethome() {
  static char homedir[512];
  char *home = getenv("HOME");
  if (home)
    sprintf(homedir, "%s/.gnuboy", home);
  mkdir(homedir, 0777);
  if (errno != EROFS && errno != EACCES && errno != EPERM)
    return homedir;
  memset(homedir, 0, 512);
  getcwd(homedir, 512);
  strcat(homedir, "/.gnuboy");
  mkdir(homedir, 0777);
  return homedir;
}


x86 assembly (clang, -O0):
<sys_gethome>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe91(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119a <sys_gethome+0x3a>
mov    -0x10(%rbp),%rdx
lea    0x2ed2(%rip),%rdi
lea    0xe70(%rip),%rsi
call   1080 <sprintf@plt>
lea    0x2ebf(%rip),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%rax
mov    0x2e2c(%rip),%rcx
cmp    (%rcx),%rax
je     1209 <sys_gethome+0xa9>
mov    0x2e04(%rip),%rax
mov    (%rax),%rax
mov    0x2e0a(%rip),%rcx
cmp    (%rcx),%rax
je     1209 <sys_gethome+0xa9>
mov    0x2dea(%rip),%rax
mov    (%rax),%rax
mov    0x2dd0(%rip),%rcx
cmp    (%rcx),%rax
je     1209 <sys_gethome+0xa9>
lea    0x2e60(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    125c <sys_gethome+0xfc>
lea    0x2e50(%rip),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1050 <memset@plt>
lea    0x2e3d(%rip),%rdi
mov    $0x200,%esi
call   1060 <getcwd@plt>
lea    0x2e2c(%rip),%rdi
lea    0xdcc(%rip),%rsi
call   1070 <strcat@plt>
lea    0x2e19(%rip),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
lea    0x2e08(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sys_gethome>:
push   %rbx
lea    0xe98(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1188 <sys_gethome+0x28>
lea    0x2ee7(%rip),%rdi
lea    0xe85(%rip),%rsi
mov    %rax,%rdx
call   1080 <sprintf@plt>
lea    0x2ed1(%rip),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%rax
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%rax
je     11c7 <sys_gethome+0x67>
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%rax
je     11c7 <sys_gethome+0x67>
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
jne    1206 <sys_gethome+0xa6>
lea    0x2e92(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
mov    $0x200,%esi
call   1060 <getcwd@plt>
lea    0xe16(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcat@plt>
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
lea    0x2e53(%rip),%rax
pop    %rbx
ret

LLVM IR (-O0):
@sys_gethome.homedir = internal global [512 x i8] zeroinitializer
@.str = [5 x i8] c"HOME\00"
@.str.1 = [11 x i8] c"%s/.gnuboy\00"
@errno = global i64 0
@EROFS = global i64 0
@EACCES = global i64 0
@EPERM = global i64 0
@.str.2 = [9 x i8] c"/.gnuboy\00"
define i8* @sys_gethome() {
%1 = alloca i8*
%2 = alloca i8*
%3 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %3, i8** %2
%4 = load i8*, i8** %2
%5 = icmp ne i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i8*, i8** %2
%8 = call i32 @sprintf(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sys_gethome.homedir, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %7)
br label %9
9: ; preds = %6, %0
%10 = call i32 @mkdir(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sys_gethome.homedir, i64 0, i64 0), i32 511)
%11 = load i64, i64* @errno
%12 = load i64, i64* @EROFS
%13 = icmp ne i64 %11, %12
br i1 %13, label %14, label %23
14: ; preds = %9
%15 = load i64, i64* @errno
%16 = load i64, i64* @EACCES
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i64, i64* @errno
%20 = load i64, i64* @EPERM
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %18
store i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sys_gethome.homedir, i64 0, i64 0), i8** %1
br label %28
23: ; preds = %18, %14, %9
%24 = call i32 @memset(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sys_gethome.homedir, i64 0, i64 0), i32 0, i32 512)
%25 = call i32 @getcwd(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sys_gethome.homedir, i64 0, i64 0), i32 512)
%26 = call i32 @strcat(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sys_gethome.homedir, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @mkdir(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sys_gethome.homedir, i64 0, i64 0), i32 511)
store i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sys_gethome.homedir, i64 0, i64 0), i8** %1
br label %28
28: ; preds = %23, %22
%29 = load i8*, i8** %1
ret i8* %29
}

LLVM IR (-O2):
@sys_gethome.homedir = internal global [512 x i8] zeroinitializer
@.str = [5 x i8] c"HOME\00"
@.str.1 = [11 x i8] c"%s/.gnuboy\00"
@errno = global i64 0
@EROFS = global i64 0
@EACCES = global i64 0
@EPERM = global i64 0
@.str.2 = [9 x i8] c"/.gnuboy\00"
define i8* @sys_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 %5, label %3
3: ; preds = %0
%4 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([512 x i8], [512 x i8]* @sys_gethome.homedir, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %5
5: ; preds = %3, %0
%6 = tail call i32 @mkdir(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sys_gethome.homedir, i64 0, i64 0), i32 511)
%7 = load i64, i64* @errno
%8 = load i64, i64* @EROFS
%9 = icmp eq i64 %7, %8
%10 = load i64, i64* @EACCES
%11 = icmp eq i64 %7, %10
%12 = select i1 %9, i1 true, i1 %11
%13 = load i64, i64* @EPERM
%14 = icmp eq i64 %7, %13
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %16, label %21
16: ; preds = %5
%17 = tail call i32 @memset(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sys_gethome.homedir, i64 0, i64 0), i32 0, i32 512)
%18 = tail call i32 @getcwd(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sys_gethome.homedir, i64 0, i64 0), i32 512)
%19 = tail call i32 @strcat(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sys_gethome.homedir, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @mkdir(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sys_gethome.homedir, i64 0, i64 0), i32 511)
br label %21
21: ; preds = %5, %16
ret i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sys_gethome.homedir, i64 0, i64 0)
}