#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_temporarily(void) {
  const int gid = getgid(), uid = getuid();
  const int egid = getegid(), euid = geteuid();
  if (setresuid(uid, uid, euid) != 0)
    goto error;
  if (setresgid(gid, gid, egid) != 0)
    goto error;
  if (geteuid() != getuid() || getegid() != getgid()) {
    printf("d_t: fun\n");
    goto error;
  }
  return;
error:
  printf("d_t: failure\n");
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<drop_temporarily>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1080 <getgid@plt>
mov    %eax,-0x4(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1090 <getegid@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1070 <geteuid@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1030 <setresuid@plt>
cmp    $0x0,%rax
je     11cd <drop_temporarily+0x4d>
jmp    123d <drop_temporarily+0xbd>
mov    -0x4(%rbp),%edi
mov    -0x4(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1050 <setresgid@plt>
cmp    $0x0,%rax
je     11ea <drop_temporarily+0x6a>
jmp    123d <drop_temporarily+0xbd>
mov    $0x0,%al
call   1070 <geteuid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
cmp    %ecx,%eax
jne    1226 <drop_temporarily+0xa6>
mov    $0x0,%al
call   1090 <getegid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1080 <getgid@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
je     1237 <drop_temporarily+0xb7>
lea    0xdd3(%rip),%rdi
call   1060 <printf@plt>
jmp    123d <drop_temporarily+0xbd>
add    $0x20,%rsp
pop    %rbp
ret
lea    0xdc6(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<drop_temporarily>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1080 <getgid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1090 <getegid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1070 <geteuid@plt>
mov    %ebp,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <setresuid@plt>
test   %rax,%rax
jne    1201 <drop_temporarily+0x81>
mov    %ebx,%edi
mov    %ebx,%esi
mov    %r14d,%edx
call   1060 <setresgid@plt>
test   %rax,%rax
jne    1201 <drop_temporarily+0x81>
xor    %eax,%eax
call   1070 <geteuid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getuid@plt>
cmp    %eax,%ebx
jne    11f5 <drop_temporarily+0x75>
xor    %eax,%eax
call   1090 <getegid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <getgid@plt>
cmp    %eax,%ebx
jne    11f5 <drop_temporarily+0x75>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe04(%rip),%rdi
call   1030 <puts@plt>
lea    0xe01(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dcc(%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_t: failure\0A\00"
@EXIT_FAILURE = global i32 0
define void @drop_temporarily() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = call i32 (...) @getgid()
store i32 %5, i32* %1
%6 = call i32 (...) @getuid()
store i32 %6, i32* %2
%7 = call i32 (...) @getegid()
store i32 %7, i32* %3
%8 = call i32 (...) @geteuid()
store i32 %8, i32* %4
%9 = load i32, i32* %2
%10 = load i32, i32* %2
%11 = load i32, i32* %4
%12 = call i64 @setresuid(i32 %9, i32 %10, i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %0
br label %33
15: ; preds = %0
%16 = load i32, i32* %1
%17 = load i32, i32* %1
%18 = load i32, i32* %3
%19 = call i64 @setresgid(i32 %16, i32 %17, i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
br label %33
22: ; preds = %15
%23 = call i32 (...) @geteuid()
%24 = call i32 (...) @getuid()
%25 = icmp ne i32 %23, %24
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = call i32 (...) @getegid()
%28 = call i32 (...) @getgid()
%29 = icmp ne i32 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %26, %22
%31 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %33
32: ; preds = %26
ret void
33: ; preds = %30, %21, %14
%34 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
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_t: failure\00"
define void @drop_temporarily() {
%1 = tail call i32 (...) @getgid()
%2 = tail call i32 (...) @getuid()
%3 = tail call i32 (...) @getegid()
%4 = tail call i32 (...) @geteuid()
%5 = tail call i64 @setresuid(i32 %2, i32 %2, i32 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %21
7: ; preds = %0
%8 = tail call i64 @setresgid(i32 %1, i32 %1, i32 %3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %21
10: ; preds = %7
%11 = tail call i32 (...) @geteuid()
%12 = tail call i32 (...) @getuid()
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = tail call i32 (...) @getegid()
%16 = tail call i32 (...) @getgid()
%17 = icmp eq i32 %15, %16
br i1 %17, label %20, label %18
18: ; preds = %14, %10
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %21
20: ; preds = %14
ret void
21: ; preds = %7, %0, %18
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.2, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
}