#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 */
scalar_t__ EEXIST;
scalar_t__ ENOTDIR;
scalar_t__ errno;
int mkdir(char *, int);
char *strchr(char *, char);
int mkdir_recursive(char *dir, size_t off) {
  char *p = dir + off;
  if (*p != '/') {
    if (off && p[-1] == '/')
      --p;
    else {
      errno = ENOTDIR;
      return -1;
    }
  }
  do {
    *p = '\0';
    int rc = mkdir(dir, 0700);
    *p = '/';
    if (0 != rc && errno != EEXIST)
      return -1;
  } while ((p = strchr(p + 1, '/')) != NULL);
  return 0;
}


x86 assembly (clang, -O0):
<mkdir_recursive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     119e <mkdir_recursive+0x7e>
cmpq   $0x0,-0x18(%rbp)
je     1179 <mkdir_recursive+0x59>
mov    -0x20(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x2f,%eax
jne    1179 <mkdir_recursive+0x59>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    1199 <mkdir_recursive+0x79>
mov    0x2e78(%rip),%rax
mov    (%rax),%rcx
mov    0x2e46(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <mkdir_recursive+0xff>
jmp    119e <mkdir_recursive+0x7e>
jmp    11a3 <mkdir_recursive+0x83>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x2f,(%rax)
xor    %eax,%eax
cmp    -0x24(%rbp),%eax
je     11f3 <mkdir_recursive+0xd3>
mov    0x2dfc(%rip),%rax
mov    (%rax),%rax
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%rax
je     11f3 <mkdir_recursive+0xd3>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <mkdir_recursive+0xff>
jmp    11f8 <mkdir_recursive+0xd8>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11a3 <mkdir_recursive+0x83>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_recursive>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    (%rdi,%rsi,1),%rbx
cmpb   $0x2f,(%rdi,%rsi,1)
je     1144 <mkdir_recursive+0x24>
test   %rsi,%rsi
je     119f <mkdir_recursive+0x7f>
cmpb   $0x2f,-0x1(%rbx)
jne    119f <mkdir_recursive+0x7f>
add    $0xffffffffffffffff,%rbx
mov    0x2e85(%rip),%r15
mov    0x2e86(%rip),%r12
jmp    1179 <mkdir_recursive+0x59>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     119b <mkdir_recursive+0x7b>
movb   $0x0,(%rbx)
mov    %r14,%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
movb   $0x2f,(%rbx)
test   %eax,%eax
je     1160 <mkdir_recursive+0x40>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1160 <mkdir_recursive+0x40>
jmp    11b3 <mkdir_recursive+0x93>
xor    %eax,%eax
jmp    11b8 <mkdir_recursive+0x98>
mov    0x2e52(%rip),%rax
mov    (%rax),%rax
mov    0x2e20(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@ENOTDIR = global i64 0
@errno = global i64 0
@EEXIST = global i64 0
define i32 @mkdir_recursive(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i8*, i8** %4
%9 = load i64, i64* %5
%10 = getelementptr inbounds i8, i8* %8, i64 %9
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 47
br i1 %14, label %15, label %30
15: ; preds = %2
%16 = load i64, i64* %5
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %15
%19 = load i8*, i8** %6
%20 = getelementptr inbounds i8, i8* %19, i64 -1
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 47
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load i8*, i8** %6
%26 = getelementptr inbounds i8, i8* %25, i32 -1
store i8* %26, i8** %6
br label %29
27: ; preds = %18, %15
%28 = load i64, i64* @ENOTDIR
store i64 %28, i64* @errno
store i32 -1, i32* %3
br label %50
29: ; preds = %24
br label %30
30: ; preds = %29, %2
br label %31
31: ; preds = %44, %30
%32 = load i8*, i8** %6
store i8 0, i8* %32
%33 = load i8*, i8** %4
%34 = call i32 @mkdir(i8* %33, i32 448)
store i32 %34, i32* %7
%35 = load i8*, i8** %6
store i8 47, i8* %35
%36 = load i32, i32* %7
%37 = icmp ne i32 0, %36
br i1 %37, label %38, label %43
38: ; preds = %31
%39 = load i64, i64* @errno
%40 = load i64, i64* @EEXIST
%41 = icmp ne i64 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %38
store i32 -1, i32* %3
br label %50
43: ; preds = %38, %31
br label %44
44: ; preds = %43
%45 = load i8*, i8** %6
%46 = getelementptr inbounds i8, i8* %45, i64 1
%47 = call i8* @strchr(i8* %46, i8 signext 47)
store i8* %47, i8** %6
%48 = icmp ne i8* %47, null
br i1 %48, label %31, label %49
49: ; preds = %44
store i32 0, i32* %3
br label %50
50: ; preds = %49, %42, %27
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@ENOTDIR = global i64 0
@errno = global i64 0
@EEXIST = global i64 0
define i32 @mkdir_recursive(i8* %0, i64 %1) {
%3 = getelementptr inbounds i8, i8* %0, i64 %1
%4 = load i8, i8* %3
%5 = icmp eq i8 %4, 47
br i1 %5, label %6, label %8
6: ; preds = %10, %2
%7 = phi i8* [ %11, %10 ], [ %3, %2 ]
br label %16
8: ; preds = %2
%9 = icmp eq i64 %1, 0
br i1 %9, label %14, label %10
10: ; preds = %8
%11 = getelementptr inbounds i8, i8* %3, i64 -1
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 47
br i1 %13, label %6, label %14
14: ; preds = %10, %8
%15 = load i64, i64* @ENOTDIR
store i64 %15, i64* @errno
br label %28
16: ; preds = %6, %24
%17 = phi i8* [ %26, %24 ], [ %7, %6 ]
store i8 0, i8* %17
%18 = tail call i32 @mkdir(i8* %0, i32 448)
store i8 47, i8* %17
%19 = icmp eq i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = load i64, i64* @EEXIST
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %28
24: ; preds = %16, %20
%25 = getelementptr inbounds i8, i8* %17, i64 1
%26 = tail call i8* @strchr(i8* %25, i8 signext 47)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %16
28: ; preds = %24, %20, %14
%29 = phi i32 [ -1, %14 ], [ 0, %24 ], [ -1, %20 ]
ret i32 %29
}