#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 */
int ENOMEM;
int /*<<< orphan*/ _SC_NGROUPS_MAX;
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ getgrouplist(char const *, int /*<<< orphan*/,
                                int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ *malloc(int);
int setgroups(int, int /*<<< orphan*/ *);
int sysconf(int /*<<< orphan*/);
int initgroups(const char *uname, gid_t agroup) {
  int ngroups, ret;
  long ngroups_max;
  gid_t *groups;
  ngroups_max = sysconf(_SC_NGROUPS_MAX) + 2;
  if ((groups = malloc(sizeof(*groups) * ngroups_max)) == NULL)
    return (ENOMEM);
  ngroups = (int)ngroups_max;
  getgrouplist(uname, agroup, groups, &ngroups);
  ret = setgroups(ngroups, groups);
  free(groups);
  return (ret);
}


x86 assembly (clang, -O0):
<initgroups>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
add    $0x2,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11a4 <initgroups+0x54>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11dd <initgroups+0x8d>
mov    -0x28(%rbp),%rax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x30(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1060 <getgrouplist@plt>
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   1040 <setgroups@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<initgroups>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %eax,%ebp
lea    0x8(,%rbp,4),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11b3 <initgroups+0x63>
mov    %rax,%rbx
add    $0x2,%ebp
mov    %ebp,0x4(%rsp)
lea    0x4(%rsp),%rcx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rax,%rdx
call   1060 <getgrouplist@plt>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
call   1040 <setgroups@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11bc <initgroups+0x6c>
mov    0x2e1e(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@_SC_NGROUPS_MAX = global i32 0
@ENOMEM = global i32 0
define i32 @initgroups(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i32, i32* @_SC_NGROUPS_MAX
%11 = call i32 @sysconf(i32 %10)
%12 = add nsw i32 %11, 2
%13 = sext i32 %12 to i64
store i64 %13, i64* %8
%14 = load i64, i64* %8
%15 = mul i64 4, %14
%16 = trunc i64 %15 to i32
%17 = call i32* @malloc(i32 %16)
store i32* %17, i32** %9
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = load i32, i32* @ENOMEM
store i32 %20, i32* %3
br label %34
21: ; preds = %2
%22 = load i64, i64* %8
%23 = trunc i64 %22 to i32
store i32 %23, i32* %6
%24 = load i8*, i8** %4
%25 = load i32, i32* %5
%26 = load i32*, i32** %9
%27 = call i32 @getgrouplist(i8* %24, i32 %25, i32* %26, i32* %6)
%28 = load i32, i32* %6
%29 = load i32*, i32** %9
%30 = call i32 @setgroups(i32 %28, i32* %29)
store i32 %30, i32* %7
%31 = load i32*, i32** %9
%32 = call i32 @free(i32* %31)
%33 = load i32, i32* %7
store i32 %33, i32* %3
br label %34
34: ; preds = %21, %19
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@_SC_NGROUPS_MAX = global i32 0
@ENOMEM = global i32 0
define i32 @initgroups(i8* %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @_SC_NGROUPS_MAX
%6 = tail call i32 @sysconf(i32 %5)
%7 = add nsw i32 %6, 2
%8 = shl i32 %7, 2
%9 = tail call i32* @malloc(i32 %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i32, i32* @ENOMEM
br label %18
13: ; preds = %2
store i32 %7, i32* %3
%14 = call i32 @getgrouplist(i8* %0, i32 %1, i32* %9, i32* %3)
%15 = load i32, i32* %3
%16 = call i32 @setgroups(i32 %15, i32* %9)
%17 = call i32 @free(i32* %9)
br label %18
18: ; preds = %13, %11
%19 = phi i32 [ %12, %11 ], [ %16, %13 ]
ret i32 %19
}