#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*/ WDIOC_GETTIMEOUT;
int /*<<< orphan*/ WDIOC_KEEPALIVE;
int /*<<< orphan*/ WDIOC_SETTIMEOUT;
int atoi(char const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ help_info();
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, const char *argv[]) {
  int fd, timeout, sleep_sec, test;
  if (argc < 2) {
    help_info();
    return 1;
  }
  timeout = atoi(argv[1]);
  sleep_sec = atoi(argv[2]);
  if (sleep_sec <= 0) {
    sleep_sec = 1;
    printf("correct 0 or negative sleep time to %d seconds\n", sleep_sec);
  }
  test = atoi(argv[3]);
  printf("Starting wdt_driver (timeout: %d, sleep: %d, test: %s)\n", timeout,
         sleep_sec, (test == 0) ? "ioctl" : "write");
  fd = open("/dev/watchdog", O_WRONLY);
  if (fd == -1) {
    perror("watchdog");
    exit(1);
  }
  printf("Trying to set timeout value=%d seconds\n", timeout);
  ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
  printf("The actual timeout was set to %d seconds\n", timeout);
  ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
  printf("Now reading back -- The timeout is %d seconds\n", timeout);
  while (1) {
    if (test == 0) {
      ioctl(fd, WDIOC_KEEPALIVE, 0);
    } else {
      write(fd, "\0", 1);
    }
    sleep(sleep_sec);
  }
  return 0;
}


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)
cmpl   $0x2,-0x8(%rbp)
jge    11c2 <main+0x32>
mov    $0x0,%al
call   1060 <help_info@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     1204 <main+0x74>
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%edi
lea    0xe4a(%rip),%rcx
lea    0xe3d(%rip),%rax
cmp    $0x0,%edi
cmove  %rax,%rcx
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
lea    0xe24(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1278 <main+0xe8>
lea    0xe10(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%esi
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d3d(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
mov    -0x18(%rbp),%esi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
mov    -0x18(%rbp),%esi
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x20(%rbp)
jne    12f9 <main+0x169>
mov    -0x14(%rbp),%edi
mov    0x2cf7(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <ioctl@plt>
jmp    130d <main+0x17d>
mov    -0x14(%rbp),%edi
lea    0xe00(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%edi
call   10b0 <sleep@plt>
jmp    12d5 <main+0x145>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jg     11ba <main+0x2a>
xor    %eax,%eax
call   1060 <help_info@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %eax,%r15d
mov    %eax,0xc(%rsp)
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
test   %eax,%eax
jg     11f6 <main+0x66>
lea    0xe1c(%rip),%rdi
mov    $0x1,%r14d
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x18(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
test   %eax,%eax
lea    0xe5e(%rip),%rax
lea    0xe5d(%rip),%rcx
cmove  %rax,%rcx
lea    0xe14(%rip),%rdi
mov    %r15d,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
lea    0xe3b(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     12ea <main+0x15a>
mov    %eax,%ebx
lea    0xe32(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%r15
mov    %ebx,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
mov    0xc(%rsp),%esi
lea    0xe2d(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
mov    0xc(%rsp),%esi
lea    0xe32(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d30(%rip),%r12
lea    0xe4c(%rip),%r15
jmp    12d7 <main+0x147>
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %r14d,%edi
call   10b0 <sleep@plt>
test   %ebp,%ebp
jne    12c0 <main+0x130>
mov    (%r12),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <ioctl@plt>
jmp    12cf <main+0x13f>
lea    0xd88(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [48 x i8] c"correct 0 or negative sleep time to %d seconds\0A\00"
@.str.1 = [56 x i8] c"Starting wdt_driver (timeout: %d, sleep: %d, test: %s)\0A\00"
@.str.2 = [6 x i8] c"ioctl\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [14 x i8] c"/dev/watchdog\00"
@O_WRONLY = global i32 0
@.str.5 = [9 x i8] c"watchdog\00"
@.str.6 = [40 x i8] c"Trying to set timeout value=%d seconds\0A\00"
@WDIOC_SETTIMEOUT = global i32 0
@.str.7 = [42 x i8] c"The actual timeout was set to %d seconds\0A\00"
@WDIOC_GETTIMEOUT = global i32 0
@.str.8 = [47 x i8] c"Now reading back -- The timeout is %d seconds\0A\00"
@WDIOC_KEEPALIVE = global i32 0
@.str.9 = [2 x i8] zeroinitializer
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (...) @help_info()
ret i32 1
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %7
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %14
store i32 1, i32* %8
%26 = load i32, i32* %8
%27 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %25, %14
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 3
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
store i32 %32, i32* %9
%33 = load i32, i32* %7
%34 = load i32, i32* %8
%35 = load i32, i32* %9
%36 = icmp eq i32 %35, 0
%37 = zext i1 %36 to i64
%38 = select i1 %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0)
%39 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i32 %33, i32 %34, i8* %38)
%40 = load i32, i32* @O_WRONLY
%41 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %40)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %28
%45 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %28
%48 = load i32, i32* %7
%49 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %6
%51 = load i32, i32* @WDIOC_SETTIMEOUT
%52 = call i32 @ioctl(i32 %50, i32 %51, i32* %7)
%53 = load i32, i32* %7
%54 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.7, i64 0, i64 0), i32 %53)
%55 = load i32, i32* %6
%56 = load i32, i32* @WDIOC_GETTIMEOUT
%57 = call i32 @ioctl(i32 %55, i32 %56, i32* %7)
%58 = load i32, i32* %7
%59 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.8, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %47, %70
%61 = load i32, i32* %9
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %60
%64 = load i32, i32* %6
%65 = load i32, i32* @WDIOC_KEEPALIVE
%66 = call i32 @ioctl(i32 %64, i32 %65, i32* null)
br label %70
67: ; preds = %60
%68 = load i32, i32* %6
%69 = call i32 @write(i32 %68, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0), i32 1)
br label %70
70: ; preds = %67, %63
%71 = load i32, i32* %8
%72 = call i32 @sleep(i32 %71)
br label %60
}

LLVM IR (-O2):
@.str = [48 x i8] c"correct 0 or negative sleep time to %d seconds\0A\00"
@.str.1 = [56 x i8] c"Starting wdt_driver (timeout: %d, sleep: %d, test: %s)\0A\00"
@.str.2 = [6 x i8] c"ioctl\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [14 x i8] c"/dev/watchdog\00"
@O_WRONLY = global i32 0
@.str.5 = [9 x i8] c"watchdog\00"
@.str.6 = [40 x i8] c"Trying to set timeout value=%d seconds\0A\00"
@WDIOC_SETTIMEOUT = global i32 0
@.str.7 = [42 x i8] c"The actual timeout was set to %d seconds\0A\00"
@WDIOC_GETTIMEOUT = global i32 0
@.str.8 = [47 x i8] c"Now reading back -- The timeout is %d seconds\0A\00"
@WDIOC_KEEPALIVE = global i32 0
@.str.9 = [2 x i8] zeroinitializer
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 (...) @help_info()
ret i32 1
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
store i32 %11, i32* %3
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 1)
br label %18
18: ; preds = %16, %8
%19 = phi i32 [ 1, %16 ], [ %14, %8 ]
%20 = getelementptr inbounds i8*, i8** %1, i64 3
%21 = load i8*, i8** %20
%22 = tail call i32 @atoi(i8* %21)
%23 = icmp eq i32 %22, 0
%24 = select i1 %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0)
%25 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %19, i8* %24)
%26 = load i32, i32* @O_WRONLY
%27 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %18
%30 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %18
%33 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i32 %11)
%34 = load i32, i32* @WDIOC_SETTIMEOUT
%35 = call i32 @ioctl(i32 %27, i32 %34, i32* %3)
%36 = load i32, i32* %3
%37 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.7, i64 0, i64 0), i32 %36)
%38 = load i32, i32* @WDIOC_GETTIMEOUT
%39 = call i32 @ioctl(i32 %27, i32 %38, i32* %3)
%40 = load i32, i32* %3
%41 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.8, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %48, %32
br i1 %23, label %43, label %46
43: ; preds = %42
%44 = load i32, i32* @WDIOC_KEEPALIVE
%45 = call i32 @ioctl(i32 %27, i32 %44, i32* null)
br label %48
46: ; preds = %42
%47 = call i32 @write(i32 %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0), i32 1)
br label %48
48: ; preds = %46, %43
%49 = call i32 @sleep(i32 %19)
br label %42
}