#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 */

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ exit(int);
int fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, const char *argv[]) {
  int pid;
  int fd = open("/dev/watchdog", O_WRONLY);
  if (fd == -1) {
    perror("watchdog");
    exit(1);
  }
  while (1) {
    pid = fork();
    if (pid)
      wait(NULL);
    else {
      write(fd, "\0", 1);
      printf("1\n");
      exit(0);
    }
    sleep(10);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    0xe5a(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11ce <main+0x4e>
lea    0xe46(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11d3 <main+0x53>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11f5 <main+0x75>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
jmp    121c <main+0x9c>
mov    -0x18(%rbp),%edi
lea    0xe12(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
lea    0xdfe(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
jmp    11d3 <main+0x53>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
lea    0xe6f(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
jne    11b1 <main+0x31>
lea    0xe63(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    %eax,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11de <main+0x5e>
xor    %edi,%edi
call   1090 <wait@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
jmp    11c0 <main+0x40>
lea    0xe2b(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
lea    0xe16(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/dev/watchdog\00"
@O_WRONLY = global i32 0
@.str.1 = [9 x i8] c"watchdog\00"
@.str.2 = [2 x i8] zeroinitializer
@.str.3 = [3 x i8] c"1\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @O_WRONLY
%9 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
br label %16
16: ; preds = %15, %27
%17 = call i32 (...) @fork()
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @wait(i32* null)
br label %27
22: ; preds = %16
%23 = load i32, i32* %7
%24 = call i32 @write(i32 %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%25 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %20
%28 = call i32 @sleep(i32 10)
br label %16
}

LLVM IR (-O2):
@.str = [14 x i8] c"/dev/watchdog\00"
@O_WRONLY = global i32 0
@.str.1 = [9 x i8] c"watchdog\00"
@.str.2 = [2 x i8] zeroinitializer
@str = [2 x i8] c"1\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_WRONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 (...) @fork()
%8 = icmp eq i32 %7, 0
br i1 %8, label %17, label %12
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %6, %12
%13 = tail call i32 @wait(i32* null)
%14 = tail call i32 @sleep(i32 10)
%15 = tail call i32 (...) @fork()
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %12
17: ; preds = %12, %6
%18 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 0)
unreachable
}