#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*/ mode_t;

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ ENOENT;
char *dirname(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
int mkdir(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strdup(char const *);
int mkdirp(const char *pathname, mode_t mode) {
  char *dname = dirname(strdup(pathname));
  fprintf(stderr, "MKDIRP trying to create directory %s\n", dname);
  if (mkdir(dname, mode)) {
    if (errno == EEXIST) {
      free(dname);
      return 0;
    } else if (errno == ENOENT) {
      if (mkdirp(dname, mode)) {
        goto error;
      }
      return mkdir(dname, mode);
    } else
      goto error;
  } else {
    free(dname);
    return 0;
  }
error:
  free(dname);
  return -1;
}


x86 assembly (clang, -O0):
<mkdirp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %eax,%edi
call   1050 <dirname@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe69(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0x0,%eax
je     122d <mkdirp+0xcd>
mov    0x2e18(%rip),%rax
mov    (%rax),%rax
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%rax
jne    11e0 <mkdirp+0x80>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1252 <mkdirp+0xf2>
mov    0x2de9(%rip),%rax
mov    (%rax),%rax
mov    0x2dcf(%rip),%rcx
cmp    (%rcx),%rax
jne    1228 <mkdirp+0xc8>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <mkdirp@plt>
cmp    $0x0,%eax
je     1214 <mkdirp+0xb4>
jmp    1242 <mkdirp+0xe2>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
mov    %eax,-0x4(%rbp)
jmp    1252 <mkdirp+0xf2>
jmp    1242 <mkdirp+0xe2>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1252 <mkdirp+0xf2>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdirp>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
call   1080 <strdup@plt>
mov    %eax,%edi
call   1050 <dirname@plt>
mov    %rax,%rbx
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
lea    0xe7c(%rip),%rsi
mov    %rbx,%rdx
call   1070 <fprintf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <mkdir@plt>
test   %eax,%eax
je     11b0 <mkdirp+0x50>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rax
mov    0x2e35(%rip),%rcx
cmp    (%rcx),%rax
jne    11bc <mkdirp+0x5c>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    11e3 <mkdirp+0x83>
mov    0x2dfd(%rip),%rcx
cmp    (%rcx),%rax
jne    11d6 <mkdirp+0x76>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <mkdirp@plt>
test   %eax,%eax
je     11ea <mkdirp+0x8a>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    %rbx,%rdi
mov    %ebp,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <mkdir@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [38 x i8] c"MKDIRP trying to create directory %s\0A\00"
@errno = global i64 0
@EEXIST = global i64 0
@ENOENT = global i64 0
define i32 @mkdirp(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = call i32 @strdup(i8* %7)
%9 = call i8* @dirname(i32 %8)
store i8* %9, i8** %6
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %6
%12 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = load i8*, i8** %6
%14 = load i32, i32* %5
%15 = call i32 @mkdir(i8* %13, i32 %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %39
17: ; preds = %2
%18 = load i64, i64* @errno
%19 = load i64, i64* @EEXIST
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i8*, i8** %6
%23 = call i32 @free(i8* %22)
store i32 0, i32* %3
br label %45
24: ; preds = %17
%25 = load i64, i64* @errno
%26 = load i64, i64* @ENOENT
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %38
28: ; preds = %24
%29 = load i8*, i8** %6
%30 = load i32, i32* %5
%31 = call i32 @mkdirp(i8* %29, i32 %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %28
br label %42
34: ; preds = %28
%35 = load i8*, i8** %6
%36 = load i32, i32* %5
%37 = call i32 @mkdir(i8* %35, i32 %36)
store i32 %37, i32* %3
br label %45
38: ; preds = %24
br label %42
39: ; preds = %2
%40 = load i8*, i8** %6
%41 = call i32 @free(i8* %40)
store i32 0, i32* %3
br label %45
42: ; preds = %38, %33
%43 = load i8*, i8** %6
%44 = call i32 @free(i8* %43)
store i32 -1, i32* %3
br label %45
45: ; preds = %42, %39, %34, %21
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [38 x i8] c"MKDIRP trying to create directory %s\0A\00"
@errno = global i64 0
@EEXIST = global i64 0
@ENOENT = global i64 0
define i32 @mkdirp(i8* %0, i32 %1) {
%3 = tail call i32 @strdup(i8* %0)
%4 = tail call i8* @dirname(i32 %3)
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %4)
%7 = tail call i32 @mkdir(i8* %4, i32 %1)
%8 = icmp eq i32 %7, 0
br i1 %8, label %23, label %9
9: ; preds = %2
%10 = load i64, i64* @errno
%11 = load i64, i64* @EEXIST
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @free(i8* %4)
br label %27
15: ; preds = %9
%16 = load i64, i64* @ENOENT
%17 = icmp eq i64 %10, %16
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = tail call i32 @mkdirp(i8* %4, i32 %1)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = tail call i32 @mkdir(i8* %4, i32 %1)
br label %27
23: ; preds = %2
%24 = tail call i32 @free(i8* %4)
br label %27
25: ; preds = %15, %18
%26 = tail call i32 @free(i8* %4)
br label %27
27: ; preds = %25, %23, %21, %13
%28 = phi i32 [ 0, %13 ], [ -1, %25 ], [ %22, %21 ], [ 0, %23 ]
ret i32 %28
}