#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) {
  int failure = 0;
  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 (-1 == sigqueue(1, 0, value)) {
    if (EPERM == errno) {
      printf("EPERM error received\n");
    } else {
      printf("sigqueue() failed but errno not set correctly\n");
      failure = 1;
    }
  } else {
    printf("sigqueue() did not return -1\n");
    failure = 1;
  }
  if (failure) {
    return 1;
  }
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%rax
jne    119d <main+0x5d>
mov    $0x0,%al
call   1050 <set_nonroot@plt>
cmp    $0x0,%rax
je     1198 <main+0x58>
lea    0xe79(%rip),%rdi
call   1040 <printf@plt>
movl   $0x5,-0x4(%rbp)
jmp    123b <main+0xfb>
jmp    119d <main+0x5d>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
xor    %esi,%esi
call   1060 <sigqueue@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11ff <main+0xbf>
mov    0x2e0d(%rip),%rax
mov    (%rax),%rax
mov    0x2e13(%rip),%rcx
cmp    (%rcx),%rax
jne    11e7 <main+0xa7>
lea    0xe4a(%rip),%rdi
call   1040 <printf@plt>
jmp    11fa <main+0xba>
lea    0xe4f(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x8(%rbp)
jmp    1212 <main+0xd2>
lea    0xe66(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1228 <main+0xe8>
movl   $0x1,-0x4(%rbp)
jmp    123b <main+0xfb>
lea    0xe5b(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
je     116e <main+0x2e>
mov    $0x1,%ebx
mov    $0x1,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <sigqueue@plt>
cmp    $0xffffffff,%eax
je     1188 <main+0x48>
lea    0xe94(%rip),%rdi
jmp    11bc <main+0x7c>
xor    %eax,%eax
call   1050 <set_nonroot@plt>
test   %rax,%rax
je     114d <main+0xd>
mov    $0x5,%ebx
lea    0xee6(%rip),%rdi
jmp    11bc <main+0x7c>
mov    0x2e41(%rip),%rax
mov    (%rax),%rax
mov    0x2e47(%rip),%rcx
cmp    (%rcx),%rax
jne    11b5 <main+0x75>
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
lea    0xe6a(%rip),%rdi
jmp    11bc <main+0x7c>
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
%union.sigval = type { i64 }
@.str = [39 x i8] c"Cannot run this test as non-root user\0A\00"
@EPERM = global i64 0
@errno = global i64 0
@.str.1 = [22 x i8] c"EPERM error received\0A\00"
@.str.2 = [47 x i8] c"sigqueue() failed but errno not set correctly\0A\00"
@.str.3 = [30 x i8] c"sigqueue() did not return -1\0A\00"
@.str.4 = [13 x i8] c"Test PASSED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %union.sigval
store i32 0, i32* %1
store i32 0, i32* %2
%4 = bitcast %union.sigval* %3 to i64*
store i64 0, i64* %4
%5 = call i64 (...) @getuid()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %0
%8 = call i64 (...) @set_nonroot()
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 5, i32* %1
br label %35
12: ; preds = %7
br label %13
13: ; preds = %12, %0
%14 = getelementptr inbounds %union.sigval, %union.sigval* %3, i32 0, i32 0
%15 = load i64, i64* %14
%16 = call i32 @sigqueue(i32 1, i32 0, i64 %15)
%17 = icmp eq i32 -1, %16
br i1 %17, label %18, label %27
18: ; preds = %13
%19 = load i64, i64* @EPERM
%20 = load i64, i64* @errno
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %26
24: ; preds = %18
%25 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %26
26: ; preds = %24, %22
br label %29
27: ; preds = %13
%28 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %2
br label %29
29: ; preds = %27, %26
%30 = load i32, i32* %2
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 1, i32* %1
br label %35
33: ; preds = %29
%34 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %35
35: ; preds = %33, %32, %10
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@EPERM = global i64 0
@errno = global i64 0
@str = [29 x i8] c"sigqueue() did not return -1\00"
@str.5 = [12 x i8] c"Test PASSED\00"
@str.6 = [46 x i8] c"sigqueue() failed but errno not set correctly\00"
@str.7 = [21 x i8] c"EPERM error received\00"
@str.8 = [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 %15
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 %15
9: ; preds = %6
%10 = load i64, i64* @EPERM
%11 = load i64, i64* @errno
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.7, i64 0, i64 0))
br label %15
15: ; preds = %6, %9, %3, %13
%16 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0), %13 ], [ getelementptr inbounds ([38 x i8], [38 x i8]* @str.8, i64 0, i64 0), %3 ], [ getelementptr inbounds ([46 x i8], [46 x i8]* @str.6, i64 0, i64 0), %9 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0), %6 ]
%17 = phi i32 [ 0, %13 ], [ 5, %3 ], [ 1, %9 ], [ 1, %6 ]
%18 = tail call i32 @puts(i8* dereferenceable(1) %16)
ret i32 %17
}