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

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
char *getenv(char *);
int /*<<< orphan*/ mkdir(char *, int);
int nacl_startup_untar(char const *, char *, char *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ write(int, char const *, int);
void setup_unix_environment(const char *arg0) {
  mkdir("/usr", 0777);
  mkdir("/usr/games", 0777);
  int ret = nacl_startup_untar(arg0, "nethack.tar", "/usr/games");
  if (ret != 0) {
    perror("Startup untar failed.");
    exit(1);
  }
  ret = chdir(getenv("HOME"));
  if (ret != 0) {
    perror("Can change to HOME dir.");
    exit(1);
  }
  if (access(".nethackrc", R_OK) < 0) {
    int fh = open(".nethackrc", O_CREAT | O_WRONLY);
    const char config[] = "OPTIONS=color\n";
    write(fh, config, sizeof(config) - 1);
    close(fh);
  }
  ret = chdir("/usr/games");
  if (ret != 0) {
    perror("Can change to /usr/games.");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<setup_unix_environment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe4d(%rip),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
lea    0xe41(%rip),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    -0x8(%rbp),%rdi
lea    0xe37(%rip),%rsi
lea    0xe25(%rip),%rdx
call   1060 <nacl_startup_untar@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1208 <setup_unix_environment+0x68>
lea    0xe23(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xe23(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1070 <chdir@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     123f <setup_unix_environment+0x9f>
lea    0xe07(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
lea    0xe00(%rip),%rdi
call   10a0 <access@plt>
cmp    $0x0,%rax
jge    12c0 <setup_unix_environment+0x120>
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
mov    0x2d5a(%rip),%rax
or     (%rax),%esi
lea    0xdd8(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x10(%rbp)
mov    0xdd4(%rip),%rax
mov    %rax,-0x1f(%rbp)
mov    0xdd2(%rip),%eax
mov    %eax,-0x17(%rbp)
mov    0xdcc(%rip),%ax
mov    %ax,-0x13(%rbp)
mov    0xdc4(%rip),%al
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x1f(%rbp),%rsi
mov    $0xe,%edx
call   1050 <write@plt>
mov    -0x10(%rbp),%edi
call   1080 <close@plt>
lea    0xd3e(%rip),%rdi
call   1070 <chdir@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     12ef <setup_unix_environment+0x14f>
lea    0xd89(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_unix_environment>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
lea    0xe4f(%rip),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
lea    0xe43(%rip),%r14
mov    %r14,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
lea    0xe3a(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1060 <nacl_startup_untar@plt>
test   %eax,%eax
jne    128d <setup_unix_environment+0xed>
lea    0xe42(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1070 <chdir@plt>
test   %eax,%eax
jne    1296 <setup_unix_environment+0xf6>
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
lea    0xe3a(%rip),%rdi
call   10a0 <access@plt>
test   %rax,%rax
jns    1275 <setup_unix_environment+0xd5>
mov    0x2dd2(%rip),%rax
mov    0x2d9b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe17(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
movabs $0xa726f6c6f633d,%rax
mov    %rax,0xf(%rsp)
movabs $0x3d534e4f4954504f,%rax
4e 53 3d
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xe,%edx
call   1050 <write@plt>
mov    %ebx,%edi
call   1080 <close@plt>
lea    0xd89(%rip),%rdi
call   1070 <chdir@plt>
test   %eax,%eax
jne    129f <setup_unix_environment+0xff>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd88(%rip),%rdi
jmp    12a6 <setup_unix_environment+0x106>
lea    0xd9a(%rip),%rdi
jmp    12a6 <setup_unix_environment+0x106>
lea    0xdc3(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"/usr\00"
@.str.1 = [11 x i8] c"/usr/games\00"
@.str.2 = [12 x i8] c"nethack.tar\00"
@.str.3 = [22 x i8] c"Startup untar failed.\00"
@.str.4 = [5 x i8] c"HOME\00"
@.str.5 = [24 x i8] c"Can change to HOME dir.\00"
@.str.6 = [11 x i8] c".nethackrc\00"
@R_OK = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@__const.setup_unix_environment.config = [15 x i8] c"OPTIONS=color\0A\00"
@.str.7 = [26 x i8] c"Can change to /usr/games.\00"
define void @setup_unix_environment(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [15 x i8]
store i8* %0, i8** %2
%6 = call i32 @mkdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 511)
%7 = call i32 @mkdir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 511)
%8 = load i8*, i8** %2
%9 = call i32 @nacl_startup_untar(i8* %8, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%17 = call i32 @chdir(i8* %16)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %15
%24 = load i32, i32* @R_OK
%25 = call i64 @access(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %38
27: ; preds = %23
%28 = load i32, i32* @O_CREAT
%29 = load i32, i32* @O_WRONLY
%30 = or i32 %28, %29
%31 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %30)
store i32 %31, i32* %4
%32 = bitcast [15 x i8]* %5 to i8*
%33 = load i32, i32* %4
%34 = getelementptr inbounds [15 x i8], [15 x i8]* %5, i64 0, i64 0
%35 = call i32 @write(i32 %33, i8* %34, i32 14)
%36 = load i32, i32* %4
%37 = call i32 @close(i32 %36)
br label %38
38: ; preds = %27, %23
%39 = call i32 @chdir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 %39, i32* %3
%40 = load i32, i32* %3
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %38
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"/usr\00"
@.str.1 = [11 x i8] c"/usr/games\00"
@.str.2 = [12 x i8] c"nethack.tar\00"
@.str.3 = [22 x i8] c"Startup untar failed.\00"
@.str.4 = [5 x i8] c"HOME\00"
@.str.5 = [24 x i8] c"Can change to HOME dir.\00"
@.str.6 = [11 x i8] c".nethackrc\00"
@R_OK = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@__const.setup_unix_environment.config = [15 x i8] c"OPTIONS=color\0A\00"
@.str.7 = [26 x i8] c"Can change to /usr/games.\00"
define void @setup_unix_environment(i8* %0) {
%2 = alloca [15 x i8]
%3 = tail call i32 @mkdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 511)
%4 = tail call i32 @mkdir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 511)
%5 = tail call i32 @nacl_startup_untar(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%12 = tail call i32 @chdir(i8* %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = load i32, i32* @R_OK
%19 = tail call i64 @access(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %17
%22 = load i32, i32* @O_CREAT
%23 = load i32, i32* @O_WRONLY
%24 = or i32 %23, %22
%25 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds [15 x i8], [15 x i8]* %2, i64 0, i64 0
%27 = call i32 @write(i32 %25, i8* %26, i32 14)
%28 = tail call i32 @close(i32 %25)
br label %29
29: ; preds = %21, %17
%30 = tail call i32 @chdir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp eq i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %29
ret void
}