#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);
void sys_checkdir(char *path, int wr) {
  char *p;
  if (access(path, X_OK | (wr ? 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    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     119e <sys_checkdir+0x3e>
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    11a8 <sys_checkdir+0x48>
xor    %eax,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a8 <sys_checkdir+0x48>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x2c(%rbp),%eax
or     %eax,%esi
call   1060 <access@plt>
cmp    $0x0,%rax
je     1295 <sys_checkdir+0x135>
mov    -0x8(%rbp),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jne    1209 <sys_checkdir+0xa9>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xdfe(%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    1234 <sys_checkdir+0xd4>
lea    0xde9(%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     1290 <sys_checkdir+0x130>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
jmp    1295 <sys_checkdir+0x135>
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    0x2e6f(%rip),%rax
test   %esi,%esi
je     117e <sys_checkdir+0x1e>
mov    0x2e6c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1060 <access@plt>
test   %rax,%rax
je     1235 <sys_checkdir+0xd5>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <access@plt>
test   %rax,%rax
jne    11c8 <sys_checkdir+0x68>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe44(%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    11eb <sys_checkdir+0x8b>
lea    0xe32(%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     1235 <sys_checkdir+0xd5>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe1a(%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
@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 i32, i32* %4
%9 = icmp ne i32 %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
@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 = icmp eq i32 %1, 0
%5 = load i32, i32* @W_OK
%6 = select i1 %4, i32 0, i32 %5
%7 = or i32 %6, %3
%8 = tail call i64 @access(i8* %0, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %30, label %10
10: ; preds = %2
%11 = load i32, i32* @F_OK
%12 = tail call i64 @access(i8* %0, i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %16)
br label %18
18: ; preds = %14, %10
%19 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
store i8 0, i8* %19
tail call void @sys_checkdir(i8* %0, i32 %1)
store i8 47, i8* %19
%24 = tail call i64 @mkdir(i8* %0, i32 511)
%25 = icmp eq i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %23
%27 = load i32, i32* @errno
%28 = tail call i32 @strerror(i32 %27)
%29 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %28)
br label %30
30: ; preds = %23, %26, %2
ret void
}