#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 dummy;
};
typedef int pid_t;
typedef int mode_t;

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ EIO;
scalar_t__ ENOENT;
int /*<<< orphan*/ _exit(int);
int chmod(char const *, int);
scalar_t__ errno;
int /*<<< orphan*/ execl(char *, char *, char const *, char *);
int fork();
scalar_t__ stat(char const *, struct stat *);
int umask(int);
int wait(int *);
int mkdir(const char *dpath, mode_t dmode) {
  pid_t cpid;
  mode_t mode;
  int status;
  struct stat statbuf;
  if (stat(dpath, &statbuf) == 0) {
    errno = EEXIST;
    return -1;
  }
  if (errno != ENOENT)
    return -1;
  cpid = fork();
  switch (cpid) {
  case -1:
    return -1;
  case 0:
    mode = umask(0);
    umask(mode | ((0700 | 0070 | 0007) & ~dmode));
    execl("/bin/mkdir", "mkdir", dpath, (char *)0);
    _exit(1);
  default:
    while (wait(&status) != cpid)
      ;
    if (status) {
      errno = EIO;
      return -1;
    }
    return chmod(dpath, dmode);
  }
}


x86 assembly (clang, -O0):
<mkdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    11b6 <mkdir+0x46>
mov    0x2e3b(%rip),%rax
mov    (%rax),%rcx
mov    0x2e29(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <mkdir+0x13f>
mov    0x2e13(%rip),%rax
mov    (%rax),%rax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%rax
je     11dc <mkdir+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <mkdir+0x13f>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     120a <mkdir+0x9a>
jmp    11fa <mkdir+0x8a>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
je     1216 <mkdir+0xa6>
jmp    125a <mkdir+0xea>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <mkdir+0x13f>
xor    %edi,%edi
call   1040 <umask@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%eax
xor    $0xffffffff,%eax
and    $0x1ff,%eax
or     %eax,%edi
call   1040 <umask@plt>
mov    -0x10(%rbp),%rdx
lea    0xdc0(%rip),%rdi
lea    0xdbe(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
call   1070 <execl@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
jmp    125f <mkdir+0xef>
lea    -0x20(%rbp),%rdi
call   1080 <wait@plt>
cmp    -0x18(%rbp),%eax
je     1276 <mkdir+0x106>
jmp    125f <mkdir+0xef>
cmpl   $0x0,-0x20(%rbp)
je     12a0 <mkdir+0x130>
mov    0x2d59(%rip),%rax
mov    (%rax),%rcx
mov    0x2d3f(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <mkdir+0x13f>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <chmod@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     11f3 <mkdir+0x83>
mov    0x2e38(%rip),%r12
mov    (%r12),%rax
mov    0x2e1d(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
jne    120c <mkdir+0x9c>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     120c <mkdir+0x9c>
test   %ebp,%ebp
je     122a <mkdir+0xba>
lea    0x4(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1080 <wait@plt>
cmp    %ebp,%eax
jne    11d0 <mkdir+0x60>
cmpl   $0x0,0x4(%rsp)
je     121b <mkdir+0xab>
mov    0x2df6(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r12)
jmp    1207 <mkdir+0x97>
mov    0x2dde(%rip),%rax
mov    (%rax),%rax
mov    0x2dcc(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
mov    %r14d,%esi
call   1060 <chmod@plt>
mov    %eax,%ebp
jmp    120c <mkdir+0x9c>
xor    %edi,%edi
call   1040 <umask@plt>
not    %r14d
and    $0x1ff,%r14d
or     %eax,%r14d
mov    %r14d,%edi
call   1040 <umask@plt>
lea    0xdb3(%rip),%rdi
lea    0xdb1(%rip),%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
call   1070 <execl@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@EEXIST = global i64 0
@errno = global i64 0
@ENOENT = global i64 0
@.str = [11 x i8] c"/bin/mkdir\00"
@.str.1 = [6 x i8] c"mkdir\00"
@EIO = global i64 0
define i32 @mkdir(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.stat
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i8*, i8** %4
%11 = call i64 @stat(i8* %10, %struct.stat* %9)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i64, i64* @EEXIST
store i64 %14, i64* @errno
store i32 -1, i32* %3
br label %50
15: ; preds = %2
%16 = load i64, i64* @errno
%17 = load i64, i64* @ENOENT
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 -1, i32* %3
br label %50
20: ; preds = %15
%21 = call i32 (...) @fork()
store i32 %21, i32* %6
%22 = load i32, i32* %6
switch i32 %22, label %35 [
i32 -1, label %23
i32 0, label %24
]
23: ; preds = %20
store i32 -1, i32* %3
br label %50
24: ; preds = %20
%25 = call i32 @umask(i32 0)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = load i32, i32* %5
%28 = xor i32 %27, -1
%29 = and i32 511, %28
%30 = or i32 %26, %29
%31 = call i32 @umask(i32 %30)
%32 = load i8*, i8** %4
%33 = call i32 @execl(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %32, i8* null)
%34 = call i32 @_exit(i32 1)
unreachable
35: ; preds = %20
br label %36
36: ; preds = %40, %35
%37 = call i32 @wait(i32* %8)
%38 = load i32, i32* %6
%39 = icmp ne i32 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %36
br label %36
41: ; preds = %36
%42 = load i32, i32* %8
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = load i64, i64* @EIO
store i64 %45, i64* @errno
store i32 -1, i32* %3
br label %50
46: ; preds = %41
%47 = load i8*, i8** %4
%48 = load i32, i32* %5
%49 = call i32 @chmod(i8* %47, i32 %48)
store i32 %49, i32* %3
br label %50
50: ; preds = %46, %44, %23, %19, %13
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@EEXIST = global i64 0
@errno = global i64 0
@ENOENT = global i64 0
@.str = [11 x i8] c"/bin/mkdir\00"
@.str.1 = [6 x i8] c"mkdir\00"
@EIO = global i64 0
define i32 @mkdir(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.stat
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.stat* %4 to i8*
%7 = call i64 @stat(i8* %0, %struct.stat* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i64, i64* @EEXIST
store i64 %10, i64* @errno
br label %35
11: ; preds = %2
%12 = load i64, i64* @errno
%13 = load i64, i64* @ENOENT
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %35
15: ; preds = %11
%16 = call i32 (...) @fork()
switch i32 %16, label %25 [
i32 -1, label %35
i32 0, label %17
]
17: ; preds = %15
%18 = call i32 @umask(i32 0)
%19 = and i32 %1, 511
%20 = xor i32 %19, 511
%21 = or i32 %18, %20
%22 = call i32 @umask(i32 %21)
%23 = call i32 @execl(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* null)
%24 = call i32 @_exit(i32 1)
unreachable
25: ; preds = %15, %25
%26 = call i32 @wait(i32* %3)
%27 = icmp eq i32 %26, %16
br i1 %27, label %28, label %25
28: ; preds = %25
%29 = load i32, i32* %3
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = load i64, i64* @EIO
store i64 %32, i64* @errno
br label %35
33: ; preds = %28
%34 = call i32 @chmod(i8* %0, i32 %1)
br label %35
35: ; preds = %15, %11, %33, %31, %9
%36 = phi i32 [ -1, %9 ], [ -1, %31 ], [ %34, %33 ], [ -1, %11 ], [ %16, %15 ]
ret i32 %36
}