#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 */

/* Variables and functions */
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int /*<<< orphan*/ chmod(char *, int);
int posix_process_running_setgid();
void SetFilePermissions(char *filename, int permission_class) {
  int running_setgid = posix_process_running_setgid();
  int perms = (permission_class == 0
                   ? ((S_IRUSR | S_IRGRP | S_IROTH) | (S_IWUSR))
                   : ((S_IRUSR | S_IRGRP | S_IROTH) | (S_IWUSR | S_IWGRP)));
  if (permission_class == 1 && !running_setgid)
    perms |= (S_IWUSR | S_IWGRP | S_IWOTH);
  chmod(filename, perms);
}


x86 assembly (clang, -O0):
<SetFilePermissions>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1040 <posix_process_running_setgid@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    116f <SetFilePermissions+0x4f>
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    0x2e7d(%rip),%rcx
or     (%rcx),%eax
mov    0x2e6c(%rip),%rcx
or     (%rcx),%eax
mov    0x2e7b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
jmp    11a1 <SetFilePermissions+0x81>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    0x2e51(%rip),%rcx
or     (%rcx),%eax
mov    0x2e40(%rip),%rcx
or     (%rcx),%eax
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e4e(%rip),%rdx
or     (%rdx),%ecx
or     %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    11dc <SetFilePermissions+0xbc>
cmpl   $0x0,-0x10(%rbp)
jne    11dc <SetFilePermissions+0xbc>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    0x2e1d(%rip),%rcx
or     (%rcx),%eax
mov    0x2ddc(%rip),%rcx
or     (%rcx),%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <chmod@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SetFilePermissions>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <posix_process_running_setgid@plt>
mov    0x2e8a(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e91(%rip),%rdx
mov    (%rdx),%esi
test   %ebp,%ebp
je     1174 <SetFilePermissions+0x54>
mov    0x2e7c(%rip),%rdx
mov    0x2e8d(%rip),%rdi
or     %ecx,%esi
or     (%rdx),%esi
or     (%rdi),%esi
mov    0x2e88(%rip),%rcx
or     (%rcx),%esi
cmp    $0x1,%ebp
jne    1188 <SetFilePermissions+0x68>
test   %eax,%eax
jne    1188 <SetFilePermissions+0x68>
mov    0x2e3e(%rip),%rax
jmp    1186 <SetFilePermissions+0x66>
or     %ecx,%esi
mov    0x2e4b(%rip),%rax
or     (%rax),%esi
mov    0x2e5a(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <chmod@plt>

LLVM IR (-O0):
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@S_IWUSR = global i32 0
@S_IWGRP = global i32 0
@S_IWOTH = global i32 0
define void @SetFilePermissions(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = call i32 (...) @posix_process_running_setgid()
store i32 %7, i32* %5
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %2
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IRGRP
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IROTH
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %15, %16
br label %28
18: ; preds = %2
%19 = load i32, i32* @S_IRUSR
%20 = load i32, i32* @S_IRGRP
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IROTH
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IWUSR
%25 = load i32, i32* @S_IWGRP
%26 = or i32 %24, %25
%27 = or i32 %23, %26
br label %28
28: ; preds = %18, %10
%29 = phi i32 [ %17, %10 ], [ %27, %18 ]
store i32 %29, i32* %6
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, 1
br i1 %31, label %32, label %43
32: ; preds = %28
%33 = load i32, i32* %5
%34 = icmp ne i32 %33, 0
br i1 %34, label %43, label %35
35: ; preds = %32
%36 = load i32, i32* @S_IWUSR
%37 = load i32, i32* @S_IWGRP
%38 = or i32 %36, %37
%39 = load i32, i32* @S_IWOTH
%40 = or i32 %38, %39
%41 = load i32, i32* %6
%42 = or i32 %41, %40
store i32 %42, i32* %6
br label %43
43: ; preds = %35, %32, %28
%44 = load i8*, i8** %3
%45 = load i32, i32* %6
%46 = call i32 @chmod(i8* %44, i32 %45)
ret void
}

LLVM IR (-O2):
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@S_IWUSR = global i32 0
@S_IWGRP = global i32 0
@S_IWOTH = global i32 0
define void @SetFilePermissions(i8* nocapture %0, i32 %1) {
%3 = tail call i32 (...) @posix_process_running_setgid()
%4 = icmp eq i32 %1, 0
%5 = load i32, i32* @S_IRUSR
%6 = load i32, i32* @S_IRGRP
br i1 %4, label %7, label %13
7: ; preds = %2
%8 = or i32 %6, %5
%9 = load i32, i32* @S_IROTH
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IWUSR
%12 = or i32 %10, %11
br label %27
13: ; preds = %2
%14 = load i32, i32* @S_IROTH
%15 = load i32, i32* @S_IWUSR
%16 = load i32, i32* @S_IWGRP
%17 = or i32 %6, %5
%18 = or i32 %17, %14
%19 = or i32 %18, %15
%20 = or i32 %19, %16
%21 = icmp ne i32 %1, 1
%22 = icmp ne i32 %3, 0
%23 = select i1 %21, i1 true, i1 %22
br i1 %23, label %27, label %24
24: ; preds = %13
%25 = load i32, i32* @S_IWOTH
%26 = or i32 %20, %25
br label %27
27: ; preds = %7, %24, %13
%28 = phi i32 [ %20, %13 ], [ %26, %24 ], [ %12, %7 ]
%29 = tail call i32 @chmod(i8* %0, i32 %28)
ret void
}