#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 /*<<< orphan*/ gid_t;

/* Variables and functions */
scalar_t__ EINVAL;
int NGROUPS;
int /*<<< orphan*/ _SC_NGROUPS_MAX;
int /*<<< orphan*/ *calloc(size_t, int);
scalar_t__ errno;
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int getgrouplist(char const *, int /*<<< orphan*/, int /*<<< orphan*/ *, int *);
int setgroups(int, int /*<<< orphan*/ *);
scalar_t__ sysconf(int /*<<< orphan*/);
int initgroups(const char *uname, gid_t agroup) {
  gid_t groups_list[NGROUPS];
  int ngroups;
  gid_t *groups = groups_list;
  int rval;
  ngroups = NGROUPS;
  if (getgrouplist(uname, agroup, groups, &ngroups) == -1) {
    int maxgroups = ngroups;
    groups = calloc((size_t)maxgroups, sizeof *groups);
    if (groups == NULL)
      return -1;
    if (getgrouplist(uname, agroup, groups, &ngroups) == -1)
      ngroups = maxgroups;
  }
  rval = setgroups(ngroups, groups);
  if (rval == -1 && errno == EINVAL) {
    int ng = (int)sysconf(_SC_NGROUPS_MAX);
    if (ng > 0 && ng < ngroups)
      rval = setgroups(ng, groups);
  }
  if (groups != groups_list)
    free(groups);
  return rval;
}


