#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 /*<<< orphan*/ F_OK;
int /*<<< orphan*/ access(char *, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ free(char *);
scalar_t__ mkdir(char *, int);
char *strchr(char *, char);
int strlen(char *);
char *strndup(char *, int);
int cc_mkdir(char *dir) {
  char *slash;
  char *d;
  char *part;
  d = dir;
  for (;;) {
    slash = strchr(d, '/');
    if (!slash)
      slash = d + strlen(d);
    if (!*slash)
      break;
    part = strndup(dir, slash - dir);
    if (!access(part, F_OK))
      if (mkdir(part, 0777) < 0)
        return -errno;
    free(part);
    d = slash + 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<cc_mkdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b2 <cc_mkdir+0x52>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11c4 <cc_mkdir+0x64>
jmp    1243 <cc_mkdir+0xe3>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
call   1070 <strndup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%eax
jne    1229 <cc_mkdir+0xc9>
mov    -0x28(%rbp),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
jge    1224 <cc_mkdir+0xc4>
xor    %eax,%eax
mov    0x2dc6(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    124a <cc_mkdir+0xea>
jmp    1229 <cc_mkdir+0xc9>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1174 <cc_mkdir+0x14>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cc_mkdir>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e5e(%rip),%r12
mov    %rdi,%r15
jmp    118f <cc_mkdir+0x2f>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <free@plt>
add    $0x1,%rbx
mov    %rbx,%r15
mov    %r15,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b2 <cc_mkdir+0x52>
mov    %r15,%rdi
call   1050 <strlen@plt>
movslq %eax,%rbx
add    %r15,%rbx
cmpb   $0x0,(%rbx)
je     11f6 <cc_mkdir+0x96>
mov    %ebx,%esi
sub    %r14d,%esi
mov    %r14,%rdi
call   1070 <strndup@plt>
mov    %rax,%r15
mov    (%r12),%esi
mov    %rax,%rdi
call   1080 <access@plt>
test   %eax,%eax
jne    1180 <cc_mkdir+0x20>
mov    %r15,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
jns    1180 <cc_mkdir+0x20>
mov    0x2df0(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    11f8 <cc_mkdir+0x98>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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