#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;
};

/* Variables and functions */
#define EEXIST 129
#define ENOENT 128
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ create_parent_directory_hierarchy(char const *, int);
int errno;
int mkdir(char const *, int);
int /*<<< orphan*/ stat(char const *, struct stat *);
int mkdir_recursive(const char *pathname, int mode) {
  if (mkdir(pathname, mode) == 0)
    return 0;
  switch (errno) {
  case ENOENT:
    create_parent_directory_hierarchy(pathname, mode);
    return mkdir(pathname, mode);
  case EEXIST: {
    struct stat target;
    if ((stat(pathname, &target) == 0) && S_ISDIR(target.st_mode))
      return 0;
  }
  }
  return -1;
}


x86 assembly (clang, -O0):
<mkdir_recursive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%eax
jne    1170 <mkdir_recursive+0x30>
movl   $0x0,-0x4(%rbp)
jmp    11fe <mkdir_recursive+0xbe>
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x80,%eax
je     119f <mkdir_recursive+0x5f>
jmp    118c <mkdir_recursive+0x4c>
mov    -0x1c(%rbp),%eax
sub    $0x81,%eax
je     11bf <mkdir_recursive+0x7f>
jmp    11f7 <mkdir_recursive+0xb7>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <create_parent_directory_hierarchy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <mkdir@plt>
mov    %eax,-0x4(%rbp)
jmp    11fe <mkdir_recursive+0xbe>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%eax
jne    11f2 <mkdir_recursive+0xb2>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
je     11f2 <mkdir_recursive+0xb2>
movl   $0x0,-0x4(%rbp)
jmp    11fe <mkdir_recursive+0xbe>
jmp    11f7 <mkdir_recursive+0xb7>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_recursive>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <mkdir@plt>
xor    %r14d,%r14d
test   %eax,%eax
je     11b3 <mkdir_recursive+0x73>
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    $0xffffffff,%r14d
cmp    $0x81,%eax
je     1192 <mkdir_recursive+0x52>
cmp    $0x80,%eax
jne    11b3 <mkdir_recursive+0x73>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <create_parent_directory_hierarchy@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <mkdir@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %eax,%eax
jne    11b3 <mkdir_recursive+0x73>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %eax,%eax
je     11b3 <mkdir_recursive+0x73>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i32 0
define i32 @mkdir_recursive(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = load i32, i32* %5
%9 = call i32 @mkdir(i8* %7, i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 0, i32* %3
br label %33
12: ; preds = %2
%13 = load i32, i32* @errno
switch i32 %13, label %32 [
i32 128, label %14
i32 129, label %21
]
14: ; preds = %12
%15 = load i8*, i8** %4
%16 = load i32, i32* %5
%17 = call i32 @create_parent_directory_hierarchy(i8* %15, i32 %16)
%18 = load i8*, i8** %4
%19 = load i32, i32* %5
%20 = call i32 @mkdir(i8* %18, i32 %19)
store i32 %20, i32* %3
br label %33
21: ; preds = %12
%22 = load i8*, i8** %4
%23 = call i32 @stat(i8* %22, %struct.stat* %6)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @S_ISDIR(i32 %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %25
store i32 0, i32* %3
br label %33
31: ; preds = %25, %21
br label %32
32: ; preds = %31, %12
store i32 -1, i32* %3
br label %33
33: ; preds = %32, %30, %14, %11
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i32 0
define i32 @mkdir_recursive(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = tail call i32 @mkdir(i8* %0, i32 %1)
%5 = icmp eq i32 %4, 0
br i1 %5, label %22, label %6
6: ; preds = %2
%7 = load i32, i32* @errno
switch i32 %7, label %22 [
i32 128, label %8
i32 129, label %11
]
8: ; preds = %6
%9 = tail call i32 @create_parent_directory_hierarchy(i8* %0, i32 %1)
%10 = tail call i32 @mkdir(i8* %0, i32 %1)
br label %22
11: ; preds = %6
%12 = bitcast %struct.stat* %3 to i8*
%13 = call i32 @stat(i8* %0, %struct.stat* %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i32 @S_ISDIR(i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %11, %15
br label %22
21: ; preds = %15
br label %22
22: ; preds = %6, %20, %21, %2, %8
%23 = phi i32 [ 0, %21 ], [ %10, %8 ], [ 0, %2 ], [ -1, %20 ], [ -1, %6 ]
ret i32 %23
}