#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
char *dirname(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ mkdir(char *, int);
scalar_t__ stat(char *, struct stat *);
char *strdup(char *);
int strlen(char *);
int mkdir_recursive(char *path) {
  char *subpath;
  char *scratchpath;
  struct stat st = {0};
  int rvalue = 0;
  if (!((stat(path, &st) == 0) && S_ISDIR(st.st_mode))) {
    scratchpath = strdup(path);
    if (!scratchpath) {
      errno = ENOMEM;
      rvalue = -1;
    } else {
      subpath = dirname(scratchpath);
      if (strlen(subpath) > 1) {
        rvalue = mkdir_recursive(subpath);
      }
      if (rvalue == 0) {
        mkdir(path, 0777);
      }
      free(scratchpath);
    }
  }
  return rvalue;
}


x86 assembly (clang, -O0):
<mkdir_recursive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jne    11dc <mkdir_recursive+0x4c>
mov    -0x20(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
jne    1263 <mkdir_recursive+0xd3>
mov    -0x8(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1212 <mkdir_recursive+0x82>
mov    0x2de5(%rip),%rax
mov    (%rax),%ecx
mov    0x2dd4(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x24(%rbp)
jmp    125e <mkdir_recursive+0xce>
mov    -0x18(%rbp),%rdi
call   1070 <dirname@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x1,%eax
jle    123d <mkdir_recursive+0xad>
mov    -0x10(%rbp),%rdi
call   10b0 <mkdir_recursive@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1255 <mkdir_recursive+0xc5>
mov    -0x8(%rbp),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
jmp    1263 <mkdir_recursive+0xd3>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_recursive>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
je     11f2 <mkdir_recursive+0x72>
mov    %r14,%rdi
call   1090 <strdup@plt>
test   %rax,%rax
je     1204 <mkdir_recursive+0x84>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <dirname@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <strlen@plt>
cmp    $0x2,%eax
jl     11d9 <mkdir_recursive+0x59>
mov    %rbp,%rdi
call   10a0 <mkdir_recursive@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11e8 <mkdir_recursive+0x68>
mov    %r14,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    121b <mkdir_recursive+0x9b>
mov    0x8(%rsp),%edi
call   1060 <S_ISDIR@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    121b <mkdir_recursive+0x9b>
jmp    11a3 <mkdir_recursive+0x23>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    0x2dc4(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @mkdir_recursive(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca i32
store i8* %0, i8** %2
%7 = bitcast %struct.stat* %5 to i8*
store i32 0, i32* %6
%8 = load i8*, i8** %2
%9 = call i64 @stat(i8* %8, %struct.stat* %5)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i64 @S_ISDIR(i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %42, label %16
16: ; preds = %11, %1
%17 = load i8*, i8** %2
%18 = call i8* @strdup(i8* %17)
store i8* %18, i8** %4
%19 = load i8*, i8** %4
%20 = icmp ne i8* %19, null
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = load i32, i32* @ENOMEM
store i32 %22, i32* @errno
store i32 -1, i32* %6
br label %41
23: ; preds = %16
%24 = load i8*, i8** %4
%25 = call i8* @dirname(i8* %24)
store i8* %25, i8** %3
%26 = load i8*, i8** %3
%27 = call i32 @strlen(i8* %26)
%28 = icmp sgt i32 %27, 1
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i8*, i8** %3
%31 = call i32 @mkdir_recursive(i8* %30)
store i32 %31, i32* %6
br label %32
32: ; preds = %29, %23
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i8*, i8** %2
%37 = call i32 @mkdir(i8* %36, i32 511)
br label %38
38: ; preds = %35, %32
%39 = load i8*, i8** %4
%40 = call i32 @free(i8* %39)
br label %41
41: ; preds = %38, %21
br label %42
42: ; preds = %41, %11
%43 = load i32, i32* %6
ret i32 %43
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @mkdir_recursive(i8* %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to %struct.stat*
%4 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = bitcast i64* %2 to i32*
%9 = load i32, i32* %8
%10 = call i64 @S_ISDIR(i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %29
12: ; preds = %7, %1
%13 = call i8* @strdup(i8* %0)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = load i32, i32* @ENOMEM
store i32 %16, i32* @errno
br label %29
17: ; preds = %12
%18 = call i8* @dirname(i8* %13)
%19 = call i32 @strlen(i8* %18)
%20 = icmp sgt i32 %19, 1
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @mkdir_recursive(i8* %18)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %17, %21
%25 = call i32 @mkdir(i8* %0, i32 511)
br label %26
26: ; preds = %24, %21
%27 = phi i32 [ 0, %24 ], [ %22, %21 ]
%28 = call i32 @free(i8* %13)
br label %29
29: ; preds = %15, %26, %7
%30 = phi i32 [ 0, %7 ], [ %27, %26 ], [ -1, %15 ]
ret i32 %30
}