#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 */
int /*<<< orphan*/ exit(int);
scalar_t__ getgid();
int getgroups(int const, scalar_t__ *);
int /*<<< orphan*/ printf(char *);
gid_t choose_gid(void) {
  const int count = 64;
  gid_t groups[count];
  int ret = getgroups(count, groups);
  if (ret < 0) {
    printf("getgroups: %m\n");
    exit(1);
  }
  gid_t current = getgid();
  for (int i = 0; i < ret; ++i) {
    if (groups[i] != current)
      return groups[i];
  }
  return 0;
}


x86 assembly (clang, -O0):
<choose_gid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x40,-0xc(%rbp)
mov    %rsp,%rax
mov    %rax,-0x18(%rbp)
mov    $0x40,%edi
lea    -0x230(%rbp),%rsi
call   1050 <getgroups@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    118d <choose_gid+0x4d>
lea    0xe82(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    $0x0,%al
call   1040 <getgid@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11f0 <choose_gid+0xb0>
movslq -0x2c(%rbp),%rax
mov    -0x230(%rbp,%rax,8),%rax
ff
cmp    -0x28(%rbp),%rax
je     11dd <choose_gid+0x9d>
movslq -0x2c(%rbp),%rax
mov    -0x230(%rbp,%rax,8),%rax
ff
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    11ff <choose_gid+0xbf>
jmp    11e2 <choose_gid+0xa2>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    119f <choose_gid+0x5f>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<choose_gid>:
push   %rbp
push   %rbx
sub    $0x208,%rsp
mov    %rsp,%rsi
mov    $0x40,%edi
call   1050 <getgroups@plt>
test   %eax,%eax
js     1191 <choose_gid+0x51>
mov    %eax,%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getgid@plt>
test   %ebp,%ebp
je     1184 <choose_gid+0x44>
mov    %ebp,%ecx
xor    %edx,%edx
nopl   (%rax)
mov    (%rsp,%rdx,8),%rbx
cmp    %rax,%rbx
jne    1184 <choose_gid+0x44>
add    $0x1,%rdx
cmp    %rdx,%rcx
jne    1170 <choose_gid+0x30>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x208,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe68(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"getgroups: %m\0A\00"
define i64 @choose_gid() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i32 64, i32* %2
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %3
%9 = alloca i64, i64 64
%10 = call i32 @getgroups(i32 64, i64* %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = call i64 (...) @getgid()
store i64 %17, i64* %5
store i32 0, i32* %6
br label %18
18: ; preds = %35, %16
%19 = load i32, i32* %6
%20 = load i32, i32* %4
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %38
22: ; preds = %18
%23 = load i32, i32* %6
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i64, i64* %9, i64 %24
%26 = load i64, i64* %25
%27 = load i64, i64* %5
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %34
29: ; preds = %22
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i64, i64* %9, i64 %31
%33 = load i64, i64* %32
store i64 %33, i64* %1
store i32 1, i32* %7
br label %39
34: ; preds = %22
br label %35
35: ; preds = %34
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 1
store i32 %37, i32* %6
br label %18
38: ; preds = %18
store i64 0, i64* %1
store i32 1, i32* %7
br label %39
39: ; preds = %38, %29
%40 = load i8*, i8** %3
%41 = load i64, i64* %1
ret i64 %41
}

LLVM IR (-O2):
@.str = [15 x i8] c"getgroups: %m\0A\00"
define i64 @choose_gid() {
%1 = alloca [64 x i64]
%2 = getelementptr inbounds [64 x i64], [64 x i64]* %1, i64 0, i64 0
%3 = call i32 @getgroups(i32 64, i64* %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = call i64 (...) @getgid()
%10 = icmp eq i32 %3, 0
br i1 %10, label %21, label %11
11: ; preds = %8
%12 = zext i32 %3 to i64
br label %15
13: ; preds = %15
%14 = icmp eq i64 %20, %12
br i1 %14, label %21, label %15
15: ; preds = %11, %13
%16 = phi i64 [ 0, %11 ], [ %20, %13 ]
%17 = getelementptr inbounds [64 x i64], [64 x i64]* %1, i64 0, i64 %16
%18 = load i64, i64* %17
%19 = icmp eq i64 %18, %9
%20 = add nuw nsw i64 %16, 1
br i1 %19, label %13, label %21
21: ; preds = %13, %15, %8
%22 = phi i64 [ 0, %8 ], [ %18, %15 ], [ 0, %13 ]
ret i64 %22
}