#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 *, ...);
scalar_t__ getpwnam(char *);
char *gettext(char *);
scalar_t__ getuid();
int /*<<< orphan*/ perror(char *);
scalar_t__ setgid(int /*<<< orphan*/);
int setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void drop_privileges(char *username) {
  struct passwd *user = (struct passwd *)getpwnam(username);
  if (user == NULL) {
    fprintf(stderr,
            gettext("Failed dropping privileges. The user %s is not a valid "
                    "username on local system.\n"),
            username);
    exit(1);
  }
  if (getuid() == 0) {
    if (setgid(user->pw_gid) != 0) {
      perror("setgid: Error dropping group privileges");
      exit(1);
    }
    if (setuid(user->pw_uid) != 0) {
      perror("setuid: Error dropping user privileges");
      exit(1);
    }
    if (setuid(0) != -1) {
      perror("Failed to drop privileges");
      exit(1);
    }
  } else {
    fprintf(
        stderr,
        gettext(
            "Failed dropping privileges. Not running as privileged user.\n"));
    exit(1);
  }
}


x86 assembly (clang, -O0):
<drop_privileges>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d7 <drop_privileges+0x57>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    0xe49(%rip),%rdi
call   1040 <gettext@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rsi
mov    -0x8(%rbp),%rdx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%rax
jne    1269 <drop_privileges+0xe9>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1070 <setgid@plt>
cmp    $0x0,%rax
je     1214 <drop_privileges+0x94>
lea    0xe4d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   10a0 <setuid@plt>
cmp    $0x0,%eax
je     123e <drop_privileges+0xbe>
lea    0xe4b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   10a0 <setuid@plt>
cmp    $0xffffffff,%eax
je     1264 <drop_privileges+0xe4>
lea    0xe4c(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1298 <drop_privileges+0x118>
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    0xe3f(%rip),%rdi
call   1040 <gettext@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drop_privileges>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1050 <getpwnam@plt>
test   %rax,%rax
je     11cc <drop_privileges+0x4c>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getuid@plt>
test   %rax,%rax
jne    11fa <drop_privileges+0x7a>
mov    0x4(%rbx),%edi
call   1070 <setgid@plt>
test   %rax,%rax
jne    1225 <drop_privileges+0xa5>
mov    (%rbx),%edi
call   10a0 <setuid@plt>
test   %eax,%eax
jne    122e <drop_privileges+0xae>
xor    %edi,%edi
call   10a0 <setuid@plt>
cmp    $0xffffffff,%eax
jne    1237 <drop_privileges+0xb7>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e0d(%rip),%rax
mov    (%rax),%ebx
lea    0xe24(%rip),%rdi
call   1040 <gettext@plt>
mov    %ebx,%edi
mov    %rax,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%ebx
lea    0xeb1(%rip),%rdi
call   1040 <gettext@plt>
mov    %ebx,%edi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe26(%rip),%rdi
jmp    123e <drop_privileges+0xbe>
lea    0xe45(%rip),%rdi
jmp    123e <drop_privileges+0xbe>
lea    0xe63(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@stderr = global i32 0
@.str = [82 x i8] c"Failed dropping privileges. The user %s is not a valid username on local system.\0A\00"
@.str.1 = [40 x i8] c"setgid: Error dropping group privileges\00"
@.str.2 = [39 x i8] c"setuid: Error dropping user privileges\00"
@.str.3 = [26 x i8] c"Failed to drop privileges\00"
@.str.4 = [61 x i8] c"Failed dropping privileges. Not running as privileged user.\0A\00"
define void @drop_privileges(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.passwd*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i64 @getpwnam(i8* %4)
%6 = inttoptr i64 %5 to %struct.passwd*
store %struct.passwd* %6, %struct.passwd** %3
%7 = load %struct.passwd*, %struct.passwd** %3
%8 = icmp eq %struct.passwd* %7, null
br i1 %8, label %9, label %15
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i8* @gettext(i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str, i64 0, i64 0))
%12 = load i8*, i8** %2
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* %11, i8* %12)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = call i64 (...) @getuid()
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %43
18: ; preds = %15
%19 = load %struct.passwd*, %struct.passwd** %3
%20 = getelementptr inbounds %struct.passwd, %struct.passwd* %19, i32 0, i32 1
%21 = load i32, i32* %20
%22 = call i64 @setgid(i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %18
%28 = load %struct.passwd*, %struct.passwd** %3
%29 = getelementptr inbounds %struct.passwd, %struct.passwd* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @setuid(i32 %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27
%37 = call i32 @setuid(i32 0)
%38 = icmp ne i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %36
br label %48
43: ; preds = %15
%44 = load i32, i32* @stderr
%45 = call i8* @gettext(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.4, i64 0, i64 0))
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %42
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@stderr = global i32 0
@.str = [82 x i8] c"Failed dropping privileges. The user %s is not a valid username on local system.\0A\00"
@.str.1 = [40 x i8] c"setgid: Error dropping group privileges\00"
@.str.2 = [39 x i8] c"setuid: Error dropping user privileges\00"
@.str.3 = [26 x i8] c"Failed to drop privileges\00"
@.str.4 = [61 x i8] c"Failed dropping privileges. Not running as privileged user.\0A\00"
define void @drop_privileges(i8* %0) {
%2 = tail call i64 @getpwnam(i8* %0)
%3 = inttoptr i64 %2 to %struct.passwd*
%4 = icmp eq i64 %2, 0
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i8* @gettext(i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* %7, i8* %0)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = tail call i64 (...) @getuid()
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %35
13: ; preds = %10
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 1
%15 = load i32, i32* %14
%16 = tail call i64 @setgid(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %13
%22 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = tail call i32 @setuid(i32 %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %21
%27 = tail call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = tail call i32 @setuid(i32 0)
%31 = icmp eq i32 %30, -1
br i1 %31, label %40, label %32
32: ; preds = %29
%33 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %10
%36 = load i32, i32* @stderr
%37 = tail call i8* @gettext(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.4, i64 0, i64 0))
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* %37)
%39 = tail call i32 @exit(i32 1)
unreachable
40: ; preds = %29
ret void
}