#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 free(char *);
scalar_t__ mkdir(char const *, int /*<<< orphan*/);
char *strdup(char const *);
size_t strlen(char const *);
char *strrchr(char *, char);
int mkdirs(const char *pathname, mode_t mode) {
  char *path = strdup(pathname);
  size_t s, n = strlen(pathname);
  char *p;
  if (path == NULL)
    return -1;
  while ((p = strrchr(path, '/')))
    *p = '\0';
  for (; s = strlen(path), s < n; path[s] = '/')
    if (mkdir(path, mode) < 0)
      if (errno != EEXIST)
        return free(path), -1;
  return free(path), mkdir(pathname, mode);
}


x86 assembly (clang, -O0):
<mkdirs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1190 <mkdirs+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    1255 <mkdirs+0x105>
jmp    1195 <mkdirs+0x45>
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     11bd <mkdirs+0x6d>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
jmp    1195 <mkdirs+0x45>
jmp    11c2 <mkdirs+0x72>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    123d <mkdirs+0xed>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
jge    1227 <mkdirs+0xd7>
mov    0x2dde(%rip),%rax
mov    (%rax),%rax
mov    0x2ddc(%rip),%rcx
cmp    (%rcx),%rax
je     1222 <mkdirs+0xd2>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1255 <mkdirs+0x105>
jmp    1227 <mkdirs+0xd7>
jmp    122c <mkdirs+0xdc>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
jmp    11c2 <mkdirs+0x72>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdirs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r12d
mov    %rdi,%r14
call   1070 <strdup@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rax,%r13
mov    $0xffffffff,%eax
test   %rbx,%rbx
je     1215 <mkdirs+0xc5>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11a7 <mkdirs+0x57>
movb   $0x0,(%rax)
jmp    1190 <mkdirs+0x40>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    %r13,%rax
jae    1202 <mkdirs+0xb2>
mov    %rax,%rbp
mov    0x2e22(%rip),%r15
jmp    11d4 <mkdirs+0x84>
movb   $0x2f,(%rbx,%rbp,1)
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
cmp    %r13,%rax
jae    1202 <mkdirs+0xb2>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
jns    11c0 <mkdirs+0x70>
mov    0x2ded(%rip),%rax
mov    (%rax),%rax
cmp    (%r15),%rax
je     11c0 <mkdirs+0x70>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
jmp    1215 <mkdirs+0xc5>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
mov    %r12d,%esi
call   1040 <mkdir@plt>
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 @mkdirs(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i8*, i8** %4
%11 = call i8* @strdup(i8* %10)
store i8* %11, i8** %6
%12 = load i8*, i8** %4
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %8
%14 = load i8*, i8** %6
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %56
17: ; preds = %2
br label %18
18: ; preds = %22, %17
%19 = load i8*, i8** %6
%20 = call i8* @strrchr(i8* %19, i8 signext 47)
store i8* %20, i8** %9
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = load i8*, i8** %9
store i8 0, i8* %23
br label %18
24: ; preds = %18
br label %25
25: ; preds = %45, %24
%26 = load i8*, i8** %6
%27 = call i64 @strlen(i8* %26)
store i64 %27, i64* %7
%28 = load i64, i64* %7
%29 = load i64, i64* %8
%30 = icmp ult i64 %28, %29
br i1 %30, label %31, label %49
31: ; preds = %25
%32 = load i8*, i8** %6
%33 = load i32, i32* %5
%34 = call i64 @mkdir(i8* %32, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %44
36: ; preds = %31
%37 = load i64, i64* @errno
%38 = load i64, i64* @EEXIST
%39 = icmp ne i64 %37, %38
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i8*, i8** %6
%42 = call i32 @free(i8* %41)
store i32 -1, i32* %3
br label %56
43: ; preds = %36
br label %44
44: ; preds = %43, %31
br label %45
45: ; preds = %44
%46 = load i8*, i8** %6
%47 = load i64, i64* %7
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8 47, i8* %48
br label %25
49: ; preds = %25
%50 = load i8*, i8** %6
%51 = call i32 @free(i8* %50)
%52 = load i8*, i8** %4
%53 = load i32, i32* %5
%54 = call i64 @mkdir(i8* %52, i32 %53)
%55 = trunc i64 %54 to i32
store i32 %55, i32* %3
br label %56
56: ; preds = %49, %40, %16
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@errno = global i64 0
@EEXIST = global i64 0
define i32 @mkdirs(i8* nocapture %0, i32 %1) {
%3 = tail call i8* @strdup(i8* %0)
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = icmp eq i8* %3, null
br i1 %5, label %34, label %6
6: ; preds = %2
%7 = tail call i8* @strrchr(i8* %3, i8 signext 47)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %12, %6
%10 = tail call i64 @strlen(i8* %3)
%11 = icmp ult i64 %10, %4
br i1 %11, label %16, label %30
12: ; preds = %6, %12
%13 = phi i8* [ %14, %12 ], [ %7, %6 ]
store i8 0, i8* %13
%14 = tail call i8* @strrchr(i8* %3, i8 signext 47)
%15 = icmp eq i8* %14, null
br i1 %15, label %9, label %12
16: ; preds = %9, %26
%17 = phi i64 [ %28, %26 ], [ %10, %9 ]
%18 = tail call i64 @mkdir(i8* %3, i32 %1)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = load i64, i64* @EEXIST
%23 = icmp eq i64 %21, %22
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = tail call i32 @free(i8* %3)
br label %34
26: ; preds = %16, %20
%27 = getelementptr inbounds i8, i8* %3, i64 %17
store i8 47, i8* %27
%28 = tail call i64 @strlen(i8* %3)
%29 = icmp ult i64 %28, %4
br i1 %29, label %16, label %30
30: ; preds = %26, %9
%31 = tail call i32 @free(i8* %3)
%32 = tail call i64 @mkdir(i8* %0, i32 %1)
%33 = trunc i64 %32 to i32
br label %34
34: ; preds = %2, %30, %24
%35 = phi i32 [ -1, %24 ], [ %33, %30 ], [ -1, %2 ]
ret i32 %35
}