#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*/ _SC_NGROUPS_MAX;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ isspace(unsigned char);
int /*<<< orphan*/ *malloc(int);
scalar_t__ setegid(int /*<<< orphan*/);
scalar_t__ setgroups(unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *strtok(char *, char *);
int /*<<< orphan*/ strtol(char *, char **, int /*<<< orphan*/);
long sysconf(int /*<<< orphan*/);
void set_gids(char *gids) {
  gid_t *gidset;
  long ngroups;
  char *g, *endp;
  unsigned i;
  ngroups = sysconf(_SC_NGROUPS_MAX);
  assert(ngroups > 0);
  gidset = malloc(sizeof(*gidset) * ngroups);
  assert(gidset != NULL);
  for (i = 0, g = strtok(gids, ","); g != NULL; g = strtok(NULL, ","), i++) {
    if (i >= ngroups) {
      fprintf(stderr, "too many gids\n");
      exit(1);
    }
    gidset[i] = strtol(g, &endp, 0);
    if (*endp != '\0' && !isspace((unsigned char)*endp)) {
      fprintf(stderr, "invalid gid '%s' - number expected\n", g);
      exit(1);
    }
  }
  if (setgroups(i, gidset) < 0) {
    fprintf(stderr, "cannot change groups: %s\n", strerror(errno));
    exit(1);
  }
  if (setegid(gidset[0]) < 0) {
    fprintf(stderr, "cannot change effective gid: %s\n", strerror(errno));
    exit(1);
  }
  free(gidset);
}


x86 assembly (clang, -O0):
<set_gids>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xdd9(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12f5 <set_gids+0x135>
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%rax
jl     1269 <set_gids+0xa9>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xdaa(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %eax,%edx
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12ce <set_gids+0x10e>
mov    -0x28(%rbp),%rax
movzbl (%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%eax
jne    12ce <set_gids+0x10e>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd54(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    12d3 <set_gids+0x113>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd22(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x20(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1230 <set_gids+0x70>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <setgroups@plt>
cmp    $0x0,%rax
jge    1343 <set_gids+0x183>
mov    0x2cc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xd03(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   10e0 <setegid@plt>
cmp    $0x0,%rax
jge    1390 <set_gids+0x1d0>
mov    0x2c79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2c65(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xcd0(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_gids>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setg   %dil
call   1060 <assert@plt>
lea    0x0(,%r14,4),%edi
call   1090 <malloc@plt>
mov    %rax,%r15
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1060 <assert@plt>
lea    0xdee(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strtok@plt>
test   %rax,%rax
je     129c <set_gids+0xdc>
mov    %rax,%r12
xor    %ebx,%ebx
mov    %rsp,%r13
lea    0xdd2(%rip),%rbp
jmp    1245 <set_gids+0x85>
xor    %edi,%edi
mov    %rbp,%rsi
call   10a0 <strtok@plt>
mov    %rax,%r12
add    $0x1,%ebx
test   %rax,%rax
je     129e <set_gids+0xde>
mov    %ebx,%ebx
cmp    %rbx,%r14
jle    12d0 <set_gids+0x110>
mov    %r12,%rdi
mov    %r13,%rsi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %eax,(%r15,%rbx,4)
mov    (%rsp),%rax
movzbl (%rax),%eax
test   %al,%al
je     1230 <set_gids+0x70>
movzbl %al,%edi
call   1050 <isspace@plt>
test   %eax,%eax
jne    1230 <set_gids+0x70>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
lea    0xd89(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %ebx,%ebx
mov    %ebx,%edi
mov    %r15,%rsi
call   1040 <setgroups@plt>
test   %rax,%rax
js     12f1 <set_gids+0x131>
mov    (%r15),%edi
call   10e0 <setegid@plt>
test   %rax,%rax
js     1311 <set_gids+0x151>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
lea    0xd22(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2ce0(%rip),%rax
mov    (%rax),%ebx
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd26(%rip),%rsi
jmp    132f <set_gids+0x16f>
mov    0x2cc0(%rip),%rax
mov    (%rax),%ebx
mov    0x2caf(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd20(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
jmp    128b <set_gids+0xcb>

LLVM IR (-O0):
@_SC_NGROUPS_MAX = global i32 0
@.str = [2 x i8] c",\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"too many gids\0A\00"
@.str.2 = [36 x i8] c"invalid gid '%s' - number expected\0A\00"
@.str.3 = [26 x i8] c"cannot change groups: %s\0A\00"
@errno = global i32 0
@.str.4 = [33 x i8] c"cannot change effective gid: %s\0A\00"
define void @set_gids(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i32, i32* @_SC_NGROUPS_MAX
%9 = call i64 @sysconf(i32 %8)
store i64 %9, i64* %4
%10 = load i64, i64* %4
%11 = icmp sgt i64 %10, 0
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i64, i64* %4
%15 = mul i64 4, %14
%16 = trunc i64 %15 to i32
%17 = call i32* @malloc(i32 %16)
store i32* %17, i32** %3
%18 = load i32*, i32** %3
%19 = icmp ne i32* %18, null
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
store i32 0, i32* %7
%22 = load i8*, i8** %2
%23 = call i8* @strtok(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %23, i8** %5
br label %24
24: ; preds = %58, %1
%25 = load i8*, i8** %5
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %62
27: ; preds = %24
%28 = load i32, i32* %7
%29 = zext i32 %28 to i64
%30 = load i64, i64* %4
%31 = icmp sge i64 %29, %30
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27
%37 = load i8*, i8** %5
%38 = call i32 @strtol(i8* %37, i8** %6, i32 0)
%39 = load i32*, i32** %3
%40 = load i32, i32* %7
%41 = zext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
store i32 %38, i32* %42
%43 = load i8*, i8** %6
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %57
47: ; preds = %36
%48 = load i8*, i8** %6
%49 = load i8, i8* %48
%50 = call i32 @isspace(i8 zeroext %49)
%51 = icmp ne i32 %50, 0
br i1 %51, label %57, label %52
52: ; preds = %47
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %5
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %54)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %47, %36
br label %58
58: ; preds = %57
%59 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %59, i8** %5
%60 = load i32, i32* %7
%61 = add i32 %60, 1
store i32 %61, i32* %7
br label %24
62: ; preds = %24
%63 = load i32, i32* %7
%64 = load i32*, i32** %3
%65 = call i64 @setgroups(i32 %63, i32* %64)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %73
67: ; preds = %62
%68 = load i32, i32* @stderr
%69 = load i32, i32* @errno
%70 = call i8* @strerror(i32 %69)
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %70)
%72 = call i32 @exit(i32 1)
unreachable
73: ; preds = %62
%74 = load i32*, i32** %3
%75 = getelementptr inbounds i32, i32* %74, i64 0
%76 = load i32, i32* %75
%77 = call i64 @setegid(i32 %76)
%78 = icmp slt i64 %77, 0
br i1 %78, label %79, label %85
79: ; preds = %73
%80 = load i32, i32* @stderr
%81 = load i32, i32* @errno
%82 = call i8* @strerror(i32 %81)
%83 = call i32 (i32, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %82)
%84 = call i32 @exit(i32 1)
unreachable
85: ; preds = %73
%86 = load i32*, i32** %3
%87 = call i32 @free(i32* %86)
ret void
}

LLVM IR (-O2):
@_SC_NGROUPS_MAX = global i32 0
@.str = [2 x i8] c",\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"too many gids\0A\00"
@.str.2 = [36 x i8] c"invalid gid '%s' - number expected\0A\00"
@.str.3 = [26 x i8] c"cannot change groups: %s\0A\00"
@errno = global i32 0
@.str.4 = [33 x i8] c"cannot change effective gid: %s\0A\00"
define void @set_gids(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = load i32, i32* @_SC_NGROUPS_MAX
%5 = tail call i64 @sysconf(i32 %4)
%6 = icmp sgt i64 %5, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = trunc i64 %5 to i32
%10 = shl i32 %9, 2
%11 = tail call i32* @malloc(i32 %10)
%12 = icmp ne i32* %11, null
%13 = zext i1 %12 to i32
%14 = tail call i32 @assert(i32 %13)
%15 = tail call i8* @strtok(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i8* %15, null
br i1 %16, label %43, label %17
17: ; preds = %1, %39
%18 = phi i32 [ %41, %39 ], [ 0, %1 ]
%19 = phi i8* [ %40, %39 ], [ %15, %1 ]
%20 = zext i32 %18 to i64
%21 = icmp sgt i64 %5, %20
br i1 %21, label %26, label %22
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = call i32 @strtol(i8* %19, i8** %2, i32 0)
%28 = getelementptr inbounds i32, i32* %11, i64 %20
store i32 %27, i32* %28
%29 = load i8*, i8** %2
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %39, label %32
32: ; preds = %26
%33 = tail call i32 @isspace(i8 zeroext %30)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = load i32, i32* @stderr
%37 = tail call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %26, %32
%40 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%41 = add i32 %18, 1
%42 = icmp eq i8* %40, null
br i1 %42, label %43, label %17
43: ; preds = %39, %1
%44 = phi i32 [ 0, %1 ], [ %41, %39 ]
%45 = tail call i64 @setgroups(i32 %44, i32* %11)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %43
%48 = load i32, i32* @stderr
%49 = load i32, i32* @errno
%50 = tail call i8* @strerror(i32 %49)
%51 = tail call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %50)
%52 = tail call i32 @exit(i32 1)
unreachable
53: ; preds = %43
%54 = load i32, i32* %11
%55 = tail call i64 @setegid(i32 %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %63
57: ; preds = %53
%58 = load i32, i32* @stderr
%59 = load i32, i32* @errno
%60 = tail call i8* @strerror(i32 %59)
%61 = tail call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %60)
%62 = tail call i32 @exit(i32 1)
unreachable
63: ; preds = %53
%64 = tail call i32 @free(i32* %11)
ret void
}