#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 gid_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getegid();
int getgid();
int setgid(int);
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  fprintf(stdout,
          "Run the two following commands as root to test this program:\n"
          "chown 1000.1000 %s\n"
          "chmod g+s %s\n\n",
          argv[0], argv[0]);
  gid_t real_group_id = getgid();
  gid_t effective_group_id = getegid();
  fprintf(stdout, "Real group ID of the process (GID) = %u\n", getgid());
  fprintf(stdout, "Effective group ID of the process (EGID) = %u\n", getegid());
  int result;
  result = setgid(real_group_id);
  fprintf(stdout,
          "\nSet EGID to the GID (lose privileges): setgid(%u) = %d [%s]\n\n",
          real_group_id, result, result == 0 ? "success" : "failure");
  fprintf(stdout, "Real group ID of the process (GID) = %u\n", getgid());
  fprintf(stdout, "Effective group ID of the process (EGID) = %u\n", getegid());
  result = setgid(effective_group_id);
  fprintf(stdout,
          "\nSet EGID to the former EGID (gain back privileges): setgid(%u) = "
          "%d [%s]\n\n",
          effective_group_id, result, result == 0 ? "success" : "failure");
  fprintf(stdout, "Real group ID of the process (GID) = %u\n", getgid());
  fprintf(stdout, "Effective group ID of the process (EGID) = %u\n", getegid());
  return 0;
}


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)
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
lea    0xe8c(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x0,%al
call   1040 <getgid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1050 <getegid@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1040 <getgid@plt>
mov    -0x34(%rbp),%edi
mov    %eax,%edx
lea    0xeb1(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1050 <getegid@plt>
mov    -0x30(%rbp),%edi
mov    %eax,%edx
lea    0xeb4(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1060 <setgid@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x1c(%rbp),%esi
lea    0xeff(%rip),%r8
lea    0xef0(%rip),%rax
cmp    $0x0,%esi
cmove  %rax,%r8
lea    0xea3(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1040 <getgid@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%edx
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1050 <getegid@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%edx
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x18(%rbp),%edi
call   1060 <setgid@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x1c(%rbp),%esi
lea    0xe73(%rip),%r8
lea    0xe64(%rip),%rax
cmp    $0x0,%esi
cmove  %rax,%r8
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1040 <getgid@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%edx
lea    0xd99(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <getegid@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%edx
lea    0xd9c(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e8e(%rip),%r13
mov    0x0(%r13),%edi
mov    (%rsi),%rcx
lea    0xea0(%rip),%rsi
mov    %rcx,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
xor    %eax,%eax
call   1040 <getgid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <getegid@plt>
mov    %eax,%r12d
mov    0x0(%r13),%ebx
xor    %eax,%eax
call   1040 <getgid@plt>
lea    0xed0(%rip),%r15
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x0(%r13),%ebx
xor    %eax,%eax
call   1050 <getegid@plt>
lea    0xed9(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %ebp,%edi
call   1060 <setgid@plt>
mov    0x0(%r13),%edi
test   %eax,%eax
lea    0xf28(%rip),%r14
lea    0xf29(%rip),%rbx
mov    %rbx,%r8
cmove  %r14,%r8
lea    0xed4(%rip),%rsi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x0(%r13),%ebp
xor    %eax,%eax
call   1040 <getgid@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x0(%r13),%ebp
xor    %eax,%eax
call   1050 <getegid@plt>
mov    %ebp,%edi
lea    0xe6d(%rip),%rbp
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %r12d,%edi
call   1060 <setgid@plt>
mov    0x0(%r13),%edi
test   %eax,%eax
cmove  %r14,%rbx
lea    0xec6(%rip),%rsi
mov    %r12d,%edx
mov    %eax,%ecx
mov    %rbx,%r8
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x0(%r13),%ebx
xor    %eax,%eax
call   1040 <getgid@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x0(%r13),%ebx
xor    %eax,%eax
call   1050 <getegid@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [95 x i8] c"Run the two following commands as root to test this program:\0Achown 1000.1000 %s\0Achmod g+s %s\0A\0A\00"
@.str.1 = [41 x i8] c"Real group ID of the process (GID) = %u\0A\00"
@.str.2 = [47 x i8] c"Effective group ID of the process (EGID) = %u\0A\00"
@.str.3 = [63 x i8] c"\0ASet EGID to the GID (lose privileges): setgid(%u) = %d [%s]\0A\0A\00"
@.str.4 = [8 x i8] c"success\00"
@.str.5 = [8 x i8] c"failure\00"
@.str.6 = [76 x i8] c"\0ASet EGID to the former EGID (gain back privileges): setgid(%u) = %d [%s]\0A\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @stdout
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([95 x i8], [95 x i8]* @.str, i64 0, i64 0), i8* %12, i8* %15)
%17 = call i32 (...) @getgid()
store i32 %17, i32* %6
%18 = call i32 (...) @getegid()
store i32 %18, i32* %7
%19 = load i32, i32* @stdout
%20 = call i32 (...) @getgid()
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = load i32, i32* @stdout
%23 = call i32 (...) @getegid()
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %6
%26 = call i32 @setgid(i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* @stdout
%28 = load i32, i32* %6
%29 = load i32, i32* %8
%30 = load i32, i32* %8
%31 = icmp eq i32 %30, 0
%32 = zext i1 %31 to i64
%33 = select i1 %31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0)
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.3, i64 0, i64 0), i32 %28, i32 %29, i8* %33)
%35 = load i32, i32* @stdout
%36 = call i32 (...) @getgid()
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = load i32, i32* @stdout
%39 = call i32 (...) @getegid()
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %7
%42 = call i32 @setgid(i32 %41)
store i32 %42, i32* %8
%43 = load i32, i32* @stdout
%44 = load i32, i32* %7
%45 = load i32, i32* %8
%46 = load i32, i32* %8
%47 = icmp eq i32 %46, 0
%48 = zext i1 %47 to i64
%49 = select i1 %47, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0)
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.6, i64 0, i64 0), i32 %44, i32 %45, i8* %49)
%51 = load i32, i32* @stdout
%52 = call i32 (...) @getgid()
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %52)
%54 = load i32, i32* @stdout
%55 = call i32 (...) @getegid()
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %55)
ret i32 0
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [95 x i8] c"Run the two following commands as root to test this program:\0Achown 1000.1000 %s\0Achmod g+s %s\0A\0A\00"
@.str.1 = [41 x i8] c"Real group ID of the process (GID) = %u\0A\00"
@.str.2 = [47 x i8] c"Effective group ID of the process (EGID) = %u\0A\00"
@.str.3 = [63 x i8] c"\0ASet EGID to the GID (lose privileges): setgid(%u) = %d [%s]\0A\0A\00"
@.str.4 = [8 x i8] c"success\00"
@.str.5 = [8 x i8] c"failure\00"
@.str.6 = [76 x i8] c"\0ASet EGID to the former EGID (gain back privileges): setgid(%u) = %d [%s]\0A\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @stdout
%4 = load i8*, i8** %1
%5 = tail call i32 (i32, i8*, ...) @fprintf(i32 %3, i8* getelementptr inbounds ([95 x i8], [95 x i8]* @.str, i64 0, i64 0), i8* %4, i8* %4)
%6 = tail call i32 (...) @getgid()
%7 = tail call i32 (...) @getegid()
%8 = load i32, i32* @stdout
%9 = tail call i32 (...) @getgid()
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @stdout
%12 = tail call i32 (...) @getegid()
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = tail call i32 @setgid(i32 %6)
%15 = load i32, i32* @stdout
%16 = icmp eq i32 %14, 0
%17 = select i1 %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0)
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.3, i64 0, i64 0), i32 %6, i32 %14, i8* %17)
%19 = load i32, i32* @stdout
%20 = tail call i32 (...) @getgid()
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = load i32, i32* @stdout
%23 = tail call i32 (...) @getegid()
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = tail call i32 @setgid(i32 %7)
%26 = load i32, i32* @stdout
%27 = icmp eq i32 %25, 0
%28 = select i1 %27, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0)
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.6, i64 0, i64 0), i32 %7, i32 %25, i8* %28)
%30 = load i32, i32* @stdout
%31 = tail call i32 (...) @getgid()
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @stdout
%34 = tail call i32 (...) @getegid()
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %34)
ret i32 0
}