#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 {
  scalar_t__ gr_gid;
};

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getegid();
int getgid();
struct group *getgrnam(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setresgid(int, scalar_t__, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  struct group *grp;
  if (argc != 2) {
    printf("Usage: %s <user>\n", argv[0]);
    return 1;
  }
  if ((grp = getgrnam(argv[1])) == NULL) {
    fprintf(stderr, "Unable to locate group %s\n", argv[1]);
    return 2;
  }
  printf("Setting egid to %d\n", (int)grp->gr_gid);
  if (setresgid(-1, grp->gr_gid, -1)) {
    fprintf(stderr, "setresgid( -1, %d, -1) failed (%s)\n", (int)grp->gr_gid,
            strerror(errno));
    return 3;
  }
  printf("Process gid now (%d, %d)\n", getgid(), getegid());
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    129d <main+0x12d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <getgrnam@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11f7 <main+0x87>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe2e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    129d <main+0x12d>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %eax,%esi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    $0xffffffff,%edx
mov    %edx,%edi
call   1030 <setresgid@plt>
cmp    $0x0,%rax
je     1272 <main+0x102>
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %eax,-0x1c(%rbp)
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%edx
mov    %rax,%rcx
lea    0xde2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x3,-0x4(%rbp)
jmp    129d <main+0x12d>
mov    $0x0,%al
call   1070 <getgid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1080 <getegid@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbp
cmp    $0x2,%edi
jne    11ea <main+0x7a>
mov    0x8(%rbp),%rdi
call   1060 <getgrnam@plt>
test   %rax,%rax
je     1203 <main+0x93>
mov    %rax,%rbx
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rbx),%rsi
mov    $0xffffffff,%edi
mov    $0xffffffff,%edx
call   1030 <setresgid@plt>
test   %rax,%rax
je     1225 <main+0xb5>
mov    0x2e24(%rip),%rax
mov    (%rax),%ebp
mov    (%rbx),%ebx
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe6c(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x3,%ebp
jmp    1247 <main+0xd7>
mov    0x0(%rbp),%rsi
lea    0xe0b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%ebp
jmp    1247 <main+0xd7>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbp),%rdx
lea    0xdfb(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x2,%ebp
jmp    1247 <main+0xd7>
xor    %eax,%eax
call   1070 <getgid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <getegid@plt>
lea    0xe29(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.group = type { i64 }
@.str = [18 x i8] c"Usage: %s <user>\0A\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Unable to locate group %s\0A\00"
@.str.2 = [20 x i8] c"Setting egid to %d\0A\00"
@.str.3 = [36 x i8] c"setresgid( -1, %d, -1) failed (%s)\0A\00"
@errno = global i32 0
@.str.4 = [26 x i8] c"Process gid now (%d, %d)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.group*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 2
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %12)
store i32 1, i32* %3
br label %50
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call %struct.group* @getgrnam(i8* %17)
store %struct.group* %18, %struct.group** %6
%19 = icmp eq %struct.group* %18, null
br i1 %19, label %20, label %26
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %24)
store i32 2, i32* %3
br label %50
26: ; preds = %14
%27 = load %struct.group*, %struct.group** %6
%28 = getelementptr inbounds %struct.group, %struct.group* %27, i32 0, i32 0
%29 = load i64, i64* %28
%30 = trunc i64 %29 to i32
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = load %struct.group*, %struct.group** %6
%33 = getelementptr inbounds %struct.group, %struct.group* %32, i32 0, i32 0
%34 = load i64, i64* %33
%35 = call i64 @setresgid(i32 -1, i64 %34, i32 -1)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %26
%38 = load i32, i32* @stderr
%39 = load %struct.group*, %struct.group** %6
%40 = getelementptr inbounds %struct.group, %struct.group* %39, i32 0, i32 0
%41 = load i64, i64* %40
%42 = trunc i64 %41 to i32
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %42, i8* %44)
store i32 3, i32* %3
br label %50
46: ; preds = %26
%47 = call i32 (...) @getgid()
%48 = call i32 (...) @getegid()
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %47, i32 %48)
store i32 0, i32* %3
br label %50
50: ; preds = %46, %37, %20, %9
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
%struct.group = type { i64 }
@.str = [18 x i8] c"Usage: %s <user>\0A\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Unable to locate group %s\0A\00"
@.str.2 = [20 x i8] c"Setting egid to %d\0A\00"
@.str.3 = [36 x i8] c"setresgid( -1, %d, -1) failed (%s)\0A\00"
@errno = global i32 0
@.str.4 = [26 x i8] c"Process gid now (%d, %d)\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %35
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call %struct.group* @getgrnam(i8* %9)
%11 = icmp eq %struct.group* %10, null
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %8
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %35
16: ; preds = %7
%17 = getelementptr inbounds %struct.group, %struct.group* %10, i64 0, i32 0
%18 = load i64, i64* %17
%19 = trunc i64 %18 to i32
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = load i64, i64* %17
%22 = tail call i64 @setresgid(i32 -1, i64 %21, i32 -1)
%23 = icmp eq i64 %22, 0
br i1 %23, label %31, label %24
24: ; preds = %16
%25 = load i32, i32* @stderr
%26 = load i64, i64* %17
%27 = trunc i64 %26 to i32
%28 = load i32, i32* @errno
%29 = tail call i8* @strerror(i32 %28)
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %27, i8* %29)
br label %35
31: ; preds = %16
%32 = tail call i32 (...) @getgid()
%33 = tail call i32 (...) @getegid()
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %32, i32 %33)
br label %35
35: ; preds = %31, %24, %12, %4
%36 = phi i32 [ 1, %4 ], [ 2, %12 ], [ 3, %24 ], [ 0, %31 ]
ret i32 %36
}