#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 NGROUPS;
int _setgroups(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ endgrent();
struct group *getgrent();
int setgid(int /*<<< orphan*/);
int /*<<< orphan*/ setgrent();
int /*<<< orphan*/ strcmp(char const *, char *);
int initgroups(const char *name, gid_t basegid) {
  struct group *gr;
  int r, n = 0;
  gid_t groups[NGROUPS];
  if ((r = setgid(basegid)) < 0)
    return (r);
  setgrent();
  while ((gr = getgrent()) != NULL && (n + 1) <= NGROUPS) {
    char **mem;
    for (mem = gr->gr_mem; mem && *mem; mem++) {
      if (!strcmp(name, *mem)) {
        groups[n++] = gr->gr_gid;
        break;
      }
    }
  }
  endgrent();
  return _setgroups(n, groups);
}


x86 assembly (clang, -O0):
<initgroups>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x28(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <setgid@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11c9 <initgroups+0x69>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12bd <initgroups+0x15d>
mov    $0x0,%al
call   1050 <setgrent@plt>
mov    $0x0,%al
call   1040 <getgrent@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x51(%rbp)
je     1202 <initgroups+0xa2>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%eax
setle  %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    1212 <initgroups+0xb2>
jmp    12a0 <initgroups+0x140>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x48(%rbp)
mov    %al,-0x52(%rbp)
je     123b <initgroups+0xdb>
mov    -0x48(%rbp),%rax
cmpq   $0x0,(%rax)
setne  %al
mov    %al,-0x52(%rbp)
mov    -0x52(%rbp),%al
test   $0x1,%al
jne    124b <initgroups+0xeb>
jmp    129b <initgroups+0x13b>
mov    -0x10(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    (%rax),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1285 <initgroups+0x125>
mov    -0x50(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    0x8(%rcx),%edx
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    129b <initgroups+0x13b>
jmp    128a <initgroups+0x12a>
mov    -0x48(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x48(%rbp)
jmp    121d <initgroups+0xbd>
jmp    11d0 <initgroups+0x70>
mov    $0x0,%al
call   1030 <endgrent@plt>
mov    -0x50(%rbp),%rsi
mov    -0x28(%rbp),%edi
call   1070 <_setgroups@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<initgroups>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %esi,%edi
call   1080 <setgid@plt>
test   %eax,%eax
js     123c <initgroups+0xdc>
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <setgrent@plt>
xor    %eax,%eax
call   1040 <getgrent@plt>
test   %rax,%rax
je     1229 <initgroups+0xc9>
mov    %rax,%r12
mov    0x2e1e(%rip),%rax
cmpl   $0x0,(%rax)
jle    1229 <initgroups+0xc9>
mov    $0x1,%r13d
xor    %r15d,%r15d
mov    (%r12),%r14
test   %r14,%r14
je     120a <initgroups+0xaa>
nopl   0x0(%rax)
mov    (%r14),%rsi
test   %rsi,%rsi
je     120a <initgroups+0xaa>
mov    %rbx,%rdi
call   1060 <strcmp@plt>
add    $0x8,%r14
test   %eax,%eax
jne    11e0 <initgroups+0x80>
mov    0x8(%r12),%eax
movslq %r15d,%rcx
mov    -0x30(%rbp),%rdx
mov    %eax,(%rdx,%rcx,4)
mov    %r13d,%r15d
xor    %eax,%eax
call   1040 <getgrent@plt>
test   %rax,%rax
je     1229 <initgroups+0xc9>
mov    %rax,%r12
lea    0x1(%r15),%r13d
mov    0x2dbc(%rip),%rax
cmp    (%rax),%r15d
jl     11d0 <initgroups+0x70>
xor    %eax,%eax
call   1030 <endgrent@plt>
mov    %r15d,%edi
mov    -0x30(%rbp),%rsi
call   1070 <_setgroups@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.group = type { i8**, i32 }
@NGROUPS = global i32 0
define i32 @initgroups(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.group*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i8**
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %8
%13 = load i32, i32* @NGROUPS
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %9
%16 = alloca i32, i64 %14
store i64 %14, i64* %10
%17 = load i32, i32* %5
%18 = call i32 @setgid(i32 %17)
store i32 %18, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = load i32, i32* %7
store i32 %21, i32* %3
store i32 1, i32* %11
br label %70
22: ; preds = %2
%23 = call i32 (...) @setgrent()
br label %24
24: ; preds = %65, %22
%25 = call %struct.group* (...) @getgrent()
store %struct.group* %25, %struct.group** %6
%26 = icmp ne %struct.group* %25, null
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i32, i32* %8
%29 = add nsw i32 %28, 1
%30 = load i32, i32* @NGROUPS
%31 = icmp sle i32 %29, %30
br label %32
32: ; preds = %27, %24
%33 = phi i1 [ false, %24 ], [ %31, %27 ]
br i1 %33, label %34, label %66
34: ; preds = %32
%35 = load %struct.group*, %struct.group** %6
%36 = getelementptr inbounds %struct.group, %struct.group* %35, i32 0, i32 0
%37 = load i8**, i8*** %36
store i8** %37, i8*** %12
br label %38
38: ; preds = %62, %34
%39 = load i8**, i8*** %12
%40 = icmp ne i8** %39, null
br i1 %40, label %41, label %45
41: ; preds = %38
%42 = load i8**, i8*** %12
%43 = load i8*, i8** %42
%44 = icmp ne i8* %43, null
br label %45
45: ; preds = %41, %38
%46 = phi i1 [ false, %38 ], [ %44, %41 ]
br i1 %46, label %47, label %65
47: ; preds = %45
%48 = load i8*, i8** %4
%49 = load i8**, i8*** %12
%50 = load i8*, i8** %49
%51 = call i32 @strcmp(i8* %48, i8* %50)
%52 = icmp ne i32 %51, 0
br i1 %52, label %61, label %53
53: ; preds = %47
%54 = load %struct.group*, %struct.group** %6
%55 = getelementptr inbounds %struct.group, %struct.group* %54, i32 0, i32 1
%56 = load i32, i32* %55
%57 = load i32, i32* %8
%58 = add nsw i32 %57, 1
store i32 %58, i32* %8
%59 = sext i32 %57 to i64
%60 = getelementptr inbounds i32, i32* %16, i64 %59
store i32 %56, i32* %60
br label %65
61: ; preds = %47
br label %62
62: ; preds = %61
%63 = load i8**, i8*** %12
%64 = getelementptr inbounds i8*, i8** %63, i32 1
store i8** %64, i8*** %12
br label %38
65: ; preds = %53, %45
br label %24
66: ; preds = %32
%67 = call i32 (...) @endgrent()
%68 = load i32, i32* %8
%69 = call i32 @_setgroups(i32 %68, i32* %16)
store i32 %69, i32* %3
store i32 1, i32* %11
br label %70
70: ; preds = %66, %20
%71 = load i8*, i8** %9
%72 = load i32, i32* %3
ret i32 %72
}

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