#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 */
int EEXIST;
int errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ mkdir(char *, int);
char *strchr(char *, char);
char *strdup(char const *);
int mkdir_recursive(const char *dirpath) {
  char *path = strdup(dirpath);
  char *tmp = path + 1;
  int errno_old = errno;
  int retval = 0;
  while ((tmp = strchr(tmp, '/'))) {
    *tmp = '\0';
    errno = 0;
    mkdir(path, 0755);
    if (errno != 0 && errno != EEXIST) {
      retval = -1;
      goto EXIT;
    }
    *tmp++ = '/';
  }
  errno = 0;
  mkdir(path, 0755);
  if (errno != 0 && errno != EEXIST)
    retval = -1;
EXIT:
  if (errno == 0)
    errno = errno_old;
  free(tmp);
  return retval;
}


x86 assembly (clang, -O0):
<mkdir_recursive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1201 <mkdir_recursive+0xc1>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2e36(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
mov    0x2e1b(%rip),%rax
cmpl   $0x0,(%rax)
je     11ea <mkdir_recursive+0xaa>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    0x2e0a(%rip),%rcx
cmp    (%rcx),%eax
je     11ea <mkdir_recursive+0xaa>
movl   $0xffffffff,-0x20(%rbp)
jmp    1250 <mkdir_recursive+0x110>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x2f,(%rax)
jmp    1178 <mkdir_recursive+0x38>
mov    0x2dd0(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
mov    0x2db5(%rip),%rax
cmpl   $0x0,(%rax)
je     124b <mkdir_recursive+0x10b>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    0x2da4(%rip),%rcx
cmp    (%rcx),%eax
je     124b <mkdir_recursive+0x10b>
movl   $0xffffffff,-0x20(%rbp)
jmp    1250 <mkdir_recursive+0x110>
mov    0x2d81(%rip),%rax
cmpl   $0x0,(%rax)
jne    126c <mkdir_recursive+0x12c>
mov    -0x1c(%rbp),%ecx
mov    0x2d6e(%rip),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_recursive>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
call   1060 <strdup@plt>
mov    %rax,%r14
mov    0x2e81(%rip),%rbp
mov    0x0(%rbp),%r15d
lea    0x1(%rax),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11c8 <mkdir_recursive+0x88>
mov    %rax,%rbx
mov    0x2e68(%rip),%r12
jmp    119c <mkdir_recursive+0x5c>
nopw   0x0(%rax,%rax,1)
movb   $0x2f,(%rbx)
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11c8 <mkdir_recursive+0x88>
movb   $0x0,(%rbx)
movl   $0x0,0x0(%rbp)
mov    %r14,%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
mov    0x0(%rbp),%eax
test   %eax,%eax
je     1180 <mkdir_recursive+0x40>
cmp    (%r12),%eax
je     1180 <mkdir_recursive+0x40>
mov    $0xffffffff,%r14d
jmp    1203 <mkdir_recursive+0xc3>
movl   $0x0,0x0(%rbp)
mov    %r14,%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
mov    0x0(%rbp),%eax
test   %eax,%eax
setne  %cl
mov    0x2df5(%rip),%rdx
cmp    (%rdx),%eax
setne  %dl
and    %cl,%dl
movzbl %dl,%r14d
neg    %r14d
test   %eax,%eax
jne    1201 <mkdir_recursive+0xc1>
mov    %r15d,0x0(%rbp)
xor    %ebx,%ebx
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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