#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__ setregid(int, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  if (argc < 3) {
    fprintf(stderr, "Usage: %s <rgroup> <egroup>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  gid_t uids[2];
  struct group *grps[2];
  for (int i = 1; i < argc; i++) {
    if (strcmp(argv[i], "-1") == 0) {
      uids[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);
      }
      uids[i - 1] = grps[i - 1]->gr_gid;
    }
  }
  printf("Before: ");
  display();
  if (setregid(uids[0], uids[1]) < 0) {
    perror("setregid");
    goto fail;
  }
  printf("After: ");
  display();
  exit(0);
fail:
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-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   1060 <fprintf@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movl   $0x1,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    129f <main+0x11f>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe2d(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1214 <main+0x94>
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
cltq
movl   $0xffffffff,-0x18(%rbp,%rax,4)
ff
jmp    128c <main+0x10c>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1070 <getgrnam@plt>
mov    %rax,%rcx
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
cltq
mov    %rcx,-0x30(%rbp,%rax,8)
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
cltq
cmpq   $0x0,-0x30(%rbp,%rax,8)
jne    1271 <main+0xf1>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
cltq
mov    -0x30(%rbp,%rax,8),%rax
mov    (%rax),%ecx
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
cltq
mov    %ecx,-0x18(%rbp,%rax,4)
jmp    1291 <main+0x111>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11d1 <main+0x51>
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <display@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1080 <setregid@plt>
cmp    $0x0,%rax
jge    12da <main+0x15a>
lea    0xd69(%rip),%rdi
call   1090 <perror@plt>
jmp    12f6 <main+0x176>
lea    0xd61(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <display@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jg     11ac <main+0x2c>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe5e(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1241 <main+0xc1>
mov    %edi,%r15d
add    $0xffffffffffffffff,%r15
xor    %ebp,%ebp
lea    0xe61(%rip),%r14
jmp    11d1 <main+0x51>
xchg   %ax,%ax
mov    $0xffffffff,%eax
mov    %eax,(%rsp,%rbp,4)
add    $0x1,%rbp
cmp    %rbp,%r15
je     11f6 <main+0x76>
mov    0x8(%rbx,%rbp,8),%rdi
mov    %r14,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11c0 <main+0x40>
mov    0x8(%rbx,%rbp,8),%rdi
call   1070 <getgrnam@plt>
test   %rax,%rax
je     122e <main+0xae>
mov    (%rax),%eax
jmp    11c5 <main+0x45>
mov    (%rsp),%ebx
mov    0x4(%rsp),%ebp
lea    0xe2c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <display@plt>
mov    %ebx,%edi
mov    %ebp,%esi
call   1080 <setregid@plt>
test   %rax,%rax
jns    124f <main+0xcf>
lea    0xe12(%rip),%rdi
call   1090 <perror@plt>
jmp    1241 <main+0xc1>
mov    0x8(%rbx,%rbp,8),%rsi
lea    0xde6(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xdec(%rip),%rdi
xor    %eax,%eax
call   1040 <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 = [29 x i8] c"Usage: %s <rgroup> <egroup>\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 = [9 x i8] c"setregid\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 [2 x i32]
%7 = alloca [2 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, 3
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 ([29 x i8], [29 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 [2 x i32], [2 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 [2 x %struct.group*], [2 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 [2 x %struct.group*], [2 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 [2 x %struct.group*], [2 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 [2 x i32], [2 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 [2 x i32], [2 x i32]* %6, i64 0, i64 0
%83 = load i32, i32* %82
%84 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%85 = load i32, i32* %84
%86 = call i64 @setregid(i32 %83, i32 %85)
%87 = icmp slt i64 %86, 0
br i1 %87, label %88, label %90
88: ; preds = %79
%89 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %94
90: ; preds = %79
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%92 = call i32 (...) @display()
%93 = call i32 @exit(i32 0)
unreachable
94: ; preds = %88
%95 = load i32, i32* @EXIT_FAILURE
%96 = call i32 @exit(i32 %95)
unreachable
}

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