#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 {
  scalar_t__ pw_gid;
  scalar_t__ pw_uid;
};

/* Variables and functions */
scalar_t__ getegid();
scalar_t__ geteuid();
scalar_t__ getgid();
int getuid();
int /*<<< orphan*/ setgid(scalar_t__);
int /*<<< orphan*/ setuid(scalar_t__);
unsigned drop_privs(struct passwd *pw) {
  setgid(pw->pw_gid);
  setuid(pw->pw_uid);
  if (getegid() != pw->pw_gid || getgid() != pw->pw_uid)
    return 0;
  if (geteuid() != pw->pw_uid || getuid() != pw->pw_uid)
    return 0;
  return getuid();
}


x86 assembly (clang, -O0):
<drop_privs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <setgid@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <setuid@plt>
mov    $0x0,%al
call   1060 <getegid@plt>
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
jne    11ae <drop_privs+0x4e>
mov    $0x0,%al
call   1050 <getgid@plt>
mov    -0x10(%rbp),%rcx
cmp    0x8(%rcx),%rax
je     11ba <drop_privs+0x5a>
movl   $0x0,-0x4(%rbp)
jmp    11fc <drop_privs+0x9c>
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    -0x10(%rbp),%rcx
cmp    0x8(%rcx),%rax
jne    11e6 <drop_privs+0x86>
mov    $0x0,%al
call   1030 <getuid@plt>
cltq
mov    -0x10(%rbp),%rcx
cmp    0x8(%rcx),%rax
je     11f2 <drop_privs+0x92>
movl   $0x0,-0x4(%rbp)
jmp    11fc <drop_privs+0x9c>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drop_privs>:
push   %rbx
mov    %rdi,%rbx
mov    (%rdi),%rdi
call   1070 <setgid@plt>
mov    0x8(%rbx),%rdi
call   1080 <setuid@plt>
xor    %eax,%eax
call   1060 <getegid@plt>
cmp    (%rbx),%rax
jne    11b2 <drop_privs+0x52>
xor    %eax,%eax
call   1050 <getgid@plt>
cmp    0x8(%rbx),%rax
jne    11b2 <drop_privs+0x52>
xor    %eax,%eax
call   1040 <geteuid@plt>
cmp    0x8(%rbx),%rax
jne    11b2 <drop_privs+0x52>
xor    %eax,%eax
call   1030 <getuid@plt>
cltq
cmp    %rax,0x8(%rbx)
jne    11b2 <drop_privs+0x52>
xor    %eax,%eax
pop    %rbx
jmp    1030 <getuid@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.passwd = type { i64, i64 }
define i32 @drop_privs(%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 0
%6 = load i64, i64* %5
%7 = call i32 @setgid(i64 %6)
%8 = load %struct.passwd*, %struct.passwd** %3
%9 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i32 0, i32 1
%10 = load i64, i64* %9
%11 = call i32 @setuid(i64 %10)
%12 = call i64 (...) @getegid()
%13 = load %struct.passwd*, %struct.passwd** %3
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i32 0, i32 0
%15 = load i64, i64* %14
%16 = icmp ne i64 %12, %15
br i1 %16, label %23, label %17
17: ; preds = %1
%18 = call i64 (...) @getgid()
%19 = load %struct.passwd*, %struct.passwd** %3
%20 = getelementptr inbounds %struct.passwd, %struct.passwd* %19, i32 0, i32 1
%21 = load i64, i64* %20
%22 = icmp ne i64 %18, %21
br i1 %22, label %23, label %24
23: ; preds = %17, %1
store i32 0, i32* %2
br label %40
24: ; preds = %17
%25 = call i64 (...) @geteuid()
%26 = load %struct.passwd*, %struct.passwd** %3
%27 = getelementptr inbounds %struct.passwd, %struct.passwd* %26, i32 0, i32 1
%28 = load i64, i64* %27
%29 = icmp ne i64 %25, %28
br i1 %29, label %37, label %30
30: ; preds = %24
%31 = call i32 (...) @getuid()
%32 = sext i32 %31 to i64
%33 = load %struct.passwd*, %struct.passwd** %3
%34 = getelementptr inbounds %struct.passwd, %struct.passwd* %33, i32 0, i32 1
%35 = load i64, i64* %34
%36 = icmp ne i64 %32, %35
br i1 %36, label %37, label %38
37: ; preds = %30, %24
store i32 0, i32* %2
br label %40
38: ; preds = %30
%39 = call i32 (...) @getuid()
store i32 %39, i32* %2
br label %40
40: ; preds = %38, %37, %23
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
%struct.passwd = type { i64, i64 }
define i32 @drop_privs(%struct.passwd* nocapture %0) {
%2 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 0
%3 = load i64, i64* %2
%4 = tail call i32 @setgid(i64 %3)
%5 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 1
%6 = load i64, i64* %5
%7 = tail call i32 @setuid(i64 %6)
%8 = tail call i64 (...) @getegid()
%9 = load i64, i64* %2
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %26
11: ; preds = %1
%12 = tail call i64 (...) @getgid()
%13 = load i64, i64* %5
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %26
15: ; preds = %11
%16 = tail call i64 (...) @geteuid()
%17 = load i64, i64* %5
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %26
19: ; preds = %15
%20 = tail call i32 (...) @getuid()
%21 = sext i32 %20 to i64
%22 = load i64, i64* %5
%23 = icmp eq i64 %22, %21
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = tail call i32 (...) @getuid()
br label %26
26: ; preds = %15, %19, %1, %11, %24
%27 = phi i32 [ %25, %24 ], [ 0, %11 ], [ 0, %1 ], [ 0, %19 ], [ 0, %15 ]
ret i32 %27
}