#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
union sigval {
  scalar_t__ sival_int;
};
typedef int /*<<< orphan*/ sigset_t;
struct TYPE_3__ {
  int si_signo;
};
typedef TYPE_1__ siginfo_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGRTMIN;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ sigqueue(scalar_t__, int /*<<< orphan*/, union sigval);
int sigwaitinfo(int /*<<< orphan*/ *, TYPE_1__ *);
int /*<<< orphan*/ timestamp(char *);
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
int main() {
  int result = 0;
  sigset_t waitset;
  siginfo_t info;
  pid_t cpid;
  union sigval val;
  sigemptyset(&waitset);
  sigaddset(&waitset, SIGRTMIN);
  sigprocmask(SIG_BLOCK, &waitset, NULL);
  cpid = fork();
  if (cpid == 0) {
    printf("child pid %d\n", getpid());
    timestamp("before sigwaitinfo()");
    result = sigwaitinfo(&waitset, &info);
    if (result < 0)
      printf("sigwaitinfo failed : \n");
    printf("sigwaitinfo() returned for signal %d\n", info.si_signo);
    timestamp("after sigwaitinfo()");
    exit(0);
  } else {
    val.sival_int = 0;
    sigqueue(cpid, SIGRTMIN, val);
    waitpid(cpid, NULL, 0);
    while (1)
      ;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <sigemptyset@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdi
call   10c0 <sigaddset@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    127c <main+0xcc>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xde3(%rip),%rdi
call   1050 <timestamp@plt>
lea    -0xc(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1080 <sigwaitinfo@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1258 <main+0xa8>
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xdf0(%rip),%rdi
call   1050 <timestamp@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
call   1090 <sigqueue@plt>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <waitpid@plt>
jmp    12a9 <main+0xf9>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <sigemptyset@plt>
mov    0x2e08(%rip),%r14
mov    (%r14),%esi
mov    %rbx,%rdi
call   10d0 <sigaddset@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
je     1222 <main+0x62>
mov    %rax,%rbx
mov    (%r14),%esi
mov    %rax,%rdi
xor    %edx,%edx
call   10a0 <sigqueue@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <waitpid@plt>
xchg   %ax,%ax
jmp    1220 <main+0x60>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xdd0(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xdce(%rip),%rdi
call   1060 <timestamp@plt>
lea    0x4(%rsp),%rdi
mov    %rsp,%rsi
call   1090 <sigwaitinfo@plt>
test   %eax,%eax
jns    1262 <main+0xa2>
lea    0xe00(%rip),%rdi
call   1040 <puts@plt>
mov    (%rsp),%esi
lea    0xdb7(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xdcf(%rip),%rdi
call   1060 <timestamp@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32 }
%union.sigval = type { i64 }
@SIGRTMIN = global i32 0
@SIG_BLOCK = global i32 0
@.str = [14 x i8] c"child pid %d\0A\00"
@.str.1 = [21 x i8] c"before sigwaitinfo()\00"
@.str.2 = [23 x i8] c"sigwaitinfo failed : \0A\00"
@.str.3 = [38 x i8] c"sigwaitinfo() returned for signal %d\0A\00"
@.str.4 = [20 x i8] c"after sigwaitinfo()\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.TYPE_3__
%5 = alloca i64
%6 = alloca %union.sigval
store i32 0, i32* %1
store i32 0, i32* %2
%7 = call i32 @sigemptyset(i32* %3)
%8 = load i32, i32* @SIGRTMIN
%9 = call i32 @sigaddset(i32* %3, i32 %8)
%10 = load i32, i32* @SIG_BLOCK
%11 = call i32 @sigprocmask(i32 %10, i32* %3, i32* null)
%12 = call i64 (...) @fork()
store i64 %12, i64* %5
%13 = load i64, i64* %5
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %30
15: ; preds = %0
%16 = call i32 (...) @getpid()
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = call i32 @timestamp(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @sigwaitinfo(i32* %3, %struct.TYPE_3__* %4)
store i32 %19, i32* %2
%20 = load i32, i32* %2
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %15
%25 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %4, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = call i32 @timestamp(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %0
%31 = bitcast %union.sigval* %6 to i64*
store i64 0, i64* %31
%32 = load i64, i64* %5
%33 = load i32, i32* @SIGRTMIN
%34 = getelementptr inbounds %union.sigval, %union.sigval* %6, i32 0, i32 0
%35 = load i64, i64* %34
%36 = call i32 @sigqueue(i64 %32, i32 %33, i64 %35)
%37 = load i64, i64* %5
%38 = call i32 @waitpid(i64 %37, i32* null, i32 0)
br label %39
39: ; preds = %30, %39
br label %39
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32 }
@SIGRTMIN = global i32 0
@SIG_BLOCK = global i32 0
@.str = [14 x i8] c"child pid %d\0A\00"
@.str.1 = [21 x i8] c"before sigwaitinfo()\00"
@.str.3 = [38 x i8] c"sigwaitinfo() returned for signal %d\0A\00"
@.str.4 = [20 x i8] c"after sigwaitinfo()\00"
@str = [22 x i8] c"sigwaitinfo failed : \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.TYPE_3__
%3 = bitcast i32* %1 to i8*
%4 = bitcast %struct.TYPE_3__* %2 to i8*
%5 = call i32 @sigemptyset(i32* %1)
%6 = load i32, i32* @SIGRTMIN
%7 = call i32 @sigaddset(i32* %1, i32 %6)
%8 = load i32, i32* @SIG_BLOCK
%9 = call i32 @sigprocmask(i32 %8, i32* %1, i32* null)
%10 = call i64 (...) @fork()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %26
12: ; preds = %0
%13 = call i32 (...) @getpid()
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = call i32 @timestamp(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @sigwaitinfo(i32* %1, %struct.TYPE_3__* %2)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %20
20: ; preds = %18, %12
%21 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %2, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = call i32 @timestamp(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %0
%27 = load i32, i32* @SIGRTMIN
%28 = call i32 @sigqueue(i64 %10, i32 %27, i64 0)
%29 = call i32 @waitpid(i64 %10, i32* null, i32 0)
br label %30
30: ; preds = %30, %26
br label %30
}