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

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ mkdirp(char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int dir_create_check_p(char *dirname) {
  struct stat sb;
  if (stat(dirname, &sb) == 0 && S_ISDIR(sb.st_mode)) {
    printf("YES\n");
  } else {
    printf("NO\n");
    DIR *dir = opendir(dirname);
    if (dir) {
      closedir(dir);
    } else if (ENOENT == errno) {
      mkdirp(dirname);
    } else {
      fprintf(stderr, "%s directory cannot open\n", dirname);
    }
  }
}


x86 assembly (clang, -O0):
<dir_create_check_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jne    11b6 <dir_create_check_p+0x46>
mov    -0x18(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11b6 <dir_create_check_p+0x46>
lea    0xe54(%rip),%rdi
call   1040 <printf@plt>
jmp    1233 <dir_create_check_p+0xc3>
lea    0xe48(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11e8 <dir_create_check_p+0x78>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
jmp    122e <dir_create_check_p+0xbe>
mov    0x2dd9(%rip),%rax
mov    (%rax),%rax
mov    0x2ddf(%rip),%rcx
cmp    (%rcx),%rax
jne    1210 <dir_create_check_p+0xa0>
mov    -0x10(%rbp),%rdi
call   1060 <mkdirp@plt>
jmp    1229 <dir_create_check_p+0xb9>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xde5(%rip),%rsi
call   1080 <fprintf@plt>
jmp    122e <dir_create_check_p+0xbe>
jmp    1233 <dir_create_check_p+0xc3>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dir_create_check_p>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1090 <stat@plt>
test   %rax,%rax
je     11aa <dir_create_check_p+0x3a>
lea    0xe8c(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11c6 <dir_create_check_p+0x56>
mov    %rax,%rdi
call   1070 <closedir@plt>
jmp    11fe <dir_create_check_p+0x8e>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %rax,%rax
je     1187 <dir_create_check_p+0x17>
lea    0xe5e(%rip),%rdi
call   1030 <puts@plt>
jmp    11fe <dir_create_check_p+0x8e>
mov    0x2dfb(%rip),%rax
mov    (%rax),%rax
mov    0x2e01(%rip),%rcx
cmp    (%rcx),%rax
jne    11e6 <dir_create_check_p+0x76>
mov    %rbx,%rdi
call   1060 <mkdirp@plt>
jmp    11fe <dir_create_check_p+0x8e>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"YES\0A\00"
@.str.1 = [4 x i8] c"NO\0A\00"
@ENOENT = global i64 0
@errno = global i64 0
@stderr = global i32 0
@.str.2 = [26 x i8] c"%s directory cannot open\0A\00"
define i32 @dir_create_check_p(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i64 @stat(i8* %6, %struct.stat* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%11 = load i32, i32* %10
%12 = call i64 @S_ISDIR(i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %38
16: ; preds = %9, %1
%17 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%18 = load i8*, i8** %3
%19 = call i32* @opendir(i8* %18)
store i32* %19, i32** %5
%20 = load i32*, i32** %5
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = load i32*, i32** %5
%24 = call i32 @closedir(i32* %23)
br label %37
25: ; preds = %16
%26 = load i64, i64* @ENOENT
%27 = load i64, i64* @errno
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i8*, i8** %3
%31 = call i32 @mkdirp(i8* %30)
br label %36
32: ; preds = %25
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %3
%35 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %32, %29
br label %37
37: ; preds = %36, %22
br label %38
38: ; preds = %37, %14
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@ENOENT = global i64 0
@errno = global i64 0
@stderr = global i32 0
@.str.2 = [26 x i8] c"%s directory cannot open\0A\00"
@str = [3 x i8] c"NO\00"
@str.3 = [4 x i8] c"YES\00"
define i32 @dir_create_check_p(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %13
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = call i64 @S_ISDIR(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %6
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str.3, i64 0, i64 0))
br label %28
13: ; preds = %6, %1
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
%15 = call i32* @opendir(i8* %0)
%16 = icmp eq i32* %15, null
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = call i32 @closedir(i32* %15)
br label %28
19: ; preds = %13
%20 = load i64, i64* @ENOENT
%21 = load i64, i64* @errno
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @mkdirp(i8* %0)
br label %28
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %28
28: ; preds = %17, %25, %23, %11
ret i32 undef
}