#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_RDWR;
int /*<<< orphan*/ WDIOC_KEEPALIVE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main(void) {
  int ii = 10, j;
  int fd = open("/dev/watchdog", O_RDWR);
  if (fd == -1) {
    perror("watchdog");
    exit(EXIT_FAILURE);
  }
  printf("Open watchdog ok\n");
  while (1) {
    printf("ii = %d\n", ii);
    if (ii-- > 0)
      ioctl(fd, WDIOC_KEEPALIVE, 0);
    sleep(1);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xa,-0x8(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
lea    0xe7a(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11b2 <main+0x52>
lea    0xe66(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
jle    11f8 <main+0x98>
mov    -0x10(%rbp),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <ioctl@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11c0 <main+0x60>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11eb <main+0x7b>
mov    %eax,%ebx
lea    0xe7d(%rip),%rdi
call   1030 <puts@plt>
mov    $0xa,%ebp
lea    0xe63(%rip),%r14
mov    0x2e2e(%rip),%r15
jmp    11cd <main+0x5d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0x1,%edi
call   1090 <sleep@plt>
add    $0xffffffff,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebp,%ebp
jle    11c0 <main+0x50>
mov    (%r15),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <ioctl@plt>
jmp    11c0 <main+0x50>
lea    0xe13(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/dev/watchdog\00"
@O_RDWR = global i32 0
@.str.1 = [9 x i8] c"watchdog\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [18 x i8] c"Open watchdog ok\0A\00"
@.str.3 = [9 x i8] c"ii = %d\0A\00"
@WDIOC_KEEPALIVE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 10, i32* %2
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %25
%16 = load i32, i32* %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %2
%19 = add nsw i32 %18, -1
store i32 %19, i32* %2
%20 = icmp sgt i32 %18, 0
br i1 %20, label %21, label %25
21: ; preds = %15
%22 = load i32, i32* %4
%23 = load i32, i32* @WDIOC_KEEPALIVE
%24 = call i32 @ioctl(i32 %22, i32 %23, i32 0)
br label %25
25: ; preds = %21, %15
%26 = call i32 @sleep(i32 1)
br label %15
}

LLVM IR (-O2):
@.str = [14 x i8] c"/dev/watchdog\00"
@O_RDWR = global i32 0
@.str.1 = [9 x i8] c"watchdog\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [9 x i8] c"ii = %d\0A\00"
@WDIOC_KEEPALIVE = global i32 0
@str = [17 x i8] c"Open watchdog ok\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%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 %4, label %8
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %10
10: ; preds = %18, %8
%11 = phi i32 [ 10, %8 ], [ %13, %18 ]
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = add nsw i32 %11, -1
%14 = icmp sgt i32 %11, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* @WDIOC_KEEPALIVE
%17 = tail call i32 @ioctl(i32 %2, i32 %16, i32 0)
br label %18
18: ; preds = %15, %10
%19 = tail call i32 @sleep(i32 1)
br label %10
}