#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 passwd {
  int /*<<< orphan*/ pw_gid;
  int /*<<< orphan*/ pw_name;
};
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int getgroupcount(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ getgrouplist(int /*<<< orphan*/, int /*<<< orphan*/, int *, int *);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
int getgroups(int gidsetsize, gid_t grouplist[]) {
  struct passwd *pw;
  int n;
  if ((pw = getpwuid(getuid())) == NULL) {
    errno = EINVAL;
    return -1;
  }
  if (gidsetsize == 0) {
    if ((n = getgroupcount(pw->pw_name, pw->pw_gid)) == 0) {
      errno = EINVAL;
      return -1;
    }
    return n;
  }
  n = gidsetsize;
  if (getgrouplist(pw->pw_name, pw->pw_gid, (int *)grouplist, &n) < 0) {
    errno = EINVAL;
    return -1;
  }
  return n;
}


x86 assembly (clang, -O0):
<getgroups>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1189 <getgroups+0x49>
mov    0x2e66(%rip),%rax
mov    (%rax),%ecx
mov    0x2e65(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <getgroups+0xe8>
cmpl   $0x0,-0x8(%rbp)
jne    11da <getgroups+0x9a>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1050 <getgroupcount@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    11cf <getgroups+0x8f>
mov    0x2e20(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <getgroups+0xe8>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1228 <getgroups+0xe8>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
lea    -0x1c(%rbp),%rcx
call   1060 <getgrouplist@plt>
cmp    $0x0,%rax
jge    1222 <getgroups+0xe2>
mov    0x2dcd(%rip),%rax
mov    (%rax),%ecx
mov    0x2dcc(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <getgroups+0xe8>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getgroups>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
test   %rax,%rax
je     118e <getgroups+0x4e>
test   %ebp,%ebp
je     1180 <getgroups+0x40>
mov    %ebp,0x4(%rsp)
mov    (%rax),%esi
mov    0x4(%rax),%edi
lea    0x4(%rsp),%rcx
mov    %rbx,%rdx
call   1060 <getgrouplist@plt>
test   %rax,%rax
js     118e <getgroups+0x4e>
mov    0x4(%rsp),%eax
jmp    11a5 <getgroups+0x65>
mov    (%rax),%esi
mov    0x4(%rax),%edi
call   1050 <getgroupcount@plt>
test   %eax,%eax
jne    11a5 <getgroups+0x65>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    0x2e42(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@EINVAL = global i32 0
@errno = global i32 0
define i32 @getgroups(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.passwd*
%7 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
%8 = call i32 (...) @getuid()
%9 = call %struct.passwd* @getpwuid(i32 %8)
store %struct.passwd* %9, %struct.passwd** %6
%10 = icmp eq %struct.passwd* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
store i32 -1, i32* %3
br label %44
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %13
%17 = load %struct.passwd*, %struct.passwd** %6
%18 = getelementptr inbounds %struct.passwd, %struct.passwd* %17, i32 0, i32 1
%19 = load i32, i32* %18
%20 = load %struct.passwd*, %struct.passwd** %6
%21 = getelementptr inbounds %struct.passwd, %struct.passwd* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @getgroupcount(i32 %19, i32 %22)
store i32 %23, i32* %7
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %16
%26 = load i32, i32* @EINVAL
store i32 %26, i32* @errno
store i32 -1, i32* %3
br label %44
27: ; preds = %16
%28 = load i32, i32* %7
store i32 %28, i32* %3
br label %44
29: ; preds = %13
%30 = load i32, i32* %4
store i32 %30, i32* %7
%31 = load %struct.passwd*, %struct.passwd** %6
%32 = getelementptr inbounds %struct.passwd, %struct.passwd* %31, i32 0, i32 1
%33 = load i32, i32* %32
%34 = load %struct.passwd*, %struct.passwd** %6
%35 = getelementptr inbounds %struct.passwd, %struct.passwd* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = load i32*, i32** %5
%38 = call i64 @getgrouplist(i32 %33, i32 %36, i32* %37, i32* %7)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %29
%41 = load i32, i32* @EINVAL
store i32 %41, i32* @errno
store i32 -1, i32* %3
br label %44
42: ; preds = %29
%43 = load i32, i32* %7
store i32 %43, i32* %3
br label %44
44: ; preds = %42, %40, %27, %25, %11
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@EINVAL = global i32 0
@errno = global i32 0
define i32 @getgroups(i32 %0, i32* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @getuid()
%6 = tail call %struct.passwd* @getpwuid(i32 %5)
%7 = icmp eq %struct.passwd* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = load i32, i32* @EINVAL
store i32 %9, i32* @errno
br label %32
10: ; preds = %2
%11 = icmp eq i32 %0, 0
br i1 %11, label %12, label %21
12: ; preds = %10
%13 = getelementptr inbounds %struct.passwd, %struct.passwd* %6, i64 0, i32 1
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %6, i64 0, i32 0
%16 = load i32, i32* %15
%17 = tail call i32 @getgroupcount(i32 %14, i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %32
19: ; preds = %12
%20 = load i32, i32* @EINVAL
store i32 %20, i32* @errno
br label %32
21: ; preds = %10
store i32 %0, i32* %3
%22 = getelementptr inbounds %struct.passwd, %struct.passwd* %6, i64 0, i32 1
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %6, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @getgrouplist(i32 %23, i32 %25, i32* %1, i32* %3)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = load i32, i32* @EINVAL
store i32 %29, i32* @errno
br label %32
30: ; preds = %21
%31 = load i32, i32* %3
br label %32
32: ; preds = %12, %30, %28, %19, %8
%33 = phi i32 [ -1, %8 ], [ -1, %19 ], [ -1, %28 ], [ %31, %30 ], [ %17, %12 ]
ret i32 %33
}