#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ getppid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ read(int /*<<< orphan*/, char **, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int happy = 1;
  while (1) {
    struct timeval timout;
    fd_set readset, exceptset;
    if (happy) {
      timout.tv_sec = 5;
      timout.tv_usec = 0;
    } else {
      timout.tv_sec = 2;
      timout.tv_usec = 0;
    }
    FD_ZERO(&readset);
    FD_SET(0, &readset);
    FD_ZERO(&exceptset);
    FD_SET(0, &exceptset);
    select(1, &readset, 0, &exceptset, &timout);
    if (FD_ISSET(0, &exceptset))
      return (0);
    if (FD_ISSET(0, &readset)) {
      char buf[100];
      happy = 1;
      if (read(0, &buf, 100) <= 0)
        return (0);
      else
        continue;
    }
    happy = 0;
    kill(getppid(), SIGHUP);
    fprintf(stderr, "watchdog: signaling pd...\n");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11be <main+0x3e>
movl   $0x5,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
jmp    11cd <main+0x4d>
movl   $0x2,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x2c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x2c(%rbp),%rsi
call   10a0 <FD_SET@plt>
lea    -0x30(%rbp),%rdi
call   1070 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x30(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    $0x1,%edi
lea    -0x2c(%rbp),%rsi
xor    %edx,%edx
lea    -0x30(%rbp),%rcx
lea    -0x28(%rbp),%r8
call   1080 <select@plt>
xor    %edi,%edi
lea    -0x30(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     122e <main+0xae>
movl   $0x0,-0x4(%rbp)
jmp    12b0 <main+0x130>
xor    %edi,%edi
lea    -0x2c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1278 <main+0xf8>
movl   $0x1,-0x14(%rbp)
lea    -0xa0(%rbp),%rsi
xor    %edi,%edi
mov    $0x64,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jg     1273 <main+0xf3>
movl   $0x0,-0x4(%rbp)
jmp    12b0 <main+0x130>
jmp    11a0 <main+0x20>
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1090 <getppid@plt>
mov    %eax,%edi
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
lea    0xd5a(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11a0 <main+0x20>
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    $0x1,%ebp
lea    0xc(%rsp),%r14
lea    0x8(%rsp),%rbx
lea    0x10(%rsp),%r15
lea    0xe54(%rip),%r12
lea    0x20(%rsp),%r13
jmp    11ea <main+0x6a>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %ebp,%ebp
xor    %eax,%eax
call   1090 <getppid@plt>
mov    0x2e20(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1060 <kill@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
test   %ebp,%ebp
setne  %al
lea    0x2(%rax,%rax,2),%eax
mov    %eax,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    %r14,%rdi
call   1070 <FD_ZERO@plt>
xor    %edi,%edi
mov    %r14,%rsi
call   10a0 <FD_SET@plt>
mov    %rbx,%rdi
call   1070 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   10a0 <FD_SET@plt>
mov    $0x1,%edi
mov    %r14,%rsi
xor    %edx,%edx
mov    %rbx,%rcx
mov    %r15,%r8
call   1080 <select@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
jne    127a <main+0xfa>
xor    %edi,%edi
mov    %r14,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11c0 <main+0x40>
xor    %edi,%edi
mov    %r13,%rsi
mov    $0x64,%edx
call   1040 <read@plt>
mov    $0x1,%ebp
test   %rax,%rax
jg     11ea <main+0x6a>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@SIGHUP = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"watchdog: signaling pd...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = alloca i32
%10 = alloca [100 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %6
br label %11
11: ; preds = %2, %37, %38
%12 = load i32, i32* %6
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i32 5, i32* %15
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i64 0, i64* %16
br label %20
17: ; preds = %11
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i32 2, i32* %18
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i64 0, i64* %19
br label %20
20: ; preds = %17, %14
%21 = call i32 @FD_ZERO(i32* %8)
%22 = call i32 @FD_SET(i32 0, i32* %8)
%23 = call i32 @FD_ZERO(i32* %9)
%24 = call i32 @FD_SET(i32 0, i32* %9)
%25 = call i32 @select(i32 1, i32* %8, i32 0, i32* %9, %struct.timeval* %7)
%26 = call i64 @FD_ISSET(i32 0, i32* %9)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %20
store i32 0, i32* %3
br label %44
29: ; preds = %20
%30 = call i64 @FD_ISSET(i32 0, i32* %8)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %29
store i32 1, i32* %6
%33 = bitcast [100 x i8]* %10 to i8**
%34 = call i64 @read(i32 0, i8** %33, i32 100)
%35 = icmp sle i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %32
store i32 0, i32* %3
br label %44
37: ; preds = %32
br label %11
38: ; preds = %29
store i32 0, i32* %6
%39 = call i32 (...) @getppid()
%40 = load i32, i32* @SIGHUP
%41 = call i32 @kill(i32 %39, i32 %40)
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %11
44: ; preds = %36, %28
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@SIGHUP = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"watchdog: signaling pd...\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = alloca i32
%6 = alloca [100 x i8]
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%8 = bitcast %struct.timeval* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = bitcast i32* %5 to i8*
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%13 = bitcast [100 x i8]* %6 to i8**
br label %14
14: ; preds = %37, %2
%15 = phi i32 [ 1, %2 ], [ %38, %37 ]
%16 = icmp eq i32 %15, 0
%17 = select i1 %16, i32 2, i32 5
store i32 %17, i32* %11
store i64 0, i64* %12
%18 = call i32 @FD_ZERO(i32* %4)
%19 = call i32 @FD_SET(i32 0, i32* %4)
%20 = call i32 @FD_ZERO(i32* %5)
%21 = call i32 @FD_SET(i32 0, i32* %5)
%22 = call i32 @select(i32 1, i32* %4, i32 0, i32* %5, %struct.timeval* %3)
%23 = call i64 @FD_ISSET(i32 0, i32* %5)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %39
25: ; preds = %14
%26 = call i64 @FD_ISSET(i32 0, i32* %4)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = call i64 @read(i32 0, i8** %13, i32 100)
%30 = icmp slt i64 %29, 1
br i1 %30, label %39, label %37
31: ; preds = %25
%32 = call i32 (...) @getppid()
%33 = load i32, i32* @SIGHUP
%34 = call i32 @kill(i32 %32, i32 %33)
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %37
37: ; preds = %28, %31
%38 = phi i32 [ 1, %28 ], [ 0, %31 ]
br label %14
39: ; preds = %28, %14
ret i32 0
}