#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;
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
scalar_t__ EEXIST;
int chown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int mkdir(char const *, int);
int mkdir_as(const char *dir, uid_t uid, gid_t gid) {
  int rv;
  rv = mkdir(dir, 0755);
  if (rv == 0) {
    return chown(dir, uid, gid);
  } else {
    if (errno == EEXIST) {
      return 0;
    } else {
      return rv;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<mkdir_as>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1164 <mkdir_as+0x44>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <chown@plt>
mov    %eax,-0x4(%rbp)
jmp    1190 <mkdir_as+0x70>
mov    0x2e6d(%rip),%rax
mov    (%rax),%rax
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%rax
jne    118a <mkdir_as+0x6a>
movl   $0x0,-0x4(%rbp)
jmp    1190 <mkdir_as+0x70>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_as>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
test   %eax,%eax
je     1158 <mkdir_as+0x38>
mov    0x2e97(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e95(%rip),%rdx
xor    %esi,%esi
cmp    (%rdx),%rcx
cmove  %esi,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <chown@plt>

LLVM IR (-O0):
@errno = global i64 0
@EEXIST = global i64 0
define i32 @mkdir_as(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = call i32 @mkdir(i8* %9, i32 493)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = load i32, i32* %6
%16 = load i32, i32* %7
%17 = call i32 @chown(i8* %14, i32 %15, i32 %16)
store i32 %17, i32* %4
br label %25
18: ; preds = %3
%19 = load i64, i64* @errno
%20 = load i64, i64* @EEXIST
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 0, i32* %4
br label %25
23: ; preds = %18
%24 = load i32, i32* %8
store i32 %24, i32* %4
br label %25
25: ; preds = %23, %22, %13
%26 = load i32, i32* %4
ret i32 %26
}

LLVM IR (-O2):
@errno = global i64 0
@EEXIST = global i64 0
define i32 @mkdir_as(i8* nocapture %0, i32 %1, i32 %2) {
%4 = tail call i32 @mkdir(i8* %0, i32 493)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @chown(i8* %0, i32 %1, i32 %2)
br label %13
8: ; preds = %3
%9 = load i64, i64* @errno
%10 = load i64, i64* @EEXIST
%11 = icmp eq i64 %9, %10
%12 = select i1 %11, i32 0, i32 %4
br label %13
13: ; preds = %8, %6
%14 = phi i32 [ %7, %6 ], [ %12, %8 ]
ret i32 %14
}