#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 */

/* Variables and functions */
scalar_t__ EPERM;
int /*<<< orphan*/ MCL_CURRENT;
scalar_t__ errno;
scalar_t__ getuid();
int mlockall(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ set_nonroot();
int main(void) {
  int result;
  if (getuid() == 0) {
    if (set_nonroot() != 0) {
      printf("Cannot run this test as non-root user\n");
      return 5;
    }
  }
  result = mlockall(MCL_CURRENT);
  if (result == -1 && errno == EPERM) {
    printf("Test PASSED\n");
    return 0;
  } else if (result == 0) {
    printf("You have the right to call mlockall\n");
    return 2;
  } else {
    perror("Unexpected error");
    return 2;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%rax
jne    119e <main+0x4e>
mov    $0x0,%al
call   1050 <set_nonroot@plt>
cmp    $0x0,%rax
je     1199 <main+0x49>
lea    0xe78(%rip),%rdi
call   1040 <printf@plt>
movl   $0x5,-0x4(%rbp)
jmp    1220 <main+0xd0>
jmp    119e <main+0x4e>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1060 <mlockall@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11eb <main+0x9b>
mov    0x2e20(%rip),%rax
mov    (%rax),%rax
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
jne    11eb <main+0x9b>
lea    0xe4d(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1220 <main+0xd0>
cmpl   $0x0,-0x8(%rbp)
jne    120d <main+0xbd>
lea    0xe38(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1220 <main+0xd0>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
je     119a <main+0x4a>
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
call   1060 <mlockall@plt>
test   %eax,%eax
je     11b9 <main+0x69>
cmp    $0xffffffff,%eax
jne    11cc <main+0x7c>
mov    0x2e65(%rip),%rax
mov    (%rax),%rax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%rax
jne    11cc <main+0x7c>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret
xor    %eax,%eax
call   1050 <set_nonroot@plt>
test   %rax,%rax
je     115d <main+0xd>
lea    0xe94(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%eax
pop    %rcx
ret
lea    0xe51(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
pop    %rcx
ret
lea    0xe2d(%rip),%rdi
call   1070 <perror@plt>
jmp    11c5 <main+0x75>

LLVM IR (-O0):
@.str = [39 x i8] c"Cannot run this test as non-root user\0A\00"
@MCL_CURRENT = global i32 0
@errno = global i64 0
@EPERM = global i64 0
@.str.1 = [13 x i8] c"Test PASSED\0A\00"
@.str.2 = [37 x i8] c"You have the right to call mlockall\0A\00"
@.str.3 = [17 x i8] c"Unexpected error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i64 (...) @getuid()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %11
5: ; preds = %0
%6 = call i64 (...) @set_nonroot()
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 5, i32* %1
br label %29
10: ; preds = %5
br label %11
11: ; preds = %10, %0
%12 = load i32, i32* @MCL_CURRENT
%13 = call i32 @mlockall(i32 %12)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %11
%17 = load i64, i64* @errno
%18 = load i64, i64* @EPERM
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %29
22: ; preds = %16, %11
%23 = load i32, i32* %2
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %1
br label %29
29: ; preds = %27, %25, %20, %8
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
@MCL_CURRENT = global i32 0
@errno = global i64 0
@EPERM = global i64 0
@.str.3 = [17 x i8] c"Unexpected error\00"
@str = [36 x i8] c"You have the right to call mlockall\00"
@str.4 = [12 x i8] c"Test PASSED\00"
@str.5 = [38 x i8] c"Cannot run this test as non-root user\00"
define i32 @main() {
%1 = tail call i64 (...) @getuid()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %8
3: ; preds = %0
%4 = tail call i64 (...) @set_nonroot()
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.5, i64 0, i64 0))
br label %21
8: ; preds = %3, %0
%9 = load i32, i32* @MCL_CURRENT
%10 = tail call i32 @mlockall(i32 %9)
switch i32 %10, label %19 [
i32 -1, label %11
i32 0, label %17
]
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @EPERM
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.4, i64 0, i64 0))
br label %21
17: ; preds = %8
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
br label %21
19: ; preds = %11, %8
%20 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %19, %17, %15, %6
%22 = phi i32 [ 5, %6 ], [ 0, %15 ], [ 2, %17 ], [ 2, %19 ]
ret i32 %22
}