#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 */
scalar_t__ ENOENT;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
int mkdir(char const *, int);
int stat(char const *, struct stat *);
int os_MakeDir(char const *path, int mode) {
  struct stat buf;
  int r;
  if (!path)
    return -1;
  errno = 0;
  r = stat(path, &buf);
  if (r < 0 && errno != ENOENT)
    return r;
  if (r >= 0 && !S_ISDIR(buf.st_mode))
    return -1;
  if (r >= 0)
    return 1;
  return mkdir(path, mode);
}


x86 assembly (clang, -O0):
<os_MakeDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1156 <os_MakeDir+0x26>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <os_MakeDir+0xbf>
mov    0x2e83(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a3 <os_MakeDir+0x73>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rax
mov    0x2e41(%rip),%rcx
cmp    (%rcx),%rax
je     11a3 <os_MakeDir+0x73>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ef <os_MakeDir+0xbf>
cmpl   $0x0,-0x1c(%rbp)
jl     11ca <os_MakeDir+0x9a>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11ca <os_MakeDir+0x9a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <os_MakeDir+0xbf>
cmpl   $0x0,-0x1c(%rbp)
jl     11e0 <os_MakeDir+0xb0>
movl   $0x1,-0x4(%rbp)
jmp    11ef <os_MakeDir+0xbf>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <mkdir@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_MakeDir>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     1171 <os_MakeDir+0x41>
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2e97(%rip),%r14
movq   $0x0,(%r14)
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %eax,%eax
js     1178 <os_MakeDir+0x48>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x1,%eax
sbb    %eax,%eax
or     $0x1,%eax
jmp    1199 <os_MakeDir+0x69>
mov    $0xffffffff,%eax
jmp    1199 <os_MakeDir+0x69>
mov    (%r14),%rcx
mov    0x2e4e(%rip),%rdx
cmp    (%rdx),%rcx
jne    1199 <os_MakeDir+0x69>
mov    %rbx,%rdi
mov    %ebp,%esi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <mkdir@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
define i32 @os_MakeDir(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 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %2
store i32 -1, i32* %3
br label %39
11: ; preds = %2
store i64 0, i64* @errno
%12 = load i8*, i8** %4
%13 = call i32 @stat(i8* %12, %struct.stat* %6)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %11
%17 = load i64, i64* @errno
%18 = load i64, i64* @ENOENT
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = load i32, i32* %7
store i32 %21, i32* %3
br label %39
22: ; preds = %16, %11
%23 = load i32, i32* %7
%24 = icmp sge i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %22
%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 %31, label %30
30: ; preds = %25
store i32 -1, i32* %3
br label %39
31: ; preds = %25, %22
%32 = load i32, i32* %7
%33 = icmp sge i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %31
store i32 1, i32* %3
br label %39
35: ; preds = %31
%36 = load i8*, i8** %4
%37 = load i32, i32* %5
%38 = call i32 @mkdir(i8* %36, i32 %37)
store i32 %38, i32* %3
br label %39
39: ; preds = %35, %34, %30, %20, %10
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
define i32 @os_MakeDir(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 %21, label %6
6: ; preds = %2
store i64 0, i64* @errno
%7 = call i32 @stat(i8* %0, %struct.stat* %3)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %6
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOENT
%12 = icmp eq i64 %10, %11
br i1 %12, label %19, label %21
13: ; preds = %6
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i32 @S_ISDIR(i32 %15)
%17 = icmp eq i32 %16, 0
%18 = select i1 %17, i32 -1, i32 1
br label %21
19: ; preds = %9
%20 = tail call i32 @mkdir(i8* %0, i32 %1)
br label %21
21: ; preds = %13, %9, %2, %19
%22 = phi i32 [ %20, %19 ], [ -1, %2 ], [ %7, %9 ], [ %18, %13 ]
ret i32 %22
}