#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*/ R_OK;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ access(char *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ perror(char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int hooks_create_dir(char *dir) {
  struct stat st;
  int err;
  err = stat(dir, &st);
  if (err == -1) {
    if (errno == ENOENT) {
      if (mkdir(dir, 0755) == 0) {
        fprintf(stderr, "created hooks dir: ./%s\n", dir);
      } else {
        fprintf(stderr, "error creating hooks dir (./%s): %s\n", dir,
                strerror(errno));
        return -1;
      }
    } else {
      perror("stat hooks dir");
      return -1;
    }
  } else {
    if (!S_ISDIR(st.st_mode)) {
      fprintf(stderr, "hooks dir (./%s) is not a directory\n", dir);
      return -1;
    }
  }
  if (access(dir, R_OK) != 0) {
    fprintf(stderr, "Can't access hooks dir (./%s): %s\n", dir,
            strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<hooks_create_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1251 <hooks_create_dir+0xe1>
mov    0x2e3b(%rip),%rax
mov    (%rax),%rax
mov    0x2e19(%rip),%rcx
cmp    (%rcx),%rax
jne    1234 <hooks_create_dir+0xc4>
mov    -0x10(%rbp),%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
jne    11e8 <hooks_create_dir+0x78>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe24(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    122f <hooks_create_dir+0xbf>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <hooks_create_dir+0x188>
jmp    124c <hooks_create_dir+0xdc>
lea    0xe03(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <hooks_create_dir+0x188>
jmp    128e <hooks_create_dir+0x11e>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1289 <hooks_create_dir+0x119>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <hooks_create_dir+0x188>
jmp    128e <hooks_create_dir+0x11e>
mov    -0x10(%rbp),%rdi
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
je     12f1 <hooks_create_dir+0x181>
mov    0x2d2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d13(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xd94(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <hooks_create_dir+0x188>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hooks_create_dir>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     11b6 <hooks_create_dir+0x46>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %eax,%eax
jne    120f <hooks_create_dir+0x9f>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
lea    0xea6(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1253 <hooks_create_dir+0xe3>
mov    0x2e1b(%rip),%r14
mov    (%r14),%rax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%rax
jne    1265 <hooks_create_dir+0xf5>
mov    %rbx,%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
mov    0x2dfc(%rip),%rcx
mov    (%rcx),%ebp
test   %rax,%rax
je     11fc <hooks_create_dir+0x8c>
mov    (%r14),%rdi
call   1090 <strerror@plt>
lea    0xe1f(%rip),%rsi
jmp    1244 <hooks_create_dir+0xd4>
lea    0xdfd(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <access@plt>
test   %rax,%rax
je     1261 <hooks_create_dir+0xf1>
mov    0x2db4(%rip),%rax
mov    (%rax),%ebp
mov    0x2da3(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
lea    0xe2e(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
jmp    1258 <hooks_create_dir+0xe8>
lea    0xdd2(%rip),%rdi
call   1080 <perror@plt>
jmp    1253 <hooks_create_dir+0xe3>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str = [25 x i8] c"created hooks dir: ./%s\0A\00"
@.str.1 = [37 x i8] c"error creating hooks dir (./%s): %s\0A\00"
@.str.2 = [15 x i8] c"stat hooks dir\00"
@.str.3 = [37 x i8] c"hooks dir (./%s) is not a directory\0A\00"
@R_OK = global i32 0
@.str.4 = [35 x i8] c"Can't access hooks dir (./%s): %s\0A\00"
define i32 @hooks_create_dir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @stat(i8* %6, %struct.stat* %4)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %32
10: ; preds = %1
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOENT
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %29
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = call i64 @mkdir(i8* %15, i32 493)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %3
%21 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %28
22: ; preds = %14
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %3
%25 = load i64, i64* @errno
%26 = call i8* @strerror(i64 %25)
%27 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %26)
store i32 -1, i32* %2
br label %54
28: ; preds = %18
br label %31
29: ; preds = %10
%30 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %54
31: ; preds = %28
br label %42
32: ; preds = %1
%33 = getelementptr inbounds %struct.stat, %struct.stat* %4, 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 %41, label %37
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %3
%40 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %39)
store i32 -1, i32* %2
br label %54
41: ; preds = %32
br label %42
42: ; preds = %41, %31
%43 = load i8*, i8** %3
%44 = load i32, i32* @R_OK
%45 = call i64 @access(i8* %43, i32 %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %3
%50 = load i64, i64* @errno
%51 = call i8* @strerror(i64 %50)
%52 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %49, i8* %51)
store i32 -1, i32* %2
br label %54
53: ; preds = %42
store i32 0, i32* %2
br label %54
54: ; preds = %53, %47, %37, %29, %22
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str = [25 x i8] c"created hooks dir: ./%s\0A\00"
@.str.1 = [37 x i8] c"error creating hooks dir (./%s): %s\0A\00"
@.str.2 = [15 x i8] c"stat hooks dir\00"
@.str.3 = [37 x i8] c"hooks dir (./%s) is not a directory\0A\00"
@R_OK = global i32 0
@.str.4 = [35 x i8] c"Can't access hooks dir (./%s): %s\0A\00"
define i32 @hooks_create_dir(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %22
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %20
10: ; preds = %6
%11 = tail call i64 @mkdir(i8* %0, i32 493)
%12 = icmp eq i64 %11, 0
%13 = load i32, i32* @stderr
br i1 %12, label %14, label %16
14: ; preds = %10
%15 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %30
16: ; preds = %10
%17 = load i64, i64* @errno
%18 = tail call i8* @strerror(i64 %17)
%19 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %18)
br label %39
20: ; preds = %6
%21 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %39
22: ; preds = %1
%23 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%24 = load i32, i32* %23
%25 = tail call i32 @S_ISDIR(i32 %24)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %39
30: ; preds = %22, %14
%31 = load i32, i32* @R_OK
%32 = tail call i64 @access(i8* %0, i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %39, label %34
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = load i64, i64* @errno
%37 = tail call i8* @strerror(i64 %36)
%38 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %37)
br label %39
39: ; preds = %30, %34, %27, %20, %16
%40 = phi i32 [ -1, %34 ], [ -1, %16 ], [ -1, %20 ], [ -1, %27 ], [ 0, %30 ]
ret i32 %40
}