#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setgroups(int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void drop_privs() {
  char *user = "challenge";
  struct passwd *pw = getpwnam(user);
  if (pw == NULL) {
    fprintf(stderr, "User \"%s\" does not exist", user);
    exit(EXIT_FAILURE);
  }
  if (setgroups(0, NULL) != 0) {
    perror("Error on setgroups");
    exit(EXIT_FAILURE);
  }
  if (setgid(pw->pw_gid) != 0) {
    perror("Error on setgid");
    exit(EXIT_FAILURE);
  }
  if (setuid(pw->pw_uid) != 0) {
    perror("Error on setuid");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<drop_privs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe81(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c2 <drop_privs+0x52>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe5b(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <setgroups@plt>
cmp    $0x0,%rax
je     11f1 <drop_privs+0x81>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1060 <setgid@plt>
cmp    $0x0,%rax
je     1221 <drop_privs+0xb1>
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1090 <setuid@plt>
cmp    $0x0,%rax
je     1250 <drop_privs+0xe0>
lea    0xe09(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drop_privs>:
push   %rbx
lea    0xe88(%rip),%rdi
call   1040 <getpwnam@plt>
test   %rax,%rax
je     11ae <drop_privs+0x3e>
mov    %rax,%rbx
xor    %edi,%edi
xor    %esi,%esi
call   1030 <setgroups@plt>
test   %rax,%rax
jne    11cc <drop_privs+0x5c>
mov    0x4(%rbx),%edi
call   1060 <setgid@plt>
test   %rax,%rax
jne    11d5 <drop_privs+0x65>
mov    (%rbx),%edi
call   1090 <setuid@plt>
test   %rax,%rax
jne    11de <drop_privs+0x6e>
pop    %rbx
ret
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
lea    0xe3b(%rip),%rdx
call   1050 <fprintf@plt>
jmp    11ea <drop_privs+0x7a>
lea    0xe50(%rip),%rdi
jmp    11e5 <drop_privs+0x75>
lea    0xe5a(%rip),%rdi
jmp    11e5 <drop_privs+0x75>
lea    0xe61(%rip),%rdi
call   1070 <perror@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@.str = [10 x i8] c"challenge\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"User \22%s\22 does not exist\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [19 x i8] c"Error on setgroups\00"
@.str.3 = [16 x i8] c"Error on setgid\00"
@.str.4 = [16 x i8] c"Error on setuid\00"
define void @drop_privs() {
%1 = alloca i8*
%2 = alloca %struct.passwd*
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %1
%3 = load i8*, i8** %1
%4 = call %struct.passwd* @getpwnam(i8* %3)
store %struct.passwd* %4, %struct.passwd** %2
%5 = load %struct.passwd*, %struct.passwd** %2
%6 = icmp eq %struct.passwd* %5, null
br i1 %6, label %7, label %13
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
%14 = call i64 @setgroups(i32 0, i32* null)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %13
%21 = load %struct.passwd*, %struct.passwd** %2
%22 = getelementptr inbounds %struct.passwd, %struct.passwd* %21, i32 0, i32 1
%23 = load i32, i32* %22
%24 = call i64 @setgid(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %20
%31 = load %struct.passwd*, %struct.passwd** %2
%32 = getelementptr inbounds %struct.passwd, %struct.passwd* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i64 @setuid(i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %30
%37 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %30
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@.str = [10 x i8] c"challenge\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"User \22%s\22 does not exist\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [19 x i8] c"Error on setgroups\00"
@.str.3 = [16 x i8] c"Error on setgid\00"
@.str.4 = [16 x i8] c"Error on setuid\00"
define void @drop_privs() {
%1 = tail call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq %struct.passwd* %1, null
br i1 %2, label %3, label %8
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = tail call i64 @setgroups(i32 0, i32* null)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %8
%16 = getelementptr inbounds %struct.passwd, %struct.passwd* %1, i64 0, i32 1
%17 = load i32, i32* %16
%18 = tail call i64 @setgid(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %15
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %1, i64 0, i32 0
%26 = load i32, i32* %25
%27 = tail call i64 @setuid(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %24
%30 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %24
ret void
}