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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EX_DATAERR;
int /*<<< orphan*/ EX_NOPERM;
int /*<<< orphan*/ EX_NOUSER;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct passwd *getpwnam(char *);
int getresuid(scalar_t__ *, scalar_t__ *, scalar_t__ *);
int /*<<< orphan*/ perror(char *);
int setresgid(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setresuid(scalar_t__, scalar_t__, scalar_t__);
int /*<<< orphan*/ stderr;
void drop_privileges(char *user) {
  struct passwd *pwent;
  uid_t uid, ruid, euid, suid;
  if ((pwent = getpwnam(user)) == NULL) {
    fprintf(stderr, "%s: unknown user\n", user);
    exit(EX_NOUSER);
  }
  if (setresgid(pwent->pw_gid, pwent->pw_gid, pwent->pw_gid) == -1) {
    perror(user);
    exit(EX_NOPERM);
  }
  uid = pwent->pw_uid;
  if (setresuid(uid, uid, uid) == -1) {
    perror(user);
    exit(EX_NOPERM);
  }
  if (getresuid(&ruid, &euid, &suid) == -1) {
    perror("getresuid");
    exit(EX_DATAERR);
  }
  if ((ruid != uid) || (euid != uid) || (suid != uid)) {
    fprintf(stderr, "got unexpected results from calling setresuid, contact "
                    "the developer\n");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<drop_privileges>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11bc <drop_privileges+0x4c>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edx
call   1040 <setresgid@plt>
cmp    $0xffffffff,%eax
jne    11f6 <drop_privileges+0x86>
mov    -0x8(%rbp),%rdi
call   1080 <perror@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <setresuid@plt>
cmp    $0xffffffff,%eax
jne    1232 <drop_privileges+0xc2>
mov    -0x8(%rbp),%rdi
call   1080 <perror@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1070 <getresuid@plt>
cmp    $0xffffffff,%eax
jne    1266 <drop_privileges+0xf6>
lea    0xdbf(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1290 <drop_privileges+0x120>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1290 <drop_privileges+0x120>
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     12b5 <drop_privileges+0x145>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
lea    0xd7c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drop_privileges>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
call   1050 <getpwnam@plt>
test   %rax,%rax
je     11f7 <drop_privileges+0x87>
mov    %rax,%rbx
mov    0x8(%rax),%edi
mov    %edi,%esi
mov    %edi,%edx
call   1040 <setresgid@plt>
cmp    $0xffffffff,%eax
je     11e1 <drop_privileges+0x71>
mov    (%rbx),%rbx
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %rbx,%rdx
call   1030 <setresuid@plt>
cmp    $0xffffffff,%eax
je     11e1 <drop_privileges+0x71>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    %rsp,%rdx
call   1070 <getresuid@plt>
cmp    $0xffffffff,%eax
je     121f <drop_privileges+0xaf>
cmp    %rbx,0x10(%rsp)
jne    1239 <drop_privileges+0xc9>
cmp    %rbx,0x8(%rsp)
jne    1239 <drop_privileges+0xc9>
cmp    %rbx,(%rsp)
jne    1239 <drop_privileges+0xc9>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    %r14,%rdi
call   1080 <perror@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xdec(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i64, i32 }
@stderr = global i32 0
@.str = [18 x i8] c"%s: unknown user\0A\00"
@EX_NOUSER = global i32 0
@EX_NOPERM = global i32 0
@.str.1 = [10 x i8] c"getresuid\00"
@EX_DATAERR = global i32 0
@.str.2 = [70 x i8] c"got unexpected results from calling setresuid, contact the developer\0A\00"
@EXIT_FAILURE = global i32 0
define void @drop_privileges(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.passwd*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call %struct.passwd* @getpwnam(i8* %8)
store %struct.passwd* %9, %struct.passwd** %3
%10 = icmp eq %struct.passwd* %9, null
br i1 %10, label %11, label %17
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %2
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EX_NOUSER
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %1
%18 = load %struct.passwd*, %struct.passwd** %3
%19 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i32 0, i32 1
%20 = load i32, i32* %19
%21 = load %struct.passwd*, %struct.passwd** %3
%22 = getelementptr inbounds %struct.passwd, %struct.passwd* %21, i32 0, i32 1
%23 = load i32, i32* %22
%24 = load %struct.passwd*, %struct.passwd** %3
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %24, i32 0, i32 1
%26 = load i32, i32* %25
%27 = call i32 @setresgid(i32 %20, i32 %23, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %17
%30 = load i8*, i8** %2
%31 = call i32 @perror(i8* %30)
%32 = load i32, i32* @EX_NOPERM
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %17
%35 = load %struct.passwd*, %struct.passwd** %3
%36 = getelementptr inbounds %struct.passwd, %struct.passwd* %35, i32 0, i32 0
%37 = load i64, i64* %36
store i64 %37, i64* %4
%38 = load i64, i64* %4
%39 = load i64, i64* %4
%40 = load i64, i64* %4
%41 = call i32 @setresuid(i64 %38, i64 %39, i64 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %48
43: ; preds = %34
%44 = load i8*, i8** %2
%45 = call i32 @perror(i8* %44)
%46 = load i32, i32* @EX_NOPERM
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %34
%49 = call i32 @getresuid(i64* %5, i64* %6, i64* %7)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %55
51: ; preds = %48
%52 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%53 = load i32, i32* @EX_DATAERR
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %48
%56 = load i64, i64* %5
%57 = load i64, i64* %4
%58 = icmp ne i64 %56, %57
br i1 %58, label %67, label %59
59: ; preds = %55
%60 = load i64, i64* %6
%61 = load i64, i64* %4
%62 = icmp ne i64 %60, %61
br i1 %62, label %67, label %63
63: ; preds = %59
%64 = load i64, i64* %7
%65 = load i64, i64* %4
%66 = icmp ne i64 %64, %65
br i1 %66, label %67, label %72
67: ; preds = %63, %59, %55
%68 = load i32, i32* @stderr
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.2, i64 0, i64 0))
%70 = load i32, i32* @EXIT_FAILURE
%71 = call i32 @exit(i32 %70)
unreachable
72: ; preds = %63
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i64, i32 }
@stderr = global i32 0
@.str = [18 x i8] c"%s: unknown user\0A\00"
@EX_NOUSER = global i32 0
@EX_NOPERM = global i32 0
@.str.1 = [10 x i8] c"getresuid\00"
@EX_DATAERR = global i32 0
@.str.2 = [70 x i8] c"got unexpected results from calling setresuid, contact the developer\0A\00"
@EXIT_FAILURE = global i32 0
define void @drop_privileges(i8* %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = bitcast i64* %2 to i8*
%6 = bitcast i64* %3 to i8*
%7 = bitcast i64* %4 to i8*
%8 = tail call %struct.passwd* @getpwnam(i8* %0)
%9 = icmp eq %struct.passwd* %8, null
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
%13 = load i32, i32* @EX_NOUSER
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i64 0, i32 1
%17 = load i32, i32* %16
%18 = tail call i32 @setresgid(i32 %17, i32 %17, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = tail call i32 @perror(i8* %0)
%22 = load i32, i32* @EX_NOPERM
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %15
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i64 0, i32 0
%26 = load i64, i64* %25
%27 = tail call i32 @setresuid(i64 %26, i64 %26, i64 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = tail call i32 @perror(i8* %0)
%31 = load i32, i32* @EX_NOPERM
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %24
%34 = call i32 @getresuid(i64* %2, i64* %3, i64* %4)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%38 = load i32, i32* @EX_DATAERR
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %33
%41 = load i64, i64* %2
%42 = icmp eq i64 %41, %26
%43 = load i64, i64* %3
%44 = icmp eq i64 %43, %26
%45 = select i1 %42, i1 %44, i1 false
%46 = load i64, i64* %4
%47 = icmp eq i64 %46, %26
%48 = select i1 %45, i1 %47, i1 false
br i1 %48, label %54, label %49
49: ; preds = %40
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.2, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %40
ret void
}