#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 */
scalar_t__ EEXIST;
scalar_t__ ENOENT;
scalar_t__ errno;
int mkdir(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
char *strrchr(char *, char);
int mkdir_p(char *pathname) {
  char pbuff[200];
  char *p;
  if (mkdir(pathname, 0755)) {
    if (errno == EEXIST) {
      return 0;
    }
    if (errno == ENOENT) {
      snprintf(pbuff, 200, "%s", pathname);
      p = strrchr(pbuff, '/');
      if (p) {
        *(p) = '\0';
        mkdir_p(pbuff);
        return mkdir(pathname, 0777);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<mkdir_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%eax
je     121e <mkdir_p+0xde>
mov    0x2e6b(%rip),%rax
mov    (%rax),%rax
mov    0x2e69(%rip),%rcx
cmp    (%rcx),%rax
jne    118c <mkdir_p+0x4c>
movl   $0x0,-0x4(%rbp)
jmp    1225 <mkdir_p+0xe5>
mov    0x2e45(%rip),%rax
mov    (%rax),%rax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
jne    1219 <mkdir_p+0xd9>
lea    -0xe0(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xc8,%esi
lea    0xe43(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0xe0(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0xe8(%rbp)
cmpq   $0x0,-0xe8(%rbp)
je     1214 <mkdir_p+0xd4>
mov    -0xe8(%rbp),%rax
movb   $0x0,(%rax)
lea    -0xe0(%rbp),%rdi
call   1060 <mkdir_p@plt>
mov    -0x10(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
mov    %eax,-0x4(%rbp)
jmp    1225 <mkdir_p+0xe5>
jmp    1219 <mkdir_p+0xd9>
jmp    121e <mkdir_p+0xde>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_p>:
push   %rbp
push   %r14
push   %rbx
sub    $0xd0,%rsp
mov    %rdi,%rbx
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     11c6 <mkdir_p+0x86>
mov    0x2e73(%rip),%rax
mov    (%rax),%rax
mov    0x2e71(%rip),%rcx
cmp    (%rcx),%rax
je     11c6 <mkdir_p+0x86>
mov    0x2e4d(%rip),%rcx
cmp    (%rcx),%rax
jne    11c6 <mkdir_p+0x86>
lea    0xe79(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc8,%esi
mov    %rbx,%rcx
call   1040 <snprintf@plt>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11c6 <mkdir_p+0x86>
movb   $0x0,(%rax)
mov    %rsp,%rdi
call   1060 <mkdir_p@plt>
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0xd0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EEXIST = global i64 0
@ENOENT = global i64 0
@.str = [3 x i8] c"%s\00"
define i32 @mkdir_p(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [200 x i8]
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @mkdir(i8* %6, i32 493)
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %34
9: ; preds = %1
%10 = load i64, i64* @errno
%11 = load i64, i64* @EEXIST
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %14
13: ; preds = %9
store i32 0, i32* %2
br label %35
14: ; preds = %9
%15 = load i64, i64* @errno
%16 = load i64, i64* @ENOENT
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %33
18: ; preds = %14
%19 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
%20 = load i8*, i8** %3
%21 = call i32 @snprintf(i8* %19, i32 200, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
%23 = call i8* @strrchr(i8* %22, i8 signext 47)
store i8* %23, i8** %5
%24 = load i8*, i8** %5
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %32
26: ; preds = %18
%27 = load i8*, i8** %5
store i8 0, i8* %27
%28 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
%29 = call i32 @mkdir_p(i8* %28)
%30 = load i8*, i8** %3
%31 = call i32 @mkdir(i8* %30, i32 511)
store i32 %31, i32* %2
br label %35
32: ; preds = %18
br label %33
33: ; preds = %32, %14
br label %34
34: ; preds = %33, %1
store i32 0, i32* %2
br label %35
35: ; preds = %34, %26, %13
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
@errno = global i64 0
@EEXIST = global i64 0
@ENOENT = global i64 0
@.str = [3 x i8] c"%s\00"
define i32 @mkdir_p(i8* %0) {
%2 = alloca [200 x i8]
%3 = getelementptr inbounds [200 x i8], [200 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @mkdir(i8* %0, i32 493)
%5 = icmp eq i32 %4, 0
br i1 %5, label %20, label %6
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @EEXIST
%9 = icmp ne i64 %7, %8
%10 = load i64, i64* @ENOENT
%11 = icmp eq i64 %7, %10
%12 = select i1 %9, i1 %11, i1 false
br i1 %12, label %13, label %20
13: ; preds = %6
%14 = call i32 @snprintf(i8* %3, i32 200, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%15 = call i8* @strrchr(i8* %3, i8 signext 47)
%16 = icmp eq i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %13
store i8 0, i8* %15
%18 = call i32 @mkdir_p(i8* %3)
%19 = call i32 @mkdir(i8* %0, i32 511)
br label %20
20: ; preds = %1, %13, %6, %17
%21 = phi i32 [ %19, %17 ], [ 0, %6 ], [ 0, %13 ], [ 0, %1 ]
ret i32 %21
}