#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 */
struct timespec {
  int tv_sec;
  scalar_t__ tv_nsec;
};
typedef int /*<<< orphan*/ sigset_t;
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ SIG_BLOCK;
scalar_t__ errno;
int /*<<< orphan*/ fork_child();
int /*<<< orphan*/ fork_test();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
scalar_t__ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                       int /*<<< orphan*/ *);
int sigtimedwait(int /*<<< orphan*/ *, int /*<<< orphan*/ *, struct timespec *);
scalar_t__ waitpid(int /*<<< orphan*/, int /*<<< orphan*/ *,
                   int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  sigset_t mask;
  sigset_t orig_mask;
  struct timespec timeout;
  pid_t pid;
  sigemptyset(&mask);
  sigaddset(&mask, SIGCHLD);
  if (sigprocmask(SIG_BLOCK, &mask, &orig_mask) < 0) {
    perror("sigprocmask");
    return 1;
  }
  pid = fork_child();
  fork_test();
  timeout.tv_sec = 5;
  timeout.tv_nsec = 0;
  do {
    int ret = sigtimedwait(&mask, NULL, &timeout);
    printf(" ret = %d\n", ret);
    if (ret < 0) {
      printf("--> after sigtimedwait\n");
      if (errno == EINTR) {
        printf("got EINTR\n");
        continue;
      } else if (errno == EAGAIN) {
        printf("Timeout, killing child\n");
        kill(pid, SIGKILL);
      } else {
        perror("sigtimedwait");
        return 1;
      }
    }
    break;
  } while (1);
  printf("wait pid...\n");
  if (waitpid(pid, NULL, 0) < 0) {
    perror("waitpid");
    return 1;
  }
  printf("ok child pid exit\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
call   1070 <sigemptyset@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdi
call   10c0 <sigaddset@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    -0x14(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1030 <sigprocmask@plt>
cmp    $0x0,%rax
jge    1209 <main+0x69>
lea    0xe08(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    135a <main+0x1ba>
mov    $0x0,%al
call   1080 <fork_child@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1060 <fork_test@plt>
movl   $0x5,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x14(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x28(%rbp),%rdx
call   1050 <sigtimedwait@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%esi
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x30(%rbp)
jge    12f3 <main+0x153>
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%rax
mov    0x2d51(%rip),%rcx
cmp    (%rcx),%rax
jne    1293 <main+0xf3>
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12f8 <main+0x158>
mov    0x2d3e(%rip),%rax
mov    (%rax),%rax
mov    0x2d0c(%rip),%rcx
cmp    (%rcx),%rax
jne    12d1 <main+0x131>
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%edi
mov    0x2cfb(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
jmp    12e9 <main+0x149>
lea    0xd7a(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    135a <main+0x1ba>
jmp    12ee <main+0x14e>
jmp    12f3 <main+0x153>
jmp    1307 <main+0x167>
mov    $0x1,%al
test   $0x1,%al
jne    1229 <main+0x89>
jmp    1307 <main+0x167>
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <waitpid@plt>
cmp    $0x0,%rax
jge    1345 <main+0x1a5>
lea    0xd38(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    135a <main+0x1ba>
lea    0xd28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <sigemptyset@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10d0 <sigaddset@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
lea    0x24(%rsp),%rdx
mov    %rbx,%rsi
call   1030 <sigprocmask@plt>
test   %rax,%rax
js     131d <main+0x16d>
xor    %eax,%eax
call   1090 <fork_child@plt>
mov    %eax,0x8(%rsp)
xor    %eax,%eax
call   1070 <fork_test@plt>
movl   $0x5,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rdx
xor    %esi,%esi
call   1060 <sigtimedwait@plt>
mov    %eax,%ebx
lea    0xdd4(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
jns    12e0 <main+0x130>
mov    0x2d78(%rip),%r15
lea    0xe14(%rip),%r12
lea    0xc(%rsp),%r13
lea    0x10(%rsp),%rbp
lea    0xda4(%rip),%rbx
nopl   0x0(%rax,%rax,1)
lea    0xdc6(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%rax
cmp    (%r15),%rax
jne    12b6 <main+0x106>
mov    %r12,%rdi
call   1040 <puts@plt>
mov    %r13,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
call   1060 <sigtimedwait@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %r14d,%r14d
js     1270 <main+0xc0>
jmp    12e0 <main+0x130>
mov    0x2cf3(%rip),%rcx
cmp    (%rcx),%rax
jne    1339 <main+0x189>
lea    0xd8b(%rip),%rdi
call   1040 <puts@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%esi
mov    0x8(%rsp),%edi
call   10a0 <kill@plt>
lea    0xd38(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
mov    0x8(%rsp),%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <waitpid@plt>
test   %rax,%rax
js     1326 <main+0x176>
lea    0xd24(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcdc(%rip),%rdi
jmp    132d <main+0x17d>
lea    0xcea(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%ebx
jmp    130c <main+0x15c>
lea    0xd07(%rip),%rdi
jmp    132d <main+0x17d>

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@SIGCHLD = global i32 0
@SIG_BLOCK = global i32 0
@.str = [12 x i8] c"sigprocmask\00"
@.str.1 = [11 x i8] c" ret = %d\0A\00"
@.str.2 = [24 x i8] c"--> after sigtimedwait\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [11 x i8] c"got EINTR\0A\00"
@EAGAIN = global i64 0
@.str.4 = [24 x i8] c"Timeout, killing child\0A\00"
@SIGKILL = global i32 0
@.str.5 = [13 x i8] c"sigtimedwait\00"
@.str.6 = [13 x i8] c"wait pid...\0A\00"
@.str.7 = [8 x i8] c"waitpid\00"
@.str.8 = [19 x i8] c"ok child pid exit\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timespec
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 @sigemptyset(i32* %6)
%12 = load i32, i32* @SIGCHLD
%13 = call i32 @sigaddset(i32* %6, i32 %12)
%14 = load i32, i32* @SIG_BLOCK
%15 = call i64 @sigprocmask(i32 %14, i32* %6, i32* %7)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %61
19: ; preds = %2
%20 = call i32 (...) @fork_child()
store i32 %20, i32* %9
%21 = call i32 (...) @fork_test()
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
store i32 5, i32* %22
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
store i64 0, i64* %23
br label %24
24: ; preds = %51, %19
%25 = call i32 @sigtimedwait(i32* %6, i32* null, %struct.timespec* %8)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %10
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %50
30: ; preds = %24
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINTR
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %51
37: ; preds = %30
%38 = load i64, i64* @errno
%39 = load i64, i64* @EAGAIN
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %46
41: ; preds = %37
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%43 = load i32, i32* %9
%44 = load i32, i32* @SIGKILL
%45 = call i32 @kill(i32 %43, i32 %44)
br label %48
46: ; preds = %37
%47 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %61
48: ; preds = %41
br label %49
49: ; preds = %48
br label %50
50: ; preds = %49, %24
br label %52
51: ; preds = %35
br i1 true, label %24, label %52
52: ; preds = %51, %50
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%54 = load i32, i32* %9
%55 = call i64 @waitpid(i32 %54, i32* null, i32 0)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %61
59: ; preds = %52
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %3
br label %61
61: ; preds = %59, %57, %46, %17
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@SIGCHLD = global i32 0
@SIG_BLOCK = global i32 0
@.str = [12 x i8] c"sigprocmask\00"
@.str.1 = [11 x i8] c" ret = %d\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@SIGKILL = global i32 0
@.str.5 = [13 x i8] c"sigtimedwait\00"
@.str.7 = [8 x i8] c"waitpid\00"
@str = [12 x i8] c"wait pid...\00"
@str.9 = [18 x i8] c"ok child pid exit\00"
@str.10 = [23 x i8] c"--> after sigtimedwait\00"
@str.11 = [23 x i8] c"Timeout, killing child\00"
@str.12 = [10 x i8] c"got EINTR\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timespec
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast %struct.timespec* %5 to i8*
%9 = call i32 @sigemptyset(i32* %3)
%10 = load i32, i32* @SIGCHLD
%11 = call i32 @sigaddset(i32* %3, i32 %10)
%12 = load i32, i32* @SIG_BLOCK
%13 = call i64 @sigprocmask(i32 %12, i32* %3, i32* %4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %52
17: ; preds = %2
%18 = call i32 (...) @fork_child()
%19 = call i32 (...) @fork_test()
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
store i32 5, i32* %20
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
store i64 0, i64* %21
%22 = call i32 @sigtimedwait(i32* %3, i32* null, %struct.timespec* %5)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = icmp slt i32 %22, 0
br i1 %24, label %25, label %44
25: ; preds = %17, %39
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.10, i64 0, i64 0))
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br i1 %29, label %39, label %30
30: ; preds = %25
%31 = load i64, i64* @EAGAIN
%32 = icmp eq i64 %27, %31
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.11, i64 0, i64 0))
%35 = load i32, i32* @SIGKILL
%36 = call i32 @kill(i32 %18, i32 %35)
br label %44
37: ; preds = %30
%38 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %52
39: ; preds = %25
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.12, i64 0, i64 0))
%41 = call i32 @sigtimedwait(i32* %3, i32* null, %struct.timespec* %5)
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %41)
%43 = icmp slt i32 %41, 0
br i1 %43, label %25, label %44
44: ; preds = %39, %17, %33
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%46 = call i64 @waitpid(i32 %18, i32* null, i32 0)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
br label %52
50: ; preds = %44
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.9, i64 0, i64 0))
br label %52
52: ; preds = %37, %50, %48, %15
%53 = phi i32 [ 1, %15 ], [ 1, %48 ], [ 0, %50 ], [ 1, %37 ]
ret i32 %53
}