#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 */
int chdir(char *);
char *getenv(char *);
scalar_t__ mkdir(char *, int);
int ch_userdir(void) {
  char *home = getenv("HOME");
  if (!home)
    return -1;
  if (chdir(home))
    return -1;
  if (chdir(".dreamchess")) {
    if (mkdir(".dreamchess", 0755))
      return -1;
    return chdir(".dreamchess");
  }
  return 0;
}


x86 assembly (clang, -O0):
<ch_userdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xec1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115f <ch_userdir+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d4 <ch_userdir+0xa4>
mov    -0x10(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%eax
je     117d <ch_userdir+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d4 <ch_userdir+0xa4>
lea    0xe81(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%eax
je     11cd <ch_userdir+0x9d>
lea    0xe6c(%rip),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     11b9 <ch_userdir+0x89>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d4 <ch_userdir+0xa4>
lea    0xe45(%rip),%rdi
call   1050 <chdir@plt>
mov    %eax,-0x4(%rbp)
jmp    11d4 <ch_userdir+0xa4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ch_userdir>:
push   %rbx
lea    0xec8(%rip),%rdi
call   1030 <getenv@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     1153 <ch_userdir+0x23>
mov    %rax,%rdi
call   1050 <chdir@plt>
test   %eax,%eax
je     1157 <ch_userdir+0x27>
mov    %ebx,%eax
pop    %rbx
ret
lea    0xea7(%rip),%rdi
call   1050 <chdir@plt>
test   %eax,%eax
je     118a <ch_userdir+0x5a>
lea    0xe97(%rip),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
jne    1153 <ch_userdir+0x23>
lea    0xe81(%rip),%rdi
pop    %rbx
jmp    1050 <chdir@plt>
xor    %ebx,%ebx
jmp    1153 <ch_userdir+0x23>

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [12 x i8] c".dreamchess\00"
define i32 @ch_userdir() {
%1 = alloca i32
%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 %7, label %6
6: ; preds = %0
store i32 -1, i32* %1
br label %22
7: ; preds = %0
%8 = load i8*, i8** %2
%9 = call i32 @chdir(i8* %8)
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %7
store i32 -1, i32* %1
br label %22
12: ; preds = %7
%13 = call i32 @chdir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = call i64 @mkdir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 493)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 -1, i32* %1
br label %22
19: ; preds = %15
%20 = call i32 @chdir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 %20, i32* %1
br label %22
21: ; preds = %12
store i32 0, i32* %1
br label %22
22: ; preds = %21, %19, %18, %11, %6
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [12 x i8] c".dreamchess\00"
define i32 @ch_userdir() {
%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 %14, label %3
3: ; preds = %0
%4 = tail call i32 @chdir(i8* %1)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %14
6: ; preds = %3
%7 = tail call i32 @chdir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %6
%10 = tail call i64 @mkdir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 493)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @chdir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %6, %9, %3, %0, %12
%15 = phi i32 [ %13, %12 ], [ -1, %0 ], [ -1, %3 ], [ -1, %9 ], [ 0, %6 ]
ret i32 %15
}