#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 stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ LogError(char *, char const *, char *);
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
char *_SysErrorMsg(scalar_t__);
scalar_t__ errno;
int mkdir(char const *, int);
int stat(char const *, struct stat *);
int CheckServerAuthDir(const char *path, struct stat *statb, int mode) {
  int r = stat(path, statb);
  if (r != 0) {
    if (errno == ENOENT) {
      r = mkdir(path, mode);
      if (r < 0) {
        LogError("cannot make authentication directory %s: %s\n", path,
                 _SysErrorMsg(errno));
      } else {
        r = 1;
      }
    } else {
      LogError("cannot access authentication directory %s: %s\n", path,
               _SysErrorMsg(errno));
    }
  } else {
    if (!S_ISDIR(statb->st_mode)) {
      LogError("cannot make authentication directory %s: %s\n", path,
               "file with that name already exists");
      return -1;
    }
  }
  return r;
}


x86 assembly (clang, -O0):
<CheckServerAuthDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     121a <CheckServerAuthDir+0xca>
mov    0x2e5c(%rip),%rax
mov    (%rax),%rax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
jne    11eb <CheckServerAuthDir+0x9b>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1030 <mkdir@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11df <CheckServerAuthDir+0x8f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%rdi
call   1070 <_SysErrorMsg@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
lea    0xe2b(%rip),%rdi
call   1050 <LogError@plt>
jmp    11e6 <CheckServerAuthDir+0x96>
movl   $0x1,-0x20(%rbp)
jmp    1215 <CheckServerAuthDir+0xc5>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%rdi
call   1070 <_SysErrorMsg@plt>
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
lea    0xe1d(%rip),%rdi
call   1050 <LogError@plt>
jmp    1256 <CheckServerAuthDir+0x106>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1251 <CheckServerAuthDir+0x101>
mov    -0x10(%rbp),%rsi
lea    0xdc7(%rip),%rdi
lea    0xe1c(%rip),%rdx
call   1050 <LogError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <CheckServerAuthDir+0x10c>
jmp    1256 <CheckServerAuthDir+0x106>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CheckServerAuthDir>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%r15
call   1060 <stat@plt>
test   %eax,%eax
je     11b7 <CheckServerAuthDir+0x67>
mov    %eax,%ebp
mov    0x2e6e(%rip),%rbx
mov    (%rbx),%rdi
mov    0x2e54(%rip),%rax
cmp    (%rax),%rdi
jne    11e1 <CheckServerAuthDir+0x91>
mov    %r15,%rdi
mov    %r14d,%esi
call   1030 <mkdir@plt>
mov    $0x1,%ebp
test   %eax,%eax
jns    11f8 <CheckServerAuthDir+0xa8>
mov    %eax,%r14d
mov    (%rbx),%rdi
call   1070 <_SysErrorMsg@plt>
lea    0xe59(%rip),%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1050 <LogError@plt>
mov    %r14d,%ebp
jmp    11f8 <CheckServerAuthDir+0xa8>
mov    (%rbx),%edi
call   1040 <S_ISDIR@plt>
xor    %ebp,%ebp
test   %eax,%eax
jne    11f8 <CheckServerAuthDir+0xa8>
lea    0xe35(%rip),%rdi
lea    0xe8a(%rip),%rdx
mov    %r15,%rsi
call   1050 <LogError@plt>
mov    $0xffffffff,%ebp
jmp    11f8 <CheckServerAuthDir+0xa8>
call   1070 <_SysErrorMsg@plt>
lea    0xe40(%rip),%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1050 <LogError@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [45 x i8] c"cannot make authentication directory %s: %s\0A\00"
@.str.1 = [47 x i8] c"cannot access authentication directory %s: %s\0A\00"
@.str.2 = [35 x i8] c"file with that name already exists\00"
define i32 @CheckServerAuthDir(i8* %0, %struct.stat* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.stat*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store %struct.stat* %1, %struct.stat** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = load %struct.stat*, %struct.stat** %6
%11 = call i32 @stat(i8* %9, %struct.stat* %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %37
14: ; preds = %3
%15 = load i64, i64* @errno
%16 = load i64, i64* @ENOENT
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %31
18: ; preds = %14
%19 = load i8*, i8** %5
%20 = load i32, i32* %7
%21 = call i32 @mkdir(i8* %19, i32 %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i8*, i8** %5
%26 = load i64, i64* @errno
%27 = call i8* @_SysErrorMsg(i64 %26)
%28 = call i32 @LogError(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %25, i8* %27)
br label %30
29: ; preds = %18
store i32 1, i32* %8
br label %30
30: ; preds = %29, %24
br label %36
31: ; preds = %14
%32 = load i8*, i8** %5
%33 = load i64, i64* @errno
%34 = call i8* @_SysErrorMsg(i64 %33)
%35 = call i32 @LogError(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i8* %32, i8* %34)
br label %36
36: ; preds = %31, %30
br label %47
37: ; preds = %3
%38 = load %struct.stat*, %struct.stat** %6
%39 = getelementptr inbounds %struct.stat, %struct.stat* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @S_ISDIR(i32 %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %37
%44 = load i8*, i8** %5
%45 = call i32 @LogError(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %44, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %49
46: ; preds = %37
br label %47
47: ; preds = %46, %36
%48 = load i32, i32* %8
store i32 %48, i32* %4
br label %49
49: ; preds = %47, %43
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [45 x i8] c"cannot make authentication directory %s: %s\0A\00"
@.str.1 = [47 x i8] c"cannot access authentication directory %s: %s\0A\00"
@.str.2 = [35 x i8] c"file with that name already exists\00"
define i32 @CheckServerAuthDir(i8* %0, %struct.stat* nocapture %1, i32 %2) {
%4 = tail call i32 @stat(i8* %0, %struct.stat* %1)
%5 = icmp eq i32 %4, 0
br i1 %5, label %20, label %6
6: ; preds = %3
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %17
10: ; preds = %6
%11 = tail call i32 @mkdir(i8* %0, i32 %2)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %27
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = tail call i8* @_SysErrorMsg(i64 %14)
%16 = tail call i32 @LogError(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %15)
br label %27
17: ; preds = %6
%18 = tail call i8* @_SysErrorMsg(i64 %7)
%19 = tail call i32 @LogError(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %18)
br label %27
20: ; preds = %3
%21 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%22 = load i32, i32* %21
%23 = tail call i32 @S_ISDIR(i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = tail call i32 @LogError(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %13, %17, %20, %10, %25
%28 = phi i32 [ -1, %25 ], [ %11, %13 ], [ %4, %17 ], [ 0, %20 ], [ 1, %10 ]
ret i32 %28
}