#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct stat {
  int /*<<< orphan*/ st_mode;
};
struct TYPE_2__ {
  char *pw_dir;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *getenv(char *);
TYPE_1__ *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
scalar_t__ mkdir(char *, int);
char *pathjoin(char *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *getworkdir() {
  char *home = getenv("HOME");
  if (home == NULL)
    home = getpwuid(getuid())->pw_dir;
  char *workdir = pathjoin(home, ".chatrpc");
  struct stat st;
  if (stat(workdir, &st) != 0) {
    fprintf(stdout, "Creating directory %s\n", workdir);
    if (mkdir(workdir, 0700) != 0) {
      fprintf(stderr, "Error creating directory %s\n", workdir);
      exit(EXIT_FAILURE);
    }
  } else {
    if (!S_ISDIR(st.st_mode)) {
      fprintf(stderr, "%s exists and is not a directory\n", workdir);
      exit(EXIT_FAILURE);
    }
  }
  return workdir;
}


x86 assembly (clang, -O0):
<getworkdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe61(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11c8 <getworkdir+0x38>
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1080 <pathjoin@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0x0,%rax
je     1250 <getworkdir+0xc0>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe07(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x1c0,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     124b <getworkdir+0xbb>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xded(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    128d <getworkdir+0xfd>
mov    -0x18(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1288 <getworkdir+0xf8>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdcd(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    128d <getworkdir+0xfd>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getworkdir>:
push   %rbx
sub    $0x10,%rsp
lea    0xe64(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
jne    11b7 <getworkdir+0x27>
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
mov    (%rax),%rax
lea    0xe47(%rip),%rsi
mov    %rax,%rdi
call   1080 <pathjoin@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
call   10a0 <stat@plt>
test   %rax,%rax
je     1217 <getworkdir+0x87>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
mov    %rbx,%rdx
call   1090 <fprintf@plt>
mov    %rbx,%rdi
mov    $0x1c0,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     1224 <getworkdir+0x94>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
jmp    123d <getworkdir+0xad>
mov    0x8(%rsp),%edi
call   1070 <S_ISDIR@plt>
test   %eax,%eax
je     122d <getworkdir+0x9d>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
mov    %rbx,%rdx
call   1090 <fprintf@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.TYPE_2__ = type { i8* }
@.str = [5 x i8] c"HOME\00"
@.str.1 = [9 x i8] c".chatrpc\00"
@stdout = global i32 0
@.str.2 = [23 x i8] c"Creating directory %s\0A\00"
@stderr = global i32 0
@.str.3 = [29 x i8] c"Error creating directory %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [34 x i8] c"%s exists and is not a directory\0A\00"
define i8* @getworkdir() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %1
%5 = load i8*, i8** %1
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = call i32 (...) @getuid()
%9 = call %struct.TYPE_2__* @getpwuid(i32 %8)
%10 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %9, i32 0, i32 0
%11 = load i8*, i8** %10
store i8* %11, i8** %1
br label %12
12: ; preds = %7, %0
%13 = load i8*, i8** %1
%14 = call i8* @pathjoin(i8* %13, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i8* %14, i8** %2
%15 = load i8*, i8** %2
%16 = call i64 @stat(i8* %15, %struct.stat* %3)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %32
18: ; preds = %12
%19 = load i32, i32* @stdout
%20 = load i8*, i8** %2
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = load i8*, i8** %2
%23 = call i64 @mkdir(i8* %22, i32 448)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %18
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %2
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %27)
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %18
br label %44
32: ; preds = %12
%33 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @S_ISDIR(i32 %34)
%36 = icmp ne i32 %35, 0
br i1 %36, label %43, label %37
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %2
%40 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %39)
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %32
br label %44
44: ; preds = %43, %31
%45 = load i8*, i8** %2
ret i8* %45
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.TYPE_2__ = type { i8* }
@.str = [5 x i8] c"HOME\00"
@.str.1 = [9 x i8] c".chatrpc\00"
@stdout = global i32 0
@.str.2 = [23 x i8] c"Creating directory %s\0A\00"
@stderr = global i32 0
@.str.3 = [29 x i8] c"Error creating directory %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [34 x i8] c"%s exists and is not a directory\0A\00"
define i8* @getworkdir() {
%1 = alloca %struct.stat
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %9
4: ; preds = %0
%5 = tail call i32 (...) @getuid()
%6 = tail call %struct.TYPE_2__* @getpwuid(i32 %5)
%7 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %6, i64 0, i32 0
%8 = load i8*, i8** %7
br label %9
9: ; preds = %4, %0
%10 = phi i8* [ %8, %4 ], [ %2, %0 ]
%11 = tail call i8* @pathjoin(i8* %10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%12 = bitcast %struct.stat* %1 to i8*
%13 = call i64 @stat(i8* %11, %struct.stat* %1)
%14 = icmp eq i64 %13, 0
br i1 %14, label %25, label %15
15: ; preds = %9
%16 = load i32, i32* @stdout
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%18 = call i64 @mkdir(i8* %11, i32 448)
%19 = icmp eq i64 %18, 0
br i1 %19, label %35, label %20
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %11)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %9
%26 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @S_ISDIR(i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %11)
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %25, %15
ret i8* %11
}