#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*/ SIGSEGV;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ getpid();
struct passwd *getpwnam(char const *const);
scalar_t__ getuid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int setresgid(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setresuid(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    errx(EXIT_FAILURE, "Usage:\n  %s USERNAME", argv[0]);
  }
  if (getuid() != 0) {
    errx(EXIT_FAILURE, "No running under root user");
  }
  const char *const username = argv[1];
  struct passwd *pwd = getpwnam(username);
  if (!pwd) {
    err(EXIT_FAILURE, "Cannot find passwd entry for the %s user", username);
  }
  if (setresgid(pwd->pw_gid, pwd->pw_gid, pwd->pw_gid) == -1) {
    err(EXIT_FAILURE, "Can't change gid!\n");
  }
  if (setresuid(pwd->pw_uid, pwd->pw_uid, pwd->pw_uid) == -1) {
    err(EXIT_FAILURE, "Can't change uid!\n");
  }
  kill(getpid(), SIGSEGV);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11be <main+0x3e>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1070 <errx@plt>
mov    $0x0,%al
call   1050 <getuid@plt>
cmp    $0x0,%rax
je     11e6 <main+0x66>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1070 <errx@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1225 <main+0xa5>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   10a0 <err@plt>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edx
call   1060 <setresgid@plt>
cmp    $0xffffffff,%eax
jne    125f <main+0xdf>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
mov    $0x0,%al
call   10a0 <err@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1040 <setresuid@plt>
cmp    $0xffffffff,%eax
jne    1296 <main+0x116>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
mov    $0x0,%al
call   10a0 <err@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
je     11a6 <main+0x26>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe61(%rip),%rsi
xor    %eax,%eax
call   1070 <errx@plt>
xor    %eax,%eax
call   1050 <getuid@plt>
test   %rax,%rax
je     11c9 <main+0x49>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
xor    %eax,%eax
call   1070 <errx@plt>
mov    0x8(%rbx),%r14
mov    %r14,%rdi
call   1080 <getpwnam@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11f7 <main+0x77>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   10a0 <err@plt>
mov    0x4(%rbx),%edi
mov    %edi,%esi
mov    %edi,%edx
call   1060 <setresgid@plt>
cmp    $0xffffffff,%eax
jne    121f <main+0x9f>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
xor    %eax,%eax
call   10a0 <err@plt>
mov    (%rbx),%edi
mov    %edi,%esi
mov    %edi,%edx
call   1040 <setresuid@plt>
cmp    $0xffffffff,%eax
jne    1246 <main+0xc6>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
xor    %eax,%eax
call   10a0 <err@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x2d94(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1090 <kill@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@EXIT_FAILURE = global i32 0
@.str = [21 x i8] c"Usage:\0A %s USERNAME\00"
@.str.1 = [27 x i8] c"No running under root user\00"
@.str.2 = [41 x i8] c"Cannot find passwd entry for the %s user\00"
@.str.3 = [19 x i8] c"Can't change gid!\0A\00"
@.str.4 = [19 x i8] c"Can't change uid!\0A\00"
@SIGSEGV = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca %struct.passwd*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %16
10: ; preds = %2
%11 = load i32, i32* @EXIT_FAILURE
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 (i32, i8*, ...) @errx(i32 %11, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %10, %2
%17 = call i64 (...) @getuid()
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 (i32, i8*, ...) @errx(i32 %20, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %19, %16
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = call %struct.passwd* @getpwnam(i8* %26)
store %struct.passwd* %27, %struct.passwd** %7
%28 = load %struct.passwd*, %struct.passwd** %7
%29 = icmp ne %struct.passwd* %28, null
br i1 %29, label %34, label %30
30: ; preds = %22
%31 = load i32, i32* @EXIT_FAILURE
%32 = load i8*, i8** %6
%33 = call i32 (i32, i8*, ...) @err(i32 %31, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %30, %22
%35 = load %struct.passwd*, %struct.passwd** %7
%36 = getelementptr inbounds %struct.passwd, %struct.passwd* %35, i32 0, i32 1
%37 = load i32, i32* %36
%38 = load %struct.passwd*, %struct.passwd** %7
%39 = getelementptr inbounds %struct.passwd, %struct.passwd* %38, i32 0, i32 1
%40 = load i32, i32* %39
%41 = load %struct.passwd*, %struct.passwd** %7
%42 = getelementptr inbounds %struct.passwd, %struct.passwd* %41, i32 0, i32 1
%43 = load i32, i32* %42
%44 = call i32 @setresgid(i32 %37, i32 %40, i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %34
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 (i32, i8*, ...) @err(i32 %47, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %49
49: ; preds = %46, %34
%50 = load %struct.passwd*, %struct.passwd** %7
%51 = getelementptr inbounds %struct.passwd, %struct.passwd* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = load %struct.passwd*, %struct.passwd** %7
%54 = getelementptr inbounds %struct.passwd, %struct.passwd* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = load %struct.passwd*, %struct.passwd** %7
%57 = getelementptr inbounds %struct.passwd, %struct.passwd* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 @setresuid(i32 %52, i32 %55, i32 %58)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %64
61: ; preds = %49
%62 = load i32, i32* @EXIT_FAILURE
%63 = call i32 (i32, i8*, ...) @err(i32 %62, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %64
64: ; preds = %61, %49
%65 = call i32 (...) @getpid()
%66 = load i32, i32* @SIGSEGV
%67 = call i32 @kill(i32 %65, i32 %66)
ret i32 0
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@EXIT_FAILURE = global i32 0
@.str = [21 x i8] c"Usage:\0A %s USERNAME\00"
@.str.1 = [27 x i8] c"No running under root user\00"
@.str.2 = [41 x i8] c"Cannot find passwd entry for the %s user\00"
@.str.3 = [19 x i8] c"Can't change gid!\0A\00"
@.str.4 = [19 x i8] c"Can't change uid!\0A\00"
@SIGSEGV = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i32, i32* @EXIT_FAILURE
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @errx(i32 %5, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %8
8: ; preds = %4, %2
%9 = tail call i64 (...) @getuid()
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 (i32, i8*, ...) @errx(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %11, %8
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call %struct.passwd* @getpwnam(i8* %16)
%18 = icmp eq %struct.passwd* %17, null
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 (i32, i8*, ...) @err(i32 %20, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %16)
br label %22
22: ; preds = %19, %14
%23 = getelementptr inbounds %struct.passwd, %struct.passwd* %17, i64 0, i32 1
%24 = load i32, i32* %23
%25 = tail call i32 @setresgid(i32 %24, i32 %24, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 (i32, i8*, ...) @err(i32 %28, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %27, %22
%31 = getelementptr inbounds %struct.passwd, %struct.passwd* %17, i64 0, i32 0
%32 = load i32, i32* %31
%33 = tail call i32 @setresuid(i32 %32, i32 %32, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i32, i32* @EXIT_FAILURE
%37 = tail call i32 (i32, i8*, ...) @err(i32 %36, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %38
38: ; preds = %35, %30
%39 = tail call i32 (...) @getpid()
%40 = load i32, i32* @SIGSEGV
%41 = tail call i32 @kill(i32 %39, i32 %40)
ret i32 0
}