#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();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ seteuid(int /*<<< orphan*/);
int /*<<< orphan*/ setpwent();
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 (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    $0x20,%rsp
mov    $0x0,%al
call   1060 <setpwent@plt>
mov    $0x0,%al
call   1070 <getpwent@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11cb <set_nonroot+0x4b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe4e(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11c6 <set_nonroot+0x46>
jmp    11cb <set_nonroot+0x4b>
jmp    118f <set_nonroot+0xf>
mov    $0x0,%al
call   1080 <endpwent@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11f7 <set_nonroot+0x77>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1286 <set_nonroot+0x106>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
call   10a0 <seteuid@plt>
cmp    $0x0,%rax
je     1259 <set_nonroot+0xd9>
mov    0x2dcc(%rip),%rax
mov    (%rax),%rax
mov    0x2db2(%rip),%rcx
cmp    (%rcx),%rax
jne    1241 <set_nonroot+0xc1>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1286 <set_nonroot+0x106>
lea    0xe1b(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1286 <set_nonroot+0x106>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    -0x18(%rbp),%rsi
mov    %eax,%edx
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1030 <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   %rbx
push   %rax
xor    %eax,%eax
call   1070 <setpwent@plt>
lea    0xe5f(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1080 <getpwent@plt>
test   %rax,%rax
je     1204 <set_nonroot+0x74>
mov    %rax,%rbx
mov    (%rax),%rdi
mov    %rbp,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11b0 <set_nonroot+0x20>
xor    %ebp,%ebp
xor    %eax,%eax
call   1090 <endpwent@plt>
mov    0x8(%rbx),%edi
call   10b0 <seteuid@plt>
test   %rax,%rax
je     1225 <set_nonroot+0x95>
mov    0x2df4(%rip),%rax
mov    (%rax),%rax
mov    0x2dda(%rip),%rcx
cmp    (%rcx),%rax
jne    1244 <set_nonroot+0xb4>
lea    0xe4c(%rip),%rdi
jmp    1212 <set_nonroot+0x82>
xor    %eax,%eax
call   1090 <endpwent@plt>
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    (%rbx),%rbx
xor    %eax,%eax
call   1050 <geteuid@plt>
lea    0xdf6(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    121c <set_nonroot+0x8c>
lea    0xdba(%rip),%rdi
call   10a0 <perror@plt>
jmp    1217 <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 = [34 x i8] c"Testing with user '%s' (uid: %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 %42
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 @seteuid(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 %42
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 %42
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 (...) @geteuid()
%40 = trunc i64 %39 to i32
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %38, i32 %40)
store i32 0, i32* %1
br label %42
42: ; preds = %35, %33, %31, %19
%43 = load i32, i32* %1
ret i32 %43
}

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 = [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.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 %33
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 @seteuid(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 %33
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 %33
27: ; preds = %13
%28 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 0
%29 = load i8*, i8** %28
%30 = tail call i64 (...) @geteuid()
%31 = trunc i64 %30 to i32
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %29, i32 %31)
br label %33
33: ; preds = %27, %25, %23, %10
%34 = phi i32 [ 1, %10 ], [ 1, %23 ], [ 1, %25 ], [ 0, %27 ]
ret i32 %34
}