#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;
  int /*<<< orphan*/ pw_dir;
};

/* Variables and functions */
scalar_t__ chdir(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;
int priv(char *username) {
  struct passwd *pw = getpwnam(username);
  if (pw == NULL) {
    fprintf(stderr, "User %s does not exist\n", username);
    return 1;
  }
  if (chdir(pw->pw_dir) != 0) {
    perror("chdir");
    return 1;
  }
  if (setgroups(0, NULL) != 0) {
    perror("setgroups");
    return 1;
  }
  if (setgid(pw->pw_gid) != 0) {
    perror("setgid");
    return 1;
  }
  if (setuid(pw->pw_uid) != 0) {
    perror("setuid");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<priv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b9 <priv+0x49>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe58(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1276 <priv+0x106>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     11e7 <priv+0x77>
lea    0xe42(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1276 <priv+0x106>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <setgroups@plt>
cmp    $0x0,%rax
je     1214 <priv+0xa4>
lea    0xe1b(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1276 <priv+0x106>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1070 <setgid@plt>
cmp    $0x0,%rax
je     1242 <priv+0xd2>
lea    0xdf7(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1276 <priv+0x106>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1090 <setuid@plt>
cmp    $0x0,%rax
je     126f <priv+0xff>
lea    0xdd1(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1276 <priv+0x106>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<priv>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1050 <getpwnam@plt>
test   %rax,%rax
je     11c6 <priv+0x56>
mov    %rax,%rbx
mov    0x8(%rax),%edi
call   1030 <chdir@plt>
test   %rax,%rax
jne    11e0 <priv+0x70>
xor    %edi,%edi
xor    %esi,%esi
call   1040 <setgroups@plt>
test   %rax,%rax
jne    11e9 <priv+0x79>
mov    0x4(%rbx),%edi
call   1070 <setgid@plt>
test   %rax,%rax
jne    11f2 <priv+0x82>
mov    (%rbx),%edi
call   1090 <setuid@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     1203 <priv+0x93>
lea    0xe6b(%rip),%rdi
jmp    11f9 <priv+0x89>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
jmp    11fe <priv+0x8e>
lea    0xe31(%rip),%rdi
jmp    11f9 <priv+0x89>
lea    0xe2e(%rip),%rdi
jmp    11f9 <priv+0x89>
lea    0xe2f(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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