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


x86 assembly (clang, -O0):
<mkdir_1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
jl     1161 <mkdir_1+0x31>
movl   $0x0,-0x4(%rbp)
jmp    11cd <mkdir_1+0x9d>
mov    0x2e70(%rip),%rax
mov    (%rax),%rax
mov    0x2e6e(%rip),%rcx
cmp    (%rcx),%rax
je     1187 <mkdir_1+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <mkdir_1+0x9d>
jmp    118c <mkdir_1+0x5c>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
mov    %rax,%rcx
mov    $0x1,%al
cmp    $0x0,%rcx
mov    %al,-0x19(%rbp)
jl     11be <mkdir_1+0x8e>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_1>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1030 <mkdir@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    1189 <mkdir_1+0x59>
mov    0x2e8a(%rip),%rax
mov    (%rax),%rcx
mov    0x2e88(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%rcx
jne    1189 <mkdir_1+0x59>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %rax,%rax
js     1184 <mkdir_1+0x54>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x1,%eax
sbb    %eax,%eax
jmp    1189 <mkdir_1+0x59>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@EEXIST = global i64 0
define i32 @mkdir_1(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = load i32, i32* %5
%9 = call i64 @mkdir(i8* %7, i32 %8)
%10 = icmp sge i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 0, i32* %3
br label %32
12: ; preds = %2
%13 = load i64, i64* @errno
%14 = load i64, i64* @EEXIST
%15 = icmp ne i64 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 -1, i32* %3
br label %32
17: ; preds = %12
br label %18
18: ; preds = %17
%19 = load i8*, i8** %4
%20 = call i64 @stat(i8* %19, %struct.stat* %6)
%21 = icmp slt i64 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %18
%23 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @S_ISDIR(i32 %24)
%26 = icmp ne i32 %25, 0
%27 = xor i1 %26, true
br label %28
28: ; preds = %22, %18
%29 = phi i1 [ true, %18 ], [ %27, %22 ]
%30 = zext i1 %29 to i32
%31 = sub nsw i32 0, %30
store i32 %31, i32* %3
br label %32
32: ; preds = %28, %16, %11
%33 = load i32, i32* %3
ret i32 %33
}

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