#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__ EAGAIN;
int /*<<< orphan*/ SIGRTMIN;
int /*<<< orphan*/ _SC_SIGQUEUE_MAX;
scalar_t__ errno;
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int reset_uid();
int /*<<< orphan*/ sighold(int /*<<< orphan*/);
int sigqueue(int, int /*<<< orphan*/, union sigval);
int /*<<< orphan*/ strerror(scalar_t__);
long sysconf(int /*<<< orphan*/);
int main(void) {
  int pid = getpid();
  int i;
  long syslimit;
  int rc;
  union sigval value;
  value.sival_int = 0;
  pid = getpid();
  sighold(SIGRTMIN);
  rc = reset_uid();
  if (rc)
    return 2;
  syslimit = sysconf(_SC_SIGQUEUE_MAX);
  if (syslimit < 0)
    goto done;
  for (i = 0; i < syslimit; i++) {
    if (sigqueue(pid, SIGRTMIN, value) != 0) {
      printf("Failed: sigqueue on %d of %ld max, errno: %s\n", i, syslimit,
             strerror(errno));
      return 2;
    }
  }
  rc = sigqueue(pid, SIGRTMIN, value);
  if (!(rc == -1 && errno == EAGAIN)) {
    printf("Failed: sigqueue() queued SIGQUEUE_MAX+1 signals\n");
    return 1;
  }
done:
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x8(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1080 <sighold@plt>
mov    $0x0,%al
call   1050 <reset_uid@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11c9 <main+0x59>
movl   $0x2,-0x4(%rbp)
jmp    12e1 <main+0x171>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11eb <main+0x7b>
jmp    12cc <main+0x15c>
movl   $0x0,-0xc(%rbp)
movslq -0xc(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    1271 <main+0x101>
mov    -0x8(%rbp),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   1060 <sigqueue@plt>
cmp    $0x0,%eax
je     125e <main+0xee>
mov    -0xc(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x34(%rbp),%esi
mov    -0x30(%rbp),%rdx
mov    %eax,%ecx
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e1 <main+0x171>
jmp    1263 <main+0xf3>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11f2 <main+0x82>
mov    -0x8(%rbp),%edi
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   1060 <sigqueue@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12ad <main+0x13d>
mov    0x2d36(%rip),%rax
mov    (%rax),%rax
mov    0x2d1c(%rip),%rcx
cmp    (%rcx),%rax
je     12c7 <main+0x157>
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e1 <main+0x171>
jmp    12cc <main+0x15c>
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %eax,%eax
call   1040 <getpid@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r15d
mov    0x2e38(%rip),%r12
mov    (%r12),%edi
call   1090 <sighold@plt>
xor    %eax,%eax
call   1060 <reset_uid@plt>
mov    $0x2,%r14d
test   %eax,%eax
jne    126d <main+0xed>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
test   %rax,%rax
js     1224 <main+0xa4>
je     11fb <main+0x7b>
mov    %rax,%rbx
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    (%r12),%esi
mov    %r15d,%edi
xor    %edx,%edx
call   1070 <sigqueue@plt>
test   %eax,%eax
jne    1249 <main+0xc9>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    11e0 <main+0x60>
mov    (%r12),%esi
mov    %r15d,%edi
xor    %edx,%edx
call   1070 <sigqueue@plt>
cmp    $0xffffffff,%eax
jne    1235 <main+0xb5>
mov    0x2dbb(%rip),%rax
mov    (%rax),%rax
mov    0x2da1(%rip),%rcx
cmp    (%rcx),%rax
jne    1235 <main+0xb5>
lea    0xe34(%rip),%rdi
call   1030 <puts@plt>
xor    %r14d,%r14d
jmp    126d <main+0xed>
lea    0xdf2(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r14d
jmp    126d <main+0xed>
mov    0x2d80(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
lea    0xda1(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%union.sigval = type { i64 }
@SIGRTMIN = global i32 0
@_SC_SIGQUEUE_MAX = global i32 0
@.str = [46 x i8] c"Failed: sigqueue on %d of %ld max, errno: %s\0A\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.1 = [50 x i8] c"Failed: sigqueue() queued SIGQUEUE_MAX+1 signals\0A\00"
@.str.2 = [13 x i8] c"Test PASSED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca %union.sigval
store i32 0, i32* %1
%7 = call i32 (...) @getpid()
store i32 %7, i32* %2
%8 = bitcast %union.sigval* %6 to i64*
store i64 0, i64* %8
%9 = call i32 (...) @getpid()
store i32 %9, i32* %2
%10 = load i32, i32* @SIGRTMIN
%11 = call i32 @sighold(i32 %10)
%12 = call i32 (...) @reset_uid()
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %0
store i32 2, i32* %1
br label %62
16: ; preds = %0
%17 = load i32, i32* @_SC_SIGQUEUE_MAX
%18 = call i64 @sysconf(i32 %17)
store i64 %18, i64* %4
%19 = load i64, i64* %4
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
br label %60
22: ; preds = %16
store i32 0, i32* %3
br label %23
23: ; preds = %42, %22
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = load i64, i64* %4
%27 = icmp slt i64 %25, %26
br i1 %27, label %28, label %45
28: ; preds = %23
%29 = load i32, i32* %2
%30 = load i32, i32* @SIGRTMIN
%31 = getelementptr inbounds %union.sigval, %union.sigval* %6, i32 0, i32 0
%32 = load i64, i64* %31
%33 = call i32 @sigqueue(i32 %29, i32 %30, i64 %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %28
%36 = load i32, i32* %3
%37 = load i64, i64* %4
%38 = load i64, i64* @errno
%39 = call i32 @strerror(i64 %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %36, i64 %37, i32 %39)
store i32 2, i32* %1
br label %62
41: ; preds = %28
br label %42
42: ; preds = %41
%43 = load i32, i32* %3
%44 = add nsw i32 %43, 1
store i32 %44, i32* %3
br label %23
45: ; preds = %23
%46 = load i32, i32* %2
%47 = load i32, i32* @SIGRTMIN
%48 = getelementptr inbounds %union.sigval, %union.sigval* %6, i32 0, i32 0
%49 = load i64, i64* %48
%50 = call i32 @sigqueue(i32 %46, i32 %47, i64 %49)
store i32 %50, i32* %5
%51 = load i32, i32* %5
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %57
53: ; preds = %45
%54 = load i64, i64* @errno
%55 = load i64, i64* @EAGAIN
%56 = icmp eq i64 %54, %55
br i1 %56, label %59, label %57
57: ; preds = %53, %45
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %62
59: ; preds = %53
br label %60
60: ; preds = %59, %21
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %62
62: ; preds = %60, %57, %35, %15
%63 = load i32, i32* %1
ret i32 %63
}

LLVM IR (-O2):
@SIGRTMIN = global i32 0
@_SC_SIGQUEUE_MAX = global i32 0
@.str = [46 x i8] c"Failed: sigqueue on %d of %ld max, errno: %s\0A\00"
@errno = global i64 0
@EAGAIN = global i64 0
@str = [49 x i8] c"Failed: sigqueue() queued SIGQUEUE_MAX+1 signals\00"
@str.3 = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (...) @getpid()
%3 = load i32, i32* @SIGRTMIN
%4 = tail call i32 @sighold(i32 %3)
%5 = tail call i32 (...) @reset_uid()
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %38
7: ; preds = %0
%8 = load i32, i32* @_SC_SIGQUEUE_MAX
%9 = tail call i64 @sysconf(i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %36, label %11
11: ; preds = %7
%12 = icmp eq i64 %9, 0
br i1 %12, label %26, label %13
13: ; preds = %11, %23
%14 = phi i64 [ %24, %23 ], [ 0, %11 ]
%15 = load i32, i32* @SIGRTMIN
%16 = tail call i32 @sigqueue(i32 %2, i32 %15, i64 0)
%17 = icmp eq i32 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = trunc i64 %14 to i32
%20 = load i64, i64* @errno
%21 = tail call i32 @strerror(i64 %20)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %19, i64 %9, i32 %21)
br label %38
23: ; preds = %13
%24 = add nuw nsw i64 %14, 1
%25 = icmp eq i64 %24, %9
br i1 %25, label %26, label %13
26: ; preds = %23, %11
%27 = load i32, i32* @SIGRTMIN
%28 = tail call i32 @sigqueue(i32 %2, i32 %27, i64 0)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i64, i64* @errno
%32 = load i64, i64* @EAGAIN
%33 = icmp eq i64 %31, %32
br i1 %33, label %36, label %34
34: ; preds = %30, %26
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
br label %38
36: ; preds = %30, %7
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.3, i64 0, i64 0))
br label %38
38: ; preds = %0, %36, %34, %18
%39 = phi i32 [ 0, %36 ], [ 2, %18 ], [ 1, %34 ], [ 2, %0 ]
ret i32 %39
}