#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*/ EXIT_FAILURE;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fsync(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int write(int, char *, int);
int main(void) {
  int fd = open("/dev/watchdog", O_WRONLY);
  int ret = 0;
  if (fd == -1) {
    perror("watchdog");
    exit(EXIT_FAILURE);
  }
  while (1) {
    ret = write(fd, "\0", 1);
    if (ret != 1) {
      ret = -1;
      break;
    }
    ret = fsync(fd);
    if (ret)
      break;
    sleep(10);
  }
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11c2 <main+0x52>
lea    0xe56(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11c7 <main+0x57>
mov    -0x8(%rbp),%edi
lea    0xe3d(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
je     11f4 <main+0x84>
movl   $0xffffffff,-0xc(%rbp)
jmp    121d <main+0xad>
mov    -0x8(%rbp),%edi
call   1050 <fsync@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     120e <main+0x9e>
jmp    121d <main+0xad>
mov    $0xa,%edi
call   1090 <sleep@plt>
jmp    11c7 <main+0x57>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
lea    0xe7c(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11fd <main+0x8d>
mov    %eax,%ebx
lea    0xe77(%rip),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0xffffffff,%r14d
cmp    $0x1,%eax
jne    11ee <main+0x7e>
lea    0xe59(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
call   1050 <fsync@plt>
test   %eax,%eax
jne    11eb <main+0x7b>
mov    $0xa,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     11c0 <main+0x50>
jmp    11ee <main+0x7e>
mov    %eax,%r14d
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe01(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1080 <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"
@EXIT_FAILURE = global i32 0
@.str.2 = [2 x i8] zeroinitializer
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @O_WRONLY
%5 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
store i32 0, i32* %3
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
br label %13
13: ; preds = %12, %25
%14 = load i32, i32* %2
%15 = call i32 @write(i32 %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp ne i32 %16, 1
br i1 %17, label %18, label %19
18: ; preds = %13
store i32 -1, i32* %3
br label %27
19: ; preds = %13
%20 = load i32, i32* %2
%21 = call i32 @fsync(i32 %20)
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
br label %27
25: ; preds = %19
%26 = call i32 @sleep(i32 10)
br label %13
27: ; preds = %24, %18
%28 = load i32, i32* %2
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %3
ret i32 %30
}

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