x86 assembly (clang, -O0):
<initgroups>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
lea    0xf(,%rcx,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x50(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x28(%rbp)
mov    %rax,-0x38(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x38(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
call   1060 <getgrouplist@plt>
cmp    $0xffffffff,%eax
jne    1219 <initgroups+0xc9>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x40(%rbp)
movslq -0x40(%rbp),%rdi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11f1 <initgroups+0xa1>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12ab <initgroups+0x15b>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x38(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
call   1060 <getgrouplist@plt>
cmp    $0xffffffff,%eax
jne    1214 <initgroups+0xc4>
mov    -0x40(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    1219 <initgroups+0xc9>
mov    -0x2c(%rbp),%edi
mov    -0x38(%rbp),%rsi
call   1040 <setgroups@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    1287 <initgroups+0x137>
mov    0x2d9f(%rip),%rax
mov    (%rax),%rax
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%rax
jne    1287 <initgroups+0x137>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jle    1282 <initgroups+0x132>
mov    -0x48(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1282 <initgroups+0x132>
mov    -0x48(%rbp),%edi
mov    -0x38(%rbp),%rsi
call   1040 <setgroups@plt>
mov    %eax,-0x3c(%rbp)
jmp    1287 <initgroups+0x137>
mov    -0x50(%rbp),%rax
cmp    %rax,-0x38(%rbp)
je     129e <initgroups+0x14e>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<initgroups>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r12
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
lea    0xf(,%rax,4),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    %eax,-0x2c(%rbp)
lea    -0x2c(%rbp),%rcx
mov    %rbx,%rdx
call   1060 <getgrouplist@plt>
mov    %rbx,%r14
cmp    $0xffffffff,%eax
je     11fb <initgroups+0xab>
mov    -0x2c(%rbp),%edi
mov    %r14,%rsi
call   1040 <setgroups@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    11ec <initgroups+0x9c>
mov    0x2e25(%rip),%rax
mov    (%rax),%rax
mov    0x2e0b(%rip),%rcx
mov    $0xffffffff,%r15d
cmp    (%rcx),%rax
jne    11ec <initgroups+0x9c>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
test   %eax,%eax
jle    11ec <initgroups+0x9c>
cmp    %eax,-0x2c(%rbp)
jle    11ec <initgroups+0x9c>
mov    %eax,%edi
mov    %r14,%rsi
call   1040 <setgroups@plt>
mov    %eax,%r15d
cmp    %rbx,%r14
je     123e <initgroups+0xee>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    123e <initgroups+0xee>
mov    -0x2c(%rbp),%r13d
movslq %r13d,%rdi
mov    $0x4,%esi
call   1050 <calloc@plt>
test   %rax,%rax
je     1238 <initgroups+0xe8>
mov    %rax,%r14
lea    -0x2c(%rbp),%rcx
mov    %r12,%rdi
mov    %r15d,%esi
mov    %rax,%rdx
call   1060 <getgrouplist@plt>
cmp    $0xffffffff,%eax
jne    1199 <initgroups+0x49>
mov    %r13d,-0x2c(%rbp)
jmp    1199 <initgroups+0x49>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@NGROUPS = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@_SC_NGROUPS_MAX = global i32 0
define i32 @initgroups(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%14 = load i32, i32* @NGROUPS
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %6
%17 = alloca i32, i64 %15
store i64 %15, i64* %7
store i32* %17, i32** %9
%18 = load i32, i32* @NGROUPS
store i32 %18, i32* %8
%19 = load i8*, i8** %4
%20 = load i32, i32* %5
%21 = load i32*, i32** %9
%22 = call i32 @getgrouplist(i8* %19, i32 %20, i32* %21, i32* %8)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %41
24: ; preds = %2
%25 = load i32, i32* %8
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = sext i32 %26 to i64
%28 = call i32* @calloc(i64 %27, i32 4)
store i32* %28, i32** %9
%29 = load i32*, i32** %9
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %32
31: ; preds = %24
store i32 -1, i32* %3
store i32 1, i32* %12
br label %74
32: ; preds = %24
%33 = load i8*, i8** %4
%34 = load i32, i32* %5
%35 = load i32*, i32** %9
%36 = call i32 @getgrouplist(i8* %33, i32 %34, i32* %35, i32* %8)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = load i32, i32* %11
store i32 %39, i32* %8
br label %40
40: ; preds = %38, %32
br label %41
41: ; preds = %40, %2
%42 = load i32, i32* %8
%43 = load i32*, i32** %9
%44 = call i32 @setgroups(i32 %42, i32* %43)
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %66
47: ; preds = %41
%48 = load i64, i64* @errno
%49 = load i64, i64* @EINVAL
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %66
51: ; preds = %47
%52 = load i32, i32* @_SC_NGROUPS_MAX
%53 = call i64 @sysconf(i32 %52)
%54 = trunc i64 %53 to i32
store i32 %54, i32* %13
%55 = load i32, i32* %13
%56 = icmp sgt i32 %55, 0
br i1 %56, label %57, label %65
57: ; preds = %51
%58 = load i32, i32* %13
%59 = load i32, i32* %8
%60 = icmp slt i32 %58, %59
br i1 %60, label %61, label %65
61: ; preds = %57
%62 = load i32, i32* %13
%63 = load i32*, i32** %9
%64 = call i32 @setgroups(i32 %62, i32* %63)
store i32 %64, i32* %10
br label %65
65: ; preds = %61, %57, %51
br label %66
66: ; preds = %65, %47, %41
%67 = load i32*, i32** %9
%68 = icmp ne i32* %67, %17
br i1 %68, label %69, label %72
69: ; preds = %66
%70 = load i32*, i32** %9
%71 = call i32 @free(i32* %70)
br label %72
72: ; preds = %69, %66
%73 = load i32, i32* %10
store i32 %73, i32* %3
store i32 1, i32* %12
br label %74
74: ; preds = %72, %31
%75 = load i8*, i8** %6
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@NGROUPS = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@_SC_NGROUPS_MAX = global i32 0
define i32 @initgroups(i8* %0, i32 %1) {
%3 = alloca i32
%4 = load i32, i32* @NGROUPS
%5 = zext i32 %4 to i64
%6 = alloca i32, i64 %5
%7 = bitcast i32* %3 to i8*
store i32 %4, i32* %3
%8 = call i32 @getgrouplist(i8* %0, i32 %1, i32* %6, i32* %3)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %19
10: ; preds = %2
%11 = load i32, i32* %3
%12 = sext i32 %11 to i64
%13 = call i32* @calloc(i64 %12, i32 4)
%14 = icmp eq i32* %13, null
br i1 %14, label %43, label %15
15: ; preds = %10
%16 = call i32 @getgrouplist(i8* %0, i32 %1, i32* %13, i32* %3)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 %11, i32* %3
br label %19
19: ; preds = %18, %15, %2
%20 = phi i32* [ %6, %2 ], [ %13, %15 ], [ %13, %18 ]
%21 = load i32, i32* %3
%22 = call i32 @setgroups(i32 %21, i32* %20)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %38
24: ; preds = %19
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINVAL
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %38
28: ; preds = %24
%29 = load i32, i32* @_SC_NGROUPS_MAX
%30 = call i64 @sysconf(i32 %29)
%31 = trunc i64 %30 to i32
%32 = icmp sgt i32 %31, 0
%33 = load i32, i32* %3
%34 = icmp sgt i32 %33, %31
%35 = select i1 %32, i1 %34, i1 false
br i1 %35, label %36, label %38
36: ; preds = %28
%37 = call i32 @setgroups(i32 %31, i32* %20)
br label %38
38: ; preds = %28, %36, %24, %19
%39 = phi i32 [ -1, %24 ], [ %22, %19 ], [ %37, %36 ], [ -1, %28 ]
%40 = icmp eq i32* %20, %6
br i1 %40, label %43, label %41
41: ; preds = %38
%42 = call i32 @free(i32* %20)
br label %43
43: ; preds = %10, %38, %41
%44 = phi i32 [ %39, %41 ], [ %39, %38 ], [ -1, %10 ]
ret i32 %44
}