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

/* Variables and functions */
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ getgrouplist(char const *, int /*<<< orphan*/, int /*<<< orphan*/ *,
                        int *);
int /*<<< orphan*/ *malloc(int);
int setgroups(int, int /*<<< orphan*/ *);
int initgroups(const char *user, gid_t group) {
  gid_t groups0[2];
  gid_t *groups = groups0;
  int ret = -1;
  int numgroups = 2;
  if (getgrouplist(user, group, groups, &numgroups) < 0) {
    groups = malloc(numgroups * sizeof(groups[0]));
    if (groups == NULL)
      return -1;
    if (getgrouplist(user, group, groups, &numgroups) < 0) {
      goto EXIT;
    }
  }
  ret = setgroups(numgroups, groups);
EXIT:
  if (groups != groups0)
    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)
lea    -0x1c(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
movl   $0x2,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1060 <getgrouplist@plt>
cmp    $0x0,%rax
jge    11d5 <initgroups+0x95>
movslq -0x30(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ad <initgroups+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1204 <initgroups+0xc4>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1060 <getgrouplist@plt>
cmp    $0x0,%rax
jge    11d0 <initgroups+0x90>
jmp    11e4 <initgroups+0xa4>
jmp    11d5 <initgroups+0x95>
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1040 <setgroups@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
lea    -0x1c(%rbp),%rcx
cmp    %rcx,%rax
je     11fe <initgroups+0xbe>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<initgroups>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
movl   $0x2,0x4(%rsp)
lea    0x8(%rsp),%rdx
lea    0x4(%rsp),%rcx
call   1060 <getgrouplist@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
js     117b <initgroups+0x3b>
lea    0x8(%rsp),%rsi
call   1040 <setgroups@plt>
mov    %eax,%ebx
jmp    11c6 <initgroups+0x86>
shl    $0x2,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11b2 <initgroups+0x72>
mov    %rax,%r14
lea    0x4(%rsp),%rcx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1060 <getgrouplist@plt>
test   %rax,%rax
js     11b9 <initgroups+0x79>
mov    0x4(%rsp),%edi
mov    %r14,%rsi
call   1040 <setgroups@plt>
mov    %eax,%ebx
jmp    11be <initgroups+0x7e>
mov    $0xffffffff,%ebx
jmp    11c6 <initgroups+0x86>
mov    $0xffffffff,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @initgroups(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [2 x i32]
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
store i32* %10, i32** %7
store i32 -1, i32* %8
store i32 2, i32* %9
%11 = load i8*, i8** %4
%12 = load i32, i32* %5
%13 = load i32*, i32** %7
%14 = call i64 @getgrouplist(i8* %11, i32 %12, i32* %13, i32* %9)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %33
16: ; preds = %2
%17 = load i32, i32* %9
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 4
%20 = trunc i64 %19 to i32
%21 = call i32* @malloc(i32 %20)
store i32* %21, i32** %7
%22 = load i32*, i32** %7
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %25
24: ; preds = %16
store i32 -1, i32* %3
br label %46
25: ; preds = %16
%26 = load i8*, i8** %4
%27 = load i32, i32* %5
%28 = load i32*, i32** %7
%29 = call i64 @getgrouplist(i8* %26, i32 %27, i32* %28, i32* %9)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25
br label %37
32: ; preds = %25
br label %33
33: ; preds = %32, %2
%34 = load i32, i32* %9
%35 = load i32*, i32** %7
%36 = call i32 @setgroups(i32 %34, i32* %35)
store i32 %36, i32* %8
br label %37
37: ; preds = %33, %31
%38 = load i32*, i32** %7
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%40 = icmp ne i32* %38, %39
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load i32*, i32** %7
%43 = call i32 @free(i32* %42)
br label %44
44: ; preds = %41, %37
%45 = load i32, i32* %8
store i32 %45, i32* %3
br label %46
46: ; preds = %44, %24
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
define i32 @initgroups(i8* %0, i32 %1) {
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%7 = bitcast i32* %4 to i8*
store i32 2, i32* %4
%8 = call i64 @getgrouplist(i8* %0, i32 %1, i32* %6, i32* %4)
%9 = icmp slt i64 %8, 0
%10 = load i32, i32* %4
br i1 %9, label %13, label %11
11: ; preds = %2
%12 = call i32 @setgroups(i32 %10, i32* %6)
br label %26
13: ; preds = %2
%14 = shl i32 %10, 2
%15 = call i32* @malloc(i32 %14)
%16 = icmp eq i32* %15, null
br i1 %16, label %26, label %17
17: ; preds = %13
%18 = call i64 @getgrouplist(i8* %0, i32 %1, i32* %15, i32* %4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = load i32, i32* %4
%22 = call i32 @setgroups(i32 %21, i32* %15)
br label %23
23: ; preds = %17, %20
%24 = phi i32 [ %22, %20 ], [ -1, %17 ]
%25 = call i32 @free(i32* %15)
br label %26
26: ; preds = %11, %23, %13
%27 = phi i32 [ -1, %13 ], [ %24, %23 ], [ %12, %11 ]
ret i32 %27
}