#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 st_mode;
  scalar_t__ st_uid;
};

/* Variables and functions */
scalar_t__ ENOENT;
int S_IRWXG;
int S_IRWXO;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ S_ISDIR(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
scalar_t__ getuid();
scalar_t__ lstat(char *, struct stat *);
int mkdir(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
void mkdir_and_check(char *dirname, int be_strict) {
  struct stat stbuf;
  if (lstat(dirname, &stbuf) != 0) {
    if (errno != ENOENT) {
      fprintf(stderr, "lstat() failed for %s: %s\n", dirname, strerror(errno));
      exit(1);
    }
    fprintf(stderr, "VNC directory %s does not exist, creating.\n", dirname);
    if (mkdir(dirname, S_IRWXU) == -1) {
      fprintf(stderr, "Error creating directory %s: %s\n", dirname,
              strerror(errno));
      exit(1);
    }
  }
  if (lstat(dirname, &stbuf) != 0) {
    fprintf(stderr, "Error in lstat() for %s: %s\n", dirname, strerror(errno));
    exit(1);
  }
  if (!S_ISDIR(stbuf.st_mode)) {
    fprintf(stderr, "Error: %s is not a directory\n", dirname);
    exit(1);
  }
  if (stbuf.st_uid != getuid()) {
    fprintf(stderr, "Error: bad ownership on %s\n", dirname);
    exit(1);
  }
  if (be_strict && ((S_IRWXG | S_IRWXO) & stbuf.st_mode)) {
    fprintf(stderr, "Error: bad access modes on %s\n", dirname);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<mkdir_and_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%rax
je     1275 <mkdir_and_check+0x105>
mov    0x2e23(%rip),%rax
mov    (%rax),%rax
mov    0x2e09(%rip),%rcx
cmp    (%rcx),%rax
je     11f5 <mkdir_and_check+0x85>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x8(%rbp),%rdi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
call   1030 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    1270 <mkdir_and_check+0x100>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d7a(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xde8(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1275 <mkdir_and_check+0x105>
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%rax
je     12d1 <mkdir_and_check+0x161>
mov    0x2d35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d19(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xda8(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1307 <mkdir_and_check+0x197>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd8f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
cmp    %rcx,%rax
je     134b <mkdir_and_check+0x1db>
mov    0x2c9b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd69(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
je     1398 <mkdir_and_check+0x228>
mov    0x2c7c(%rip),%rax
mov    (%rax),%eax
mov    0x2c8b(%rip),%rcx
or     (%rcx),%eax
and    -0x20(%rbp),%eax
cmp    $0x0,%eax
je     1398 <mkdir_and_check+0x228>
mov    0x2c4e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd38(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_and_check>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%r13
lea    0x8(%rsp),%rsi
call   1060 <lstat@plt>
test   %rax,%rax
je     11e5 <mkdir_and_check+0x75>
mov    0x2e26(%rip),%r15
mov    (%r15),%rdi
mov    0x2e0c(%rip),%rax
mov    0x2e1d(%rip),%r12
mov    (%r12),%ebp
cmp    (%rax),%rdi
jne    128a <mkdir_and_check+0x11a>
lea    0xe5c(%rip),%rsi
mov    %ebp,%edi
mov    %r13,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1030 <mkdir@plt>
cmp    $0xffffffff,%eax
je     1298 <mkdir_and_check+0x128>
lea    0x8(%rsp),%rsi
mov    %r13,%rdi
call   1060 <lstat@plt>
test   %rax,%rax
jne    1245 <mkdir_and_check+0xd5>
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     1266 <mkdir_and_check+0xf6>
mov    0x10(%rsp),%rbx
xor    %eax,%eax
call   1040 <getuid@plt>
cmp    %rax,%rbx
jne    1278 <mkdir_and_check+0x108>
test   %r14d,%r14d
je     1236 <mkdir_and_check+0xc6>
mov    0x2db5(%rip),%rax
mov    0x2dc6(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
test   %ebp,%ecx
jne    12c4 <mkdir_and_check+0x154>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d7c(%rip),%rax
mov    (%rax),%ebp
mov    0x2d6b(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
lea    0xe04(%rip),%rsi
jmp    12ab <mkdir_and_check+0x13b>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
jmp    12d4 <mkdir_and_check+0x164>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
jmp    12d4 <mkdir_and_check+0x164>
call   1090 <strerror@plt>
lea    0xd6a(%rip),%rsi
jmp    12ab <mkdir_and_check+0x13b>
mov    (%r12),%ebp
mov    (%r15),%rdi
call   1090 <strerror@plt>
lea    0xd9c(%rip),%rsi
mov    %ebp,%edi
mov    %r13,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32, i64 }
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str = [27 x i8] c"lstat() failed for %s: %s\0A\00"
@.str.1 = [44 x i8] c"VNC directory %s does not exist, creating.\0A\00"
@S_IRWXU = global i32 0
@.str.2 = [33 x i8] c"Error creating directory %s: %s\0A\00"
@.str.3 = [29 x i8] c"Error in lstat() for %s: %s\0A\00"
@.str.4 = [30 x i8] c"Error: %s is not a directory\0A\00"
@.str.5 = [28 x i8] c"Error: bad ownership on %s\0A\00"
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str.6 = [31 x i8] c"Error: bad access modes on %s\0A\00"
define void @mkdir_and_check(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
%7 = call i64 @lstat(i8* %6, %struct.stat* %5)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %36
9: ; preds = %2
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOENT
%12 = icmp ne i64 %10, %11
br i1 %12, label %13, label %20
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %3
%16 = load i64, i64* @errno
%17 = call i8* @strerror(i64 %16)
%18 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %9
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %3
%23 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i8*, i8** %3
%25 = load i32, i32* @S_IRWXU
%26 = call i32 @mkdir(i8* %24, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %35
28: ; preds = %20
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %3
%31 = load i64, i64* @errno
%32 = call i8* @strerror(i64 %31)
%33 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %30, i8* %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %20
br label %36
36: ; preds = %35, %2
%37 = load i8*, i8** %3
%38 = call i64 @lstat(i8* %37, %struct.stat* %5)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %47
40: ; preds = %36
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %3
%43 = load i64, i64* @errno
%44 = call i8* @strerror(i64 %43)
%45 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %42, i8* %44)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %36
%48 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i32 @S_ISDIR(i32 %49)
%51 = icmp ne i32 %50, 0
br i1 %51, label %57, label %52
52: ; preds = %47
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %3
%55 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %54)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %47
%58 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 1
%59 = load i64, i64* %58
%60 = call i64 (...) @getuid()
%61 = icmp ne i64 %59, %60
br i1 %61, label %62, label %67
62: ; preds = %57
%63 = load i32, i32* @stderr
%64 = load i8*, i8** %3
%65 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %64)
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %57
%68 = load i32, i32* %4
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %83
70: ; preds = %67
%71 = load i32, i32* @S_IRWXG
%72 = load i32, i32* @S_IRWXO
%73 = or i32 %71, %72
%74 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%75 = load i32, i32* %74
%76 = and i32 %73, %75
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %83
78: ; preds = %70
%79 = load i32, i32* @stderr
%80 = load i8*, i8** %3
%81 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %79, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i8* %80)
%82 = call i32 @exit(i32 1)
unreachable
83: ; preds = %70, %67
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32, i64 }
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str = [27 x i8] c"lstat() failed for %s: %s\0A\00"
@.str.1 = [44 x i8] c"VNC directory %s does not exist, creating.\0A\00"
@S_IRWXU = global i32 0
@.str.2 = [33 x i8] c"Error creating directory %s: %s\0A\00"
@.str.3 = [29 x i8] c"Error in lstat() for %s: %s\0A\00"
@.str.4 = [30 x i8] c"Error: %s is not a directory\0A\00"
@.str.5 = [28 x i8] c"Error: bad ownership on %s\0A\00"
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str.6 = [31 x i8] c"Error: bad access modes on %s\0A\00"
define void @mkdir_and_check(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @lstat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %27, label %7
7: ; preds = %2
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOENT
%10 = icmp eq i64 %8, %9
%11 = load i32, i32* @stderr
br i1 %10, label %16, label %12
12: ; preds = %7
%13 = tail call i8* @strerror(i64 %8)
%14 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %7
%17 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%18 = load i32, i32* @S_IRWXU
%19 = tail call i32 @mkdir(i8* %0, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = load i64, i64* @errno
%24 = tail call i8* @strerror(i64 %23)
%25 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %24)
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %16, %2
%28 = call i64 @lstat(i8* %0, %struct.stat* %3)
%29 = icmp eq i64 %28, 0
br i1 %29, label %36, label %30
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = load i64, i64* @errno
%33 = tail call i8* @strerror(i64 %32)
%34 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %33)
%35 = tail call i32 @exit(i32 1)
unreachable
36: ; preds = %27
%37 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%38 = load i32, i32* %37
%39 = tail call i32 @S_ISDIR(i32 %38)
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = load i32, i32* @stderr
%43 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%44 = tail call i32 @exit(i32 1)
unreachable
45: ; preds = %36
%46 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%47 = load i64, i64* %46
%48 = tail call i64 (...) @getuid()
%49 = icmp eq i64 %47, %48
br i1 %49, label %54, label %50
50: ; preds = %45
%51 = load i32, i32* @stderr
%52 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %0)
%53 = tail call i32 @exit(i32 1)
unreachable
54: ; preds = %45
%55 = icmp eq i32 %1, 0
br i1 %55, label %66, label %56
56: ; preds = %54
%57 = load i32, i32* @S_IRWXG
%58 = load i32, i32* @S_IRWXO
%59 = or i32 %58, %57
%60 = and i32 %59, %38
%61 = icmp eq i32 %60, 0
br i1 %61, label %66, label %62
62: ; preds = %56
%63 = load i32, i32* @stderr
%64 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i8* %0)
%65 = tail call i32 @exit(i32 1)
unreachable
66: ; preds = %56, %54
ret void
}