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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getegid();
int geteuid();
int getgid();
int getuid();
int /*<<< orphan*/ printf(char *);
scalar_t__ setresgid(int const, int const, int const);
scalar_t__ setresuid(int const, int const, int const);
void drop_pernamently(void) {
  const int gid = getgid(), uid = getuid();
  if (setresuid(uid, uid, uid) != 0)
    goto error;
  if (setresgid(gid, gid, gid) != 0)
    goto error;
  if (geteuid() != getuid() || getegid() != getgid()) {
    printf("d_t: fun\n");
    goto error;
  }
  return;
error:
  printf("d_p: failure\n");
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<drop_pernamently>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1080 <getgid@plt>
mov    %eax,-0x4(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x8(%rbp),%edx
call   1030 <setresuid@plt>
cmp    $0x0,%rax
je     11b9 <drop_pernamently+0x39>
jmp    1229 <drop_pernamently+0xa9>
mov    -0x4(%rbp),%edi
mov    -0x4(%rbp),%esi
mov    -0x4(%rbp),%edx
call   1050 <setresgid@plt>
cmp    $0x0,%rax
je     11d6 <drop_pernamently+0x56>
jmp    1229 <drop_pernamently+0xa9>
mov    $0x0,%al
call   1070 <geteuid@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,%ecx
mov    -0xc(%rbp),%eax
cmp    %ecx,%eax
jne    1212 <drop_pernamently+0x92>
mov    $0x0,%al
call   1090 <getegid@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1080 <getgid@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
cmp    %ecx,%eax
je     1223 <drop_pernamently+0xa3>
lea    0xde7(%rip),%rdi
call   1060 <printf@plt>
jmp    1229 <drop_pernamently+0xa9>
add    $0x10,%rsp
pop    %rbp
ret
lea    0xdda(%rip),%rdi
call   1060 <printf@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<drop_pernamently>:
push   %rbx
xor    %eax,%eax
call   1080 <getgid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%edi
mov    %eax,%esi
mov    %eax,%edx
call   1040 <setresuid@plt>
test   %rax,%rax
jne    11e7 <drop_pernamently+0x67>
mov    %ebx,%edi
mov    %ebx,%esi
mov    %ebx,%edx
call   1060 <setresgid@plt>
test   %rax,%rax
jne    11e7 <drop_pernamently+0x67>
xor    %eax,%eax
call   1070 <geteuid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getuid@plt>
cmp    %eax,%ebx
jne    11db <drop_pernamently+0x5b>
xor    %eax,%eax
call   1090 <getegid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <getgid@plt>
cmp    %eax,%ebx
jne    11db <drop_pernamently+0x5b>
pop    %rbx
ret
lea    0xe1e(%rip),%rdi
call   1030 <puts@plt>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"d_t: fun\0A\00"
@.str.1 = [14 x i8] c"d_p: failure\0A\00"
@EXIT_FAILURE = global i32 0
define void @drop_pernamently() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @getgid()
store i32 %3, i32* %1
%4 = call i32 (...) @getuid()
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = load i32, i32* %2
%7 = load i32, i32* %2
%8 = call i64 @setresuid(i32 %5, i32 %6, i32 %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %0
br label %29
11: ; preds = %0
%12 = load i32, i32* %1
%13 = load i32, i32* %1
%14 = load i32, i32* %1
%15 = call i64 @setresgid(i32 %12, i32 %13, i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %11
br label %29
18: ; preds = %11
%19 = call i32 (...) @geteuid()
%20 = call i32 (...) @getuid()
%21 = icmp ne i32 %19, %20
br i1 %21, label %26, label %22
22: ; preds = %18
%23 = call i32 (...) @getegid()
%24 = call i32 (...) @getgid()
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %22, %18
%27 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %29
28: ; preds = %22
ret void
29: ; preds = %26, %17, %10
%30 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@str = [9 x i8] c"d_t: fun\00"
@str.2 = [13 x i8] c"d_p: failure\00"
define void @drop_pernamently() {
%1 = tail call i32 (...) @getgid()
%2 = tail call i32 (...) @getuid()
%3 = tail call i64 @setresuid(i32 %2, i32 %2, i32 %2)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %19
5: ; preds = %0
%6 = tail call i64 @setresgid(i32 %1, i32 %1, i32 %1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %19
8: ; preds = %5
%9 = tail call i32 (...) @geteuid()
%10 = tail call i32 (...) @getuid()
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = tail call i32 (...) @getegid()
%14 = tail call i32 (...) @getgid()
%15 = icmp eq i32 %13, %14
br i1 %15, label %18, label %16
16: ; preds = %12, %8
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %19
18: ; preds = %12
ret void
19: ; preds = %5, %0, %16
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.2, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
}