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

/* Variables and functions */
scalar_t__ EINVAL;
scalar_t__ ENOENT;
scalar_t__ ENOTDIR;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
char *dirname(char *);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memset(struct stat *, int /*<<< orphan*/, int);
int mkdir(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stat(char const *, struct stat *);
char *strdup(char const *);
int sdb_mkdir_all(const char *pathname, mode_t mode) {
  struct stat st;
  char *pathname_copy;
  char *base_dir;
  int status = 0;
  if ((!pathname) || (!*pathname)) {
    errno = EINVAL;
    return -1;
  }
  memset(&st, 0, sizeof(st));
  if (!stat(pathname, &st)) {
    if (!S_ISDIR(st.st_mode)) {
      errno = ENOTDIR;
      return -1;
    }
    return 0;
  }
  if (errno != ENOENT)
    return -1;
  pathname_copy = strdup(pathname);
  if (!pathname_copy)
    return -1;
  base_dir = dirname(pathname_copy);
  status = sdb_mkdir_all(base_dir, mode);
  if (!status)
    status = mkdir(pathname, mode);
  free(pathname_copy);
  return status;
}


x86 assembly (clang, -O0):
<sdb_mkdir_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ae <sdb_mkdir_all+0x2e>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11ce <sdb_mkdir_all+0x4e>
mov    0x2e1b(%rip),%rax
mov    (%rax),%rcx
mov    0x2e19(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12bf <sdb_mkdir_all+0x13f>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%eax
jne    1231 <sdb_mkdir_all+0xb1>
mov    -0x18(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1225 <sdb_mkdir_all+0xa5>
mov    0x2dec(%rip),%rax
mov    (%rax),%rcx
mov    0x2dc2(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12bf <sdb_mkdir_all+0x13f>
movl   $0x0,-0x4(%rbp)
jmp    12bf <sdb_mkdir_all+0x13f>
mov    0x2da0(%rip),%rax
mov    (%rax),%rax
mov    0x2d7e(%rip),%rcx
cmp    (%rcx),%rax
je     1257 <sdb_mkdir_all+0xd7>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bf <sdb_mkdir_all+0x13f>
mov    -0x10(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    127b <sdb_mkdir_all+0xfb>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bf <sdb_mkdir_all+0x13f>
mov    -0x20(%rbp),%rdi
call   1060 <dirname@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <sdb_mkdir_all@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    12b0 <sdb_mkdir_all+0x130>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sdb_mkdir_all>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1221 <sdb_mkdir_all+0xa1>
mov    %rdi,%rbp
cmpb   $0x0,(%rdi)
je     1221 <sdb_mkdir_all+0xa1>
mov    %esi,%r14d
mov    %rsp,%r15
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1080 <stat@plt>
test   %eax,%eax
je     1247 <sdb_mkdir_all+0xc7>
mov    0x2e0b(%rip),%rax
mov    (%rax),%rax
mov    0x2de9(%rip),%rcx
mov    $0xffffffff,%ebx
cmp    (%rcx),%rax
jne    123a <sdb_mkdir_all+0xba>
mov    %rbp,%rdi
call   10a0 <strdup@plt>
test   %rax,%rax
je     123a <sdb_mkdir_all+0xba>
mov    %rax,%r15
mov    %rax,%rdi
call   1060 <dirname@plt>
mov    %rax,%rdi
mov    %r14d,%esi
call   1090 <sdb_mkdir_all@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1217 <sdb_mkdir_all+0x97>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1040 <mkdir@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1030 <free@plt>
jmp    123a <sdb_mkdir_all+0xba>
mov    0x2da8(%rip),%rax
mov    (%rax),%rax
mov    0x2da6(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
jne    123a <sdb_mkdir_all+0xba>
mov    0x2d9e(%rip),%rax
jmp    1228 <sdb_mkdir_all+0xa8>

LLVM IR (-O0):
%struct.stat = type { i32 }
@EINVAL = global i64 0
@errno = global i64 0
@ENOTDIR = global i64 0
@ENOENT = global i64 0
define i32 @sdb_mkdir_all(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %9
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i8, i8* %13
%15 = icmp ne i8 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %12, %2
%17 = load i64, i64* @EINVAL
store i64 %17, i64* @errno
store i32 -1, i32* %3
br label %58
18: ; preds = %12
%19 = call i32 @memset(%struct.stat* %6, i32 0, i32 4)
%20 = load i8*, i8** %4
%21 = call i32 @stat(i8* %20, %struct.stat* %6)
%22 = icmp ne i32 %21, 0
br i1 %22, label %31, label %23
23: ; preds = %18
%24 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @S_ISDIR(i32 %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %23
%29 = load i64, i64* @ENOTDIR
store i64 %29, i64* @errno
store i32 -1, i32* %3
br label %58
30: ; preds = %23
store i32 0, i32* %3
br label %58
31: ; preds = %18
%32 = load i64, i64* @errno
%33 = load i64, i64* @ENOENT
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 -1, i32* %3
br label %58
36: ; preds = %31
%37 = load i8*, i8** %4
%38 = call i8* @strdup(i8* %37)
store i8* %38, i8** %7
%39 = load i8*, i8** %7
%40 = icmp ne i8* %39, null
br i1 %40, label %42, label %41
41: ; preds = %36
store i32 -1, i32* %3
br label %58
42: ; preds = %36
%43 = load i8*, i8** %7
%44 = call i8* @dirname(i8* %43)
store i8* %44, i8** %8
%45 = load i8*, i8** %8
%46 = load i32, i32* %5
%47 = call i32 @sdb_mkdir_all(i8* %45, i32 %46)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = icmp ne i32 %48, 0
br i1 %49, label %54, label %50
50: ; preds = %42
%51 = load i8*, i8** %4
%52 = load i32, i32* %5
%53 = call i32 @mkdir(i8* %51, i32 %52)
store i32 %53, i32* %9
br label %54
54: ; preds = %50, %42
%55 = load i8*, i8** %7
%56 = call i32 @free(i8* %55)
%57 = load i32, i32* %9
store i32 %57, i32* %3
br label %58
58: ; preds = %54, %41, %35, %30, %28, %16
%59 = load i32, i32* %3
ret i32 %59
}

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