#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 */
struct timeval {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ write(int, unsigned char *, int);
int main(int ac, char *av[]) {
  int fd, limit;
  unsigned char odata, data, state, otype;
  if (ac != 3) {
    printf("Usage: timer /dev/ttyACM0 limit\n");
    exit(1);
  }
  limit = atoi(av[2]);
  fd = open(av[1], O_RDWR);
  if (fd == -1) {
    perror(av[1]);
    return 1;
  }
  data = 'X';
  write(fd, &data, 1);
  data = '1';
  write(fd, &data, 1);
  data = '8';
  write(fd, &data, 1);
  data = '\r';
  write(fd, &data, 1);
  data = '\n';
  write(fd, &data, 1);
  state = 0;
  while (read(fd, &data, 1) == 1) {
    if (state == 0) {
      if (data == 'r' || data == 'f') {
        odata = data;
        state++;
      } else if (data == '.') {
        otype = 0;
        printf("------\n");
      } else {
        printf("%x (%d)..\n", data, data << 4);
      }
    } else {
      struct timeval tv;
      gettimeofday(&tv, 0);
      printf("%u.%06d %c %s %5d  %d\n", (int)tv.tv_sec, (int)tv.tv_usec, odata,
             otype == odata ? "?" : " ", data << 4,
             (data << 4) < limit ? 0 : 1);
      otype = odata;
      fflush(stdout);
      state = 0;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11c8 <main+0x38>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1214 <main+0x84>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ad <main+0x21d>
movb   $0x58,-0x1a(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movb   $0x31,-0x1a(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movb   $0x38,-0x1a(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movb   $0xd,-0x1a(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movb   $0xa,-0x1a(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movb   $0x0,-0x1b(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
jne    13a6 <main+0x216>
movzbl -0x1b(%rbp),%eax
cmp    $0x0,%eax
jne    131c <main+0x18c>
movzbl -0x1a(%rbp),%eax
cmp    $0x72,%eax
je     12c2 <main+0x132>
movzbl -0x1a(%rbp),%eax
cmp    $0x66,%eax
jne    12d5 <main+0x145>
mov    -0x1a(%rbp),%al
mov    %al,-0x19(%rbp)
mov    -0x1b(%rbp),%al
add    $0x1,%al
mov    %al,-0x1b(%rbp)
jmp    1317 <main+0x187>
movzbl -0x1a(%rbp),%eax
cmp    $0x2e,%eax
jne    12f9 <main+0x169>
movb   $0x0,-0x1c(%rbp)
lea    0xd34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1312 <main+0x182>
movzbl -0x1a(%rbp),%esi
movzbl -0x1a(%rbp),%edx
shl    $0x4,%edx
lea    0xd1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1317 <main+0x187>
jmp    13a1 <main+0x211>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    -0x28(%rbp),%rax
mov    %eax,%esi
mov    -0x30(%rbp),%rax
mov    %eax,%edx
movzbl -0x19(%rbp),%ecx
movzbl -0x1c(%rbp),%edi
movzbl -0x19(%rbp),%r9d
lea    0xd06(%rip),%r8
lea    0xcfd(%rip),%rax
cmp    %r9d,%edi
cmove  %rax,%r8
movzbl -0x1a(%rbp),%r9d
shl    $0x4,%r9d
movzbl -0x1a(%rbp),%r10d
shl    $0x4,%r10d
mov    -0x18(%rbp),%r11d
mov    $0x1,%eax
xor    %edi,%edi
cmp    %r11d,%r10d
cmovl  %edi,%eax
lea    0xcb5(%rip),%rdi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x19(%rbp),%al
mov    %al,-0x1c(%rbp)
mov    0x2c42(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
movb   $0x0,-0x1b(%rbp)
jmp    1281 <main+0xf1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x3,%edi
jne    136e <main+0x1ce>
mov    %rsi,%rbx
mov    0x10(%rsi),%rdi
call   10b0 <atoi@plt>
mov    %eax,%r12d
mov    0x8(%rbx),%rdi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     135e <main+0x1be>
mov    %eax,%ebp
movb   $0x58,0x17(%rsp)
lea    0x17(%rsp),%rbx
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
movb   $0x31,0x17(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
movb   $0x38,0x17(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
movb   $0xd,0x17(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
movb   $0xa,0x17(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
jne    134f <main+0x1af>
xor    %r13d,%r13d
lea    0x17(%rsp),%rbx
jmp    12f8 <main+0x158>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %r13d,%r13d
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    0x18(%rsp),%edx
mov    0x20(%rsp),%esi
movzbl %r15b,%ecx
cmp    %cl,%r14b
lea    0xd7f(%rip),%r8
lea    0xd76(%rip),%rax
cmove  %rax,%r8
movzbl 0x17(%rsp),%r9d
shl    $0x4,%r9d
xor    %eax,%eax
cmp    %r12d,%r9d
setge  %al
mov    %eax,(%rsp)
lea    0xd3f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    %r15d,%r14d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
jne    134d <main+0x1ad>
test   %r13b,%r13b
jne    1280 <main+0xe0>
movzbl 0x17(%rsp),%esi
mov    $0x1,%r13b
cmp    $0x72,%esi
je     1330 <main+0x190>
cmp    $0x66,%sil
je     1330 <main+0x190>
cmp    $0x2e,%sil
jne    1335 <main+0x195>
lea    0xd09(%rip),%rdi
call   1030 <puts@plt>
xor    %r13d,%r13d
xor    %r14d,%r14d
jmp    12e4 <main+0x144>
nopw   0x0(%rax,%rax,1)
mov    %esi,%r15d
jmp    12e4 <main+0x144>
mov    %esi,%edx
shl    $0x4,%edx
xor    %r13d,%r13d
lea    0xcbc(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12e4 <main+0x144>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rbx),%rdi
call   10a0 <perror@plt>
mov    $0x1,%eax
jmp    134f <main+0x1af>
lea    0xcb8(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@.str = [33 x i8] c"Usage: timer /dev/ttyACM0 limit\0A\00"
@O_RDWR = global i32 0
@.str.1 = [8 x i8] c"------\0A\00"
@.str.2 = [11 x i8] c"%x (%d)..\0A\00"
@.str.3 = [23 x i8] c"%u.%06d %c %s %5d %d\0A\00"
@.str.4 = [2 x i8] c"?\00"
@.str.5 = [2 x i8] c" \00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8
%11 = alloca i8
%12 = alloca %struct.timeval
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%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* %7
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = load i32, i32* @O_RDWR
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %18
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 @perror(i8* %33)
store i32 1, i32* %3
br label %114
35: ; preds = %18
store i8 88, i8* %9
%36 = load i32, i32* %6
%37 = call i32 @write(i32 %36, i8* %9, i32 1)
store i8 49, i8* %9
%38 = load i32, i32* %6
%39 = call i32 @write(i32 %38, i8* %9, i32 1)
store i8 56, i8* %9
%40 = load i32, i32* %6
%41 = call i32 @write(i32 %40, i8* %9, i32 1)
store i8 13, i8* %9
%42 = load i32, i32* %6
%43 = call i32 @write(i32 %42, i8* %9, i32 1)
store i8 10, i8* %9
%44 = load i32, i32* %6
%45 = call i32 @write(i32 %44, i8* %9, i32 1)
store i8 0, i8* %10
br label %46
46: ; preds = %112, %35
%47 = load i32, i32* %6
%48 = call i32 @read(i32 %47, i8* %9, i32 1)
%49 = icmp eq i32 %48, 1
br i1 %49, label %50, label %113
50: ; preds = %46
%51 = load i8, i8* %10
%52 = zext i8 %51 to i32
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %81
54: ; preds = %50
%55 = load i8, i8* %9
%56 = zext i8 %55 to i32
%57 = icmp eq i32 %56, 114
br i1 %57, label %62, label %58
58: ; preds = %54
%59 = load i8, i8* %9
%60 = zext i8 %59 to i32
%61 = icmp eq i32 %60, 102
br i1 %61, label %62, label %66
62: ; preds = %58, %54
%63 = load i8, i8* %9
store i8 %63, i8* %8
%64 = load i8, i8* %10
%65 = add i8 %64, 1
store i8 %65, i8* %10
br label %80
66: ; preds = %58
%67 = load i8, i8* %9
%68 = zext i8 %67 to i32
%69 = icmp eq i32 %68, 46
br i1 %69, label %70, label %72
70: ; preds = %66
store i8 0, i8* %11
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %79
72: ; preds = %66
%73 = load i8, i8* %9
%74 = zext i8 %73 to i32
%75 = load i8, i8* %9
%76 = zext i8 %75 to i32
%77 = shl i32 %76, 4
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %74, i32 %77)
br label %79
79: ; preds = %72, %70
br label %80
80: ; preds = %79, %62
br label %112
81: ; preds = %50
%82 = call i32 @gettimeofday(%struct.timeval* %12, i32 0)
%83 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
%84 = load i64, i64* %83
%85 = trunc i64 %84 to i32
%86 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
%87 = load i64, i64* %86
%88 = trunc i64 %87 to i32
%89 = load i8, i8* %8
%90 = zext i8 %89 to i32
%91 = load i8, i8* %11
%92 = zext i8 %91 to i32
%93 = load i8, i8* %8
%94 = zext i8 %93 to i32
%95 = icmp eq i32 %92, %94
%96 = zext i1 %95 to i64
%97 = select i1 %95, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0)
%98 = load i8, i8* %9
%99 = zext i8 %98 to i32
%100 = shl i32 %99, 4
%101 = load i8, i8* %9
%102 = zext i8 %101 to i32
%103 = shl i32 %102, 4
%104 = load i32, i32* %7
%105 = icmp slt i32 %103, %104
%106 = zext i1 %105 to i64
%107 = select i1 %105, i32 0, i32 1
%108 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %85, i32 %88, i32 %90, i8* %97, i32 %100, i32 %107)
%109 = load i8, i8* %8
store i8 %109, i8* %11
%110 = load i32, i32* @stdout
%111 = call i32 @fflush(i32 %110)
store i8 0, i8* %10
br label %112
112: ; preds = %81, %80
br label %46
113: ; preds = %46
store i32 0, i32* %3
br label %114
114: ; preds = %113, %30
%115 = load i32, i32* %3
ret i32 %115
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@O_RDWR = global i32 0
@.str.2 = [11 x i8] c"%x (%d)..\0A\00"
@.str.3 = [23 x i8] c"%u.%06d %c %s %5d %d\0A\00"
@.str.4 = [2 x i8] c"?\00"
@.str.5 = [2 x i8] c" \00"
@stdout = global i32 0
@str = [7 x i8] c"------\00"
@str.6 = [32 x i8] c"Usage: timer /dev/ttyACM0 limit\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = alloca %struct.timeval
%5 = icmp eq i32 %0, 3
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.6, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* %14, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %9
%19 = load i8*, i8** %13
%20 = tail call i32 @perror(i8* %19)
br label %69
21: ; preds = %9
store i8 88, i8* %3
%22 = call i32 @write(i32 %16, i8* %3, i32 1)
store i8 49, i8* %3
%23 = call i32 @write(i32 %16, i8* %3, i32 1)
store i8 56, i8* %3
%24 = call i32 @write(i32 %16, i8* %3, i32 1)
store i8 13, i8* %3
%25 = call i32 @write(i32 %16, i8* %3, i32 1)
store i8 10, i8* %3
%26 = call i32 @write(i32 %16, i8* %3, i32 1)
%27 = call i32 @read(i32 %16, i8* %3, i32 1)
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %69
29: ; preds = %21
%30 = bitcast %struct.timeval* %4 to i8*
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
br label %33
33: ; preds = %29, %63
%34 = phi i8 [ undef, %29 ], [ %66, %63 ]
%35 = phi i8 [ 0, %29 ], [ %65, %63 ]
%36 = phi i8 [ undef, %29 ], [ %64, %63 ]
%37 = icmp eq i8 %35, 0
br i1 %37, label %38, label %46
38: ; preds = %33
%39 = load i8, i8* %3
%40 = zext i8 %39 to i32
switch i8 %39, label %43 [
i8 114, label %63
i8 102, label %63
i8 46, label %41
]
41: ; preds = %38
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
br label %63
43: ; preds = %38
%44 = shl nuw nsw i32 %40, 4
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %40, i32 %44)
br label %63
46: ; preds = %33
%47 = call i32 @gettimeofday(%struct.timeval* %4, i32 0)
%48 = load i64, i64* %31
%49 = trunc i64 %48 to i32
%50 = load i64, i64* %32
%51 = trunc i64 %50 to i32
%52 = zext i8 %36 to i32
%53 = icmp eq i8 %34, %36
%54 = select i1 %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0)
%55 = load i8, i8* %3
%56 = zext i8 %55 to i32
%57 = shl nuw nsw i32 %56, 4
%58 = icmp sge i32 %57, %12
%59 = zext i1 %58 to i32
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %49, i32 %51, i32 %52, i8* %54, i32 %57, i32 %59)
%61 = load i32, i32* @stdout
%62 = call i32 @fflush(i32 %61)
br label %63
63: ; preds = %38, %38, %43, %41, %46
%64 = phi i8 [ %36, %41 ], [ %36, %43 ], [ %36, %46 ], [ %39, %38 ], [ %39, %38 ]
%65 = phi i8 [ 0, %41 ], [ 0, %43 ], [ 0, %46 ], [ 1, %38 ], [ 1, %38 ]
%66 = phi i8 [ 0, %41 ], [ %34, %43 ], [ %36, %46 ], [ %34, %38 ], [ %34, %38 ]
%67 = call i32 @read(i32 %16, i8* %3, i32 1)
%68 = icmp eq i32 %67, 1
br i1 %68, label %33, label %69
69: ; preds = %63, %21, %18
%70 = phi i32 [ 1, %18 ], [ 0, %21 ], [ 0, %63 ]
ret i32 %70
}