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

/* Variables and functions */
int EPERM;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int getgroups(int, int *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setgroups(int, int *);
int /*<<< orphan*/ stderr;
int main() {
  int group_ct;
  gid_t groups[128];
  group_ct = getgroups(128, groups);
  if (group_ct == -1) {
    printf("ERROR\tgetgroups(2) failed with errno=%d\n", errno);
    return 1;
  }
  fprintf(stderr, "found %d groups; trying to drop last group %d\n", group_ct,
          groups[group_ct - 1]);
  if (setgroups(group_ct - 1, groups)) {
    if (errno == EPERM) {
      printf("SAFE\tsetgroups(2) failed with EPERM\n");
      return 0;
    } else {
      printf("ERROR\tsetgroups(2) failed with errno=%d\n", errno);
      return 1;
    }
  } else {
    printf("RISK\tsetgroups(2) succeeded\n");
    return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x210,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x210(%rbp),%rsi
mov    $0x80,%edi
call   1060 <getgroups@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1193 <main+0x53>
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1240 <main+0x100>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
cltq
mov    -0x210(%rbp,%rax,4),%ecx
lea    0xe74(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
sub    $0x1,%edi
lea    -0x210(%rbp),%rsi
call   1040 <setgroups@plt>
cmp    $0x0,%rax
je     122b <main+0xeb>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    0x2de2(%rip),%rcx
cmp    (%rcx),%eax
jne    1208 <main+0xc8>
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1240 <main+0x100>
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1240 <main+0x100>
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x210,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x200,%rsp
mov    %rsp,%rsi
mov    $0x80,%edi
call   1070 <getgroups@plt>
cmp    $0xffffffff,%eax
je     11c0 <main+0x70>
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%edi
movslq %eax,%rcx
mov    %eax,%ebx
add    $0xffffffff,%ebx
mov    -0x4(%rsp,%rcx,4),%ecx
lea    0xea3(%rip),%rsi
mov    %eax,%edx
call   1060 <fprintf@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1050 <setgroups@plt>
test   %rax,%rax
je     11d2 <main+0x82>
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    0x2e1c(%rip),%rax
cmp    (%rax),%esi
jne    11e0 <main+0x90>
lea    0xee6(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    11f3 <main+0xa3>
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
lea    0xe30(%rip),%rdi
jmp    11e7 <main+0x97>
lea    0xea8(%rip),%rdi
call   1030 <puts@plt>
jmp    11ee <main+0x9e>
lea    0xe71(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%eax
add    $0x200,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [41 x i8] c"ERROR\09getgroups(2) failed with errno=%d\0A\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [47 x i8] c"found %d groups; trying to drop last group %d\0A\00"
@EPERM = global i32 0
@.str.2 = [37 x i8] c"SAFE\09setgroups(2) failed with EPERM\0A\00"
@.str.3 = [41 x i8] c"ERROR\09setgroups(2) failed with errno=%d\0A\00"
@.str.4 = [29 x i8] c"RISK\09setgroups(2) succeeded\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [128 x i32]
store i32 0, i32* %1
%4 = getelementptr inbounds [128 x i32], [128 x i32]* %3, i64 0, i64 0
%5 = call i32 @getgroups(i32 128, i32* %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* @errno
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 1, i32* %1
br label %36
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = load i32, i32* %2
%14 = load i32, i32* %2
%15 = sub nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [128 x i32], [128 x i32]* %3, i64 0, i64 %16
%18 = load i32, i32* %17
%19 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %18)
%20 = load i32, i32* %2
%21 = sub nsw i32 %20, 1
%22 = getelementptr inbounds [128 x i32], [128 x i32]* %3, i64 0, i64 0
%23 = call i64 @setgroups(i32 %21, i32* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %11
%26 = load i32, i32* @errno
%27 = load i32, i32* @EPERM
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %36
31: ; preds = %25
%32 = load i32, i32* @errno
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %32)
store i32 1, i32* %1
br label %36
34: ; preds = %11
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %36
36: ; preds = %34, %31, %29, %8
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
@.str = [41 x i8] c"ERROR\09getgroups(2) failed with errno=%d\0A\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [47 x i8] c"found %d groups; trying to drop last group %d\0A\00"
@EPERM = global i32 0
@.str.3 = [41 x i8] c"ERROR\09setgroups(2) failed with errno=%d\0A\00"
@str = [28 x i8] c"RISK\09setgroups(2) succeeded\00"
@str.5 = [36 x i8] c"SAFE\09setgroups(2) failed with EPERM\00"
define i32 @main() {
%1 = alloca [128 x i32]
%2 = bitcast [128 x i32]* %1 to i8*
%3 = getelementptr inbounds [128 x i32], [128 x i32]* %1, i64 0, i64 0
%4 = call i32 @getgroups(i32 128, i32* %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @errno
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %28
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = add nsw i32 %4, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [128 x i32], [128 x i32]* %1, i64 0, i64 %12
%14 = load i32, i32* %13
%15 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %4, i32 %14)
%16 = call i64 @setgroups(i32 %11, i32* %3)
%17 = icmp eq i64 %16, 0
br i1 %17, label %26, label %18
18: ; preds = %9
%19 = load i32, i32* @errno
%20 = load i32, i32* @EPERM
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.5, i64 0, i64 0))
br label %28
24: ; preds = %18
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %19)
br label %28
26: ; preds = %9
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %28
28: ; preds = %26, %24, %22, %6
%29 = phi i32 [ 1, %6 ], [ 0, %22 ], [ 1, %24 ], [ 1, %26 ]
ret i32 %29
}