#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 */
struct passwd {
  int /*<<< orphan*/ pw_uid;
  int /*<<< orphan*/ pw_gid;
  int /*<<< orphan*/ pw_name;
};

/* Variables and functions */
int /*<<< orphan*/ EPERM;
int /*<<< orphan*/ errno;
scalar_t__ initgroups(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
int drop_privileges(const struct passwd *user) {
  if (setgid(user->pw_gid))
    return 1;
  if (initgroups(user->pw_name, user->pw_gid))
    return 1;
  if (setuid(user->pw_uid))
    return 1;
  if (setuid(0) == 0) {
    errno = EPERM;
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<drop_privileges>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1030 <setgid@plt>
cmp    $0x0,%rax
je     115e <drop_privileges+0x2e>
movl   $0x1,-0x4(%rbp)
jmp    11de <drop_privileges+0xae>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
call   1050 <initgroups@plt>
cmp    $0x0,%rax
je     1187 <drop_privileges+0x57>
movl   $0x1,-0x4(%rbp)
jmp    11de <drop_privileges+0xae>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <setuid@plt>
cmp    $0x0,%rax
je     11a8 <drop_privileges+0x78>
movl   $0x1,-0x4(%rbp)
jmp    11de <drop_privileges+0xae>
xor    %edi,%edi
call   1040 <setuid@plt>
cmp    $0x0,%rax
jne    11d7 <drop_privileges+0xa7>
mov    0x2e10(%rip),%rax
mov    (%rax),%ecx
mov    0x2e17(%rip),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    11de <drop_privileges+0xae>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drop_privileges>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x4(%rdi),%edi
call   1030 <setgid@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    1189 <drop_privileges+0x59>
mov    0x4(%rbx),%esi
mov    0x8(%rbx),%edi
call   1050 <initgroups@plt>
test   %rax,%rax
jne    1189 <drop_privileges+0x59>
mov    (%rbx),%edi
call   1040 <setuid@plt>
test   %rax,%rax
jne    1189 <drop_privileges+0x59>
xor    %ebp,%ebp
xor    %edi,%edi
call   1040 <setuid@plt>
test   %rax,%rax
jne    1189 <drop_privileges+0x59>
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    0x2e5e(%rip),%rcx
mov    %eax,(%rcx)
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i32, i32, i32 }
@EPERM = global i32 0
@errno = global i32 0
define i32 @drop_privileges(%struct.passwd* %0) {
%2 = alloca i32
%3 = alloca %struct.passwd*
store %struct.passwd* %0, %struct.passwd** %3
%4 = load %struct.passwd*, %struct.passwd** %3
%5 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i32 0, i32 1
%6 = load i32, i32* %5
%7 = call i64 @setgid(i32 %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 1, i32* %2
br label %33
10: ; preds = %1
%11 = load %struct.passwd*, %struct.passwd** %3
%12 = getelementptr inbounds %struct.passwd, %struct.passwd* %11, i32 0, i32 2
%13 = load i32, i32* %12
%14 = load %struct.passwd*, %struct.passwd** %3
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i32 0, i32 1
%16 = load i32, i32* %15
%17 = call i64 @initgroups(i32 %13, i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %10
store i32 1, i32* %2
br label %33
20: ; preds = %10
%21 = load %struct.passwd*, %struct.passwd** %3
%22 = getelementptr inbounds %struct.passwd, %struct.passwd* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i64 @setuid(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 1, i32* %2
br label %33
27: ; preds = %20
%28 = call i64 @setuid(i32 0)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = load i32, i32* @EPERM
store i32 %31, i32* @errno
store i32 1, i32* %2
br label %33
32: ; preds = %27
store i32 0, i32* %2
br label %33
33: ; preds = %32, %30, %26, %19, %9
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32, i32 }
@EPERM = global i32 0
@errno = global i32 0
define i32 @drop_privileges(%struct.passwd* nocapture %0) {
%2 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 1
%3 = load i32, i32* %2
%4 = tail call i64 @setgid(i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %22
6: ; preds = %1
%7 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 2
%8 = load i32, i32* %7
%9 = load i32, i32* %2
%10 = tail call i64 @initgroups(i32 %8, i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %6
%13 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i64 @setuid(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = tail call i64 @setuid(i32 0)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = load i32, i32* @EPERM
store i32 %21, i32* @errno
br label %22
22: ; preds = %17, %12, %6, %1, %20
%23 = phi i32 [ 1, %20 ], [ 1, %1 ], [ 1, %6 ], [ 1, %12 ], [ 0, %17 ]
ret i32 %23
}