#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int getgroups(int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ warn(char *);
int xgetgroups(gid_t gid, int *n_groups, gid_t **groups) {
  int max_n_groups;
  int ng;
  gid_t *g;
  int fail = 0;
  max_n_groups = getgroups(0, NULL);
  g = (gid_t *)malloc(max_n_groups * sizeof(gid_t) + 1);
  if (g == NULL)
    err(EXIT_FAILURE, "out of memory");
  ng = getgroups(max_n_groups, g);
  if (ng < 0) {
    warn("cannot get supplemental group list");
    ++fail;
    free(g);
  }
  if (!fail) {
    *n_groups = ng;
    *groups = g;
  }
  return fail;
}


x86 assembly (clang, -O0):
<xgetgroups>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <getgroups@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11af <xgetgroups+0x5f>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
call   1060 <err@plt>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1050 <getgroups@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11e6 <xgetgroups+0x96>
lea    0xe3f(%rip),%rdi
call   1040 <warn@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    1204 <xgetgroups+0xb4>
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xgetgroups>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
xor    %edi,%edi
xor    %esi,%esi
call   1050 <getgroups@plt>
mov    %eax,%r12d
lea    0x1(,%r12,4),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1194 <xgetgroups+0x44>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
lea    0xe71(%rip),%rsi
call   1060 <err@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1050 <getgroups@plt>
test   %eax,%eax
js     11ad <xgetgroups+0x5d>
mov    %eax,(%r15)
mov    %rbx,(%r14)
xor    %eax,%eax
jmp    11c6 <xgetgroups+0x76>
lea    0xe5a(%rip),%rdi
call   1040 <warn@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [14 x i8] c"out of memory\00"
@.str.1 = [35 x i8] c"cannot get supplemental group list\00"
define i32 @xgetgroups(i32 %0, i32* %1, i32** %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32** %2, i32*** %6
store i32 0, i32* %10
%11 = call i32 @getgroups(i32 0, i32* null)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = sext i32 %12 to i64
%14 = mul i64 %13, 4
%15 = add i64 %14, 1
%16 = trunc i64 %15 to i32
%17 = call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i32*
store i32* %18, i32** %9
%19 = load i32*, i32** %9
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %3
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @err(i32 %22, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %21, %3
%25 = load i32, i32* %7
%26 = load i32*, i32** %9
%27 = call i32 @getgroups(i32 %25, i32* %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %24
%31 = call i32 @warn(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* %10
%33 = add nsw i32 %32, 1
store i32 %33, i32* %10
%34 = load i32*, i32** %9
%35 = call i32 @free(i32* %34)
br label %36
36: ; preds = %30, %24
%37 = load i32, i32* %10
%38 = icmp ne i32 %37, 0
br i1 %38, label %44, label %39
39: ; preds = %36
%40 = load i32, i32* %8
%41 = load i32*, i32** %5
store i32 %40, i32* %41
%42 = load i32*, i32** %9
%43 = load i32**, i32*** %6
store i32* %42, i32** %43
br label %44
44: ; preds = %39, %36
%45 = load i32, i32* %10
ret i32 %45
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [14 x i8] c"out of memory\00"
@.str.1 = [35 x i8] c"cannot get supplemental group list\00"
define i32 @xgetgroups(i32 %0, i32* nocapture writeonly %1, i32** nocapture writeonly %2) {
%4 = tail call i32 @getgroups(i32 0, i32* null)
%5 = shl i32 %4, 2
%6 = or i32 %5, 1
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i32*
%9 = icmp eq i64 %7, 0
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @err(i32 %11, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %10, %3
%14 = tail call i32 @getgroups(i32 %4, i32* %8)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = tail call i32 @warn(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @free(i32* %8)
br label %20
19: ; preds = %13
store i32 %14, i32* %1
store i32* %8, i32** %2
br label %20
20: ; preds = %16, %19
%21 = phi i32 [ 1, %16 ], [ 0, %19 ]
ret i32 %21
}