#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 rlimit {
  scalar_t__ rlim_max;
  scalar_t__ rlim_cur;
};
struct passwd {
  char *pw_name;
  int /*<<< orphan*/ pw_uid;
};

/* Variables and functions */
scalar_t__ EPERM;
int /*<<< orphan*/ RLIMIT_MEMLOCK;
int /*<<< orphan*/ endpwent();
scalar_t__ errno;
scalar_t__ geteuid();
struct passwd *getpwent();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ seteuid(int /*<<< orphan*/);
int /*<<< orphan*/ setpwent();
int setrlimit(int /*<<< orphan*/, struct rlimit *);
scalar_t__ strcmp(char *, char *);
int set_nonroot() {
  struct passwd *pw;
  struct rlimit rlim;
  int ret = 0;
  setpwent();
  while ((pw = getpwent()) != NULL)
    if (strcmp(pw->pw_name, "root"))
      break;
  endpwent();
  if (pw == NULL) {
    printf("There is no other user than current and root.\n");
    return 1;
  }
  rlim.rlim_cur = 0;
  rlim.rlim_max = 0;
  if ((ret = setrlimit(RLIMIT_MEMLOCK, &rlim)) != 0)
    printf("Failed at setrlimit() return %d \n", ret);
  if (seteuid(pw->pw_uid) != 0) {
    if (errno == EPERM) {
      printf("You don't have permission to change your UID.\n");
      return 1;
    }
    perror("An error occurs when calling seteuid()");
    return 1;
  }
  printf("Testing with user '%s' (uid: %d)\n", pw->pw_name, (int)geteuid());
  return 0;
}


