#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 */
typedef int /*<<< orphan*/ uid_t;
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ mode_t;
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ chown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ mkdir(char const *, int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
extern int mk_dir(const char *dir, mode_t mode, uid_t user, gid_t group) {
  struct stat sr;
  if ((stat(dir, &sr) && (errno == ENOENT)) && !mkdir(dir, mode) &&
      !chown(dir, user, group)) {
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<mk_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
je     11af <mk_dir+0x7f>
mov    0x2e7d(%rip),%rax
mov    (%rax),%rax
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%rax
jne    11af <mk_dir+0x7f>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%eax
jne    11af <mk_dir+0x7f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1050 <chown@plt>
cmp    $0x0,%eax
jne    11af <mk_dir+0x7f>
movl   $0x0,-0x4(%rbp)
jmp    11b6 <mk_dir+0x86>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mk_dir>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %rax,%rax
je     1185 <mk_dir+0x55>
mov    0x2e8a(%rip),%rax
mov    (%rax),%rax
mov    0x2e70(%rip),%rcx
cmp    (%rcx),%rax
jne    1185 <mk_dir+0x55>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <mkdir@plt>
test   %eax,%eax
jne    1185 <mk_dir+0x55>
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
call   1050 <chown@plt>
test   %eax,%eax
je     1195 <mk_dir+0x65>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    118a <mk_dir+0x5a>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
define i32 @mk_dir(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%11 = load i8*, i8** %6
%12 = call i64 @stat(i8* %11, %struct.stat* %10)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %30
14: ; preds = %4
%15 = load i64, i64* @errno
%16 = load i64, i64* @ENOENT
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %30
18: ; preds = %14
%19 = load i8*, i8** %6
%20 = load i32, i32* %7
%21 = call i32 @mkdir(i8* %19, i32 %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %18
%24 = load i8*, i8** %6
%25 = load i32, i32* %8
%26 = load i32, i32* %9
%27 = call i32 @chown(i8* %24, i32 %25, i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %23
store i32 0, i32* %5
br label %31
30: ; preds = %23, %18, %14, %4
store i32 -1, i32* %5
br label %31
31: ; preds = %30, %29
%32 = load i32, i32* %5
ret i32 %32
}

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