#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;
scalar_t__ errno;
scalar_t__ getuid();
void *malloc(int);
int mlock(void *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ set_nonroot();
int main(void) {
  int result;
  void *ptr;
  if (getuid() == 0) {
    if (set_nonroot() != 0) {
      printf("Cannot run this test as non-root user\n");
      return 5;
    }
  }
  ptr = malloc(8);
  if (ptr == NULL) {
    printf("Can not allocate memory.\n");
    return 2;
  }
  result = mlock(ptr, 8);
  if (result == -1 && errno == EPERM) {
    printf("Test PASSED\n");
    return 0;
  } else if (result == 0) {
    printf("You have the right to call mlock\n");
    return 1;
  } 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   1040 <getuid@plt>
cmp    $0x0,%rax
jne    11ae <main+0x4e>
mov    $0x0,%al
call   1060 <set_nonroot@plt>
cmp    $0x0,%rax
je     11a9 <main+0x49>
lea    0xe68(%rip),%rdi
call   1050 <printf@plt>
movl   $0x5,-0x4(%rbp)
jmp    1261 <main+0x101>
jmp    11ae <main+0x4e>
mov    $0x8,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11df <main+0x7f>
lea    0xe59(%rip),%rdi
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1261 <main+0x101>
mov    -0x10(%rbp),%rdi
mov    $0x8,%esi
call   1030 <mlock@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    122c <main+0xcc>
mov    0x2ddf(%rip),%rax
mov    (%rax),%rax
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%rax
jne    122c <main+0xcc>
lea    0xe26(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1261 <main+0x101>
cmpl   $0x0,-0x8(%rbp)
jne    124e <main+0xee>
lea    0xe11(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1261 <main+0x101>
lea    0xe1b(%rip),%rdi
call   1080 <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   1050 <getuid@plt>
test   %rax,%rax
je     11b8 <main+0x58>
mov    $0x8,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11d7 <main+0x77>
mov    %rax,%rdi
mov    $0x8,%esi
call   1030 <mlock@plt>
test   %eax,%eax
je     11ea <main+0x8a>
cmp    $0xffffffff,%eax
jne    11fd <main+0x9d>
mov    0x2e47(%rip),%rax
mov    (%rax),%rax
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%rax
jne    11fd <main+0x9d>
lea    0xe83(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret
xor    %eax,%eax
call   1060 <set_nonroot@plt>
test   %rax,%rax
je     116d <main+0xd>
lea    0xe8c(%rip),%rdi
call   1040 <puts@plt>
mov    $0x5,%eax
pop    %rcx
ret
lea    0xe60(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%eax
pop    %rcx
ret
lea    0xe20(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
pop    %rcx
ret
lea    0xdfc(%rip),%rdi
call   1080 <perror@plt>
jmp    11e3 <main+0x83>

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

LLVM IR (-O2):
@errno = global i64 0
@EPERM = global i64 0
@.str.4 = [17 x i8] c"Unexpected error\00"
@str = [33 x i8] c"You have the right to call mlock\00"
@str.5 = [12 x i8] c"Test PASSED\00"
@str.6 = [25 x i8] c"Can not allocate memory.\00"
@str.7 = [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.7, i64 0, i64 0))
br label %25
8: ; preds = %3, %0
%9 = tail call dereferenceable_or_null(8) i8* @malloc(i32 8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.6, i64 0, i64 0))
br label %25
13: ; preds = %8
%14 = tail call i32 @mlock(i8* %9, i32 8)
switch i32 %14, label %23 [
i32 -1, label %15
i32 0, label %21
]
15: ; preds = %13
%16 = load i64, i64* @errno
%17 = load i64, i64* @EPERM
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0))
br label %25
21: ; preds = %13
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %25
23: ; preds = %15, %13
%24 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %25
25: ; preds = %23, %21, %19, %11, %6
%26 = phi i32 [ 5, %6 ], [ 2, %11 ], [ 0, %19 ], [ 1, %21 ], [ 2, %23 ]
ret i32 %26
}