#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__ errno;
scalar_t__ mkdir(char *, int /*<<< orphan*/);
char *strchr(char *, char);
int openr2_mkdir_recursive(char *dir, mode_t mode) {
  char *currslash = NULL;
  char *str = dir;
  if (!dir) {
    return -1;
  }
  str++;
  while ((currslash = strchr(str, '/'))) {
    *currslash = 0;
    if (mkdir(dir, mode) && errno != EEXIST) {
      return -1;
    }
    *currslash = '/';
    str = currslash + 1;
  }
  if (str[0] != 0) {
    if (mkdir(dir, mode)) {
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<openr2_mkdir_recursive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1156 <openr2_mkdir_recursive+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <openr2_mkdir_recursive+0xf7>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11d9 <openr2_mkdir_recursive+0xb9>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     11c1 <openr2_mkdir_recursive+0xa1>
mov    0x2e36(%rip),%rax
mov    (%rax),%rax
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%rax
je     11c1 <openr2_mkdir_recursive+0xa1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <openr2_mkdir_recursive+0xf7>
mov    -0x20(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1162 <openr2_mkdir_recursive+0x42>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1210 <openr2_mkdir_recursive+0xf0>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     120b <openr2_mkdir_recursive+0xeb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <openr2_mkdir_recursive+0xf7>
jmp    1210 <openr2_mkdir_recursive+0xf0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openr2_mkdir_recursive>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     11cf <openr2_mkdir_recursive+0xaf>
mov    %esi,%r15d
mov    %rdi,%r12
lea    0x1(%rdi),%rbp
mov    %rbp,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11b2 <openr2_mkdir_recursive+0x92>
mov    %rax,%rbx
mov    0x2e79(%rip),%r13
mov    0x2e7a(%rip),%r14
jmp    118f <openr2_mkdir_recursive+0x6f>
nopl   0x0(%rax,%rax,1)
movb   $0x2f,(%rbx)
mov    %rbx,%rbp
add    $0x1,%rbp
mov    %rbp,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11b2 <openr2_mkdir_recursive+0x92>
movb   $0x0,(%rbx)
mov    %r12,%rdi
mov    %r15d,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
je     1170 <openr2_mkdir_recursive+0x50>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
je     1170 <openr2_mkdir_recursive+0x50>
mov    $0xffffffff,%ebx
jmp    11cf <openr2_mkdir_recursive+0xaf>
cmpb   $0x0,0x0(%rbp)
mov    $0xffffffff,%ebx
je     11cd <openr2_mkdir_recursive+0xad>
mov    %r12,%rdi
mov    %r15d,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
jne    11cf <openr2_mkdir_recursive+0xaf>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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