#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 group {
  int gr_gid;
};
typedef int gid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ display();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct group *getgrnam(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setresgid(int, int, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  if (argc < 4) {
    fprintf(stderr, "Usage: %s <rgroup> <egroup> <sgroup>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  gid_t gids[3];
  struct group *grps[3];
  for (int i = 1; i < argc; i++) {
    if (strcmp(argv[i], "-1") == 0) {
      gids[i - 1] = -1;
    } else {
      grps[i - 1] = getgrnam(argv[i]);
      if (grps[i - 1] == NULL) {
        printf("'%s' not found\n", argv[i]);
        exit(EXIT_FAILURE);
      }
      gids[i - 1] = grps[i - 1]->gr_gid;
    }
  }
  printf("Before: ");
  display();
  if (setresgid(gids[0], gids[1], gids[2]) < 0) {
    perror("setresgid");
    goto fail;
  }
  printf("After: ");
  display();
  exit(0);
fail:
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11ca <main+0x4a>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movl   $0x1,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    129f <main+0x11f>
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe36(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1214 <main+0x94>
mov    -0x44(%rbp),%eax
sub    $0x1,%eax
cltq
movl   $0xffffffff,-0x1c(%rbp,%rax,4)
ff
jmp    128c <main+0x10c>
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1080 <getgrnam@plt>
mov    %rax,%rcx
mov    -0x44(%rbp),%eax
sub    $0x1,%eax
cltq
mov    %rcx,-0x40(%rbp,%rax,8)
mov    -0x44(%rbp),%eax
sub    $0x1,%eax
cltq
cmpq   $0x0,-0x40(%rbp,%rax,8)
jne    1271 <main+0xf1>
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x44(%rbp),%eax
sub    $0x1,%eax
cltq
mov    -0x40(%rbp,%rax,8),%rax
mov    (%rax),%ecx
mov    -0x44(%rbp),%eax
sub    $0x1,%eax
cltq
mov    %ecx,-0x1c(%rbp,%rax,4)
jmp    1291 <main+0x111>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    11d1 <main+0x51>
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <display@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1040 <setresgid@plt>
cmp    $0x0,%rax
jge    12dd <main+0x15d>
lea    0xd6f(%rip),%rdi
call   1090 <perror@plt>
jmp    12f9 <main+0x179>
lea    0xd68(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <display@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jg     11af <main+0x2f>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe5b(%rip),%rsi
call   1070 <fprintf@plt>
jmp    125b <main+0xdb>
mov    %edi,%r15d
add    $0xffffffffffffffff,%r15
xor    %ebp,%ebp
lea    0xe67(%rip),%r14
jmp    11e2 <main+0x62>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0xffffffff,%eax
mov    %eax,0xc(%rsp,%rbp,4)
add    $0x1,%rbp
cmp    %rbp,%r15
je     1207 <main+0x87>
mov    0x8(%rbx,%rbp,8),%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11d0 <main+0x50>
mov    0x8(%rbx,%rbp,8),%rdi
call   1080 <getgrnam@plt>
test   %rax,%rax
je     1248 <main+0xc8>
mov    (%rax),%eax
jmp    11d5 <main+0x55>
mov    0xc(%rsp),%ebx
mov    0x10(%rsp),%ebp
mov    0x14(%rsp),%r14d
lea    0xe1e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1030 <display@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %r14d,%edx
call   1040 <setresgid@plt>
test   %rax,%rax
jns    1269 <main+0xe9>
lea    0xe01(%rip),%rdi
call   1090 <perror@plt>
jmp    125b <main+0xdb>
mov    0x8(%rbx,%rbp,8),%rsi
lea    0xdd5(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xddc(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1030 <display@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.group = type { i32 }
@stderr = global i32 0
@.str = [38 x i8] c"Usage: %s <rgroup> <egroup> <sgroup>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"-1\00"
@.str.2 = [16 x i8] c"'%s' not found\0A\00"
@.str.3 = [9 x i8] c"Before: \00"
@.str.4 = [10 x i8] c"setresgid\00"
@.str.5 = [8 x i8] c"After: \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [3 x i32]
%7 = alloca [3 x %struct.group*]
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 4
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
store i32 1, i32* %8
br label %20
20: ; preds = %76, %19
%21 = load i32, i32* %8
%22 = load i32, i32* %4
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %79
24: ; preds = %20
%25 = load i8**, i8*** %5
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8*, i8** %25, i64 %27
%29 = load i8*, i8** %28
%30 = call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %24
%33 = load i32, i32* %8
%34 = sub nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [3 x i32], [3 x i32]* %6, i64 0, i64 %35
store i32 -1, i32* %36
br label %75
37: ; preds = %24
%38 = load i8**, i8*** %5
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %38, i64 %40
%42 = load i8*, i8** %41
%43 = call %struct.group* @getgrnam(i8* %42)
%44 = load i32, i32* %8
%45 = sub nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [3 x %struct.group*], [3 x %struct.group*]* %7, i64 0, i64 %46
store %struct.group* %43, %struct.group** %47
%48 = load i32, i32* %8
%49 = sub nsw i32 %48, 1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [3 x %struct.group*], [3 x %struct.group*]* %7, i64 0, i64 %50
%52 = load %struct.group*, %struct.group** %51
%53 = icmp eq %struct.group* %52, null
br i1 %53, label %54, label %63
54: ; preds = %37
%55 = load i8**, i8*** %5
%56 = load i32, i32* %8
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8*, i8** %55, i64 %57
%59 = load i8*, i8** %58
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %59)
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %37
%64 = load i32, i32* %8
%65 = sub nsw i32 %64, 1
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds [3 x %struct.group*], [3 x %struct.group*]* %7, i64 0, i64 %66
%68 = load %struct.group*, %struct.group** %67
%69 = getelementptr inbounds %struct.group, %struct.group* %68, i32 0, i32 0
%70 = load i32, i32* %69
%71 = load i32, i32* %8
%72 = sub nsw i32 %71, 1
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds [3 x i32], [3 x i32]* %6, i64 0, i64 %73
store i32 %70, i32* %74
br label %75
75: ; preds = %63, %32
br label %76
76: ; preds = %75
%77 = load i32, i32* %8
%78 = add nsw i32 %77, 1
store i32 %78, i32* %8
br label %20
79: ; preds = %20
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%81 = call i32 (...) @display()
%82 = getelementptr inbounds [3 x i32], [3 x i32]* %6, i64 0, i64 0
%83 = load i32, i32* %82
%84 = getelementptr inbounds [3 x i32], [3 x i32]* %6, i64 0, i64 1
%85 = load i32, i32* %84
%86 = getelementptr inbounds [3 x i32], [3 x i32]* %6, i64 0, i64 2
%87 = load i32, i32* %86
%88 = call i64 @setresgid(i32 %83, i32 %85, i32 %87)
%89 = icmp slt i64 %88, 0
br i1 %89, label %90, label %92
90: ; preds = %79
%91 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %96
92: ; preds = %79
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%94 = call i32 (...) @display()
%95 = call i32 @exit(i32 0)
unreachable
96: ; preds = %90
%97 = load i32, i32* @EXIT_FAILURE
%98 = call i32 @exit(i32 %97)
unreachable
}

LLVM IR (-O2):
%struct.group = type { i32 }
@stderr = global i32 0
@.str = [38 x i8] c"Usage: %s <rgroup> <egroup> <sgroup>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"-1\00"
@.str.2 = [16 x i8] c"'%s' not found\0A\00"
@.str.3 = [9 x i8] c"Before: \00"
@.str.4 = [10 x i8] c"setresgid\00"
@.str.5 = [8 x i8] c"After: \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [3 x i32]
%4 = icmp slt i32 %0, 4
br i1 %4, label %5, label %11
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = bitcast [3 x i32]* %3 to i8*
%13 = zext i32 %0 to i64
br label %25
14: ; preds = %43
%15 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 0
%16 = load i32, i32* %15
%17 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 2
%20 = load i32, i32* %19
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%22 = tail call i32 (...) @display()
%23 = tail call i64 @setresgid(i32 %16, i32 %18, i32 %20)
%24 = icmp slt i64 %23, 0
br i1 %24, label %49, label %53
25: ; preds = %11, %43
%26 = phi i64 [ 1, %11 ], [ %47, %43 ]
%27 = getelementptr inbounds i8*, i8** %1, i64 %26
%28 = load i8*, i8** %27
%29 = tail call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %43, label %31
31: ; preds = %25
%32 = load i8*, i8** %27
%33 = tail call %struct.group* @getgrnam(i8* %32)
%34 = icmp eq %struct.group* %33, null
br i1 %34, label %35, label %40
35: ; preds = %31
%36 = load i8*, i8** %27
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = load i32, i32* @EXIT_FAILURE
%39 = tail call i32 @exit(i32 %38)
unreachable
40: ; preds = %31
%41 = getelementptr inbounds %struct.group, %struct.group* %33, i64 0, i32 0
%42 = load i32, i32* %41
br label %43
43: ; preds = %25, %40
%44 = phi i32 [ %42, %40 ], [ -1, %25 ]
%45 = add nsw i64 %26, -1
%46 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 %45
store i32 %44, i32* %46
%47 = add nuw nsw i64 %26, 1
%48 = icmp eq i64 %47, %13
br i1 %48, label %14, label %25
49: ; preds = %14
%50 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
%52 = tail call i32 @exit(i32 %51)
unreachable
53: ; preds = %14
%54 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%55 = tail call i32 (...) @display()
%56 = tail call i32 @exit(i32 0)
unreachable
}