x86 assembly (clang, -O0):
<set_nonroot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   1060 <setpwent@plt>
mov    $0x0,%al
call   1070 <getpwent@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11e2 <set_nonroot+0x52>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe37(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11dd <set_nonroot+0x4d>
jmp    11e2 <set_nonroot+0x52>
jmp    11a6 <set_nonroot+0x16>
mov    $0x0,%al
call   1080 <endpwent@plt>
cmpq   $0x0,-0x10(%rbp)
jne    120e <set_nonroot+0x7e>
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dc <set_nonroot+0x14c>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1090 <setrlimit@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     124d <set_nonroot+0xbd>
mov    -0x24(%rbp),%esi
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
call   10b0 <seteuid@plt>
cmp    $0x0,%rax
je     12af <set_nonroot+0x11f>
mov    0x2d6e(%rip),%rax
mov    (%rax),%rax
mov    0x2d54(%rip),%rcx
cmp    (%rcx),%rax
jne    1297 <set_nonroot+0x107>
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dc <set_nonroot+0x14c>
lea    0xde7(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dc <set_nonroot+0x14c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_nonroot>:
push   %r14
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   1070 <setpwent@plt>
lea    0xe4b(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1080 <getpwent@plt>
test   %rax,%rax
je     123e <set_nonroot+0x9e>
mov    %rax,%rbx
mov    (%rax),%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11c0 <set_nonroot+0x20>
xor    %eax,%eax
call   1090 <endpwent@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   10a0 <setrlimit@plt>
test   %eax,%eax
je     1212 <set_nonroot+0x72>
lea    0xdfc(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rbx),%edi
call   10c0 <seteuid@plt>
test   %rax,%rax
je     1260 <set_nonroot+0xc0>
mov    0x2db2(%rip),%rax
mov    (%rax),%rax
mov    0x2d98(%rip),%rcx
cmp    (%rcx),%rax
jne    1281 <set_nonroot+0xe1>
lea    0xe34(%rip),%rdi
jmp    124c <set_nonroot+0xac>
xor    %eax,%eax
call   1090 <endpwent@plt>
lea    0xe52(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    (%rbx),%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <geteuid@plt>
lea    0xddb(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1256 <set_nonroot+0xb6>
lea    0xd9f(%rip),%rdi
call   10b0 <perror@plt>
jmp    1251 <set_nonroot+0xb1>

LLVM IR (-O0):
%struct.passwd = type { i8*, i32 }
%struct.rlimit = type { i64, i64 }
@.str = [5 x i8] c"root\00"
@.str.1 = [47 x i8] c"There is no other user than current and root.\0A\00"
@RLIMIT_MEMLOCK = global i32 0
@.str.2 = [34 x i8] c"Failed at setrlimit() return %d \0A\00"
@errno = global i64 0
@EPERM = global i64 0
@.str.3 = [47 x i8] c"You don't have permission to change your UID.\0A\00"
@.str.4 = [39 x i8] c"An error occurs when calling seteuid()\00"
@.str.5 = [34 x i8] c"Testing with user '%s' (uid: %d)\0A\00"
define i32 @set_nonroot() {
%1 = alloca i32
%2 = alloca %struct.passwd*
%3 = alloca %struct.rlimit
%4 = alloca i32
store i32 0, i32* %4
%5 = call i32 (...) @setpwent()
br label %6
6: ; preds = %16, %0
%7 = call %struct.passwd* (...) @getpwent()
store %struct.passwd* %7, %struct.passwd** %2
%8 = icmp ne %struct.passwd* %7, null
br i1 %8, label %9, label %17
9: ; preds = %6
%10 = load %struct.passwd*, %struct.passwd** %2
%11 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i32 0, i32 0
%12 = load i8*, i8** %11
%13 = call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %9
br label %17
16: ; preds = %9
br label %6
17: ; preds = %15, %6
%18 = call i32 (...) @endpwent()
%19 = load %struct.passwd*, %struct.passwd** %2
%20 = icmp eq %struct.passwd* %19, null
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %53
23: ; preds = %17
%24 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 1
store i64 0, i64* %24
%25 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 0
store i64 0, i64* %25
%26 = load i32, i32* @RLIMIT_MEMLOCK
%27 = call i32 @setrlimit(i32 %26, %struct.rlimit* %3)
store i32 %27, i32* %4
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* %4
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %29, %23
%33 = load %struct.passwd*, %struct.passwd** %2
%34 = getelementptr inbounds %struct.passwd, %struct.passwd* %33, i32 0, i32 1
%35 = load i32, i32* %34
%36 = call i64 @seteuid(i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %46
38: ; preds = %32
%39 = load i64, i64* @errno
%40 = load i64, i64* @EPERM
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %53
44: ; preds = %38
%45 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %53
46: ; preds = %32
%47 = load %struct.passwd*, %struct.passwd** %2
%48 = getelementptr inbounds %struct.passwd, %struct.passwd* %47, i32 0, i32 0
%49 = load i8*, i8** %48
%50 = call i64 (...) @geteuid()
%51 = trunc i64 %50 to i32
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %49, i32 %51)
store i32 0, i32* %1
br label %53
53: ; preds = %46, %44, %42, %21
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
%struct.rlimit = type { i64, i64 }
%struct.passwd = type { i8*, i32 }
@.str = [5 x i8] c"root\00"
@RLIMIT_MEMLOCK = global i32 0
@.str.2 = [34 x i8] c"Failed at setrlimit() return %d \0A\00"
@errno = global i64 0
@EPERM = global i64 0
@.str.4 = [39 x i8] c"An error occurs when calling seteuid()\00"
@.str.5 = [34 x i8] c"Testing with user '%s' (uid: %d)\0A\00"
@str = [46 x i8] c"You don't have permission to change your UID.\00"
@str.6 = [46 x i8] c"There is no other user than current and root.\00"
define i32 @set_nonroot() {
%1 = alloca %struct.rlimit
%2 = bitcast %struct.rlimit* %1 to i8*
%3 = tail call i32 (...) @setpwent()
br label %4
4: ; preds = %7, %0
%5 = tail call %struct.passwd* (...) @getpwent()
%6 = icmp eq %struct.passwd* %5, null
br i1 %6, label %12, label %7
7: ; preds = %4
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 0
%9 = load i8*, i8** %8
%10 = tail call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %4, label %15
12: ; preds = %4
%13 = tail call i32 (...) @endpwent()
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.6, i64 0, i64 0))
br label %42
15: ; preds = %7
%16 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 0
%17 = tail call i32 (...) @endpwent()
%18 = bitcast %struct.rlimit* %1 to i8*
%19 = load i32, i32* @RLIMIT_MEMLOCK
%20 = call i32 @setrlimit(i32 %19, %struct.rlimit* %1)
%21 = icmp eq i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %15
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %20)
br label %24
24: ; preds = %22, %15
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 1
%26 = load i32, i32* %25
%27 = call i64 @seteuid(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %37, label %29
29: ; preds = %24
%30 = load i64, i64* @errno
%31 = load i64, i64* @EPERM
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
br label %42
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
br label %42
37: ; preds = %24
%38 = load i8*, i8** %16
%39 = call i64 (...) @geteuid()
%40 = trunc i64 %39 to i32
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %38, i32 %40)
br label %42
42: ; preds = %37, %35, %33, %12
%43 = phi i32 [ 1, %12 ], [ 1, %33 ], [ 1, %35 ], [ 0, %37 ]
ret i32 %43
}