#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*/ LOG_CRIT;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ _(char *);
struct passwd *getpwnam(char const *);
scalar_t__ initgroups(char const *, int /*<<< orphan*/);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, int /*<<< orphan*/, char const *);
int drop_privileges(const char *username) {
  if (username == NULL)
    return 0;
  struct passwd *pw = getpwnam(username);
  if (pw == NULL) {
    syslog(LOG_ERR, _("Cannot find user \"%s\""), username);
    return -1;
  }
  if (setgid(pw->pw_gid)) {
    syslog(LOG_CRIT, _("Fatal error (%s): %m"), "setgid");
    return -1;
  }
  if (initgroups(username, pw->pw_gid)) {
    syslog(LOG_CRIT, _("Fatal error (%s): %m"), "setgid");
    return -1;
  }
  if (setuid(pw->pw_uid)) {
    syslog(LOG_CRIT, _("Fatal error (%s): %m"), "setuid");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<drop_privileges>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1183 <drop_privileges+0x23>
movl   $0x0,-0x4(%rbp)
jmp    12b8 <drop_privileges+0x158>
mov    -0x10(%rbp),%rdi
call   1030 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cd <drop_privileges+0x6d>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    0xe52(%rip),%rdi
call   1050 <_@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
call   1040 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <drop_privileges+0x158>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1060 <setgid@plt>
cmp    $0x0,%rax
je     1218 <drop_privileges+0xb8>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    0xe20(%rip),%rdi
call   1050 <_@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%esi
lea    0xe24(%rip),%rdx
call   1040 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <drop_privileges+0x158>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
call   1080 <initgroups@plt>
cmp    $0x0,%rax
je     1267 <drop_privileges+0x107>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    0xdd1(%rip),%rdi
call   1050 <_@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%esi
lea    0xdd5(%rip),%rdx
call   1040 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <drop_privileges+0x158>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1070 <setuid@plt>
cmp    $0x0,%rax
je     12b1 <drop_privileges+0x151>
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
lea    0xd87(%rip),%rdi
call   1050 <_@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%esi
lea    0xd92(%rip),%rdx
call   1040 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <drop_privileges+0x158>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drop_privileges>:
push   %rbp
push   %r14
push   %rbx
xor    %r14d,%r14d
test   %rdi,%rdi
je     11e2 <drop_privileges+0x82>
mov    %rdi,%rbx
call   1030 <getpwnam@plt>
test   %rax,%rax
je     11bb <drop_privileges+0x5b>
mov    %rax,%rbp
mov    0x4(%rax),%edi
call   1060 <setgid@plt>
test   %rax,%rax
jne    1199 <drop_privileges+0x39>
mov    0x4(%rbp),%esi
mov    %rbx,%rdi
call   1080 <initgroups@plt>
test   %rax,%rax
je     11ea <drop_privileges+0x8a>
mov    0x2e48(%rip),%rax
mov    (%rax),%ebx
lea    0xe6d(%rip),%rdi
call   1050 <_@plt>
lea    0xe76(%rip),%rdx
mov    %ebx,%edi
mov    %eax,%esi
jmp    11d7 <drop_privileges+0x77>
mov    0x2e0e(%rip),%rax
mov    (%rax),%ebp
lea    0xe35(%rip),%rdi
call   1050 <_@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    %rbx,%rdx
call   1040 <syslog@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x0(%rbp),%edi
call   1070 <setuid@plt>
test   %rax,%rax
je     11e2 <drop_privileges+0x82>
mov    0x2dea(%rip),%rax
mov    (%rax),%ebx
lea    0xe0f(%rip),%rdi
call   1050 <_@plt>
lea    0xe1f(%rip),%rdx
jmp    11b5 <drop_privileges+0x55>

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@LOG_ERR = global i32 0
@.str = [22 x i8] c"Cannot find user \22%s\22\00"
@LOG_CRIT = global i32 0
@.str.1 = [21 x i8] c"Fatal error (%s): %m\00"
@.str.2 = [7 x i8] c"setgid\00"
@.str.3 = [7 x i8] c"setuid\00"
define i32 @drop_privileges(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.passwd*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %8
7: ; preds = %1
store i32 0, i32* %2
br label %50
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = call %struct.passwd* @getpwnam(i8* %9)
store %struct.passwd* %10, %struct.passwd** %4
%11 = load %struct.passwd*, %struct.passwd** %4
%12 = icmp eq %struct.passwd* %11, null
br i1 %12, label %13, label %18
13: ; preds = %8
%14 = load i32, i32* @LOG_ERR
%15 = call i32 @_(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%16 = load i8*, i8** %3
%17 = call i32 @syslog(i32 %14, i32 %15, i8* %16)
store i32 -1, i32* %2
br label %50
18: ; preds = %8
%19 = load %struct.passwd*, %struct.passwd** %4
%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 %28
24: ; preds = %18
%25 = load i32, i32* @LOG_CRIT
%26 = call i32 @_(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @syslog(i32 %25, i32 %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %50
28: ; preds = %18
%29 = load i8*, i8** %3
%30 = load %struct.passwd*, %struct.passwd** %4
%31 = getelementptr inbounds %struct.passwd, %struct.passwd* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = call i64 @initgroups(i8* %29, i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %28
%36 = load i32, i32* @LOG_CRIT
%37 = call i32 @_(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @syslog(i32 %36, i32 %37, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %50
39: ; preds = %28
%40 = load %struct.passwd*, %struct.passwd** %4
%41 = getelementptr inbounds %struct.passwd, %struct.passwd* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i64 @setuid(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %39
%46 = load i32, i32* @LOG_CRIT
%47 = call i32 @_(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%48 = call i32 @syslog(i32 %46, i32 %47, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %50
49: ; preds = %39
store i32 0, i32* %2
br label %50
50: ; preds = %49, %45, %35, %24, %13, %7
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@LOG_ERR = global i32 0
@.str = [22 x i8] c"Cannot find user \22%s\22\00"
@LOG_CRIT = global i32 0
@.str.1 = [21 x i8] c"Fatal error (%s): %m\00"
@.str.2 = [7 x i8] c"setgid\00"
@.str.3 = [7 x i8] c"setuid\00"
define i32 @drop_privileges(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %36, label %3
3: ; preds = %1
%4 = tail call %struct.passwd* @getpwnam(i8* %0)
%5 = icmp eq %struct.passwd* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i32, i32* @LOG_ERR
%8 = tail call i32 @_(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @syslog(i32 %7, i32 %8, i8* %0)
br label %36
10: ; preds = %3
%11 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 1
%12 = load i32, i32* %11
%13 = tail call i64 @setgid(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %10
%16 = load i32, i32* @LOG_CRIT
%17 = tail call i32 @_(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @syslog(i32 %16, i32 %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %36
19: ; preds = %10
%20 = load i32, i32* %11
%21 = tail call i64 @initgroups(i8* %0, i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i32, i32* @LOG_CRIT
%25 = tail call i32 @_(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%26 = tail call i32 @syslog(i32 %24, i32 %25, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %36
27: ; preds = %19
%28 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 0
%29 = load i32, i32* %28
%30 = tail call i64 @setuid(i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %27
%33 = load i32, i32* @LOG_CRIT
%34 = tail call i32 @_(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%35 = tail call i32 @syslog(i32 %33, i32 %34, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %6, %15, %23, %32, %27, %1
%37 = phi i32 [ 0, %1 ], [ -1, %6 ], [ -1, %15 ], [ -1, %23 ], [ -1, %32 ], [ 0, %27 ]
ret i32 %37
}