#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 F_OK;
int W_OK;
int X_OK;
scalar_t__ access(char *, int);
int /*<<< orphan*/ die(char *, ...);
int /*<<< orphan*/ errno;
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
char *strrchr(char *, char);
scalar_t__ write;
void sys_checkdir(char *path, int wr) {
  char *p;
  if (access(path, X_OK | (write ? W_OK : 0))) {
    if (!access(path, F_OK))
      die("cannot access %s: %s\n", path, strerror(errno));
    p = strrchr(path, '/');
    if (!p)
      die("descended to root trying to create dirs\n");
    *p = 0;
    sys_checkdir(path, wr);
    *p = '/';
    if (mkdir(path, 0777))
      die("cannot create %s: %s\n", path, strerror(errno));
  }
}


x86 assembly (clang, -O0):
<sys_checkdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e5e(%rip),%rax
cmpq   $0x0,(%rax)
je     11a5 <sys_checkdir+0x45>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    11af <sys_checkdir+0x4f>
xor    %eax,%eax
mov    %eax,-0x2c(%rbp)
jmp    11af <sys_checkdir+0x4f>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x2c(%rbp),%eax
or     %eax,%esi
call   1060 <access@plt>
cmp    $0x0,%rax
je     129c <sys_checkdir+0x13c>
mov    -0x8(%rbp),%rdi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jne    1210 <sys_checkdir+0xb0>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    123b <sys_checkdir+0xdb>
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <sys_checkdir@plt>
mov    -0x18(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x8(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     1297 <sys_checkdir+0x137>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
jmp    129c <sys_checkdir+0x13c>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sys_checkdir>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e67(%rip),%rax
mov    0x2e70(%rip),%rcx
xor    %esi,%esi
cmpq   $0x0,(%rcx)
je     1189 <sys_checkdir+0x29>
mov    0x2e59(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1060 <access@plt>
test   %rax,%rax
je     1240 <sys_checkdir+0xe0>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <access@plt>
test   %rax,%rax
jne    11d3 <sys_checkdir+0x73>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe39(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <die@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11f6 <sys_checkdir+0x96>
lea    0xe27(%rip),%rdi
xor    %eax,%eax
call   1070 <die@plt>
movb   $0x0,0x0(%rbp)
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <sys_checkdir@plt>
movb   $0x2f,0x0(%rbp)
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
je     1240 <sys_checkdir+0xe0>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe0f(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <die@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@X_OK = global i32 0
@write = global i64 0
@W_OK = global i32 0
@F_OK = global i32 0
@.str = [22 x i8] c"cannot access %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [41 x i8] c"descended to root trying to create dirs\0A\00"
@.str.2 = [22 x i8] c"cannot create %s: %s\0A\00"
define void @sys_checkdir(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
%7 = load i32, i32* @X_OK
%8 = load i64, i64* @write
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i32, i32* @W_OK
br label %13
12: ; preds = %2
br label %13
13: ; preds = %12, %10
%14 = phi i32 [ %11, %10 ], [ 0, %12 ]
%15 = or i32 %7, %14
%16 = call i64 @access(i8* %6, i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %49
18: ; preds = %13
%19 = load i8*, i8** %3
%20 = load i32, i32* @F_OK
%21 = call i64 @access(i8* %19, i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %18
%24 = load i8*, i8** %3
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %24, i32 %26)
br label %28
28: ; preds = %23, %18
%29 = load i8*, i8** %3
%30 = call i8* @strrchr(i8* %29, i8 signext 47)
store i8* %30, i8** %5
%31 = load i8*, i8** %5
%32 = icmp ne i8* %31, null
br i1 %32, label %35, label %33
33: ; preds = %28
%34 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
%36 = load i8*, i8** %5
store i8 0, i8* %36
%37 = load i8*, i8** %3
%38 = load i32, i32* %4
%39 = load i8*, i8** %5
store i8 47, i8* %39
%40 = load i8*, i8** %3
%41 = call i64 @mkdir(i8* %40, i32 511)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %35
%44 = load i8*, i8** %3
%45 = load i32, i32* @errno
%46 = call i32 @strerror(i32 %45)
%47 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %44, i32 %46)
br label %48
48: ; preds = %43, %35
br label %49
49: ; preds = %48, %13
ret void
}

LLVM IR (-O2):
@X_OK = global i32 0
@write = global i64 0
@W_OK = global i32 0
@F_OK = global i32 0
@.str = [22 x i8] c"cannot access %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [41 x i8] c"descended to root trying to create dirs\0A\00"
@.str.2 = [22 x i8] c"cannot create %s: %s\0A\00"
define void @sys_checkdir(i8* %0, i32 %1) {
%3 = load i32, i32* @X_OK
%4 = load i64, i64* @write
%5 = icmp eq i64 %4, 0
%6 = load i32, i32* @W_OK
%7 = select i1 %5, i32 0, i32 %6
%8 = or i32 %7, %3
%9 = tail call i64 @access(i8* %0, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %31, label %11
11: ; preds = %2
%12 = load i32, i32* @F_OK
%13 = tail call i64 @access(i8* %0, i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i32, i32* @errno
%17 = tail call i32 @strerror(i32 %16)
%18 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %17)
br label %19
19: ; preds = %15, %11
%20 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
store i8 0, i8* %20
tail call void @sys_checkdir(i8* %0, i32 %1)
store i8 47, i8* %20
%25 = tail call i64 @mkdir(i8* %0, i32 511)
%26 = icmp eq i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %24
%28 = load i32, i32* @errno
%29 = tail call i32 @strerror(i32 %28)
%30 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %29)
br label %31
31: ; preds = %24, %27, %2
ret void
}