#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 */
struct group {
  char **gr_mem;
  int /*<<< orphan*/ gr_gid;
};
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ _SC_LOGIN_NAME_MAX;
int /*<<< orphan*/ endgrent();
int /*<<< orphan*/ errno;
struct group *getgrent();
int /*<<< orphan*/ setgrent();
scalar_t__ strncmp(char const *, char *, long);
long sysconf(int /*<<< orphan*/);
int getGroups(const char *user, int size, gid_t *groupsList) {
  struct group *g;
  int n = 0;
  long usernameMax;
  char **p;
  usernameMax = sysconf(_SC_LOGIN_NAME_MAX);
  if (usernameMax == -1) {
    usernameMax = 256;
  }
  setgrent();
  for (g = getgrent(); g != NULL; g = getgrent()) {
    for (p = g->gr_mem; *p != NULL; ++p) {
      if (strncmp(user, *p, usernameMax) == 0) {
        if (n < size) {
          groupsList[n++] = g->gr_gid;
          break;
        } else {
          errno = ENOMEM;
          return -1;
        }
      }
    }
  }
  endgrent();
  return n;
}


x86 assembly (clang, -O0):
<getGroups>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0xffffffffffffffff,-0x38(%rbp)
jne    118f <getGroups+0x3f>
movq   $0x100,-0x38(%rbp)
mov    $0x0,%al
call   1060 <setgrent@plt>
mov    $0x0,%al
call   1050 <getgrent@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1259 <getGroups+0x109>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmpq   $0x0,(%rax)
je     1244 <getGroups+0xf4>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%rsi
mov    -0x38(%rbp),%rdx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    122e <getGroups+0xde>
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1210 <getGroups+0xc0>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%edx
mov    -0x20(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x2c(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1244 <getGroups+0xf4>
mov    0x2dc1(%rip),%rax
mov    (%rax),%ecx
mov    0x2db0(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <getGroups+0x116>
jmp    1233 <getGroups+0xe3>
mov    -0x40(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x40(%rbp)
jmp    11b7 <getGroups+0x67>
jmp    1249 <getGroups+0xf9>
mov    $0x0,%al
call   1050 <getgrent@plt>
mov    %rax,-0x28(%rbp)
jmp    11a1 <getGroups+0x51>
mov    $0x0,%al
call   1030 <endgrent@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getGroups>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
mov    %esi,%r15d
mov    %rdi,%rbx
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
cmp    $0xffffffffffffffff,%rax
mov    $0x100,%ebp
cmovne %rax,%rbp
xor    %r12d,%r12d
xor    %eax,%eax
call   1060 <setgrent@plt>
xor    %eax,%eax
call   1050 <getgrent@plt>
test   %rax,%rax
je     11f9 <getGroups+0xa9>
mov    %rax,%r13
xor    %r12d,%r12d
jmp    11af <getGroups+0x5f>
xchg   %ax,%ax
xor    %eax,%eax
call   1050 <getgrent@plt>
mov    %rax,%r13
test   %rax,%rax
je     11f9 <getGroups+0xa9>
mov    0x0(%r13),%r14
mov    (%r14),%rsi
test   %rsi,%rsi
je     11a0 <getGroups+0x50>
add    $0x8,%r14
nop
mov    %rbx,%rdi
mov    %rbp,%rdx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11e0 <getGroups+0x90>
mov    (%r14),%rsi
add    $0x8,%r14
test   %rsi,%rsi
jne    11c0 <getGroups+0x70>
jmp    11a0 <getGroups+0x50>
xchg   %ax,%ax
cmp    %r15d,%r12d
jge    1212 <getGroups+0xc2>
mov    0x8(%r13),%eax
movslq %r12d,%rcx
add    $0x1,%r12d
mov    (%rsp),%rdx
mov    %eax,(%rdx,%rcx,4)
jmp    11a0 <getGroups+0x50>
xor    %eax,%eax
call   1030 <endgrent@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    0x2dae(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r12d
jmp    1200 <getGroups+0xb0>

LLVM IR (-O0):
%struct.group = type { i8**, i32 }
@_SC_LOGIN_NAME_MAX = global i32 0
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @getGroups(i8* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.group*
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8**
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32 0, i32* %9
%12 = load i32, i32* @_SC_LOGIN_NAME_MAX
%13 = call i64 @sysconf(i32 %12)
store i64 %13, i64* %10
%14 = load i64, i64* %10
%15 = icmp eq i64 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %3
store i64 256, i64* %10
br label %17
17: ; preds = %16, %3
%18 = call i32 (...) @setgrent()
%19 = call %struct.group* (...) @getgrent()
store %struct.group* %19, %struct.group** %8
br label %20
20: ; preds = %58, %17
%21 = load %struct.group*, %struct.group** %8
%22 = icmp ne %struct.group* %21, null
br i1 %22, label %23, label %60
23: ; preds = %20
%24 = load %struct.group*, %struct.group** %8
%25 = getelementptr inbounds %struct.group, %struct.group* %24, i32 0, i32 0
%26 = load i8**, i8*** %25
store i8** %26, i8*** %11
br label %27
27: ; preds = %54, %23
%28 = load i8**, i8*** %11
%29 = load i8*, i8** %28
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %57
31: ; preds = %27
%32 = load i8*, i8** %5
%33 = load i8**, i8*** %11
%34 = load i8*, i8** %33
%35 = load i64, i64* %10
%36 = call i64 @strncmp(i8* %32, i8* %34, i64 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %53
38: ; preds = %31
%39 = load i32, i32* %9
%40 = load i32, i32* %6
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %51
42: ; preds = %38
%43 = load %struct.group*, %struct.group** %8
%44 = getelementptr inbounds %struct.group, %struct.group* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = load i32*, i32** %7
%47 = load i32, i32* %9
%48 = add nsw i32 %47, 1
store i32 %48, i32* %9
%49 = sext i32 %47 to i64
%50 = getelementptr inbounds i32, i32* %46, i64 %49
store i32 %45, i32* %50
br label %57
51: ; preds = %38
%52 = load i32, i32* @ENOMEM
store i32 %52, i32* @errno
store i32 -1, i32* %4
br label %63
53: ; preds = %31
br label %54
54: ; preds = %53
%55 = load i8**, i8*** %11
%56 = getelementptr inbounds i8*, i8** %55, i32 1
store i8** %56, i8*** %11
br label %27
57: ; preds = %42, %27
br label %58
58: ; preds = %57
%59 = call %struct.group* (...) @getgrent()
store %struct.group* %59, %struct.group** %8
br label %20
60: ; preds = %20
%61 = call i32 (...) @endgrent()
%62 = load i32, i32* %9
store i32 %62, i32* %4
br label %63
63: ; preds = %60, %51
%64 = load i32, i32* %4
ret i32 %64
}

LLVM IR (-O2):
%struct.group = type { i8**, i32 }
@_SC_LOGIN_NAME_MAX = global i32 0
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @getGroups(i8* %0, i32 %1, i32* nocapture writeonly %2) {
%4 = load i32, i32* @_SC_LOGIN_NAME_MAX
%5 = tail call i64 @sysconf(i32 %4)
%6 = icmp eq i64 %5, -1
%7 = select i1 %6, i64 256, i64 %5
%8 = tail call i32 (...) @setgrent()
%9 = tail call %struct.group* (...) @getgrent()
%10 = icmp eq %struct.group* %9, null
br i1 %10, label %41, label %11
11: ; preds = %3, %37
%12 = phi i32 [ %38, %37 ], [ 0, %3 ]
%13 = phi %struct.group* [ %39, %37 ], [ %9, %3 ]
%14 = getelementptr inbounds %struct.group, %struct.group* %13, i64 0, i32 0
%15 = load i8**, i8*** %14
%16 = load i8*, i8** %15
%17 = icmp eq i8* %16, null
br i1 %17, label %37, label %21
18: ; preds = %21
%19 = load i8*, i8** %26
%20 = icmp eq i8* %19, null
br i1 %20, label %37, label %21
21: ; preds = %11, %18
%22 = phi i8* [ %19, %18 ], [ %16, %11 ]
%23 = phi i8** [ %26, %18 ], [ %15, %11 ]
%24 = tail call i64 @strncmp(i8* %0, i8* %22, i64 %7)
%25 = icmp eq i64 %24, 0
%26 = getelementptr inbounds i8*, i8** %23, i64 1
br i1 %25, label %27, label %18
27: ; preds = %21
%28 = icmp slt i32 %12, %1
br i1 %28, label %29, label %35
29: ; preds = %27
%30 = getelementptr inbounds %struct.group, %struct.group* %13, i64 0, i32 1
%31 = load i32, i32* %30
%32 = add nsw i32 %12, 1
%33 = sext i32 %12 to i64
%34 = getelementptr inbounds i32, i32* %2, i64 %33
store i32 %31, i32* %34
br label %37
35: ; preds = %27
%36 = load i32, i32* @ENOMEM
store i32 %36, i32* @errno
br label %44
37: ; preds = %18, %11, %29
%38 = phi i32 [ %32, %29 ], [ %12, %11 ], [ %12, %18 ]
%39 = tail call %struct.group* (...) @getgrent()
%40 = icmp eq %struct.group* %39, null
br i1 %40, label %41, label %11
41: ; preds = %37, %3
%42 = phi i32 [ 0, %3 ], [ %38, %37 ]
%43 = tail call i32 (...) @endgrent()
br label %44
44: ; preds = %41, %35
%45 = phi i32 [ -1, %35 ], [ %42, %41 ]
ret i32 %45
}