#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;
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ getgid();
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ perror(char *);
scalar_t__ setresgid(int /*<<< orphan*/, int /*<<< orphan*/,
                     int /*<<< orphan*/);
scalar_t__ setresuid(int /*<<< orphan*/, int /*<<< orphan*/,
                     int /*<<< orphan*/);
void drop_privileges(void) {
  uid_t uid = getuid();
  gid_t gid = getgid();
  if (setresgid(gid, gid, gid) != 0) {
    perror("setresgid");
    exit(1);
  }
  if (setresuid(uid, uid, uid) != 0) {
    perror("setresuid");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<drop_privileges>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,-0x4(%rbp)
mov    $0x0,%al
call   1060 <getgid@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x8(%rbp),%edx
call   1050 <setresgid@plt>
cmp    $0x0,%rax
je     11aa <drop_privileges+0x4a>
lea    0xe65(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%edi
mov    -0x4(%rbp),%esi
mov    -0x4(%rbp),%edx
call   1030 <setresuid@plt>
cmp    $0x0,%rax
je     11d8 <drop_privileges+0x78>
lea    0xe41(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drop_privileges>:
push   %rbx
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getgid@plt>
mov    %eax,%edi
mov    %eax,%esi
mov    %eax,%edx
call   1050 <setresgid@plt>
test   %rax,%rax
jne    1193 <drop_privileges+0x33>
mov    %ebx,%edi
mov    %ebx,%esi
mov    %ebx,%edx
call   1030 <setresuid@plt>
test   %rax,%rax
jne    119c <drop_privileges+0x3c>
pop    %rbx
ret
lea    0xe66(%rip),%rdi
jmp    11a3 <drop_privileges+0x43>
lea    0xe67(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"setresgid\00"
@.str.1 = [10 x i8] c"setresuid\00"
define void @drop_privileges() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @getuid()
store i32 %3, i32* %1
%4 = call i32 (...) @getgid()
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = load i32, i32* %2
%7 = load i32, i32* %2
%8 = call i64 @setresgid(i32 %5, i32 %6, i32 %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32, i32* %1
%15 = load i32, i32* %1
%16 = load i32, i32* %1
%17 = call i64 @setresuid(i32 %14, i32 %15, i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %13
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"setresgid\00"
@.str.1 = [10 x i8] c"setresuid\00"
define void @drop_privileges() {
%1 = tail call i32 (...) @getuid()
%2 = tail call i32 (...) @getgid()
%3 = tail call i64 @setresgid(i32 %2, i32 %2, i32 %2)
%4 = icmp eq i64 %3, 0
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = tail call i64 @setresuid(i32 %1, i32 %1, i32 %1)
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8
ret void
}