#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 scalar_t__ uid_t;
struct group {
  scalar_t__ gr_gid;
};
typedef scalar_t__ gid_t;

/* Variables and functions */
int NGROUPS_MAX;
struct group *getgrnam(int /*<<< orphan*/);
int getgroups(int, scalar_t__ *);
scalar_t__ getuid();
int /*<<< orphan*/ groupNameForTask(int);
scalar_t__ isAuthenticatedAsRoot();
int isAuthenticatedAsAdministratorForTask(int taskNum) {
  int admin = 0;
  uid_t ruid;
  if (isAuthenticatedAsRoot()) {
    return 1;
  }
  ruid = getuid();
  if (ruid) {
    gid_t groups[NGROUPS_MAX];
    int numgroups;
    if ((numgroups = getgroups(NGROUPS_MAX, groups)) > 0) {
      int i;
      gid_t admingid = 0;
      struct group *admingroup;
      if ((admingroup = getgrnam(groupNameForTask(taskNum))) != NULL) {
        admingid = admingroup->gr_gid;
        for (i = 0; i < numgroups; i++) {
          if (groups[i] == admingid) {
            admin = 1;
            break;
          }
        }
      }
    }
  }
  return admin;
}


x86 assembly (clang, -O0):
<isAuthenticatedAsAdministratorForTask>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1060 <isAuthenticatedAsRoot@plt>
cmp    $0x0,%rax
je     117f <isAuthenticatedAsAdministratorForTask+0x2f>
movl   $0x1,-0x4(%rbp)
jmp    126b <isAuthenticatedAsAdministratorForTask+0x11b>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1265 <isAuthenticatedAsAdministratorForTask+0x115>
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
lea    0xf(,%rax,8),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x48(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x28(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1050 <getgroups@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jle    125e <isAuthenticatedAsAdministratorForTask+0x10e>
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <groupNameForTask@plt>
mov    %eax,%edi
call   1040 <getgrnam@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     1259 <isAuthenticatedAsAdministratorForTask+0x109>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1254 <isAuthenticatedAsAdministratorForTask+0x104>
mov    -0x48(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
cmp    -0x38(%rbp),%rax
jne    1241 <isAuthenticatedAsAdministratorForTask+0xf1>
movl   $0x1,-0xc(%rbp)
jmp    1254 <isAuthenticatedAsAdministratorForTask+0x104>
jmp    1246 <isAuthenticatedAsAdministratorForTask+0xf6>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1213 <isAuthenticatedAsAdministratorForTask+0xc3>
jmp    1259 <isAuthenticatedAsAdministratorForTask+0x109>
jmp    125e <isAuthenticatedAsAdministratorForTask+0x10e>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isAuthenticatedAsAdministratorForTask>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
xor    %eax,%eax
call   1060 <isAuthenticatedAsRoot@plt>
mov    $0x1,%ebx
test   %rax,%rax
jne    11fb <isAuthenticatedAsAdministratorForTask+0xab>
xor    %eax,%eax
call   1030 <getuid@plt>
mov    $0x0,%ebx
test   %rax,%rax
je     11fb <isAuthenticatedAsAdministratorForTask+0xab>
mov    %rsp,%r13
mov    0x2e57(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%r15
lea    0xf(,%rcx,8),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r15
mov    %r15,%rsp
mov    (%rax),%edi
mov    %r15,%rsi
call   1050 <getgroups@plt>
xor    %ebx,%ebx
test   %eax,%eax
jle    11f8 <isAuthenticatedAsAdministratorForTask+0xa8>
mov    %eax,%r12d
mov    %r14d,%edi
call   1070 <groupNameForTask@plt>
mov    %eax,%edi
call   1040 <getgrnam@plt>
test   %rax,%rax
je     11f8 <isAuthenticatedAsAdministratorForTask+0xa8>
mov    (%rax),%rax
mov    %r12d,%ecx
xor    %edx,%edx
nopw   0x0(%rax,%rax,1)
cmp    %rax,(%r15,%rdx,8)
je     11f3 <isAuthenticatedAsAdministratorForTask+0xa3>
add    $0x1,%rdx
cmp    %rdx,%rcx
jne    11e0 <isAuthenticatedAsAdministratorForTask+0x90>
xor    %ebx,%ebx
jmp    11f8 <isAuthenticatedAsAdministratorForTask+0xa8>
mov    $0x1,%ebx
mov    %r13,%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.group = type { i64 }
@NGROUPS_MAX = global i32 0
define i32 @isAuthenticatedAsAdministratorForTask(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca %struct.group*
store i32 %0, i32* %3
store i32 0, i32* %4
%12 = call i64 (...) @isAuthenticatedAsRoot()
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 1, i32* %2
br label %58
15: ; preds = %1
%16 = call i64 (...) @getuid()
store i64 %16, i64* %5
%17 = load i64, i64* %5
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %56
19: ; preds = %15
%20 = load i32, i32* @NGROUPS_MAX
%21 = zext i32 %20 to i64
%22 = call i8* @llvm.stacksave()
store i8* %22, i8** %6
%23 = alloca i64, i64 %21
store i64 %21, i64* %7
%24 = load i32, i32* @NGROUPS_MAX
%25 = call i32 @getgroups(i32 %24, i64* %23)
store i32 %25, i32* %8
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %54
27: ; preds = %19
store i64 0, i64* %10
%28 = load i32, i32* %3
%29 = call i32 @groupNameForTask(i32 %28)
%30 = call %struct.group* @getgrnam(i32 %29)
store %struct.group* %30, %struct.group** %11
%31 = icmp ne %struct.group* %30, null
br i1 %31, label %32, label %53
32: ; preds = %27
%33 = load %struct.group*, %struct.group** %11
%34 = getelementptr inbounds %struct.group, %struct.group* %33, i32 0, i32 0
%35 = load i64, i64* %34
store i64 %35, i64* %10
store i32 0, i32* %9
br label %36
36: ; preds = %49, %32
%37 = load i32, i32* %9
%38 = load i32, i32* %8
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %52
40: ; preds = %36
%41 = load i32, i32* %9
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i64, i64* %23, i64 %42
%44 = load i64, i64* %43
%45 = load i64, i64* %10
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %40
store i32 1, i32* %4
br label %52
48: ; preds = %40
br label %49
49: ; preds = %48
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 1
store i32 %51, i32* %9
br label %36
52: ; preds = %47, %36
br label %53
53: ; preds = %52, %27
br label %54
54: ; preds = %53, %19
%55 = load i8*, i8** %6
br label %56
56: ; preds = %54, %15
%57 = load i32, i32* %4
store i32 %57, i32* %2
br label %58
58: ; preds = %56, %14
%59 = load i32, i32* %2
ret i32 %59
}

LLVM IR (-O2):
%struct.group = type { i64 }
@NGROUPS_MAX = global i32 0
define i32 @isAuthenticatedAsAdministratorForTask(i32 %0) {
%2 = tail call i64 (...) @isAuthenticatedAsRoot()
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %33
4: ; preds = %1
%5 = tail call i64 (...) @getuid()
%6 = icmp eq i64 %5, 0
br i1 %6, label %33, label %7
7: ; preds = %4
%8 = load i32, i32* @NGROUPS_MAX
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i64, i64 %9
%12 = load i32, i32* @NGROUPS_MAX
%13 = call i32 @getgroups(i32 %12, i64* %11)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %31
15: ; preds = %7
%16 = call i32 @groupNameForTask(i32 %0)
%17 = call %struct.group* @getgrnam(i32 %16)
%18 = icmp eq %struct.group* %17, null
br i1 %18, label %31, label %19
19: ; preds = %15
%20 = getelementptr inbounds %struct.group, %struct.group* %17, i64 0, i32 0
%21 = load i64, i64* %20
%22 = zext i32 %13 to i64
br label %25
23: ; preds = %25
%24 = icmp eq i64 %30, %22
br i1 %24, label %31, label %25
25: ; preds = %19, %23
%26 = phi i64 [ 0, %19 ], [ %30, %23 ]
%27 = getelementptr inbounds i64, i64* %11, i64 %26
%28 = load i64, i64* %27
%29 = icmp eq i64 %28, %21
%30 = add nuw nsw i64 %26, 1
br i1 %29, label %31, label %23
31: ; preds = %25, %23, %15, %7
%32 = phi i32 [ 0, %7 ], [ 0, %15 ], [ 1, %25 ], [ 0, %23 ]
br label %33
33: ; preds = %4, %31, %1
%34 = phi i32 [ 1, %1 ], [ %32, %31 ], [ 0, %4 ]
ret i32 %34
}