#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 mode_t;

/* Variables and functions */
int /*<<< orphan*/ Error(int, char *, char *, char *);
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IRWXG;
int S_ISGID;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int S_IXGRP;
int S_IXOTH;
int S_IXUSR;
int /*<<< orphan*/ chmod(char *, int);
int /*<<< orphan*/ fileExists(char *);
scalar_t__ posix_mkdir(char *, int);
int posix_process_running_setgid();
int posix_umask(int);
void createDirectory(char *dir, char *text, int permission_class) {
  mode_t dir_mode =
      (permission_class == 0
           ? ((S_IRUSR | S_IRGRP | S_IROTH) | (S_IXUSR | S_IXGRP | S_IXOTH) |
              (S_IWUSR))
           : ((S_IRUSR | S_IRGRP | S_IROTH) | (S_IXUSR | S_IXGRP | S_IXOTH) |
              (S_IWUSR | S_IWGRP | S_ISGID)));
  mode_t last_umask = posix_umask(0);
  mode_t group_umask = ~(dir_mode & S_IRWXG);
  int running_setgid = posix_process_running_setgid();
  if (running_setgid)
    posix_umask(last_umask & group_umask);
  else
    dir_mode |= (S_IWUSR | S_IWGRP | S_IWOTH);
  if (!fileExists(dir))
    if (posix_mkdir(dir, dir_mode) != 0)
      Error((1 << 1), "cannot create %s directory '%s'", text, dir);
  if (permission_class == 1 && !running_setgid)
    chmod(dir, dir_mode);
  posix_umask(last_umask);
}


