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

/* Variables and functions */
int /*<<< orphan*/ getgid();
scalar_t__ getuid();
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setgroups(int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ setuid(scalar_t__);
int dropPrivileges(uid_t uid, gid_t gid) {
  if (uid == 0) {
    uid = getuid();
    gid = getgid();
  }
  if (setgroups(0, NULL) < 0) {
    return -1;
  }
  if (setgid(gid) < 0) {
    return -1;
  }
  if (setuid(uid) < 0) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<dropPrivileges>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117f <dropPrivileges+0x2f>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getgid@plt>
mov    %eax,-0x14(%rbp)
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <setgroups@plt>
cmp    $0x0,%rax
jge    11a0 <dropPrivileges+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <dropPrivileges+0x94>
mov    -0x14(%rbp),%edi
call   1060 <setgid@plt>
cmp    $0x0,%rax
jge    11be <dropPrivileges+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <dropPrivileges+0x94>
mov    -0x10(%rbp),%rdi
call   1070 <setuid@plt>
cmp    $0x0,%rax
jge    11dd <dropPrivileges+0x8d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <dropPrivileges+0x94>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dropPrivileges>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
test   %rdi,%rdi
jne    1173 <dropPrivileges+0x23>
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1050 <getgid@plt>
mov    %eax,%r14d
xor    %edi,%edi
xor    %esi,%esi
call   1040 <setgroups@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     11a2 <dropPrivileges+0x52>
mov    %r14d,%edi
call   1060 <setgid@plt>
test   %rax,%rax
js     11a2 <dropPrivileges+0x52>
mov    %rbx,%rdi
call   1070 <setuid@plt>
mov    %rax,%rbp
sar    $0x3f,%rbp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @dropPrivileges(i64 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
store i64 %0, i64* %4
store i32 %1, i32* %5
%6 = load i64, i64* %4
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = call i64 (...) @getuid()
store i64 %9, i64* %4
%10 = call i32 (...) @getgid()
store i32 %10, i32* %5
br label %11
11: ; preds = %8, %2
%12 = call i64 @setgroups(i32 0, i32* null)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %11
store i32 -1, i32* %3
br label %26
15: ; preds = %11
%16 = load i32, i32* %5
%17 = call i64 @setgid(i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 -1, i32* %3
br label %26
20: ; preds = %15
%21 = load i64, i64* %4
%22 = call i64 @setuid(i64 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20
store i32 -1, i32* %3
br label %26
25: ; preds = %20
store i32 0, i32* %3
br label %26
26: ; preds = %25, %24, %19, %14
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
define i32 @dropPrivileges(i64 %0, i32 %1) {
%3 = icmp eq i64 %0, 0
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i64 (...) @getuid()
%6 = tail call i32 (...) @getgid()
br label %7
7: ; preds = %4, %2
%8 = phi i64 [ %5, %4 ], [ %0, %2 ]
%9 = phi i32 [ %6, %4 ], [ %1, %2 ]
%10 = tail call i64 @setgroups(i32 0, i32* null)
%11 = icmp slt i64 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %7
%13 = tail call i64 @setgid(i32 %9)
%14 = icmp slt i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = tail call i64 @setuid(i64 %8)
%17 = ashr i64 %16, 63
%18 = trunc i64 %17 to i32
br label %19
19: ; preds = %15, %12, %7
%20 = phi i32 [ -1, %7 ], [ -1, %12 ], [ %18, %15 ]
ret i32 %20
}