#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 _error(char const *, char *, int);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int getgroups(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int _getgroups(char const *progname) {
  int n;
  gid_t *groups;
  int i;
  char const *sep = "";
  printf("%s: Testing getgroups()\n", progname);
  if ((n = getgroups(0, NULL)) < 0)
    return _error(progname, "getgroups", 16);
  if (n == 0)
    return 0;
  if ((groups = malloc(sizeof(*groups) * n)) == NULL)
    return _error(progname, "malloc", 16);
  if ((n = getgroups(n, groups)) < 0) {
    free(groups);
    return _error(progname, "getgroups", 16);
  }
  printf("Groups: ");
  for (i = 0; i < n; i++) {
    printf("%s%u", sep, groups[i]);
    sep = ", ";
  }
  printf("\n");
  free(groups);
  return 0;
}


x86 assembly (clang, -O0):
<_getgroups>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xeb5(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <getgroups@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11ad <_getgroups+0x5d>
mov    -0x10(%rbp),%rdi
lea    0xe7e(%rip),%rsi
mov    $0x10,%edx
call   1070 <_error@plt>
mov    %eax,-0x4(%rbp)
jmp    12b0 <_getgroups+0x160>
cmpl   $0x0,-0x14(%rbp)
jne    11c3 <_getgroups+0x73>
movl   $0x0,-0x4(%rbp)
jmp    12b0 <_getgroups+0x160>
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11fd <_getgroups+0xad>
mov    -0x10(%rbp),%rdi
lea    0xe38(%rip),%rsi
mov    $0x10,%edx
call   1070 <_error@plt>
mov    %eax,-0x4(%rbp)
jmp    12b0 <_getgroups+0x160>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1050 <getgroups@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    123b <_getgroups+0xeb>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
lea    0xdf0(%rip),%rsi
mov    $0x10,%edx
call   1070 <_error@plt>
mov    %eax,-0x4(%rbp)
jmp    12b0 <_getgroups+0x160>
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1292 <_getgroups+0x142>
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdb8(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1250 <_getgroups+0x100>
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_getgroups>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
lea    0xe8e(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1060 <getgroups@plt>
test   %eax,%eax
js     1258 <_getgroups+0xf8>
je     123c <_getgroups+0xdc>
mov    %eax,%ebp
lea    0x0(,%rbp,4),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1247 <_getgroups+0xe7>
mov    %rax,%r14
mov    %ebp,%edi
mov    %rax,%rsi
call   1060 <getgroups@plt>
test   %eax,%eax
js     1250 <_getgroups+0xf0>
mov    %eax,%ebp
lea    0xe62(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebp,%ebp
je     122a <_getgroups+0xca>
mov    (%r14),%edx
lea    0xe56(%rip),%rdi
lea    0xe34(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x1,%ebp
je     122a <_getgroups+0xca>
mov    %ebp,%ebx
mov    $0x1,%ebp
lea    0xe35(%rip),%r15
lea    0xe33(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r14,%rbp,4),%edx
mov    %r15,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1210 <_getgroups+0xb0>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd5(%rip),%rsi
jmp    125f <_getgroups+0xff>
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xdba(%rip),%rsi
mov    %rbx,%rdi
mov    $0x10,%edx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <_error@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [25 x i8] c"%s: Testing getgroups()\0A\00"
@.str.2 = [10 x i8] c"getgroups\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.4 = [9 x i8] c"Groups: \00"
@.str.5 = [5 x i8] c"%s%u\00"
@.str.6 = [3 x i8] c", \00"
@.str.7 = [2 x i8] c"\0A\00"
define i32 @_getgroups(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %7
%8 = load i8*, i8** %3
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = call i32 @getgroups(i32 0, i32* null)
store i32 %10, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32 @_error(i8* %13, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 16)
store i32 %14, i32* %2
br label %60
15: ; preds = %1
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 0, i32* %2
br label %60
19: ; preds = %15
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = mul i64 4, %21
%23 = trunc i64 %22 to i32
%24 = call i32* @malloc(i32 %23)
store i32* %24, i32** %5
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = load i8*, i8** %3
%28 = call i32 @_error(i8* %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 16)
store i32 %28, i32* %2
br label %60
29: ; preds = %19
%30 = load i32, i32* %4
%31 = load i32*, i32** %5
%32 = call i32 @getgroups(i32 %30, i32* %31)
store i32 %32, i32* %4
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = load i32*, i32** %5
%36 = call i32 @free(i32* %35)
%37 = load i8*, i8** %3
%38 = call i32 @_error(i8* %37, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 16)
store i32 %38, i32* %2
br label %60
39: ; preds = %29
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %6
br label %41
41: ; preds = %53, %39
%42 = load i32, i32* %6
%43 = load i32, i32* %4
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %56
45: ; preds = %41
%46 = load i8*, i8** %7
%47 = load i32*, i32** %5
%48 = load i32, i32* %6
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32, i32* %47, i64 %49
%51 = load i32, i32* %50
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %46, i32 %51)
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8** %7
br label %53
53: ; preds = %45
%54 = load i32, i32* %6
%55 = add nsw i32 %54, 1
store i32 %55, i32* %6
br label %41
56: ; preds = %41
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%58 = load i32*, i32** %5
%59 = call i32 @free(i32* %58)
store i32 0, i32* %2
br label %60
60: ; preds = %56, %34, %26, %18, %12
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [25 x i8] c"%s: Testing getgroups()\0A\00"
@.str.2 = [10 x i8] c"getgroups\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.4 = [9 x i8] c"Groups: \00"
@.str.5 = [5 x i8] c"%s%u\00"
@.str.6 = [3 x i8] c", \00"
define i32 @_getgroups(i8* %0) {
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%3 = tail call i32 @getgroups(i32 0, i32* null)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @_error(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 16)
br label %39
7: ; preds = %1
%8 = icmp eq i32 %3, 0
br i1 %8, label %39, label %9
9: ; preds = %7
%10 = shl i32 %3, 2
%11 = tail call i32* @malloc(i32 %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @_error(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 16)
br label %39
15: ; preds = %9
%16 = tail call i32 @getgroups(i32 %3, i32* %11)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = tail call i32 @free(i32* %11)
%20 = tail call i32 @_error(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 16)
br label %39
21: ; preds = %15
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%23 = icmp eq i32 %16, 0
br i1 %23, label %36, label %24
24: ; preds = %21
%25 = zext i32 %16 to i64
%26 = load i32, i32* %11
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = icmp eq i32 %16, 1
br i1 %28, label %36, label %29
29: ; preds = %24, %29
%30 = phi i64 [ %34, %29 ], [ 1, %24 ]
%31 = getelementptr inbounds i32, i32* %11, i64 %30
%32 = load i32, i32* %31
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %32)
%34 = add nuw nsw i64 %30, 1
%35 = icmp eq i64 %34, %25
br i1 %35, label %36, label %29
36: ; preds = %29, %24, %21
%37 = tail call i32 @putchar(i32 10)
%38 = tail call i32 @free(i32* %11)
br label %39
39: ; preds = %7, %36, %18, %13, %5
%40 = phi i32 [ %6, %5 ], [ %14, %13 ], [ %20, %18 ], [ 0, %36 ], [ 0, %7 ]
ret i32 %40
}