#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;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, scalar_t__,
                           char *);
int /*<<< orphan*/ free(char *);
int mkdir(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
char *strdup(char const *);
char *strerror(scalar_t__);
scalar_t__ strncmp(char *, char *, int);
int mkdir_r(const char *path, mode_t mode) {
  if (!path) {
    return -1;
  }
  char *temp = strdup(path);
  char *pos = temp;
  int ret = 0;
  if (strncmp(temp, "/", 1) == 0) {
    pos += 1;
  } else if (strncmp(temp, "./", 2) == 0) {
    pos += 2;
  }
  for (; *pos != '\0'; ++pos) {
    if (*pos == '/') {
      *pos = '\0';
      if (-1 == (ret = mkdir(temp, mode))) {
        if (errno == EEXIST) {
          ret = 0;
        } else {
          fprintf(stderr, "failed to mkdir %s: %d:%s\n", temp, errno,
                  strerror(errno));
          break;
        }
      }
      *pos = '/';
    }
  }
  if (*(pos - 1) != '/') {
    printf("if %s\n", temp);
    if (-1 == (ret = mkdir(temp, mode))) {
      if (errno == EEXIST) {
        ret = 0;
      } else {
        fprintf(stderr, "failed to mkdir %s: %d:%s\n", temp, errno,
                strerror(errno));
      }
    }
  }
  free(temp);
  return ret;
}


x86 assembly (clang, -O0):
<mkdir_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1196 <mkdir_r+0x26>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bd <mkdir_r+0x24d>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe44(%rip),%rsi
mov    $0x1,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    11e2 <mkdir_r+0x72>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1212 <mkdir_r+0xa2>
mov    -0x20(%rbp),%rdi
lea    0xe13(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    120d <mkdir_r+0x9d>
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
jmp    1212 <mkdir_r+0xa2>
jmp    1217 <mkdir_r+0xa7>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12f4 <mkdir_r+0x184>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    12de <mkdir_r+0x16e>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <mkdir@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12d7 <mkdir_r+0x167>
mov    0x2d6d(%rip),%rax
mov    (%rax),%rax
mov    0x2d73(%rip),%rcx
cmp    (%rcx),%rax
jne    1282 <mkdir_r+0x112>
movl   $0x0,-0x2c(%rbp)
jmp    12d2 <mkdir_r+0x162>
mov    0x2d4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d33(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d25(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    %rax,%r8
lea    0xd3b(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12f4 <mkdir_r+0x184>
jmp    12d7 <mkdir_r+0x167>
mov    -0x28(%rbp),%rax
movb   $0x2f,(%rax)
jmp    12e3 <mkdir_r+0x173>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1217 <mkdir_r+0xa7>
mov    -0x28(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x2f,%eax
je     13ae <mkdir_r+0x23e>
mov    -0x20(%rbp),%rsi
lea    0xd0e(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <mkdir@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    13a9 <mkdir_r+0x239>
mov    0x2c96(%rip),%rax
mov    (%rax),%rax
mov    0x2c9c(%rip),%rcx
cmp    (%rcx),%rax
jne    1359 <mkdir_r+0x1e9>
movl   $0x0,-0x2c(%rbp)
jmp    13a4 <mkdir_r+0x234>
mov    0x2c78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2c5c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    0x2c4e(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    -0x50(%rbp),%rcx
mov    %rax,%r8
lea    0xc64(%rip),%rsi
call   1070 <fprintf@plt>
jmp    13a9 <mkdir_r+0x239>
jmp    13ae <mkdir_r+0x23e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11c5 <mkdir_r+0x55>
mov    %esi,%r15d
call   1080 <strdup@plt>
mov    %rax,%r12
lea    0xe6f(%rip),%rsi
mov    %rax,%rdi
mov    $0x1,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11cf <mkdir_r+0x5f>
lea    0xe55(%rip),%rsi
mov    %r12,%rdi
mov    $0x2,%edx
call   1040 <strncmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
add    %rcx,%rcx
jmp    11d4 <mkdir_r+0x64>
mov    $0xffffffff,%ebx
jmp    12ca <mkdir_r+0x15a>
mov    $0x1,%ecx
mov    %r12,%r14
add    %rcx,%r14
xor    %ebx,%ebx
mov    0x2ded(%rip),%r13
jmp    11f8 <mkdir_r+0x88>
cs nopw 0x0(%rax,%rax,1)
nop
test   %al,%al
je     125b <mkdir_r+0xeb>
add    $0x1,%r14
movzbl (%r14),%eax
cmp    $0x2f,%al
jne    11f0 <mkdir_r+0x80>
movb   $0x0,(%r14)
mov    %r12,%rdi
mov    %r15d,%esi
call   1050 <mkdir@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1228 <mkdir_r+0xb8>
mov    0x0(%r13),%rbp
xor    %ebx,%ebx
mov    0x2dbd(%rip),%rax
cmp    (%rax),%rbp
jne    122e <mkdir_r+0xbe>
movb   $0x2f,(%r14)
jmp    11f4 <mkdir_r+0x84>
mov    0x2da3(%rip),%rax
mov    (%rax),%ebx
mov    %rbp,%rdi
call   1090 <strerror@plt>
lea    0xdbd(%rip),%rsi
mov    %ebx,%edi
mov    %r12,%rdx
mov    %rbp,%rcx
mov    %rax,%r8
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebx
cmpb   $0x2f,-0x1(%r14)
je     12c2 <mkdir_r+0x152>
lea    0xdb5(%rip),%rdi
mov    %r12,%rsi
call   1060 <printf@plt>
mov    %r12,%rdi
mov    %r15d,%esi
call   1050 <mkdir@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    12c2 <mkdir_r+0x152>
mov    0x0(%r13),%rbp
xor    %ebx,%ebx
mov    0x2d50(%rip),%rax
cmp    (%rax),%rbp
je     12c2 <mkdir_r+0x152>
mov    0x2d3c(%rip),%rax
mov    (%rax),%ebx
mov    %rbp,%rdi
call   1090 <strerror@plt>
lea    0xd56(%rip),%rsi
mov    %ebx,%edi
mov    %r12,%rdx
mov    %rbp,%rcx
mov    %rax,%r8
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [3 x i8] c"./\00"
@errno = global i64 0
@EEXIST = global i64 0
@stderr = global i32 0
@.str.2 = [27 x i8] c"failed to mkdir %s: %d:%s\0A\00"
@.str.3 = [7 x i8] c"if %s\0A\00"
define i32 @mkdir_r(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%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 %97
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i8* @strdup(i8* %13)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
store i8* %15, i8** %7
store i32 0, i32* %8
%16 = load i8*, i8** %6
%17 = call i64 @strncmp(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i8*, i8** %7
%21 = getelementptr inbounds i8, i8* %20, i64 1
store i8* %21, i8** %7
br label %30
22: ; preds = %12
%23 = load i8*, i8** %6
%24 = call i64 @strncmp(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i8*, i8** %7
%28 = getelementptr inbounds i8, i8* %27, i64 2
store i8* %28, i8** %7
br label %29
29: ; preds = %26, %22
br label %30
30: ; preds = %29, %19
br label %31
31: ; preds = %63, %30
%32 = load i8*, i8** %7
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %66
36: ; preds = %31
%37 = load i8*, i8** %7
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 47
br i1 %40, label %41, label %62
41: ; preds = %36
%42 = load i8*, i8** %7
store i8 0, i8* %42
%43 = load i8*, i8** %6
%44 = load i32, i32* %5
%45 = call i32 @mkdir(i8* %43, i32 %44)
store i32 %45, i32* %8
%46 = icmp eq i32 -1, %45
br i1 %46, label %47, label %60
47: ; preds = %41
%48 = load i64, i64* @errno
%49 = load i64, i64* @EEXIST
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %52
51: ; preds = %47
store i32 0, i32* %8
br label %59
52: ; preds = %47
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %6
%55 = load i64, i64* @errno
%56 = load i64, i64* @errno
%57 = call i8* @strerror(i64 %56)
%58 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %54, i64 %55, i8* %57)
br label %66
59: ; preds = %51
br label %60
60: ; preds = %59, %41
%61 = load i8*, i8** %7
store i8 47, i8* %61
br label %62
62: ; preds = %60, %36
br label %63
63: ; preds = %62
%64 = load i8*, i8** %7
%65 = getelementptr inbounds i8, i8* %64, i32 1
store i8* %65, i8** %7
br label %31
66: ; preds = %52, %31
%67 = load i8*, i8** %7
%68 = getelementptr inbounds i8, i8* %67, i64 -1
%69 = load i8, i8* %68
%70 = sext i8 %69 to i32
%71 = icmp ne i32 %70, 47
br i1 %71, label %72, label %93
72: ; preds = %66
%73 = load i8*, i8** %6
%74 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %73)
%75 = load i8*, i8** %6
%76 = load i32, i32* %5
%77 = call i32 @mkdir(i8* %75, i32 %76)
store i32 %77, i32* %8
%78 = icmp eq i32 -1, %77
br i1 %78, label %79, label %92
79: ; preds = %72
%80 = load i64, i64* @errno
%81 = load i64, i64* @EEXIST
%82 = icmp eq i64 %80, %81
br i1 %82, label %83, label %84
83: ; preds = %79
store i32 0, i32* %8
br label %91
84: ; preds = %79
%85 = load i32, i32* @stderr
%86 = load i8*, i8** %6
%87 = load i64, i64* @errno
%88 = load i64, i64* @errno
%89 = call i8* @strerror(i64 %88)
%90 = call i32 @fprintf(i32 %85, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %86, i64 %87, i8* %89)
br label %91
91: ; preds = %84, %83
br label %92
92: ; preds = %91, %72
br label %93
93: ; preds = %92, %66
%94 = load i8*, i8** %6
%95 = call i32 @free(i8* %94)
%96 = load i32, i32* %8
store i32 %96, i32* %3
br label %97
97: ; preds = %93, %11
%98 = load i32, i32* %3
ret i32 %98
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [3 x i8] c"./\00"
@errno = global i64 0
@EEXIST = global i64 0
@stderr = global i32 0
@.str.2 = [27 x i8] c"failed to mkdir %s: %d:%s\0A\00"
@.str.3 = [7 x i8] c"if %s\0A\00"
define i32 @mkdir_r(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %55, label %4
4: ; preds = %2
%5 = tail call i8* @strdup(i8* %0)
%6 = tail call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = tail call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%10 = icmp eq i64 %9, 0
%11 = select i1 %10, i64 2, i64 0
br label %12
12: ; preds = %4, %8
%13 = phi i64 [ %11, %8 ], [ 1, %4 ]
%14 = getelementptr i8, i8* %5, i64 %13
br label %15
15: ; preds = %32, %12
%16 = phi i8* [ %14, %12 ], [ %34, %32 ]
%17 = phi i32 [ 0, %12 ], [ %33, %32 ]
%18 = load i8, i8* %16
switch i8 %18, label %32 [
i8 0, label %35
i8 47, label %19
]
19: ; preds = %15
store i8 0, i8* %16
%20 = tail call i32 @mkdir(i8* %5, i32 %1)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %30
22: ; preds = %19
%23 = load i64, i64* @errno
%24 = load i64, i64* @EEXIST
%25 = icmp eq i64 %23, %24
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = tail call i8* @strerror(i64 %23)
%29 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %5, i64 %23, i8* %28)
br label %35
30: ; preds = %22, %19
%31 = phi i32 [ %20, %19 ], [ 0, %22 ]
store i8 47, i8* %16
br label %32
32: ; preds = %15, %30
%33 = phi i32 [ %31, %30 ], [ %17, %15 ]
%34 = getelementptr inbounds i8, i8* %16, i64 1
br label %15
35: ; preds = %15, %26
%36 = phi i32 [ -1, %26 ], [ %17, %15 ]
%37 = getelementptr inbounds i8, i8* %16, i64 -1
%38 = load i8, i8* %37
%39 = icmp eq i8 %38, 47
br i1 %39, label %52, label %40
40: ; preds = %35
%41 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%42 = tail call i32 @mkdir(i8* %5, i32 %1)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %52
44: ; preds = %40
%45 = load i64, i64* @errno
%46 = load i64, i64* @EEXIST
%47 = icmp eq i64 %45, %46
br i1 %47, label %52, label %48
48: ; preds = %44
%49 = load i32, i32* @stderr
%50 = tail call i8* @strerror(i64 %45)
%51 = tail call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %5, i64 %45, i8* %50)
br label %52
52: ; preds = %44, %40, %48, %35
%53 = phi i32 [ -1, %48 ], [ %42, %40 ], [ %36, %35 ], [ 0, %44 ]
%54 = tail call i32 @free(i8* %5)
br label %55
55: ; preds = %2, %52
%56 = phi i32 [ %53, %52 ], [ -1, %2 ]
ret i32 %56
}