x86 assembly (clang, -O0):
<createDirectory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11c6 <createDirectory+0x66>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    0x2e1b(%rip),%rcx
or     (%rcx),%eax
mov    0x2e0a(%rip),%rcx
or     (%rcx),%eax
mov    0x2e39(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e18(%rip),%rdx
or     (%rdx),%ecx
mov    0x2e37(%rip),%rdx
or     (%rdx),%ecx
or     %ecx,%eax
mov    0x2e0c(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
jmp    121e <createDirectory+0xbe>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    0x2dd2(%rip),%rcx
or     (%rcx),%eax
mov    0x2dc1(%rip),%rcx
or     (%rcx),%eax
mov    0x2df0(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2dcf(%rip),%rdx
or     (%rdx),%ecx
mov    0x2dee(%rip),%rdx
or     (%rdx),%ecx
or     %ecx,%eax
mov    0x2dc3(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2dc2(%rip),%rdx
or     (%rdx),%ecx
mov    0x2d99(%rip),%rdx
or     (%rdx),%ecx
or     %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x18(%rbp)
xor    %edi,%edi
call   1030 <posix_umask@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2da8(%rip),%rcx
and    (%rcx),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <posix_process_running_setgid@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1264 <createDirectory+0x104>
mov    -0x1c(%rbp),%edi
and    -0x20(%rbp),%edi
call   1030 <posix_umask@plt>
jmp    1285 <createDirectory+0x125>
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
mov    0x2d5c(%rip),%rcx
or     (%rcx),%eax
mov    0x2d0b(%rip),%rcx
or     (%rcx),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <fileExists@plt>
cmp    $0x0,%eax
jne    12cb <createDirectory+0x16b>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1080 <posix_mkdir@plt>
cmp    $0x0,%rax
je     12c6 <createDirectory+0x166>
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    $0x2,%edi
lea    0xd3f(%rip),%rsi
call   1050 <Error@plt>
jmp    12cb <createDirectory+0x16b>
cmpl   $0x1,-0x14(%rbp)
jne    12eb <createDirectory+0x18b>
cmpl   $0x0,-0x24(%rbp)
jne    12eb <createDirectory+0x18b>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1060 <chmod@plt>
mov    -0x1c(%rbp),%edi
call   1030 <posix_umask@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createDirectory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rdi,%r14
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%ebp
mov    0x2e16(%rip),%rax
mov    (%rax),%ebx
mov    0x2e45(%rip),%rax
mov    (%rax),%ecx
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
mov    0x2e43(%rip),%rax
mov    (%rax),%r11d
mov    0x2e19(%rip),%rax
mov    (%rax),%r10d
test   %r12d,%r12d
mov    %rsi,(%rsp)
je     11df <createDirectory+0x7f>
mov    0x2e0e(%rip),%r8
mov    0x2de7(%rip),%r9
or     %edi,%ebp
or     %ebx,%ebp
or     %ecx,%ebp
or     %edx,%ebp
or     %r11d,%ebp
or     %r10d,%ebp
or     (%r8),%ebp
or     (%r9),%ebp
jmp    11ed <createDirectory+0x8d>
or     %edi,%ebp
or     %ebx,%ebp
or     %ecx,%ebp
or     %edx,%ebp
or     %r11d,%ebp
or     %r10d,%ebp
xor    %edi,%edi
call   1030 <posix_umask@plt>
mov    %eax,%r13d
mov    0x2de2(%rip),%rax
mov    (%rax),%r15d
xor    %eax,%eax
call   1070 <posix_process_running_setgid@plt>
mov    %eax,%ebx
test   %eax,%eax
je     1221 <createDirectory+0xc1>
and    %ebp,%r15d
not    %r15d
and    %r13d,%r15d
mov    %r15d,%edi
call   1030 <posix_umask@plt>
jmp    123c <createDirectory+0xdc>
mov    0x2da8(%rip),%rax
mov    0x2d59(%rip),%rcx
mov    0x2d92(%rip),%rdx
or     (%rdx),%ebp
or     (%rax),%ebp
or     (%rcx),%ebp
mov    %r14,%rdi
call   1040 <fileExists@plt>
test   %eax,%eax
jne    126f <createDirectory+0x10f>
mov    %r14,%rdi
mov    %ebp,%esi
call   1080 <posix_mkdir@plt>
test   %rax,%rax
je     126f <createDirectory+0x10f>
lea    0xda2(%rip),%rsi
mov    $0x2,%edi
mov    (%rsp),%rdx
mov    %r14,%rcx
call   1050 <Error@plt>
cmp    $0x1,%r12d
jne    1283 <createDirectory+0x123>
test   %ebx,%ebx
jne    1283 <createDirectory+0x123>
mov    %r14,%rdi
mov    %ebp,%esi
call   1060 <chmod@plt>
mov    %r13d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <posix_umask@plt>

LLVM IR (-O0):
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@S_IXUSR = global i32 0
@S_IXGRP = global i32 0
@S_IXOTH = global i32 0
@S_IWUSR = global i32 0
@S_IWGRP = global i32 0
@S_ISGID = global i32 0
@S_IRWXG = global i32 0
@S_IWOTH = global i32 0
@.str = [32 x i8] c"cannot create %s directory '%s'\00"
define void @createDirectory(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %27
13: ; preds = %3
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IRGRP
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IROTH
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IXUSR
%20 = load i32, i32* @S_IXGRP
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IXOTH
%23 = or i32 %21, %22
%24 = or i32 %18, %23
%25 = load i32, i32* @S_IWUSR
%26 = or i32 %24, %25
br label %45
27: ; preds = %3
%28 = load i32, i32* @S_IRUSR
%29 = load i32, i32* @S_IRGRP
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IROTH
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IXUSR
%34 = load i32, i32* @S_IXGRP
%35 = or i32 %33, %34
%36 = load i32, i32* @S_IXOTH
%37 = or i32 %35, %36
%38 = or i32 %32, %37
%39 = load i32, i32* @S_IWUSR
%40 = load i32, i32* @S_IWGRP
%41 = or i32 %39, %40
%42 = load i32, i32* @S_ISGID
%43 = or i32 %41, %42
%44 = or i32 %38, %43
br label %45
45: ; preds = %27, %13
%46 = phi i32 [ %26, %13 ], [ %44, %27 ]
store i32 %46, i32* %7
%47 = call i32 @posix_umask(i32 0)
store i32 %47, i32* %8
%48 = load i32, i32* %7
%49 = load i32, i32* @S_IRWXG
%50 = and i32 %48, %49
%51 = xor i32 %50, -1
store i32 %51, i32* %9
%52 = call i32 (...) @posix_process_running_setgid()
store i32 %52, i32* %10
%53 = load i32, i32* %10
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %45
%56 = load i32, i32* %8
%57 = load i32, i32* %9
%58 = and i32 %56, %57
%59 = call i32 @posix_umask(i32 %58)
br label %68
60: ; preds = %45
%61 = load i32, i32* @S_IWUSR
%62 = load i32, i32* @S_IWGRP
%63 = or i32 %61, %62
%64 = load i32, i32* @S_IWOTH
%65 = or i32 %63, %64
%66 = load i32, i32* %7
%67 = or i32 %66, %65
store i32 %67, i32* %7
br label %68
68: ; preds = %60, %55
%69 = load i8*, i8** %4
%70 = call i32 @fileExists(i8* %69)
%71 = icmp ne i32 %70, 0
br i1 %71, label %82, label %72
72: ; preds = %68
%73 = load i8*, i8** %4
%74 = load i32, i32* %7
%75 = call i64 @posix_mkdir(i8* %73, i32 %74)
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %81
77: ; preds = %72
%78 = load i8*, i8** %5
%79 = load i8*, i8** %4
%80 = call i32 @Error(i32 2, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %78, i8* %79)
br label %81
81: ; preds = %77, %72
br label %82
82: ; preds = %81, %68
%83 = load i32, i32* %6
%84 = icmp eq i32 %83, 1
br i1 %84, label %85, label %92
85: ; preds = %82
%86 = load i32, i32* %10
%87 = icmp ne i32 %86, 0
br i1 %87, label %92, label %88
88: ; preds = %85
%89 = load i8*, i8** %4
%90 = load i32, i32* %7
%91 = call i32 @chmod(i8* %89, i32 %90)
br label %92
92: ; preds = %88, %85, %82
%93 = load i32, i32* %8
%94 = call i32 @posix_umask(i32 %93)
ret void
}

LLVM IR (-O2):
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@S_IXUSR = global i32 0
@S_IXGRP = global i32 0
@S_IXOTH = global i32 0
@S_IWUSR = global i32 0
@S_IWGRP = global i32 0
@S_ISGID = global i32 0
@S_IRWXG = global i32 0
@S_IWOTH = global i32 0
@.str = [32 x i8] c"cannot create %s directory '%s'\00"
define void @createDirectory(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i32 %2, 0
%5 = load i32, i32* @S_IRUSR
%6 = load i32, i32* @S_IRGRP
%7 = load i32, i32* @S_IROTH
%8 = load i32, i32* @S_IXUSR
%9 = load i32, i32* @S_IXGRP
%10 = load i32, i32* @S_IXOTH
%11 = load i32, i32* @S_IWUSR
br i1 %4, label %12, label %19
12: ; preds = %3
%13 = or i32 %6, %5
%14 = or i32 %13, %7
%15 = or i32 %14, %8
%16 = or i32 %15, %9
%17 = or i32 %16, %10
%18 = or i32 %17, %11
br label %30
19: ; preds = %3
%20 = load i32, i32* @S_IWGRP
%21 = load i32, i32* @S_ISGID
%22 = or i32 %6, %5
%23 = or i32 %22, %7
%24 = or i32 %23, %8
%25 = or i32 %24, %9
%26 = or i32 %25, %10
%27 = or i32 %26, %11
%28 = or i32 %27, %20
%29 = or i32 %28, %21
br label %30
30: ; preds = %19, %12
%31 = phi i32 [ %18, %12 ], [ %29, %19 ]
%32 = tail call i32 @posix_umask(i32 0)
%33 = load i32, i32* @S_IRWXG
%34 = tail call i32 (...) @posix_process_running_setgid()
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %30
%37 = and i32 %33, %31
%38 = xor i32 %37, -1
%39 = and i32 %32, %38
%40 = tail call i32 @posix_umask(i32 %39)
br label %48
41: ; preds = %30
%42 = load i32, i32* @S_IWUSR
%43 = load i32, i32* @S_IWGRP
%44 = load i32, i32* @S_IWOTH
%45 = or i32 %42, %31
%46 = or i32 %45, %43
%47 = or i32 %46, %44
br label %48
48: ; preds = %41, %36
%49 = phi i32 [ %31, %36 ], [ %47, %41 ]
%50 = tail call i32 @fileExists(i8* %0)
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %48
%53 = tail call i64 @posix_mkdir(i8* %0, i32 %49)
%54 = icmp eq i64 %53, 0
br i1 %54, label %57, label %55
55: ; preds = %52
%56 = tail call i32 @Error(i32 2, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %0)
br label %57
57: ; preds = %52, %55, %48
%58 = icmp ne i32 %2, 1
%59 = select i1 %58, i1 true, i1 %35
br i1 %59, label %62, label %60
60: ; preds = %57
%61 = tail call i32 @chmod(i8* %0, i32 %49)
br label %62
62: ; preds = %60, %57
%63 = tail call i32 @posix_umask(i32 %32)
ret void
}