#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*/ 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*/ *);
scalar_t__ 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();
  timeout.tv_sec = 5;
  timeout.tv_nsec = 0;
  do {
    if (sigtimedwait(&mask, NULL, &timeout) < 0) {
      if (errno == EINTR) {
        continue;
      } else if (errno == EAGAIN) {
        printf("Timeout, killing child\n");
        kill(pid, SIGKILL);
      } else {
        perror("sigtimedwait");
        return 1;
      }
    }
    break;
  } while (1);
  if (waitpid(pid, NULL, 0) < 0) {
    perror("waitpid");
    return 1;
  }
  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   1060 <sigemptyset@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdi
call   10b0 <sigaddset@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    -0x14(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1030 <sigprocmask@plt>
cmp    $0x0,%rax
jge    11f9 <main+0x69>
lea    0xe18(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f5 <main+0x165>
mov    $0x0,%al
call   1070 <fork_child@plt>
mov    %eax,-0x2c(%rbp)
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>
cmp    $0x0,%rax
jge    12aa <main+0x11a>
mov    0x2da4(%rip),%rax
mov    (%rax),%rax
mov    0x2d8a(%rip),%rcx
cmp    (%rcx),%rax
jne    124c <main+0xbc>
jmp    12af <main+0x11f>
mov    0x2d85(%rip),%rax
mov    (%rax),%rax
mov    0x2d53(%rip),%rcx
cmp    (%rcx),%rax
jne    1288 <main+0xf8>
lea    0xd9f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x2c(%rbp),%edi
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
jmp    12a0 <main+0x110>
lea    0xd95(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f5 <main+0x165>
jmp    12a5 <main+0x115>
jmp    12aa <main+0x11a>
jmp    12be <main+0x12e>
mov    $0x1,%al
test   $0x1,%al
jne    1212 <main+0x82>
jmp    12be <main+0x12e>
mov    -0x2c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <waitpid@plt>
cmp    $0x0,%rax
jge    12ee <main+0x15e>
lea    0xd54(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f5 <main+0x165>
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   %r12
push   %rbx
sub    $0x20,%rsp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <sigemptyset@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10b0 <sigaddset@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rdx
mov    %rbx,%rsi
call   1030 <sigprocmask@plt>
test   %rax,%rax
js     1276 <main+0xe6>
xor    %eax,%eax
call   1070 <fork_child@plt>
mov    %eax,%r14d
movl   $0x5,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%rbp
mov    0x2dd3(%rip),%r12
mov    0x2dbc(%rip),%rbx
nopl   0x0(%rax)
mov    %r15,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
call   1050 <sigtimedwait@plt>
test   %rax,%rax
jns    1254 <main+0xc4>
mov    (%r12),%rax
cmp    (%rbx),%rax
je     1210 <main+0x80>
mov    0x2d7e(%rip),%rcx
cmp    (%rcx),%rax
jne    1288 <main+0xf8>
lea    0xde3(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1080 <kill@plt>
xor    %ebx,%ebx
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <waitpid@plt>
test   %rax,%rax
js     127f <main+0xef>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd83(%rip),%rdi
jmp    128f <main+0xff>
lea    0xd93(%rip),%rdi
jmp    128f <main+0xff>
lea    0xd7d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%ebx
jmp    1267 <main+0xd7>

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

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@SIGCHLD = global i32 0
@SIG_BLOCK = global i32 0
@.str = [12 x i8] c"sigprocmask\00"
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@SIGKILL = global i32 0
@.str.2 = [13 x i8] c"sigtimedwait\00"
@.str.3 = [8 x i8] c"waitpid\00"
@str = [23 x i8] c"Timeout, killing child\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 %36, label %15
15: ; preds = %2
%16 = call i32 (...) @fork_child()
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
store i32 5, i32* %17
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
store i64 0, i64* %18
br label %19
19: ; preds = %22, %15
%20 = call i64 @sigtimedwait(i32* %3, i32* null, %struct.timespec* %5)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %33
22: ; preds = %19
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %19, label %26
26: ; preds = %22
%27 = load i64, i64* @EAGAIN
%28 = icmp eq i64 %23, %27
br i1 %28, label %29, label %36
29: ; preds = %26
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%31 = load i32, i32* @SIGKILL
%32 = call i32 @kill(i32 %16, i32 %31)
br label %33
33: ; preds = %19, %29
%34 = call i64 @waitpid(i32 %16, i32* null, i32 0)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33, %26, %2
%37 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), %26 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), %33 ]
%38 = call i32 @perror(i8* %37)
br label %39
39: ; preds = %36, %33
%40 = phi i32 [ 0, %33 ], [ 1, %36 ]
ret i32 %40
}