void rpctest_drop_privileges(void) {
  if (getuid() != 0 || geteuid() != 0)
    log_fatal("%s: I don't seem to be root", __FUNCTION__);
  setreuid(0, 65534);
  if (getuid() != 0)
    log_fatal("lost real uid 0");
  if (geteuid() != 65534)
    log_fatal("unable to set euid to -2");
}


x86 assembly (clang, -O0):
<rpctest_drop_privileges>:
push   %rbp
mov    %rsp,%rbp
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%eax
jne    1164 <rpctest_drop_privileges+0x24>
mov    $0x0,%al
call   1040 <geteuid@plt>
cmp    $0x0,%eax
je     1179 <rpctest_drop_privileges+0x39>
lea    0xe95(%rip),%rdi
lea    0xeaa(%rip),%rsi
mov    $0x0,%al
call   1060 <log_fatal@plt>
xor    %edi,%edi
mov    $0xfffe,%esi
mov    $0x0,%al
call   1050 <setreuid@plt>
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%eax
je     11a5 <rpctest_drop_privileges+0x65>
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1060 <log_fatal@plt>
mov    $0x0,%al
call   1040 <geteuid@plt>
cmp    $0xfffe,%eax
je     11c5 <rpctest_drop_privileges+0x85>
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1060 <log_fatal@plt>
pop    %rbp
ret

x86 assembly (clang, -O2):
<rpctest_drop_privileges>:
push   %rax
xor    %eax,%eax
call   1030 <getuid@plt>
test   %eax,%eax
jne    1157 <rpctest_drop_privileges+0x17>
xor    %eax,%eax
call   1040 <geteuid@plt>
test   %eax,%eax
je     116c <rpctest_drop_privileges+0x2c>
lea    0xea2(%rip),%rdi
lea    0xeb7(%rip),%rsi
xor    %eax,%eax
call   1060 <log_fatal@plt>
xor    %edi,%edi
mov    $0xfffe,%esi
xor    %eax,%eax
call   1050 <setreuid@plt>
xor    %eax,%eax
call   1030 <getuid@plt>
test   %eax,%eax
je     1193 <rpctest_drop_privileges+0x53>
lea    0xea8(%rip),%rdi
xor    %eax,%eax
call   1060 <log_fatal@plt>
xor    %eax,%eax
call   1040 <geteuid@plt>
cmp    $0xfffe,%eax
jne    11a3 <rpctest_drop_privileges+0x63>
pop    %rax
ret
lea    0xe9a(%rip),%rdi
xor    %eax,%eax
pop    %rcx
jmp    1060 <log_fatal@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"%s: I don't seem to be root\00"
@__FUNCTION__.rpctest_drop_privileges = [24 x i8] c"rpctest_drop_privileges\00"
@.str.1 = [16 x i8] c"lost real uid 0\00"
@.str.2 = [25 x i8] c"unable to set euid to -2\00"
define void @rpctest_drop_privileges() {
%1 = call i32 (...) @getuid()
%2 = icmp ne i32 %1, 0
br i1 %2, label %6, label %3
3: ; preds = %0
%4 = call i32 (...) @geteuid()
%5 = icmp ne i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3, %0
%7 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @log_fatal to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @__FUNCTION__.rpctest_drop_privileges, i64 0, i64 0))
br label %8
8: ; preds = %6, %3
%9 = call i32 (i32, i32, ...) bitcast (i32 (...)* @setreuid to i32 (i32, i32, ...)*)(i32 0, i32 65534)
%10 = call i32 (...) @getuid()
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 (i8*, ...) bitcast (i32 (...)* @log_fatal to i32 (i8*, ...)*)(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = call i32 (...) @geteuid()
%16 = icmp ne i32 %15, 65534
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 (i8*, ...) bitcast (i32 (...)* @log_fatal to i32 (i8*, ...)*)(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
ret void
}

LLVM IR (-O2):
@.str = [28 x i8] c"%s: I don't seem to be root\00"
@__FUNCTION__.rpctest_drop_privileges = [24 x i8] c"rpctest_drop_privileges\00"
@.str.1 = [16 x i8] c"lost real uid 0\00"
@.str.2 = [25 x i8] c"unable to set euid to -2\00"
define void @rpctest_drop_privileges() {
%1 = tail call i32 (...) @getuid()
%2 = icmp eq i32 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 (...) @geteuid()
%5 = icmp eq i32 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %3, %0
%7 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @log_fatal to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @__FUNCTION__.rpctest_drop_privileges, i64 0, i64 0))
br label %8
8: ; preds = %6, %3
%9 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @setreuid to i32 (i32, i32, ...)*)(i32 0, i32 65534)
%10 = tail call i32 (...) @getuid()
%11 = icmp eq i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = tail call i32 (i8*, ...) bitcast (i32 (...)* @log_fatal to i32 (i8*, ...)*)(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = tail call i32 (...) @geteuid()
%16 = icmp eq i32 %15, 65534
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 (i8*, ...) bitcast (i32 (...)* @log_fatal to i32 (i8*, ...)*)(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
ret void
}