#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int getgroups(int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
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) {
    fprintf(stderr, "out of memory\n");
    exit(EXIT_FAILURE);
  }
  ng = getgroups(max_n_groups, g);
  if (ng < 0) {
    fprintf(stderr, "cannot get supplemental group list\n");
    ++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   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bd <xgetgroups+0x6d>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1050 <getgroups@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11fd <xgetgroups+0xad>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
call   1040 <fprintf@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    121b <xgetgroups+0xcb>
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,%ebx
lea    0x1(,%rbx,4),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11c3 <xgetgroups+0x73>
mov    %rax,%r12
mov    %ebx,%edi
mov    %rax,%rsi
call   1050 <getgroups@plt>
test   %eax,%eax
js     1195 <xgetgroups+0x45>
mov    %eax,(%r15)
mov    %r12,(%r14)
xor    %eax,%eax
jmp    11b7 <xgetgroups+0x67>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
call   1040 <fprintf@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"out of memory\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [36 x i8] c"cannot get supplemental group list\0A\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 %26
21: ; preds = %3
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %3
%27 = load i32, i32* %7
%28 = load i32*, i32** %9
%29 = call i32 @getgroups(i32 %27, i32* %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* %10
%36 = add nsw i32 %35, 1
store i32 %36, i32* %10
%37 = load i32*, i32** %9
%38 = call i32 @free(i32* %37)
br label %39
39: ; preds = %32, %26
%40 = load i32, i32* %10
%41 = icmp ne i32 %40, 0
br i1 %41, label %47, label %42
42: ; preds = %39
%43 = load i32, i32* %8
%44 = load i32*, i32** %5
store i32 %43, i32* %44
%45 = load i32*, i32** %9
%46 = load i32**, i32*** %6
store i32* %45, i32** %46
br label %47
47: ; preds = %42, %39
%48 = load i32, i32* %10
ret i32 %48
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"out of memory\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [36 x i8] c"cannot get supplemental group list\0A\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 %15
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %3
%16 = tail call i32 @getgroups(i32 %4, i32* %8)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @free(i32* %8)
br label %23
22: ; preds = %15
store i32 %16, i32* %1
store i32* %8, i32** %2
br label %23
23: ; preds = %18, %22
%24 = phi i32 [ 1, %18 ], [ 0, %22 ]
ret i32 %24
}