#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ getgroups(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ stderr;
gid_t *read_groups(void) {
  int n;
  gid_t *groups;
  n = getgroups(0, NULL);
  if (n < 0) {
    fprintf(stderr, "Unable to retrieve groups: %m\n");
    return NULL;
  }
  groups = malloc(n * sizeof(gid_t));
  if (!groups)
    return NULL;
  if (getgroups(n, groups) < 0) {
    fprintf(stderr, "Unable to retrieve groups: %m\n");
    free(groups);
    return NULL;
  }
  return groups;
}


x86 assembly (clang, -O0):
<read_groups>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <getgroups@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1182 <read_groups+0x42>
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
lea    0xe90(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f6 <read_groups+0xb6>
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ad <read_groups+0x6d>
movq   $0x0,-0x8(%rbp)
jmp    11f6 <read_groups+0xb6>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1050 <getgroups@plt>
cmp    $0x0,%rax
jge    11ee <read_groups+0xae>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f6 <read_groups+0xb6>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_groups>:
push   %r14
push   %rbx
push   %rax
xor    %edi,%edi
xor    %esi,%esi
call   1050 <getgroups@plt>
mov    %rax,%rbx
test   %ebx,%ebx
js     1196 <read_groups+0x56>
lea    0x0(,%rbx,4),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11ab <read_groups+0x6b>
mov    %rax,%r14
mov    %ebx,%edi
mov    %rax,%rsi
call   1050 <getgroups@plt>
test   %rax,%rax
jns    11ae <read_groups+0x6e>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
call   1040 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    11ab <read_groups+0x6b>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    0xe5a(%rip),%rsi
call   1040 <fprintf@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"Unable to retrieve groups: %m\0A\00"
define i32* @read_groups() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i32*
%4 = call i64 @getgroups(i32 0, i32* null)
%5 = trunc i64 %4 to i32
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32* null, i32** %1
br label %32
11: ; preds = %0
%12 = load i32, i32* %2
%13 = sext i32 %12 to i64
%14 = mul i64 %13, 4
%15 = trunc i64 %14 to i32
%16 = call i32* @malloc(i32 %15)
store i32* %16, i32** %3
%17 = load i32*, i32** %3
%18 = icmp ne i32* %17, null
br i1 %18, label %20, label %19
19: ; preds = %11
store i32* null, i32** %1
br label %32
20: ; preds = %11
%21 = load i32, i32* %2
%22 = load i32*, i32** %3
%23 = call i64 @getgroups(i32 %21, i32* %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%28 = load i32*, i32** %3
%29 = call i32 @free(i32* %28)
store i32* null, i32** %1
br label %32
30: ; preds = %20
%31 = load i32*, i32** %3
store i32* %31, i32** %1
br label %32
32: ; preds = %30, %25, %19, %8
%33 = load i32*, i32** %1
ret i32* %33
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"Unable to retrieve groups: %m\0A\00"
define i32* @read_groups() {
%1 = tail call i64 @getgroups(i32 0, i32* null)
%2 = trunc i64 %1 to i32
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %18
7: ; preds = %0
%8 = shl i32 %2, 2
%9 = tail call i32* @malloc(i32 %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %18, label %11
11: ; preds = %7
%12 = tail call i64 @getgroups(i32 %2, i32* %9)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @free(i32* %9)
br label %18
18: ; preds = %11, %7, %14, %4
%19 = phi i32* [ null, %4 ], [ null, %14 ], [ null, %7 ], [ %9, %11 ]
ret i32* %19
}