#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 */
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ ENOENT;
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_ERR;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ mkdir(char *, int /*<<< orphan*/);
char *strdup(char *);
int /*<<< orphan*/ strerror(scalar_t__);
int strlen(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *, ...);
int rmkdir(char *dir, mode_t mode) {
  char *parent;
  int i;
  int r = 0;
  if (mkdir(dir, mode) != 0) {
    if (errno == ENOENT) {
      parent = strdup(dir);
      for (i = strlen(parent); i > 0; --i) {
        if (parent[i] == '/') {
          parent[i] = '\0';
          if ((r = rmkdir(parent, mode)) != 1)
            break;
        }
      }
      free(parent);
      if (r == 0)
        return 0;
      rmkdir(dir, mode);
    } else if (errno != EEXIST) {
      syslog(LOG_ERR, "Error creating directory '%s': %s", dir,
             strerror(errno));
      return 0;
    } else {
      return -1;
    }
  } else {
    syslog(LOG_DEBUG, "Directory '%s' created", dir);
    return 1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<rmkdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     12d0 <rmkdir+0x160>
mov    0x2e35(%rip),%rax
mov    (%rax),%rax
mov    0x2e13(%rip),%rcx
cmp    (%rcx),%rax
jne    125f <rmkdir+0xef>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    122f <rmkdir+0xbf>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    121c <rmkdir+0xac>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <rmkdir@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x1,%eax
je     1217 <rmkdir+0xa7>
jmp    122f <rmkdir+0xbf>
jmp    121c <rmkdir+0xac>
jmp    1221 <rmkdir+0xb1>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    11cf <rmkdir+0x5f>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x28(%rbp)
jne    124e <rmkdir+0xde>
movl   $0x0,-0x4(%rbp)
jmp    12fe <rmkdir+0x18e>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <rmkdir@plt>
jmp    12cb <rmkdir+0x15b>
mov    0x2d72(%rip),%rax
mov    (%rax),%rax
mov    0x2d70(%rip),%rcx
cmp    (%rcx),%rax
je     12bf <rmkdir+0x14f>
mov    0x2d38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d44(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %eax,%ecx
lea    0xd54(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
movl   $0x0,-0x4(%rbp)
jmp    12fe <rmkdir+0x18e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fe <rmkdir+0x18e>
jmp    12f7 <rmkdir+0x187>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd3e(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <rmkdir+0x18e>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rmkdir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r15
call   1040 <mkdir@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     1247 <rmkdir+0xd7>
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x2e31(%rip),%rax
mov    (%rax),%rdi
mov    0x2e0f(%rip),%rax
cmp    (%rax),%rdi
jne    1271 <rmkdir+0x101>
mov    %r15,%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    12a9 <rmkdir+0x139>
mov    %eax,%r13d
add    $0x1,%r13
xor    %r14d,%r14d
jmp    11fa <rmkdir+0x8a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0xffffffffffffffff,%r13
cmp    $0x1,%r13
jle    1220 <rmkdir+0xb0>
cmpb   $0x2f,-0x1(%rbx,%r13,1)
jne    11f0 <rmkdir+0x80>
movb   $0x0,-0x1(%rbx,%r13,1)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <rmkdir@plt>
mov    $0x1,%r14d
cmp    $0x1,%eax
je     11f0 <rmkdir+0x80>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1030 <free@plt>
test   %r14d,%r14d
je     12b1 <rmkdir+0x141>
mov    %r15,%rdi
mov    %ebp,%esi
call   1040 <mkdir@plt>
mov    $0x1,%r12b
test   %rax,%rax
jne    11a0 <rmkdir+0x30>
cmpq   $0x0,(%rsp)
setne  %r12b
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    0xdc2(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <syslog@plt>
mov    $0x1,%ebp
jmp    12b3 <rmkdir+0x143>
mov    0x2d68(%rip),%rax
mov    $0xffffffff,%ebp
cmp    (%rax),%rdi
je     12b3 <rmkdir+0x143>
mov    0x2d2f(%rip),%rax
mov    (%rax),%ebx
call   1090 <strerror@plt>
lea    0xd69(%rip),%rsi
xor    %ebp,%ebp
mov    %ebx,%edi
mov    %r15,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1070 <syslog@plt>
jmp    12b3 <rmkdir+0x143>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
test   $0x1,%r12b
mov    $0xffffffff,%eax
cmove  %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@ENOENT = global i64 0
@EEXIST = global i64 0
@LOG_ERR = global i32 0
@.str = [34 x i8] c"Error creating directory '%s': %s\00"
@LOG_DEBUG = global i32 0
@.str.1 = [23 x i8] c"Directory '%s' created\00"
define i32 @rmkdir(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %8
%9 = load i8*, i8** %4
%10 = load i32, i32* %5
%11 = call i64 @mkdir(i8* %9, i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %70
13: ; preds = %2
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOENT
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %58
17: ; preds = %13
%18 = load i8*, i8** %4
%19 = call i8* @strdup(i8* %18)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = call i32 @strlen(i8* %20)
store i32 %21, i32* %7
br label %22
22: ; preds = %45, %17
%23 = load i32, i32* %7
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %48
25: ; preds = %22
%26 = load i8*, i8** %6
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 47
br i1 %32, label %33, label %44
33: ; preds = %25
%34 = load i8*, i8** %6
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 0, i8* %37
%38 = load i8*, i8** %6
%39 = load i32, i32* %5
%40 = call i32 @rmkdir(i8* %38, i32 %39)
store i32 %40, i32* %8
%41 = icmp ne i32 %40, 1
br i1 %41, label %42, label %43
42: ; preds = %33
br label %48
43: ; preds = %33
br label %44
44: ; preds = %43, %25
br label %45
45: ; preds = %44
%46 = load i32, i32* %7
%47 = add nsw i32 %46, -1
store i32 %47, i32* %7
br label %22
48: ; preds = %42, %22
%49 = load i8*, i8** %6
%50 = call i32 @free(i8* %49)
%51 = load i32, i32* %8
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %48
store i32 0, i32* %3
br label %75
54: ; preds = %48
%55 = load i8*, i8** %4
%56 = load i32, i32* %5
%57 = call i32 @rmkdir(i8* %55, i32 %56)
br label %69
58: ; preds = %13
%59 = load i64, i64* @errno
%60 = load i64, i64* @EEXIST
%61 = icmp ne i64 %59, %60
br i1 %61, label %62, label %68
62: ; preds = %58
%63 = load i32, i32* @LOG_ERR
%64 = load i8*, i8** %4
%65 = load i64, i64* @errno
%66 = call i32 @strerror(i64 %65)
%67 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %63, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %64, i32 %66)
store i32 0, i32* %3
br label %75
68: ; preds = %58
store i32 -1, i32* %3
br label %75
69: ; preds = %54
br label %74
70: ; preds = %2
%71 = load i32, i32* @LOG_DEBUG
%72 = load i8*, i8** %4
%73 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %71, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %72)
store i32 1, i32* %3
br label %75
74: ; preds = %69
store i32 -1, i32* %3
br label %75
75: ; preds = %74, %70, %68, %62, %53
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@errno = global i64 0
@ENOENT = global i64 0
@EEXIST = global i64 0
@LOG_ERR = global i32 0
@.str = [34 x i8] c"Error creating directory '%s': %s\00"
@LOG_DEBUG = global i32 0
@.str.1 = [23 x i8] c"Directory '%s' created\00"
define i32 @rmkdir(i8* %0, i32 %1) {
%3 = tail call i64 @mkdir(i8* %0, i32 %1)
%4 = icmp eq i64 %3, 0
br i1 %4, label %45, label %5
5: ; preds = %2, %35
%6 = phi i1 [ true, %35 ], [ false, %2 ]
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %38
10: ; preds = %5
%11 = tail call i8* @strdup(i8* %0)
%12 = tail call i32 @strlen(i8* %11)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 @free(i8* %11)
br label %49
16: ; preds = %10
%17 = zext i32 %12 to i64
br label %18
18: ; preds = %16, %27
%19 = phi i64 [ %17, %16 ], [ %29, %27 ]
%20 = phi i32 [ 0, %16 ], [ %28, %27 ]
%21 = getelementptr inbounds i8, i8* %11, i64 %19
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 47
br i1 %23, label %24, label %27
24: ; preds = %18
store i8 0, i8* %21
%25 = tail call i32 @rmkdir(i8* %11, i32 %1)
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %31
27: ; preds = %18, %24
%28 = phi i32 [ 1, %24 ], [ %20, %18 ]
%29 = add nsw i64 %19, -1
%30 = icmp sgt i64 %19, 1
br i1 %30, label %18, label %31
31: ; preds = %27, %24
%32 = phi i32 [ %28, %27 ], [ %25, %24 ]
%33 = tail call i32 @free(i8* %11)
%34 = icmp eq i32 %32, 0
br i1 %34, label %49, label %35
35: ; preds = %31
%36 = tail call i64 @mkdir(i8* %0, i32 %1)
%37 = icmp eq i64 %36, 0
br i1 %37, label %45, label %5
38: ; preds = %5
%39 = load i64, i64* @EEXIST
%40 = icmp eq i64 %7, %39
br i1 %40, label %49, label %41
41: ; preds = %38
%42 = load i32, i32* @LOG_ERR
%43 = tail call i32 @strerror(i64 %7)
%44 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %42, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %43)
br label %49
45: ; preds = %35, %2
%46 = xor i1 %4, true
%47 = load i32, i32* @LOG_DEBUG
%48 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %47, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %49
49: ; preds = %31, %14, %38, %45, %41
%50 = phi i1 [ %6, %41 ], [ %46, %45 ], [ %6, %38 ], [ %6, %14 ], [ %6, %31 ]
%51 = phi i32 [ 0, %41 ], [ 1, %45 ], [ -1, %38 ], [ 0, %14 ], [ 0, %31 ]
%52 = select i1 %50, i32 -1, i32 %51
ret i32 %52
}