#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 {
  scalar_t__ pw_uid;
  char *pw_name;
  int /*<<< orphan*/ pw_gid;
};

/* Variables and functions */
scalar_t__ EPERM;
int /*<<< orphan*/ endpwent();
scalar_t__ errno;
int geteuid();
struct passwd *getpwent();
int getuid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setgid(int /*<<< orphan*/);
int /*<<< orphan*/ setpwent();
scalar_t__ setuid(scalar_t__);
int set_nonroot() {
  struct passwd *pw;
  setpwent();
  while ((pw = getpwent()) != NULL)
    if (pw->pw_uid != 0)
      break;
  endpwent();
  if (pw == NULL) {
    printf("There is no other user than current and root.\n");
    return 1;
  }
  if (setgid(pw->pw_gid) != 0) {
    if (errno == EPERM)
      printf("You don't have permission to change "
             "your GID.\n");
    else
      perror("setgid failed");
    return 1;
  }
  if (setuid(pw->pw_uid) != 0) {
    if (errno == EPERM)
      printf("You don't have permission to change "
             "your UID.\n");
    else
      perror("setuid failed");
    return 1;
  }
  printf("Testing with user '%s' (euid,uid) = (%d,%d)\n", pw->pw_name,
         geteuid(), getuid());
  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     11cc <set_nonroot+0x3c>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     11c7 <set_nonroot+0x37>
jmp    11cc <set_nonroot+0x3c>
jmp    119f <set_nonroot+0xf>
mov    $0x0,%al
call   1080 <endpwent@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11f8 <set_nonroot+0x68>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e9 <set_nonroot+0x159>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%edi
call   1090 <setgid@plt>
cmp    $0x0,%rax
je     1253 <set_nonroot+0xc3>
mov    0x2dcb(%rip),%rax
mov    (%rax),%rax
mov    0x2db1(%rip),%rcx
cmp    (%rcx),%rax
jne    123b <set_nonroot+0xab>
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1247 <set_nonroot+0xb7>
lea    0xe1c(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e9 <set_nonroot+0x159>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10b0 <setuid@plt>
cmp    $0x0,%rax
je     12ae <set_nonroot+0x11e>
mov    0x2d70(%rip),%rax
mov    (%rax),%rax
mov    0x2d56(%rip),%rcx
cmp    (%rcx),%rax
jne    1296 <set_nonroot+0x106>
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12a2 <set_nonroot+0x112>
lea    0xdfe(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e9 <set_nonroot+0x159>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    %eax,%ecx
lea    0xdce(%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   1070 <setpwent@plt>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1080 <getpwent@plt>
test   %rax,%rax
je     11fc <set_nonroot+0x5c>
mov    %rax,%rbx
cmpq   $0x0,(%rax)
je     11b0 <set_nonroot+0x10>
xor    %eax,%eax
call   1090 <endpwent@plt>
mov    0x10(%rbx),%edi
call   10a0 <setgid@plt>
test   %rax,%rax
je     121b <set_nonroot+0x7b>
mov    0x2e00(%rip),%rax
mov    (%rax),%rax
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%rax
jne    1274 <set_nonroot+0xd4>
lea    0xe7d(%rip),%rdi
jmp    120a <set_nonroot+0x6a>
xor    %eax,%eax
call   1090 <endpwent@plt>
lea    0xe9b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    (%rbx),%rdi
call   10c0 <setuid@plt>
test   %rax,%rax
je     1247 <set_nonroot+0xa7>
mov    0x2db1(%rip),%rax
mov    (%rax),%rax
mov    0x2d97(%rip),%rcx
cmp    (%rcx),%rax
jne    127d <set_nonroot+0xdd>
lea    0xe04(%rip),%rdi
jmp    120a <set_nonroot+0x6a>
mov    0x8(%rbx),%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <geteuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <getuid@plt>
lea    0xdb8(%rip),%rdi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1214 <set_nonroot+0x74>
lea    0xd85(%rip),%rdi
jmp    1284 <set_nonroot+0xe4>
lea    0xd8a(%rip),%rdi
call   10b0 <perror@plt>
jmp    120f <set_nonroot+0x6f>

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

LLVM IR (-O2):
%struct.passwd = type { i64, i8*, i32 }
@errno = global i64 0
@EPERM = global i64 0
@.str.2 = [14 x i8] c"setgid failed\00"
@.str.4 = [14 x i8] c"setuid failed\00"
@.str.5 = [45 x i8] c"Testing with user '%s' (euid,uid) = (%d,%d)\0A\00"
@str = [46 x i8] c"You don't have permission to change your UID.\00"
@str.6 = [46 x i8] c"You don't have permission to change your GID.\00"
@str.7 = [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 %9, label %5
5: ; preds = %2
%6 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 0
%7 = load i64, i64* %6
%8 = icmp eq i64 %7, 0
br i1 %8, label %2, label %12
9: ; preds = %2
%10 = tail call i32 (...) @endpwent()
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.7, i64 0, i64 0))
br label %45
12: ; preds = %5
%13 = tail call i32 (...) @endpwent()
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 2
%15 = load i32, i32* %14
%16 = tail call i64 @setgid(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %26, label %18
18: ; preds = %12
%19 = load i64, i64* @errno
%20 = load i64, i64* @EPERM
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.6, i64 0, i64 0))
br label %45
24: ; preds = %18
%25 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %45
26: ; preds = %12
%27 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 0
%28 = load i64, i64* %27
%29 = tail call i64 @setuid(i64 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %39, label %31
31: ; preds = %26
%32 = load i64, i64* @errno
%33 = load i64, i64* @EPERM
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
br label %45
37: ; preds = %31
%38 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %45
39: ; preds = %26
%40 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 1
%41 = load i8*, i8** %40
%42 = tail call i32 (...) @geteuid()
%43 = tail call i32 (...) @getuid()
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i8* %41, i32 %42, i32 %43)
br label %45
45: ; preds = %35, %37, %22, %24, %39, %9
%46 = phi i32 [ 1, %9 ], [ 0, %39 ], [ 1, %24 ], [ 1, %22 ], [ 1, %37 ], [ 1, %35 ]
ret i32 %46
}