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

/* Variables and functions */
scalar_t__ EEXIST;
int /*<<< orphan*/ S_IRWXU;
scalar_t__ chown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ lfprintf(int /*<<< orphan*/, char *, char const *,
                            int /*<<< orphan*/);
scalar_t__ mkdir(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strerror(scalar_t__);
int mkdir_and_force_owner(const char *path, uid_t uid, gid_t gid) {
  if (mkdir(path, S_IRWXU) < 0) {
    if (errno != EEXIST) {
      lfprintf(stderr, "Cannot create %s: %s\n", path, strerror(errno));
    } else {
      DIR *temp_dir = opendir(path);
      if (!path) {
        lfprintf(stderr, "Cannot open %s: %s\n", path, strerror(errno));
      } else {
        closedir(temp_dir);
        return 1;
      }
    }
  } else {
    if (chown(path, uid, gid) < 0) {
      lfprintf(stderr, "Cannot change owner of %s: %s.\n", path,
               strerror(errno));
    } else {
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<mkdir_and_force_owner>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
jge    1259 <mkdir_and_force_owner+0xf9>
mov    0x2e33(%rip),%rax
mov    (%rax),%rax
mov    0x2e39(%rip),%rcx
cmp    (%rcx),%rax
je     11e5 <mkdir_and_force_owner+0x85>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %eax,%ecx
lea    0xe25(%rip),%rsi
call   1060 <lfprintf@plt>
jmp    1254 <mkdir_and_force_owner+0xf4>
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    123a <mkdir_and_force_owner+0xda>
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2db0(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %eax,%ecx
lea    0xde6(%rip),%rsi
call   1060 <lfprintf@plt>
jmp    124f <mkdir_and_force_owner+0xef>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c7 <mkdir_and_force_owner+0x167>
jmp    1254 <mkdir_and_force_owner+0xf4>
jmp    12c0 <mkdir_and_force_owner+0x160>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1070 <chown@plt>
cmp    $0x0,%rax
jge    12af <mkdir_and_force_owner+0x14f>
mov    0x2d57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d3b(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %eax,%ecx
lea    0xd85(%rip),%rsi
call   1060 <lfprintf@plt>
jmp    12bb <mkdir_and_force_owner+0x15b>
movl   $0x1,-0x4(%rbp)
jmp    12c7 <mkdir_and_force_owner+0x167>
jmp    12c0 <mkdir_and_force_owner+0x160>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_and_force_owner>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
call   1030 <mkdir@plt>
test   %rax,%rax
js     11b5 <mkdir_and_force_owner+0x55>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1070 <chown@plt>
mov    $0x1,%ebx
test   %rax,%rax
jns    120a <mkdir_and_force_owner+0xaa>
mov    0x2e35(%rip),%rax
mov    (%rax),%ebx
mov    0x2e24(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
lea    0xe77(%rip),%rsi
jmp    11fc <mkdir_and_force_owner+0x9c>
mov    0x2e0c(%rip),%rbx
mov    (%rbx),%rdi
mov    0x2e12(%rip),%rax
cmp    (%rax),%rdi
jne    11e7 <mkdir_and_force_owner+0x87>
mov    %r14,%rdi
call   1040 <opendir@plt>
test   %r14,%r14
je     1211 <mkdir_and_force_owner+0xb1>
mov    %rax,%rdi
call   1050 <closedir@plt>
mov    $0x1,%ebx
jmp    120a <mkdir_and_force_owner+0xaa>
mov    0x2de2(%rip),%rax
mov    (%rax),%ebx
call   1080 <strerror@plt>
lea    0xe04(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %eax,%ecx
call   1060 <lfprintf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2db8(%rip),%rax
mov    (%rax),%ebp
mov    (%rbx),%rdi
call   1080 <strerror@plt>
lea    0xded(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %edx,%edx
mov    %eax,%ecx
call   1060 <lfprintf@plt>
jmp    120a <mkdir_and_force_owner+0xaa>

LLVM IR (-O0):
@S_IRWXU = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@stderr = global i32 0
@.str = [22 x i8] c"Cannot create %s: %s\0A\00"
@.str.1 = [20 x i8] c"Cannot open %s: %s\0A\00"
@.str.2 = [32 x i8] c"Cannot change owner of %s: %s.\0A\00"
define i32 @mkdir_and_force_owner(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 = load i32, i32* @S_IRWXU
%11 = call i64 @mkdir(i8* %9, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %39
13: ; preds = %3
%14 = load i64, i64* @errno
%15 = load i64, i64* @EEXIST
%16 = icmp ne i64 %14, %15
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %5
%20 = load i64, i64* @errno
%21 = call i32 @strerror(i64 %20)
%22 = call i32 @lfprintf(i32 %18, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %19, i32 %21)
br label %38
23: ; preds = %13
%24 = load i8*, i8** %5
%25 = call i32* @opendir(i8* %24)
store i32* %25, i32** %8
%26 = load i8*, i8** %5
%27 = icmp ne i8* %26, null
br i1 %27, label %34, label %28
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %5
%31 = load i64, i64* @errno
%32 = call i32 @strerror(i64 %31)
%33 = call i32 @lfprintf(i32 %29, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %30, i32 %32)
br label %37
34: ; preds = %23
%35 = load i32*, i32** %8
%36 = call i32 @closedir(i32* %35)
store i32 1, i32* %4
br label %54
37: ; preds = %28
br label %38
38: ; preds = %37, %17
br label %53
39: ; preds = %3
%40 = load i8*, i8** %5
%41 = load i32, i32* %6
%42 = load i32, i32* %7
%43 = call i64 @chown(i8* %40, i32 %41, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %39
%46 = load i32, i32* @stderr
%47 = load i8*, i8** %5
%48 = load i64, i64* @errno
%49 = call i32 @strerror(i64 %48)
%50 = call i32 @lfprintf(i32 %46, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %47, i32 %49)
br label %52
51: ; preds = %39
store i32 1, i32* %4
br label %54
52: ; preds = %45
br label %53
53: ; preds = %52, %38
store i32 0, i32* %4
br label %54
54: ; preds = %53, %51, %34
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
@S_IRWXU = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@stderr = global i32 0
@.str = [22 x i8] c"Cannot create %s: %s\0A\00"
@.str.1 = [20 x i8] c"Cannot open %s: %s\0A\00"
@.str.2 = [32 x i8] c"Cannot change owner of %s: %s.\0A\00"
define i32 @mkdir_and_force_owner(i8* %0, i32 %1, i32 %2) {
%4 = load i32, i32* @S_IRWXU
%5 = tail call i64 @mkdir(i8* %0, i32 %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %25
7: ; preds = %3
%8 = load i64, i64* @errno
%9 = load i64, i64* @EEXIST
%10 = icmp eq i64 %8, %9
br i1 %10, label %15, label %11
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = tail call i32 @strerror(i64 %8)
%14 = tail call i32 @lfprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %13)
br label %33
15: ; preds = %7
%16 = tail call i32* @opendir(i8* %0)
%17 = icmp eq i8* %0, null
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = load i64, i64* @errno
%21 = tail call i32 @strerror(i64 %20)
%22 = tail call i32 @lfprintf(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* null, i32 %21)
br label %33
23: ; preds = %15
%24 = tail call i32 @closedir(i32* %16)
br label %33
25: ; preds = %3
%26 = tail call i64 @chown(i8* %0, i32 %1, i32 %2)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = load i64, i64* @errno
%31 = tail call i32 @strerror(i64 %30)
%32 = tail call i32 @lfprintf(i32 %29, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %31)
br label %33
33: ; preds = %28, %11, %18, %23, %25
%34 = phi i32 [ 1, %23 ], [ 1, %25 ], [ 0, %18 ], [ 0, %11 ], [ 0, %28 ]
ret i32 %34
}