#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;
};

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
struct passwd *getpwnam(char const *);
int setregid(int /*<<< orphan*/, int /*<<< orphan*/);
int setreuid(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int set_proper_privs(const char *user) {
  struct passwd *pw = getpwnam(user);
  if (pw == NULL) {
    fprintf(stderr, "set_proper_privs: could not find proper user \"%s\"!\n",
            user);
    exit(-1);
  }
  if (setregid(pw->pw_gid, pw->pw_gid) == -1 ||
      setreuid(pw->pw_uid, pw->pw_uid) == -1) {
    fprintf(stderr, "attempt to drop privileges to \"%s\" failed", user);
    return -1;
  }
  fprintf(stdout, "set_proper_privs: privileges lowered to that of user %s",
          user);
  return 0;
}


x86 assembly (clang, -O0):
<set_proper_privs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1197 <set_proper_privs+0x47>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe78(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
call   1060 <setregid@plt>
cmp    $0xffffffff,%eax
je     11cd <set_proper_privs+0x7d>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1050 <setreuid@plt>
cmp    $0xffffffff,%eax
jne    11f2 <set_proper_privs+0xa2>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe53(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1212 <set_proper_privs+0xc2>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe58(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_proper_privs>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
call   1030 <getpwnam@plt>
test   %rax,%rax
je     11c4 <set_proper_privs+0x74>
mov    %rax,%rbp
mov    0x4(%rax),%edi
mov    %edi,%esi
call   1060 <setregid@plt>
mov    $0xffffffff,%r15d
lea    0xeb5(%rip),%r12
mov    0x2e5a(%rip),%rbx
cmp    $0xffffffff,%eax
je     11ab <set_proper_privs+0x5b>
mov    0x0(%rbp),%edi
mov    %edi,%esi
call   1050 <setreuid@plt>
cmp    $0xffffffff,%eax
je     11ab <set_proper_privs+0x5b>
xor    %r15d,%r15d
lea    0xeba(%rip),%r12
mov    0x2e2d(%rip),%rbx
mov    (%rbx),%edi
mov    %r12,%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@stderr = global i32 0
@.str = [52 x i8] c"set_proper_privs: could not find proper user \22%s\22!\0A\00"
@.str.1 = [42 x i8] c"attempt to drop privileges to \22%s\22 failed\00"
@stdout = global i32 0
@.str.2 = [56 x i8] c"set_proper_privs: privileges lowered to that of user %s\00"
define i32 @set_proper_privs(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.passwd*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call %struct.passwd* @getpwnam(i8* %5)
store %struct.passwd* %6, %struct.passwd** %4
%7 = load %struct.passwd*, %struct.passwd** %4
%8 = icmp eq %struct.passwd* %7, null
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %3
%12 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %1
%15 = load %struct.passwd*, %struct.passwd** %4
%16 = getelementptr inbounds %struct.passwd, %struct.passwd* %15, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load %struct.passwd*, %struct.passwd** %4
%19 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i32 0, i32 1
%20 = load i32, i32* %19
%21 = call i32 @setregid(i32 %17, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %32, label %23
23: ; preds = %14
%24 = load %struct.passwd*, %struct.passwd** %4
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = load %struct.passwd*, %struct.passwd** %4
%28 = getelementptr inbounds %struct.passwd, %struct.passwd* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @setreuid(i32 %26, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %23, %14
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %3
%35 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %34)
store i32 -1, i32* %2
br label %40
36: ; preds = %23
%37 = load i32, i32* @stdout
%38 = load i8*, i8** %3
%39 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i8* %38)
store i32 0, i32* %2
br label %40
40: ; preds = %36, %32
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@stderr = global i32 0
@.str = [52 x i8] c"set_proper_privs: could not find proper user \22%s\22!\0A\00"
@.str.1 = [42 x i8] c"attempt to drop privileges to \22%s\22 failed\00"
@stdout = global i32 0
@.str.2 = [56 x i8] c"set_proper_privs: privileges lowered to that of user %s\00"
define i32 @set_proper_privs(i8* %0) {
%2 = tail call %struct.passwd* @getpwnam(i8* %0)
%3 = icmp eq %struct.passwd* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = tail call i32 @exit(i32 -1)
unreachable
8: ; preds = %1
%9 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 1
%10 = load i32, i32* %9
%11 = tail call i32 @setregid(i32 %10, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %19, label %13
13: ; preds = %8
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i32 @setreuid(i32 %15, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %19, label %18
18: ; preds = %13
br label %19
19: ; preds = %8, %13, %18
%20 = phi i32* [ @stdout, %18 ], [ @stderr, %13 ], [ @stderr, %8 ]
%21 = phi i8* [ getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), %18 ], [ getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), %8 ]
%22 = phi i32 [ 0, %18 ], [ -1, %13 ], [ -1, %8 ]
%23 = load i32, i32* %20
%24 = tail call i32 @fprintf(i32 %23, i8* %21, i8* %0)
ret i32 %22
}