#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 /*<<< orphan*/ assert(char const *);
int errno;
int mkdir(char *, int);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
int /*<<< orphan*/ strlen(char const *);
int mkdir_p(const char *path) {
  char *p;
  char t[strlen(path) + 1];
  assert(path);
  strcpy(t, path);
  for (p = strchr(t, '/'); p && *p; p = strchr(p + 1, '/')) {
    int r;
    *p = 0;
    r = mkdir(t, 0777);
    if (r < 0 && errno != EEXIST)
      return -errno;
    *p = '/';
  }
  return 0;
}


x86 assembly (clang, -O0):
<mkdir_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <assert@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x38(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x39(%rbp)
je     11d3 <mkdir_p+0x83>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11e3 <mkdir_p+0x93>
jmp    1259 <mkdir_p+0x109>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1237 <mkdir_p+0xe7>
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    0x2dcb(%rip),%rcx
cmp    (%rcx),%eax
je     1237 <mkdir_p+0xe7>
xor    %eax,%eax
mov    0x2db2(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    1267 <mkdir_p+0x117>
mov    -0x18(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,-0x18(%rbp)
jmp    11b3 <mkdir_p+0x63>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_p>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %rbx,%rdi
call   1070 <assert@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     1204 <mkdir_p+0xb4>
mov    %rax,%rbx
mov    0x2e2c(%rip),%r12
mov    0x2e2d(%rip),%r13
jmp    11dc <mkdir_p+0x8c>
cs nopw 0x0(%rax,%rax,1)
nop
movb   $0x2f,(%rbx)
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1204 <mkdir_p+0xb4>
cmpb   $0x0,(%rbx)
je     1204 <mkdir_p+0xb4>
movb   $0x0,(%rbx)
mov    %r15,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
test   %eax,%eax
jns    11c0 <mkdir_p+0x70>
mov    (%r12),%eax
cmp    0x0(%r13),%eax
je     11c0 <mkdir_p+0x70>
neg    %eax
mov    %eax,%r14d
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EEXIST = global i32 0
define i32 @mkdir_p(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %5
%14 = alloca i8, i64 %12
store i64 %12, i64* %6
%15 = load i8*, i8** %3
%16 = call i32 @assert(i8* %15)
%17 = load i8*, i8** %3
%18 = call i32 @strcpy(i8* %14, i8* %17)
%19 = call i8* @strchr(i8* %14, i8 signext 47)
store i8* %19, i8** %4
br label %20
20: ; preds = %44, %1
%21 = load i8*, i8** %4
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i8*, i8** %4
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 0
br label %28
28: ; preds = %23, %20
%29 = phi i1 [ false, %20 ], [ %27, %23 ]
br i1 %29, label %30, label %48
30: ; preds = %28
%31 = load i8*, i8** %4
store i8 0, i8* %31
%32 = call i32 @mkdir(i8* %14, i32 511)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %42
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 %42
39: ; preds = %35
%40 = load i32, i32* @errno
%41 = sub nsw i32 0, %40
store i32 %41, i32* %2
store i32 1, i32* %8
br label %49
42: ; preds = %35, %30
%43 = load i8*, i8** %4
store i8 47, i8* %43
br label %44
44: ; preds = %42
%45 = load i8*, i8** %4
%46 = getelementptr inbounds i8, i8* %45, i64 1
%47 = call i8* @strchr(i8* %46, i8 signext 47)
store i8* %47, i8** %4
br label %20
48: ; preds = %28
store i32 0, i32* %2
store i32 1, i32* %8
br label %49
49: ; preds = %48, %39
%50 = load i8*, i8** %5
%51 = load i32, i32* %2
ret i32 %51
}

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