#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 NGROUPS_MAX;
scalar_t__ getgrouplist(char const *, int /*<<< orphan*/, int /*<<< orphan*/ *,
                        int *);
int setgroups(int, int /*<<< orphan*/ *);
int initgroups(const char *user, gid_t gid) {
  gid_t groups[NGROUPS_MAX];
  int count = NGROUPS_MAX;
  if (getgrouplist(user, gid, groups, &count) < 0)
    return -1;
  return setgroups(count, groups);
}


x86 assembly (clang, -O0):
<initgroups>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2eb2(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
lea    0xf(,%rax,4),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,-0x38(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x28(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x2c(%rbp),%rcx
call   1040 <getgrouplist@plt>
cmp    $0x0,%rax
jge    1195 <initgroups+0x75>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    11ab <initgroups+0x8b>
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%edi
call   1030 <setgroups@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x30(%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   %rbx
push   %rax
mov    0x2ebb(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
lea    0xf(,%rax,4),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    %eax,-0xc(%rbp)
lea    -0xc(%rbp),%rcx
mov    %rbx,%rdx
call   1040 <getgrouplist@plt>
test   %rax,%rax
js     1165 <initgroups+0x45>
mov    -0xc(%rbp),%edi
mov    %rbx,%rsi
call   1030 <setgroups@plt>
jmp    116a <initgroups+0x4a>
mov    $0xffffffff,%eax
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@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
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i32, i32* @NGROUPS_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i32, i64 %11
store i64 %11, i64* %7
%14 = load i32, i32* @NGROUPS_MAX
store i32 %14, i32* %8
%15 = load i8*, i8** %4
%16 = load i32, i32* %5
%17 = call i64 @getgrouplist(i8* %15, i32 %16, i32* %13, i32* %8)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %9
br label %23
20: ; preds = %2
%21 = load i32, i32* %8
%22 = call i32 @setgroups(i32 %21, i32* %13)
store i32 %22, i32* %3
store i32 1, i32* %9
br label %23
23: ; preds = %20, %19
%24 = load i8*, i8** %6
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@NGROUPS_MAX = global i32 0
define i32 @initgroups(i8* %0, i32 %1) {
%3 = alloca i32
%4 = load i32, i32* @NGROUPS_MAX
%5 = zext i32 %4 to i64
%6 = alloca i32, i64 %5
%7 = bitcast i32* %3 to i8*
store i32 %4, i32* %3
%8 = call i64 @getgrouplist(i8* %0, i32 %1, i32* %6, i32* %3)
%9 = icmp slt i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32, i32* %3
%12 = call i32 @setgroups(i32 %11, i32* %6)
br label %13
13: ; preds = %2, %10
%14 = phi i32 [ %12, %10 ], [ -1, %2 ]
ret i32 %14
}