#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 EINVAL;
int ENOTDIR;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int errno;
scalar_t__ mkdir(char const *, int);
scalar_t__ stat(char const *, struct stat *);
int os_dir_create(const char *path) {
  struct stat st;
  if (path == NULL || path[0] == '\0')
    return -EINVAL;
  if (stat(path, &st) == 0) {
    if (S_ISDIR(st.st_mode))
      return 0;
    return -ENOTDIR;
  }
  if (mkdir(path, 0777) != 0)
    return -errno;
  return 0;
}


x86 assembly (clang, -O0):
<os_dir_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1157 <os_dir_create+0x27>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    116a <os_dir_create+0x3a>
xor    %eax,%eax
mov    0x2e70(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e4 <os_dir_create+0xb4>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    11b2 <os_dir_create+0x82>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     119f <os_dir_create+0x6f>
movl   $0x0,-0x4(%rbp)
jmp    11e4 <os_dir_create+0xb4>
xor    %eax,%eax
mov    0x2e50(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e4 <os_dir_create+0xb4>
mov    -0x10(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     11dd <os_dir_create+0xad>
xor    %eax,%eax
mov    0x2e05(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e4 <os_dir_create+0xb4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_dir_create>:
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     116f <os_dir_create+0x3f>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     116f <os_dir_create+0x3f>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %rax,%rax
je     1180 <os_dir_create+0x50>
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
je     119c <os_dir_create+0x6c>
mov    0x2e6b(%rip),%rcx
jmp    1176 <os_dir_create+0x46>
mov    0x2e5a(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    117a <os_dir_create+0x4a>
mov    0x2e5e(%rip),%rcx
jmp    1176 <os_dir_create+0x46>
xor    %eax,%eax
jmp    117a <os_dir_create+0x4a>

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

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