#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 */
union sigval {
  scalar_t__ sival_int;
};

/* Variables and functions */
scalar_t__ EPERM;
scalar_t__ errno;
scalar_t__ getuid();
int /*<<< orphan*/ printf(char *);
scalar_t__ set_nonroot();
int sigqueue(int, int /*<<< orphan*/, union sigval);
int main(void) {
  union sigval value;
  value.sival_int = 0;
  if (getuid() == 0) {
    if (set_nonroot() != 0) {
      printf("Cannot run this test as non-root user\n");
      return 5;
    }
  }
  if (sigqueue(1, 0, value) != -1) {
    printf("Test FAILED: sigqueue() succeeded even though this program's user "
           "id did not match the recieving process's user id\n");
    return 1;
  }
  if (EPERM != errno) {
    printf("Test FAILED: EPERM error not received\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%rax
jne    1196 <main+0x56>
mov    $0x0,%al
call   1050 <set_nonroot@plt>
cmp    $0x0,%rax
je     1191 <main+0x51>
lea    0xe80(%rip),%rdi
call   1040 <printf@plt>
movl   $0x5,-0x4(%rbp)
jmp    1200 <main+0xc0>
jmp    1196 <main+0x56>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
xor    %esi,%esi
call   1060 <sigqueue@plt>
cmp    $0xffffffff,%eax
je     11c7 <main+0x87>
lea    0xe71(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1200 <main+0xc0>
mov    0x2e02(%rip),%rax
mov    (%rax),%rax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%rax
je     11f9 <main+0xb9>
lea    0xeb3(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1200 <main+0xc0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
je     1172 <main+0x32>
xor    %ebx,%ebx
mov    $0x1,%ebp
mov    $0x1,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <sigqueue@plt>
cmp    $0xffffffff,%eax
je     118c <main+0x4c>
lea    0xeb6(%rip),%rdi
jmp    11a9 <main+0x69>
xor    %eax,%eax
call   1050 <set_nonroot@plt>
test   %rax,%rax
je     114f <main+0xf>
mov    $0x5,%ebp
lea    0xf0f(%rip),%rdi
jmp    11a9 <main+0x69>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%rax
je     11b0 <main+0x70>
lea    0xe57(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%union.sigval = type { i64 }
@.str = [39 x i8] c"Cannot run this test as non-root user\0A\00"
@.str.1 = [116 x i8] c"Test FAILED: sigqueue() succeeded even though this program's user id did not match the recieving process's user id\0A\00"
@EPERM = global i64 0
@errno = global i64 0
@.str.2 = [39 x i8] c"Test FAILED: EPERM error not received\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %union.sigval
store i32 0, i32* %1
%3 = bitcast %union.sigval* %2 to i64*
store i64 0, i64* %3
%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 %26
11: ; preds = %6
br label %12
12: ; preds = %11, %0
%13 = getelementptr inbounds %union.sigval, %union.sigval* %2, i32 0, i32 0
%14 = load i64, i64* %13
%15 = call i32 @sigqueue(i32 1, i32 0, i64 %14)
%16 = icmp ne i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @printf(i8* getelementptr inbounds ([116 x i8], [116 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %26
19: ; preds = %12
%20 = load i64, i64* @EPERM
%21 = load i64, i64* @errno
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %26
25: ; preds = %19
store i32 0, i32* %1
br label %26
26: ; preds = %25, %23, %17, %9
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@EPERM = global i64 0
@errno = global i64 0
@str = [38 x i8] c"Test FAILED: EPERM error not received\00"
@str.3 = [115 x i8] c"Test FAILED: sigqueue() succeeded even though this program's user id did not match the recieving process's user id\00"
@str.4 = [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 %6
3: ; preds = %0
%4 = tail call i64 (...) @set_nonroot()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %13
6: ; preds = %3, %0
%7 = tail call i32 @sigqueue(i32 1, i32 0, i64 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %6
%10 = load i64, i64* @EPERM
%11 = load i64, i64* @errno
%12 = icmp eq i64 %10, %11
br i1 %12, label %17, label %13
13: ; preds = %9, %6, %3
%14 = phi i8* [ getelementptr inbounds ([38 x i8], [38 x i8]* @str.4, i64 0, i64 0), %3 ], [ getelementptr inbounds ([115 x i8], [115 x i8]* @str.3, i64 0, i64 0), %6 ], [ getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0), %9 ]
%15 = phi i32 [ 5, %3 ], [ 1, %6 ], [ 1, %9 ]
%16 = tail call i32 @puts(i8* dereferenceable(1) %14)
br label %17
17: ; preds = %13, %9
%18 = phi i32 [ 0, %9 ], [ %15, %13 ]
ret i32 %18
}