#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 atoi(char *);
int /*<<< orphan*/ close_keyboard();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ init_keyboard();
scalar_t__ kbhit();
int /*<<< orphan*/ printf(char *, ...);
int read_wdt();
int readch();
int /*<<< orphan*/ set_wdt_min();
int /*<<< orphan*/ set_wdt_sec();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write_wdt(int);
main(int argc, char *argv[]) {
  int timeout, key;
  if (argc != 3) {
    printf("Usage: timer <time> <sec/min>\n");
    printf("Example: timer 100 sec\n");
    exit(1);
  } else if (read_wdt() < 0) {
    fprintf(stderr, "Can't access device WDT - Aborting\n");
    exit(1);
  } else {
    printf("Test program WinSystems Watchdog Timer\n");
    timeout = atoi(argv[1]);
    printf("  Settings: %d %s\n", timeout, argv[2]);
    printf("  Hit any key to start program\n");
    init_keyboard();
  }
  while (!kbhit())
    ;
  readch();
  if (*argv[2] == 's')
    set_wdt_sec();
  else
    set_wdt_min();
  write_wdt(timeout);
  while (1) {
    printf("  ***** Current count = %d *****\n", read_wdt());
    if (kbhit()) {
      key = readch();
      if (key == 'q') {
        write_wdt(0);
        close_keyboard();
        exit(0);
      } else
        write_wdt(timeout);
    }
    usleep(1000000);
  }
}


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   $0x3,-0x8(%rbp)
je     1216 <main+0x46>
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   10a0 <read_wdt@plt>
cmp    $0x0,%eax
jge    1245 <main+0x75>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1090 <init_keyboard@plt>
jmp    1296 <main+0xc6>
jmp    129b <main+0xcb>
mov    $0x0,%al
call   1040 <kbhit@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12b8 <main+0xe8>
jmp    12bd <main+0xed>
jmp    129b <main+0xcb>
mov    $0x0,%al
call   10e0 <readch@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x73,%eax
jne    12e4 <main+0x114>
mov    $0x0,%al
call   1070 <set_wdt_sec@plt>
jmp    12eb <main+0x11b>
mov    $0x0,%al
call   1050 <set_wdt_min@plt>
mov    -0x14(%rbp),%edi
call   1060 <write_wdt@plt>
mov    $0x0,%al
call   10a0 <read_wdt@plt>
mov    %eax,%esi
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <kbhit@plt>
cmp    $0x0,%rax
je     1351 <main+0x181>
mov    $0x0,%al
call   10e0 <readch@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x71,-0x18(%rbp)
jne    1344 <main+0x174>
xor    %edi,%edi
call   1060 <write_wdt@plt>
mov    $0x0,%al
call   10b0 <close_keyboard@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <write_wdt@plt>
jmp    1351 <main+0x181>
mov    $0xf4240,%edi
call   10f0 <usleep@plt>
jmp    12f3 <main+0x123>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x3,%edi
jne    1216 <main+0x36>
mov    %rsi,%r14
xor    %eax,%eax
call   10b0 <read_wdt@plt>
test   %eax,%eax
jns    1238 <main+0x58>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xe82(%rip),%rdi
call   1030 <puts@plt>
lea    0xe94(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xe1a(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%r14),%rdi
call   10d0 <atoi@plt>
mov    %eax,%ebp
mov    0x10(%r14),%rdx
lea    0xdca(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe16(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10a0 <init_keyboard@plt>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <kbhit@plt>
test   %rax,%rax
je     1280 <main+0xa0>
xor    %eax,%eax
call   10f0 <readch@plt>
mov    0x10(%r14),%rax
cmpb   $0x73,(%rax)
jne    12a5 <main+0xc5>
xor    %eax,%eax
call   1080 <set_wdt_sec@plt>
jmp    12ac <main+0xcc>
xor    %eax,%eax
call   1060 <set_wdt_min@plt>
mov    %ebp,%edi
call   1070 <write_wdt@plt>
lea    0xd7d(%rip),%rbx
jmp    12ca <main+0xea>
nopl   0x0(%rax)
mov    $0xf4240,%edi
call   1100 <usleep@plt>
xor    %eax,%eax
call   10b0 <read_wdt@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <kbhit@plt>
test   %rax,%rax
je     12c0 <main+0xe0>
xor    %eax,%eax
call   10f0 <readch@plt>
cmp    $0x71,%eax
je     12fe <main+0x11e>
mov    %ebp,%edi
call   1070 <write_wdt@plt>
jmp    12c0 <main+0xe0>
xor    %edi,%edi
call   1070 <write_wdt@plt>
xor    %eax,%eax
call   10c0 <close_keyboard@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [31 x i8] c"Usage: timer <time> <sec/min>\0A\00"
@.str.1 = [24 x i8] c"Example: timer 100 sec\0A\00"
@stderr = global i32 0
@.str.2 = [36 x i8] c"Can't access device WDT - Aborting\0A\00"
@.str.3 = [40 x i8] c"Test program WinSystems Watchdog Timer\0A\00"
@.str.4 = [19 x i8] c" Settings: %d %s\0A\00"
@.str.5 = [32 x i8] c" Hit any key to start program\0A\00"
@.str.6 = [34 x i8] c" ***** Current count = %d *****\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* %4
%9 = icmp ne i32 %8, 3
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = call i32 (...) @read_wdt()
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %27, i8* %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
%33 = call i32 (...) @init_keyboard()
br label %34
34: ; preds = %21
br label %35
35: ; preds = %34
br label %36
36: ; preds = %40, %35
%37 = call i64 (...) @kbhit()
%38 = icmp ne i64 %37, 0
%39 = xor i1 %38, true
br i1 %39, label %40, label %41
40: ; preds = %36
br label %36
41: ; preds = %36
%42 = call i32 (...) @readch()
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 2
%45 = load i8*, i8** %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 115
br i1 %48, label %49, label %51
49: ; preds = %41
%50 = call i32 (...) @set_wdt_sec()
br label %53
51: ; preds = %41
%52 = call i32 (...) @set_wdt_min()
br label %53
53: ; preds = %51, %49
%54 = load i32, i32* %6
%55 = call i32 @write_wdt(i32 %54)
br label %56
56: ; preds = %53, %73
%57 = call i32 (...) @read_wdt()
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i32 %57)
%59 = call i64 (...) @kbhit()
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %73
61: ; preds = %56
%62 = call i32 (...) @readch()
store i32 %62, i32* %7
%63 = load i32, i32* %7
%64 = icmp eq i32 %63, 113
br i1 %64, label %65, label %69
65: ; preds = %61
%66 = call i32 @write_wdt(i32 0)
%67 = call i32 (...) @close_keyboard()
%68 = call i32 @exit(i32 0)
unreachable
69: ; preds = %61
%70 = load i32, i32* %6
%71 = call i32 @write_wdt(i32 %70)
br label %72
72: ; preds = %69
br label %73
73: ; preds = %72, %56
%74 = call i32 @usleep(i32 1000000)
br label %56
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.2 = [36 x i8] c"Can't access device WDT - Aborting\0A\00"
@.str.4 = [19 x i8] c" Settings: %d %s\0A\00"
@.str.6 = [34 x i8] c" ***** Current count = %d *****\0A\00"
@str = [39 x i8] c"Test program WinSystems Watchdog Timer\00"
@str.7 = [31 x i8] c" Hit any key to start program\00"
@str.8 = [30 x i8] c"Usage: timer <time> <sec/min>\00"
@str.9 = [23 x i8] c"Example: timer 100 sec\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.8, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.9, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = tail call i32 (...) @read_wdt()
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %8
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = tail call i32 @atoi(i8* %18)
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %19, i8* %21)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.7, i64 0, i64 0))
%24 = tail call i32 (...) @init_keyboard()
br label %25
25: ; preds = %25, %15
%26 = tail call i64 (...) @kbhit()
%27 = icmp eq i64 %26, 0
br i1 %27, label %25, label %28
28: ; preds = %25
%29 = tail call i32 (...) @readch()
%30 = load i8*, i8** %20
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 115
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = tail call i32 (...) @set_wdt_sec()
br label %37
35: ; preds = %28
%36 = tail call i32 (...) @set_wdt_min()
br label %37
37: ; preds = %35, %33
%38 = tail call i32 @write_wdt(i32 %19)
br label %39
39: ; preds = %53, %37
%40 = tail call i32 (...) @read_wdt()
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i32 %40)
%42 = tail call i64 (...) @kbhit()
%43 = icmp eq i64 %42, 0
br i1 %43, label %53, label %44
44: ; preds = %39
%45 = tail call i32 (...) @readch()
%46 = icmp eq i32 %45, 113
br i1 %46, label %47, label %51
47: ; preds = %44
%48 = tail call i32 @write_wdt(i32 0)
%49 = tail call i32 (...) @close_keyboard()
%50 = tail call i32 @exit(i32 0)
unreachable
51: ; preds = %44
%52 = tail call i32 @write_wdt(i32 %19)
br label %53
53: ; preds = %51, %39
%54 = tail call i32 @usleep(i32 1000000)
br label %39
}