#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*/ EINVAL;
int /*<<< orphan*/ __chk_fail();
int __getgroups(int, int /*<<< orphan*/ *);
scalar_t__ __glibc_unlikely(int);
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
int __getgroups_chk(int size, __gid_t list[], size_t listlen) {
  if (__glibc_unlikely(size < 0)) {
    __set_errno(EINVAL);
    return -1;
  }
  if (__glibc_unlikely(size * sizeof(__gid_t) > listlen))
    __chk_fail();
  return __getgroups(size, list);
}


x86 assembly (clang, -O0):
<__getgroups_chk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <__glibc_unlikely@plt>
cmp    $0x0,%rax
je     1188 <__getgroups_chk+0x48>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c1 <__getgroups_chk+0x81>
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
cmp    -0x18(%rbp),%rax
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <__glibc_unlikely@plt>
cmp    $0x0,%rax
je     11b2 <__getgroups_chk+0x72>
mov    $0x0,%al
call   1050 <__chk_fail@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <__getgroups@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__getgroups_chk>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
shr    $0x1f,%edi
call   1060 <__glibc_unlikely@plt>
test   %rax,%rax
je     1171 <__getgroups_chk+0x31>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
movslq %ebp,%rax
shl    $0x2,%rax
xor    %edi,%edi
cmp    %rbx,%rax
seta   %dil
call   1060 <__glibc_unlikely@plt>
test   %rax,%rax
je     1192 <__getgroups_chk+0x52>
xor    %eax,%eax
call   1050 <__chk_fail@plt>
mov    %ebp,%edi
mov    %r14,%rsi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <__getgroups@plt>

LLVM IR (-O0):
@EINVAL = global i32 0
define i32 @__getgroups_chk(i32 %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
store i32 %0, i32* %5
store i32* %1, i32** %6
store i64 %2, i64* %7
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 0
%10 = zext i1 %9 to i32
%11 = call i64 @__glibc_unlikely(i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i32, i32* @EINVAL
%15 = call i32 @__set_errno(i32 %14)
store i32 -1, i32* %4
br label %31
16: ; preds = %3
%17 = load i32, i32* %5
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 4
%20 = load i64, i64* %7
%21 = icmp ugt i64 %19, %20
%22 = zext i1 %21 to i32
%23 = call i64 @__glibc_unlikely(i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %16
%26 = call i32 (...) @__chk_fail()
br label %27
27: ; preds = %25, %16
%28 = load i32, i32* %5
%29 = load i32*, i32** %6
%30 = call i32 @__getgroups(i32 %28, i32* %29)
store i32 %30, i32* %4
br label %31
31: ; preds = %27, %13
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@EINVAL = global i32 0
define i32 @__getgroups_chk(i32 %0, i32* %1, i64 %2) {
%4 = lshr i32 %0, 31
%5 = tail call i64 @__glibc_unlikely(i32 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %3
%8 = load i32, i32* @EINVAL
%9 = tail call i32 @__set_errno(i32 %8)
br label %21
10: ; preds = %3
%11 = sext i32 %0 to i64
%12 = shl nsw i64 %11, 2
%13 = icmp ugt i64 %12, %2
%14 = zext i1 %13 to i32
%15 = tail call i64 @__glibc_unlikely(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %10
%18 = tail call i32 (...) @__chk_fail()
br label %19
19: ; preds = %17, %10
%20 = tail call i32 @__getgroups(i32 %0, i32* %1)
br label %21
21: ; preds = %19, %7
%22 = phi i32 [ -1, %7 ], [ %20, %19 ]
ret i32 %22
}