#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 {
  char *pw_name;
  char *pw_passwd;
  int pw_uid;
  int pw_gid;
  char *pw_gecos;
  char *pw_dir;
  char *pw_shell;
};

/* Variables and functions */
int chdir(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct passwd *getpwnam(char *);
int initgroups(char *, int);
int setgid(int);
int setuid(int);
int /*<<< orphan*/ stderr;
void drop_priv() {
  struct passwd *pw;
  int rv = -1;
  pw = getpwnam("username");
  if (!pw) {
    fprintf(stderr, "[ERROR] getpwnam() failed\n");
    exit(-1);
  }
  if (0) {
    fprintf(stderr, "[Verbose] pw_name: %s\n", pw->pw_name);
    fprintf(stderr, "[Verbose] pw_passwd: %s\n", pw->pw_passwd);
    fprintf(stderr, "[Verbose] pw_uid: %d\n", pw->pw_uid);
    fprintf(stderr, "[Verbose] pw_gid: %d\n", pw->pw_gid);
    fprintf(stderr, "[Verbose] pw_gecos: %s\n", pw->pw_gecos);
    fprintf(stderr, "[Verbose] pw_dir: %s\n", pw->pw_dir);
    fprintf(stderr, "[Verbose] pw_shell: %s\n", pw->pw_shell);
    fprintf(stderr, "getpwnam: %p\n", getpwnam);
  }
  rv = initgroups("username", pw->pw_gid);
  if (0 != rv) {
    fprintf(stderr, "[ERROR] initgroups() failed\n");
    exit(-1);
  }
  rv = setgid(pw->pw_gid);
  if (0 != rv) {
    fprintf(stderr, "[ERROR] setgid() failed\n");
    exit(-1);
  }
  rv = setuid(pw->pw_uid);
  if (0 != rv) {
    fprintf(stderr, "[ERROR] setuid() failed\n");
    exit(-1);
  }
  rv = chdir(pw->pw_dir);
  if (0 != rv) {
    fprintf(stderr, "[ERROR] chdir() failed\n");
    exit(-1);
  }
}


x86 assembly (clang, -O0):
<drop_priv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0xffffffff,-0xc(%rbp)
lea    0xe7a(%rip),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11bb <drop_priv+0x4b>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rax
mov    0x14(%rax),%esi
lea    0xe37(%rip),%rdi
call   1090 <initgroups@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmp    -0xc(%rbp),%eax
je     11fd <drop_priv+0x8d>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rax
mov    0x14(%rax),%edi
call   1060 <setgid@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmp    -0xc(%rbp),%eax
je     1238 <drop_priv+0xc8>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%edi
call   1080 <setuid@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmp    -0xc(%rbp),%eax
je     1273 <drop_priv+0x103>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rax
mov    0x20(%rax),%rdi
call   1030 <chdir@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmp    -0xc(%rbp),%eax
je     12af <drop_priv+0x13f>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drop_priv>:
push   %rbx
lea    0xe88(%rip),%rdi
call   1040 <getpwnam@plt>
test   %rax,%rax
je     11bf <drop_priv+0x4f>
mov    %rax,%rbx
mov    0x14(%rax),%esi
lea    0xe71(%rip),%rdi
call   1090 <initgroups@plt>
test   %eax,%eax
jne    11d1 <drop_priv+0x61>
mov    0x14(%rbx),%edi
call   1060 <setgid@plt>
test   %eax,%eax
jne    11e3 <drop_priv+0x73>
mov    0x10(%rbx),%edi
call   1080 <setuid@plt>
test   %eax,%eax
jne    11f5 <drop_priv+0x85>
mov    0x20(%rbx),%rdi
call   1030 <chdir@plt>
test   %eax,%eax
jne    1207 <drop_priv+0x97>
pop    %rbx
ret
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
jmp    1217 <drop_priv+0xa7>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
jmp    1217 <drop_priv+0xa7>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
jmp    1217 <drop_priv+0xa7>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
jmp    1217 <drop_priv+0xa7>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i8*, i8*, i32, i32, i8*, i8*, i8* }
@.str = [9 x i8] c"username\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"[ERROR] getpwnam() failed\0A\00"
@.str.2 = [29 x i8] c"[ERROR] initgroups() failed\0A\00"
@.str.3 = [25 x i8] c"[ERROR] setgid() failed\0A\00"
@.str.4 = [25 x i8] c"[ERROR] setuid() failed\0A\00"
@.str.5 = [24 x i8] c"[ERROR] chdir() failed\0A\00"
define void @drop_priv() {
%1 = alloca %struct.passwd*
%2 = alloca i32
store i32 -1, i32* %2
%3 = call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store %struct.passwd* %3, %struct.passwd** %1
%4 = load %struct.passwd*, %struct.passwd** %1
%5 = icmp ne %struct.passwd* %4, null
br i1 %5, label %10, label %6
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @exit(i32 -1)
unreachable
10: ; preds = %0
%11 = load %struct.passwd*, %struct.passwd** %1
%12 = getelementptr inbounds %struct.passwd, %struct.passwd* %11, i32 0, i32 3
%13 = load i32, i32* %12
%14 = call i32 @initgroups(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = icmp ne i32 0, %15
br i1 %16, label %17, label %21
17: ; preds = %10
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %10
%22 = load %struct.passwd*, %struct.passwd** %1
%23 = getelementptr inbounds %struct.passwd, %struct.passwd* %22, i32 0, i32 3
%24 = load i32, i32* %23
%25 = call i32 @setgid(i32 %24)
store i32 %25, i32* %2
%26 = load i32, i32* %2
%27 = icmp ne i32 0, %26
br i1 %27, label %28, label %32
28: ; preds = %21
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %21
%33 = load %struct.passwd*, %struct.passwd** %1
%34 = getelementptr inbounds %struct.passwd, %struct.passwd* %33, i32 0, i32 2
%35 = load i32, i32* %34
%36 = call i32 @setuid(i32 %35)
store i32 %36, i32* %2
%37 = load i32, i32* %2
%38 = icmp ne i32 0, %37
br i1 %38, label %39, label %43
39: ; preds = %32
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%42 = call i32 @exit(i32 -1)
unreachable
43: ; preds = %32
%44 = load %struct.passwd*, %struct.passwd** %1
%45 = getelementptr inbounds %struct.passwd, %struct.passwd* %44, i32 0, i32 5
%46 = load i8*, i8** %45
%47 = call i32 @chdir(i8* %46)
store i32 %47, i32* %2
%48 = load i32, i32* %2
%49 = icmp ne i32 0, %48
br i1 %49, label %50, label %54
50: ; preds = %43
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%53 = call i32 @exit(i32 -1)
unreachable
54: ; preds = %43
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i8*, i8*, i32, i32, i8*, i8*, i8* }
@.str = [9 x i8] c"username\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"[ERROR] getpwnam() failed\0A\00"
@.str.2 = [29 x i8] c"[ERROR] initgroups() failed\0A\00"
@.str.3 = [25 x i8] c"[ERROR] setgid() failed\0A\00"
@.str.4 = [25 x i8] c"[ERROR] setuid() failed\0A\00"
@.str.5 = [24 x i8] c"[ERROR] chdir() failed\0A\00"
define void @drop_priv() {
%1 = tail call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq %struct.passwd* %1, null
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 (i32, i8*, ...) @fprintf(i32 %4, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %0
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %1, i64 0, i32 3
%9 = load i32, i32* %8
%10 = tail call i32 @initgroups(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %7
%17 = load i32, i32* %8
%18 = tail call i32 @setgid(i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%23 = tail call i32 @exit(i32 -1)
unreachable
24: ; preds = %16
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %1, i64 0, i32 2
%26 = load i32, i32* %25
%27 = tail call i32 @setuid(i32 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%32 = tail call i32 @exit(i32 -1)
unreachable
33: ; preds = %24
%34 = getelementptr inbounds %struct.passwd, %struct.passwd* %1, i64 0, i32 5
%35 = load i8*, i8** %34
%36 = tail call i32 @chdir(i8* %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%41 = tail call i32 @exit(i32 -1)
unreachable
42: ; preds = %33
ret void
}