#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 */
int ENOENT;
int ENOTDIR;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int errno;
int lstat(char const *, struct stat *);
int mkdirhier(char const *, int);
int maybe_mkdir(const char *dn, int mode) {
  struct stat st;
  int r;
  r = lstat(dn, &st);
  if (r == 0) {
    if (S_ISDIR(st.st_mode))
      return 0;
    return -ENOTDIR;
  } else if (errno != ENOENT)
    return -errno;
  r = mkdirhier(dn, mode);
  if (r < 0)
    return -errno;
  return 0;
}


x86 assembly (clang, -O0):
<maybe_mkdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <lstat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    118a <maybe_mkdir+0x5a>
mov    -0x18(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1177 <maybe_mkdir+0x47>
movl   $0x0,-0x4(%rbp)
jmp    11ed <maybe_mkdir+0xbd>
xor    %eax,%eax
mov    0x2e78(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11ed <maybe_mkdir+0xbd>
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%eax
je     11b5 <maybe_mkdir+0x85>
xor    %eax,%eax
mov    0x2e2d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11ed <maybe_mkdir+0xbd>
jmp    11ba <maybe_mkdir+0x8a>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdirhier@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11e6 <maybe_mkdir+0xb6>
xor    %eax,%eax
mov    0x2dfc(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11ed <maybe_mkdir+0xbd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<maybe_mkdir>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <lstat@plt>
test   %eax,%eax
je     1179 <maybe_mkdir+0x49>
mov    0x2e86(%rip),%r14
mov    (%r14),%eax
mov    0x2e6c(%rip),%rcx
cmp    (%rcx),%eax
jne    1199 <maybe_mkdir+0x69>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <mkdirhier@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jns    119b <maybe_mkdir+0x6b>
xor    %eax,%eax
sub    (%r14),%eax
jmp    119b <maybe_mkdir+0x6b>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    119b <maybe_mkdir+0x6b>
mov    0x2e65(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    119b <maybe_mkdir+0x6b>
neg    %eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@ENOTDIR = global i32 0
@errno = global i32 0
@ENOENT = global i32 0
define i32 @maybe_mkdir(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = call i32 @lstat(i8* %8, %struct.stat* %6)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %2
%13 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i64 @S_ISDIR(i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
store i32 0, i32* %3
br label %39
18: ; preds = %12
%19 = load i32, i32* @ENOTDIR
%20 = sub nsw i32 0, %19
store i32 %20, i32* %3
br label %39
21: ; preds = %2
%22 = load i32, i32* @errno
%23 = load i32, i32* @ENOENT
%24 = icmp ne i32 %22, %23
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* @errno
%27 = sub nsw i32 0, %26
store i32 %27, i32* %3
br label %39
28: ; preds = %21
br label %29
29: ; preds = %28
%30 = load i8*, i8** %4
%31 = load i32, i32* %5
%32 = call i32 @mkdirhier(i8* %30, i32 %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = load i32, i32* @errno
%37 = sub nsw i32 0, %36
store i32 %37, i32* %3
br label %39
38: ; preds = %29
store i32 0, i32* %3
br label %39
39: ; preds = %38, %35, %25, %18, %17
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@ENOTDIR = global i32 0
@errno = global i32 0
@ENOENT = global i32 0
define i32 @maybe_mkdir(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @lstat(i8* %0, %struct.stat* %3)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %15
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call i64 @S_ISDIR(i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %27
12: ; preds = %7
%13 = load i32, i32* @ENOTDIR
%14 = sub nsw i32 0, %13
br label %27
15: ; preds = %2
%16 = load i32, i32* @errno
%17 = load i32, i32* @ENOENT
%18 = icmp eq i32 %16, %17
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = sub nsw i32 0, %16
br label %27
21: ; preds = %15
%22 = tail call i32 @mkdirhier(i8* %0, i32 %1)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* @errno
%26 = sub nsw i32 0, %25
br label %27
27: ; preds = %21, %7, %24, %19, %12
%28 = phi i32 [ %14, %12 ], [ %20, %19 ], [ %26, %24 ], [ 0, %7 ], [ 0, %21 ]
ret i32 %28
}