#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 scalar_t__ gid_t;

/* Variables and functions */
scalar_t__ getegid();
scalar_t__ getgid();
int getgroups(int, scalar_t__ *);
scalar_t__ *stalloc(int);
int bash_group_member(gid_t gid) {
  register int i;
  gid_t *group_array;
  int ngroups;
  if (gid == getgid() || gid == getegid())
    return (1);
  ngroups = getgroups(0, NULL);
  group_array = stalloc(ngroups * sizeof(gid_t));
  if ((getgroups(ngroups, group_array)) != ngroups)
    return (0);
  for (i = 0; i < ngroups; i++)
    if (gid == group_array[i])
      return (1);
  return (0);
}


x86 assembly (clang, -O0):
<bash_group_member>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1030 <getgid@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
je     118a <bash_group_member+0x4a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1050 <getegid@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
jne    1196 <bash_group_member+0x56>
movl   $0x1,-0x4(%rbp)
jmp    1227 <bash_group_member+0xe7>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <getgroups@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <stalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1040 <getgroups@plt>
cmp    -0x24(%rbp),%eax
je     11d8 <bash_group_member+0x98>
movl   $0x0,-0x4(%rbp)
jmp    1227 <bash_group_member+0xe7>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1220 <bash_group_member+0xe0>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movslq -0x14(%rbp),%rdx
cmp    (%rcx,%rdx,8),%rax
jne    120d <bash_group_member+0xcd>
movl   $0x1,-0x4(%rbp)
jmp    1227 <bash_group_member+0xe7>
jmp    1212 <bash_group_member+0xd2>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11df <bash_group_member+0x9f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bash_group_member>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <getgid@plt>
cmp    %rbx,%rax
je     1166 <bash_group_member+0x26>
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <getegid@plt>
cmp    %rbx,%rax
jne    117b <bash_group_member+0x3b>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %edi,%edi
xor    %esi,%esi
call   1040 <getgroups@plt>
mov    %eax,%r12d
lea    0x0(,%r12,8),%edi
call   1060 <stalloc@plt>
mov    %rax,%r15
mov    %r12d,%edi
mov    %rax,%rsi
call   1040 <getgroups@plt>
cmp    %r12d,%eax
jne    116c <bash_group_member+0x2c>
test   %r12d,%r12d
jle    116c <bash_group_member+0x2c>
mov    %r12d,%eax
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    %rbx,(%r15,%rcx,8)
je     1166 <bash_group_member+0x26>
add    $0x1,%rcx
cmp    %rcx,%rax
jne    11c0 <bash_group_member+0x80>
xor    %r14d,%r14d
jmp    116c <bash_group_member+0x2c>

LLVM IR (-O0):
define i32 @bash_group_member(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i64*
%6 = alloca i32
store i64 %0, i64* %3
%7 = load i64, i64* %3
%8 = call i64 (...) @getgid()
%9 = icmp eq i64 %7, %8
br i1 %9, label %14, label %10
10: ; preds = %1
%11 = load i64, i64* %3
%12 = call i64 (...) @getegid()
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %10, %1
store i32 1, i32* %2
br label %47
15: ; preds = %10
%16 = call i32 @getgroups(i32 0, i64* null)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 8
%20 = trunc i64 %19 to i32
%21 = call i64* @stalloc(i32 %20)
store i64* %21, i64** %5
%22 = load i32, i32* %6
%23 = load i64*, i64** %5
%24 = call i32 @getgroups(i32 %22, i64* %23)
%25 = load i32, i32* %6
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %15
store i32 0, i32* %2
br label %47
28: ; preds = %15
store i32 0, i32* %4
br label %29
29: ; preds = %43, %28
%30 = load i32, i32* %4
%31 = load i32, i32* %6
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %46
33: ; preds = %29
%34 = load i64, i64* %3
%35 = load i64*, i64** %5
%36 = load i32, i32* %4
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i64, i64* %35, i64 %37
%39 = load i64, i64* %38
%40 = icmp eq i64 %34, %39
br i1 %40, label %41, label %42
41: ; preds = %33
store i32 1, i32* %2
br label %47
42: ; preds = %33
br label %43
43: ; preds = %42
%44 = load i32, i32* %4
%45 = add nsw i32 %44, 1
store i32 %45, i32* %4
br label %29
46: ; preds = %29
store i32 0, i32* %2
br label %47
47: ; preds = %46, %41, %27, %14
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
define i32 @bash_group_member(i64 %0) {
%2 = tail call i64 (...) @getgid()
%3 = icmp eq i64 %2, %0
br i1 %3, label %25, label %4
4: ; preds = %1
%5 = tail call i64 (...) @getegid()
%6 = icmp eq i64 %5, %0
br i1 %6, label %25, label %7
7: ; preds = %4
%8 = tail call i32 @getgroups(i32 0, i64* null)
%9 = shl i32 %8, 3
%10 = tail call i64* @stalloc(i32 %9)
%11 = tail call i32 @getgroups(i32 %8, i64* %10)
%12 = icmp eq i32 %11, %8
%13 = icmp sgt i32 %8, 0
%14 = and i1 %12, %13
br i1 %14, label %15, label %25
15: ; preds = %7
%16 = zext i32 %8 to i64
br label %19
17: ; preds = %19
%18 = icmp eq i64 %24, %16
br i1 %18, label %25, label %19
19: ; preds = %15, %17
%20 = phi i64 [ 0, %15 ], [ %24, %17 ]
%21 = getelementptr inbounds i64, i64* %10, i64 %20
%22 = load i64, i64* %21
%23 = icmp eq i64 %22, %0
%24 = add nuw nsw i64 %20, 1
br i1 %23, label %25, label %17
25: ; preds = %19, %17, %7, %1, %4
%26 = phi i32 [ 1, %4 ], [ 1, %1 ], [ 0, %7 ], [ 1, %19 ], [ 0, %17 ]
ret i32 %26
}