#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 group {
  char *gr_name;
};
typedef int gid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ _SC_NGROUPS_MAX;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
struct group *getgrgid(int);
void *getgroups(size_t, int *);
int *malloc(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
size_t sysconf(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int result;
  gid_t *groups;
  struct group *grp;
  size_t grpmax;
  grpmax = sysconf(_SC_NGROUPS_MAX);
  if (grpmax == -1)
    grpmax = 16;
  printf("grpmax = %d\n", grpmax);
  grpmax = getgroups(0, 0);
  printf("grpmax by getgroups = %d\n", grpmax);
  groups = malloc(grpmax);
  if (groups == NULL) {
    perror("malloc");
    exit(EXIT_FAILURE);
  }
  result = getgroups(grpmax, groups);
  if (result == -1) {
    perror("getgroups");
    exit(EXIT_FAILURE);
  }
  printf("group count: %d\n", result);
  for (int i = 0; i < result; ++i) {
    grp = getgrgid(groups[i]);
    printf("  [%d] %s\n", groups[i], grp->gr_name);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    11ab <main+0x3b>
movq   $0x10,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %eax,%esi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%esi
mov    %rsi,%rdi
call   1050 <getgroups@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %eax,%esi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1215 <main+0xa5>
lea    0xe25(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <getgroups@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1249 <main+0xd9>
lea    0xdf8(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%esi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12af <main+0x13f>
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1040 <getgrgid@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%rdx
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1261 <main+0xf1>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
cmp    $0xffffffffffffffff,%rax
mov    $0x10,%esi
cmovne %eax,%esi
lea    0xe67(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1050 <getgroups@plt>
mov    %rax,%r14
lea    0xe5a(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r14,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
jne    11d3 <main+0x63>
lea    0xe56(%rip),%rdi
jmp    11f1 <main+0x81>
mov    %rax,%r12
mov    %r14,%rdi
mov    %rax,%rsi
call   1050 <getgroups@plt>
mov    %rax,%r14
cmp    $0xffffffff,%r14d
jne    1204 <main+0x94>
lea    0xe3d(%rip),%rdi
call   1070 <perror@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xe2d(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1030 <printf@plt>
test   %r14d,%r14d
jle    1253 <main+0xe3>
mov    %r14d,%r15d
lea    0xe25(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    (%r12,%rbx,4),%edi
call   1040 <getgrgid@plt>
mov    (%r12,%rbx,4),%esi
mov    (%rax),%rdx
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1230 <main+0xc0>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.group = type { i8* }
@_SC_NGROUPS_MAX = global i32 0
@.str = [13 x i8] c"grpmax = %d\0A\00"
@.str.1 = [26 x i8] c"grpmax by getgroups = %d\0A\00"
@.str.2 = [7 x i8] c"malloc\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [10 x i8] c"getgroups\00"
@.str.4 = [17 x i8] c"group count: %d\0A\00"
@.str.5 = [11 x i8] c" [%d] %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.group*
%9 = alloca i64
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @_SC_NGROUPS_MAX
%12 = call i64 @sysconf(i32 %11)
store i64 %12, i64* %9
%13 = load i64, i64* %9
%14 = icmp eq i64 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %2
store i64 16, i64* %9
br label %16
16: ; preds = %15, %2
%17 = load i64, i64* %9
%18 = trunc i64 %17 to i32
%19 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = call i8* @getgroups(i64 0, i32* null)
%21 = ptrtoint i8* %20 to i64
store i64 %21, i64* %9
%22 = load i64, i64* %9
%23 = trunc i64 %22 to i32
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = load i64, i64* %9
%26 = call i32* @malloc(i64 %25)
store i32* %26, i32** %7
%27 = load i32*, i32** %7
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %33
29: ; preds = %16
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %16
%34 = load i64, i64* %9
%35 = load i32*, i32** %7
%36 = call i8* @getgroups(i64 %34, i32* %35)
%37 = ptrtoint i8* %36 to i32
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %44
40: ; preds = %33
%41 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %33
%45 = load i32, i32* %6
%46 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %45)
store i32 0, i32* %10
br label %47
47: ; preds = %67, %44
%48 = load i32, i32* %10
%49 = load i32, i32* %6
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %70
51: ; preds = %47
%52 = load i32*, i32** %7
%53 = load i32, i32* %10
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %52, i64 %54
%56 = load i32, i32* %55
%57 = call %struct.group* @getgrgid(i32 %56)
store %struct.group* %57, %struct.group** %8
%58 = load i32*, i32** %7
%59 = load i32, i32* %10
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i32, i32* %58, i64 %60
%62 = load i32, i32* %61
%63 = load %struct.group*, %struct.group** %8
%64 = getelementptr inbounds %struct.group, %struct.group* %63, i32 0, i32 0
%65 = load i8*, i8** %64
%66 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i32 %62, i8* %65)
br label %67
67: ; preds = %51
%68 = load i32, i32* %10
%69 = add nsw i32 %68, 1
store i32 %69, i32* %10
br label %47
70: ; preds = %47
%71 = load i32, i32* @EXIT_SUCCESS
%72 = call i32 @exit(i32 %71)
unreachable
}

LLVM IR (-O2):
%struct.group = type { i8* }
@_SC_NGROUPS_MAX = global i32 0
@.str = [13 x i8] c"grpmax = %d\0A\00"
@.str.1 = [26 x i8] c"grpmax by getgroups = %d\0A\00"
@.str.2 = [7 x i8] c"malloc\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [10 x i8] c"getgroups\00"
@.str.4 = [17 x i8] c"group count: %d\0A\00"
@.str.5 = [11 x i8] c" [%d] %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @_SC_NGROUPS_MAX
%4 = tail call i64 @sysconf(i32 %3)
%5 = icmp eq i64 %4, -1
%6 = trunc i64 %4 to i32
%7 = select i1 %5, i32 16, i32 %6
%8 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = tail call i8* @getgroups(i64 0, i32* null)
%10 = ptrtoint i8* %9 to i64
%11 = trunc i64 %10 to i32
%12 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = tail call i32* @malloc(i64 %10)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = tail call i8* @getgroups(i64 %10, i32* %13)
%21 = ptrtoint i8* %20 to i64
%22 = trunc i64 %21 to i32
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %19
%29 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%30 = icmp sgt i32 %22, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = and i64 %21, 4294967295
br label %36
33: ; preds = %36, %28
%34 = load i32, i32* @EXIT_SUCCESS
%35 = tail call i32 @exit(i32 %34)
unreachable
36: ; preds = %31, %36
%37 = phi i64 [ 0, %31 ], [ %45, %36 ]
%38 = getelementptr inbounds i32, i32* %13, i64 %37
%39 = load i32, i32* %38
%40 = tail call %struct.group* @getgrgid(i32 %39)
%41 = load i32, i32* %38
%42 = getelementptr inbounds %struct.group, %struct.group* %40, i64 0, i32 0
%43 = load i8*, i8** %42
%44 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i32 %41, i8* %43)
%45 = add nuw nsw i64 %37, 1
%46 = icmp eq i64 %45, %32
br i1 %46, label %33, label %36
}