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

/* Variables and functions */
scalar_t__ EPERM;
int /*<<< orphan*/ endpwent();
scalar_t__ errno;
scalar_t__ geteuid();
struct passwd *getpwent();
scalar_t__ getuid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setpwent();
scalar_t__ setuid(int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int set_nonroot() {
  struct passwd *pw;
  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;
  }
  if (setuid(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)(euid: %d)\n", pw->pw_name,
         (int)getuid(), (int)geteuid());
  return 0;
}


x86 assembly (clang, -O0):
<set_nonroot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1070 <setpwent@plt>
mov    $0x0,%al
call   1080 <getpwent@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11db <set_nonroot+0x4b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe3e(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     11d6 <set_nonroot+0x46>
jmp    11db <set_nonroot+0x4b>
jmp    119f <set_nonroot+0xf>
mov    $0x0,%al
call   1090 <endpwent@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1207 <set_nonroot+0x77>
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a3 <set_nonroot+0x113>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
call   10b0 <setuid@plt>
cmp    $0x0,%rax
je     1269 <set_nonroot+0xd9>
mov    0x2dbc(%rip),%rax
mov    (%rax),%rax
mov    0x2da2(%rip),%rcx
cmp    (%rcx),%rax
jne    1251 <set_nonroot+0xc1>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a3 <set_nonroot+0x113>
lea    0xe0b(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a3 <set_nonroot+0x113>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    %eax,%ecx
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_nonroot>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1080 <setpwent@plt>
lea    0xe4e(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1090 <getpwent@plt>
test   %rax,%rax
je     1214 <set_nonroot+0x74>
mov    %rax,%rbx
mov    (%rax),%rdi
mov    %rbp,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11c0 <set_nonroot+0x20>
xor    %ebp,%ebp
xor    %eax,%eax
call   10a0 <endpwent@plt>
mov    0x8(%rbx),%edi
call   10c0 <setuid@plt>
test   %rax,%rax
je     1233 <set_nonroot+0x93>
mov    0x2de4(%rip),%rax
mov    (%rax),%rax
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%rax
jne    125e <set_nonroot+0xbe>
lea    0xe46(%rip),%rdi
jmp    1222 <set_nonroot+0x82>
xor    %eax,%eax
call   10a0 <endpwent@plt>
lea    0xe64(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    (%rbx),%r14
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <geteuid@plt>
lea    0xdde(%rip),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    122c <set_nonroot+0x8c>
lea    0xda0(%rip),%rdi
call   10b0 <perror@plt>
jmp    1227 <set_nonroot+0x87>

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

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