#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*/ uid_t;

/* Variables and functions */
int /*<<< orphan*/ execve(char const *, char **, char **);
int /*<<< orphan*/ getegid();
int /*<<< orphan*/ geteuid();
scalar_t__ getresuid(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                     int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
scalar_t__ setresgid(int /*<<< orphan*/, int /*<<< orphan*/,
                     int /*<<< orphan*/);
scalar_t__ setresuid(int /*<<< orphan*/, int /*<<< orphan*/,
                     int /*<<< orphan*/);
int main(int argc, char *argv[], char *envp[]) {
  const char wrapped[] = "WRAPPED";
  uid_t new_gid;
  uid_t new_uid, ruid, euid, suid;
  new_gid = getegid();
  new_uid = geteuid();
  if (setresgid(new_gid, new_gid, new_gid) < 0) {
    perror("setresgid()");
    return (1);
  }
  if (setresuid(new_uid, new_uid, new_uid) < 0) {
    perror("setresuid()");
    return (1);
  }
  if (getresuid(&ruid, &euid, &suid) < 0) {
    perror("getresuid()");
    return (1);
  }
  execve(wrapped, argv, envp);
  perror("execve()");
  return (1);
}


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    %rdx,-0x18(%rbp)
mov    0xe6f(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1080 <getegid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x24(%rbp),%edx
call   1040 <setresgid@plt>
cmp    $0x0,%rax
jge    11d9 <main+0x69>
lea    0xe40(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1260 <main+0xf0>
mov    -0x28(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1030 <setresuid@plt>
cmp    $0x0,%rax
jge    1209 <main+0x99>
lea    0xe1c(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1260 <main+0xf0>
lea    -0x2c(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   1070 <getresuid@plt>
cmp    $0x0,%rax
jge    123c <main+0xcc>
lea    0xdf5(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1260 <main+0xf0>
lea    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <execve@plt>
lea    0xdd8(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
movabs $0x44455050415257,%rax
mov    %rax,(%rsp)
xor    %eax,%eax
call   1080 <getegid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <geteuid@plt>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %ebx,%esi
mov    %ebx,%edx
call   1040 <setresgid@plt>
test   %rax,%rax
jns    11b9 <main+0x49>
lea    0xe49(%rip),%rdi
jmp    1209 <main+0x99>
mov    %ebp,%edi
mov    %ebp,%esi
mov    %ebp,%edx
call   1030 <setresuid@plt>
test   %rax,%rax
jns    11d2 <main+0x62>
lea    0xe3c(%rip),%rdi
jmp    1209 <main+0x99>
lea    0x14(%rsp),%rdi
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1070 <getresuid@plt>
test   %rax,%rax
jns    11f4 <main+0x84>
lea    0xe26(%rip),%rdi
jmp    1209 <main+0x99>
mov    %rsp,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <execve@plt>
lea    0xe1b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.wrapped = [8 x i8] c"WRAPPED\00"
@.str = [12 x i8] c"setresgid()\00"
@.str.1 = [12 x i8] c"setresuid()\00"
@.str.2 = [12 x i8] c"getresuid()\00"
@.str.3 = [9 x i8] c"execve()\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca [8 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%14 = bitcast [8 x i8]* %8 to i8*
%15 = call i32 (...) @getegid()
store i32 %15, i32* %9
%16 = call i32 (...) @geteuid()
store i32 %16, i32* %10
%17 = load i32, i32* %9
%18 = load i32, i32* %9
%19 = load i32, i32* %9
%20 = call i64 @setresgid(i32 %17, i32 %18, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %3
%23 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %43
24: ; preds = %3
%25 = load i32, i32* %10
%26 = load i32, i32* %10
%27 = load i32, i32* %10
%28 = call i64 @setresuid(i32 %25, i32 %26, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %43
32: ; preds = %24
%33 = call i64 @getresuid(i32* %11, i32* %12, i32* %13)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %4
br label %43
37: ; preds = %32
%38 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%39 = load i8**, i8*** %6
%40 = load i8**, i8*** %7
%41 = call i32 @execve(i8* %38, i8** %39, i8** %40)
%42 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %4
br label %43
43: ; preds = %37, %35, %30, %22
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
@.str = [12 x i8] c"setresgid()\00"
@.str.1 = [12 x i8] c"setresuid()\00"
@.str.2 = [12 x i8] c"getresuid()\00"
@.str.3 = [9 x i8] c"execve()\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = bitcast i64* %4 to i8*
store i64 19216509662483031, i64* %4
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = bitcast i32* %7 to i8*
%12 = tail call i32 (...) @getegid()
%13 = tail call i32 (...) @geteuid()
%14 = tail call i64 @setresgid(i32 %12, i32 %12, i32 %12)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %31
18: ; preds = %3
%19 = tail call i64 @setresuid(i32 %13, i32 %13, i32 %13)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %31
23: ; preds = %18
%24 = call i64 @getresuid(i32* %5, i32* %6, i32* %7)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %31
28: ; preds = %23
%29 = call i32 @execve(i8* %8, i8** %1, i8** %2)
%30 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %28, %26, %21, %16
ret i32 1
}