#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*/ parent;
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ errno;
scalar_t__ mkdir(char const *, int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int mkdirp(const char *pathname, mode_t mode) {
  char parent[512], *p;
  strncpy(parent, pathname, sizeof(parent));
  parent[sizeof(parent) - 1] = '\0';
  for (p = parent + strlen(parent); *p != '/' && p != parent; p--)
    ;
  *p = '\0';
  if (p != parent && mkdirp(parent, mode) != 0)
    return -1;
  if (mkdir(pathname, mode) == 0)
    return 0;
  if (errno == EEXIST)
    return 0;
  return -1;
}


x86 assembly (clang, -O0):
<mkdirp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x21(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x230(%rbp)
lea    -0x220(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x230(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x228(%rbp)
mov    -0x228(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x2f,%ecx
mov    %al,-0x231(%rbp)
je     11d0 <mkdirp+0x90>
mov    -0x228(%rbp),%rax
lea    -0x220(%rbp),%rcx
cmp    %rcx,%rax
setne  %al
mov    %al,-0x231(%rbp)
mov    -0x231(%rbp),%al
test   $0x1,%al
jne    11e3 <mkdirp+0xa3>
jmp    11ff <mkdirp+0xbf>
jmp    11e8 <mkdirp+0xa8>
mov    -0x228(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x228(%rbp)
jmp    119b <mkdirp+0x5b>
mov    -0x228(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x228(%rbp),%rax
lea    -0x220(%rbp),%rcx
cmp    %rcx,%rax
je     1244 <mkdirp+0x104>
lea    -0x220(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <mkdirp@plt>
cmp    $0x0,%eax
je     1244 <mkdirp+0x104>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <mkdirp+0x153>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
jne    1266 <mkdirp+0x126>
movl   $0x0,-0x4(%rbp)
jmp    1293 <mkdirp+0x153>
mov    0x2d6b(%rip),%rax
mov    (%rax),%rax
mov    0x2d69(%rip),%rcx
cmp    (%rcx),%rax
jne    128c <mkdirp+0x14c>
movl   $0x0,-0x4(%rbp)
jmp    1293 <mkdirp+0x153>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdirp>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x1ff(%rsp)
mov    %rbp,%rdi
call   1050 <strlen@plt>
movslq %eax,%rcx
nopw   0x0(%rax,%rax,1)
mov    %rcx,%rax
test   %rcx,%rcx
je     1192 <mkdirp+0x52>
lea    -0x1(%rax),%rcx
cmpb   $0x2f,(%rsp,%rax,1)
jne    1180 <mkdirp+0x40>
movb   $0x0,(%rsp,%rax,1)
test   %rax,%rax
je     11b1 <mkdirp+0x71>
mov    %rsp,%rdi
mov    %r14d,%esi
call   1060 <mkdirp@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
jne    11e0 <mkdirp+0xa0>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     11de <mkdirp+0x9e>
mov    0x2e10(%rip),%rax
mov    (%rax),%rcx
mov    0x2e0e(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
setne  %al
neg    %eax
jmp    11e0 <mkdirp+0xa0>
xor    %eax,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EEXIST = global i64 0
define i32 @mkdirp(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [512 x i8]
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%9 = load i8*, i8** %4
%10 = call i32 @strncpy(i8* %8, i8* %9, i32 512)
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 511
store i8 0, i8* %11
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%14 = call i32 @strlen(i8* %13)
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %12, i64 %15
store i8* %16, i8** %7
br label %17
17: ; preds = %29, %2
%18 = load i8*, i8** %7
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 47
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i8*, i8** %7
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%25 = icmp ne i8* %23, %24
br label %26
26: ; preds = %22, %17
%27 = phi i1 [ false, %17 ], [ %25, %22 ]
br i1 %27, label %28, label %32
28: ; preds = %26
br label %29
29: ; preds = %28
%30 = load i8*, i8** %7
%31 = getelementptr inbounds i8, i8* %30, i32 -1
store i8* %31, i8** %7
br label %17
32: ; preds = %26
%33 = load i8*, i8** %7
store i8 0, i8* %33
%34 = load i8*, i8** %7
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%36 = icmp ne i8* %34, %35
br i1 %36, label %37, label %43
37: ; preds = %32
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%39 = load i32, i32* %5
%40 = call i32 @mkdirp(i8* %38, i32 %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %37
store i32 -1, i32* %3
br label %55
43: ; preds = %37, %32
%44 = load i8*, i8** %4
%45 = load i32, i32* %5
%46 = call i64 @mkdir(i8* %44, i32 %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %43
store i32 0, i32* %3
br label %55
49: ; preds = %43
%50 = load i64, i64* @errno
%51 = load i64, i64* @EEXIST
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %49
store i32 0, i32* %3
br label %55
54: ; preds = %49
store i32 -1, i32* %3
br label %55
55: ; preds = %54, %53, %48, %42
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@errno = global i64 0
@EEXIST = global i64 0
define i32 @mkdirp(i8* %0, i32 %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = call i32 @strncpy(i8* %4, i8* %0, i32 512)
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 511
store i8 0, i8* %6
%7 = call i32 @strlen(i8* %4)
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %8
br label %10
10: ; preds = %10, %2
%11 = phi i8* [ %9, %2 ], [ %16, %10 ]
%12 = load i8, i8* %11
%13 = icmp ne i8 %12, 47
%14 = icmp ne i8* %11, %4
%15 = and i1 %14, %13
%16 = getelementptr inbounds i8, i8* %11, i64 -1
br i1 %15, label %10, label %17
17: ; preds = %10
store i8 0, i8* %11
br i1 %14, label %18, label %21
18: ; preds = %17
%19 = call i32 @mkdirp(i8* %4, i32 %1)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %18, %17
%22 = call i64 @mkdir(i8* %0, i32 %1)
%23 = icmp eq i64 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = load i64, i64* @EEXIST
%27 = icmp ne i64 %25, %26
%28 = sext i1 %27 to i32
br label %29
29: ; preds = %24, %21, %18
%30 = phi i32 [ -1, %18 ], [ 0, %21 ], [ %28, %24 ]
ret i32 %30
}