#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 com_getc(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
long readval() {
  int i;
  int j = 257;
  long val = 0;
  while (1) {
    i = com_getc(300);
    if (i == -1) {
      fprintf(stderr, "...Device does not answer!\n");
      exit(-1);
    }
    switch (j) {
    case 1:
      if (i == 0xAA) {
        return val;
      }
      break;
    case 2:
      val = val * 256 + i;
      j = 1;
      break;
    case 3:
      val = val * 256 + i;
      j = 2;
      break;
    case 4:
      val = val * 256 + i;
      j = 3;
      break;
    case 256:
      j = i;
      break;
    case 257:
      if (i == 0xAB) {
        return -2;
      } else if (i == 0xA8) {
        j = 256;
      }
      break;
    default:
      fprintf(stderr, "\nError: readval, i = %i, j = %i, val = %li\n", i, j,
              val);
      return -2;
    }
  }
}


x86 assembly (clang, -O0):
<readval>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x101,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x12c,%edi
call   1030 <com_getc@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    117f <readval+0x4f>
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
lea    0xe92(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x1,%eax
je     11ec <readval+0xbc>
jmp    1193 <readval+0x63>
mov    -0x1c(%rbp),%eax
sub    $0x2,%eax
je     120b <readval+0xdb>
jmp    11a4 <readval+0x74>
mov    -0x1c(%rbp),%eax
sub    $0x3,%eax
je     122a <readval+0xfa>
jmp    11b5 <readval+0x85>
mov    -0x1c(%rbp),%eax
sub    $0x4,%eax
je     1249 <readval+0x119>
jmp    11c6 <readval+0x96>
mov    -0x1c(%rbp),%eax
sub    $0x100,%eax
je     1268 <readval+0x138>
jmp    11d9 <readval+0xa9>
mov    -0x1c(%rbp),%eax
sub    $0x101,%eax
je     1273 <readval+0x143>
jmp    12ab <readval+0x17b>
cmpl   $0xaa,-0xc(%rbp)
jne    1206 <readval+0xd6>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12de <readval+0x1ae>
jmp    12d9 <readval+0x1a9>
mov    -0x18(%rbp),%rax
shl    $0x8,%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x10(%rbp)
jmp    12d9 <readval+0x1a9>
mov    -0x18(%rbp),%rax
shl    $0x8,%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
movl   $0x2,-0x10(%rbp)
jmp    12d9 <readval+0x1a9>
mov    -0x18(%rbp),%rax
shl    $0x8,%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
movl   $0x3,-0x10(%rbp)
jmp    12d9 <readval+0x1a9>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    12d9 <readval+0x1a9>
cmpl   $0xab,-0xc(%rbp)
jne    128d <readval+0x15d>
movq   $0xfffffffffffffffe,-0x8(%rbp)
ff
jmp    12de <readval+0x1ae>
cmpl   $0xa8,-0xc(%rbp)
jne    12a1 <readval+0x171>
movl   $0x100,-0x10(%rbp)
jmp    12a6 <readval+0x176>
jmp    12d9 <readval+0x1a9>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
mov    -0x18(%rbp),%r8
lea    0xd57(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movq   $0xfffffffffffffffe,-0x8(%rbp)
ff
jmp    12de <readval+0x1ae>
jmp    1147 <readval+0x17>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readval>:
push   %r15
push   %r14
push   %rbx
mov    $0x12c,%edi
call   1030 <com_getc@plt>
cmp    $0xffffffff,%eax
je     1230 <readval+0x100>
mov    $0x101,%ebx
xor    %r14d,%r14d
lea    0xea9(%rip),%r15
jmp    1185 <readval+0x55>
shl    $0x8,%r14
cltq
add    %rax,%r14
mov    $0x1,%eax
nopw   0x0(%rax,%rax,1)
mov    %eax,%ebx
mov    $0x12c,%edi
call   1030 <com_getc@plt>
cmp    $0xffffffff,%eax
je     1230 <readval+0x100>
lea    -0x1(%rbx),%ecx
cmp    $0x3,%ecx
ja     11b0 <readval+0x80>
movslq (%r15,%rcx,4),%rcx
add    %r15,%rcx
jmp    *%rcx
cmp    $0xaa,%eax
mov    $0x1,%eax
jne    1170 <readval+0x40>
jmp    1227 <readval+0xf7>
nopw   0x0(%rax,%rax,1)
cmp    $0x100,%ebx
je     1170 <readval+0x40>
cmp    $0x101,%ebx
jne    1202 <readval+0xd2>
cmp    $0xa8,%eax
je     11f8 <readval+0xc8>
cmp    $0xab,%eax
mov    $0x101,%eax
jne    1170 <readval+0x40>
jmp    1220 <readval+0xf0>
shl    $0x8,%r14
cltq
add    %rax,%r14
mov    $0x2,%eax
jmp    1170 <readval+0x40>
shl    $0x8,%r14
cltq
add    %rax,%r14
mov    $0x3,%eax
jmp    1170 <readval+0x40>
mov    $0x100,%eax
jmp    1170 <readval+0x40>
mov    0x2dd7(%rip),%rcx
mov    (%rcx),%edi
lea    0xe1a(%rip),%rsi
mov    %eax,%edx
mov    %ebx,%ecx
mov    %r14,%r8
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xfffffffffffffffe,%r14
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xdd0(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"...Device does not answer!\0A\00"
@.str.1 = [44 x i8] c"\0AError: readval, i = %i, j = %i, val = %li\0A\00"
define i64 @readval() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
store i32 257, i32* %3
store i64 0, i64* %4
br label %5
5: ; preds = %0, %57
%6 = call i32 @com_getc(i32 300)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %5
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %5
%14 = load i32, i32* %3
switch i32 %14, label %51 [
i32 1, label %15
i32 2, label %21
i32 3, label %27
i32 4, label %33
i32 256, label %39
i32 257, label %41
]
15: ; preds = %13
%16 = load i32, i32* %2
%17 = icmp eq i32 %16, 170
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = load i64, i64* %4
store i64 %19, i64* %1
br label %58
20: ; preds = %15
br label %57
21: ; preds = %13
%22 = load i64, i64* %4
%23 = mul nsw i64 %22, 256
%24 = load i32, i32* %2
%25 = sext i32 %24 to i64
%26 = add nsw i64 %23, %25
store i64 %26, i64* %4
store i32 1, i32* %3
br label %57
27: ; preds = %13
%28 = load i64, i64* %4
%29 = mul nsw i64 %28, 256
%30 = load i32, i32* %2
%31 = sext i32 %30 to i64
%32 = add nsw i64 %29, %31
store i64 %32, i64* %4
store i32 2, i32* %3
br label %57
33: ; preds = %13
%34 = load i64, i64* %4
%35 = mul nsw i64 %34, 256
%36 = load i32, i32* %2
%37 = sext i32 %36 to i64
%38 = add nsw i64 %35, %37
store i64 %38, i64* %4
store i32 3, i32* %3
br label %57
39: ; preds = %13
%40 = load i32, i32* %2
store i32 %40, i32* %3
br label %57
41: ; preds = %13
%42 = load i32, i32* %2
%43 = icmp eq i32 %42, 171
br i1 %43, label %44, label %45
44: ; preds = %41
store i64 -2, i64* %1
br label %58
45: ; preds = %41
%46 = load i32, i32* %2
%47 = icmp eq i32 %46, 168
br i1 %47, label %48, label %49
48: ; preds = %45
store i32 256, i32* %3
br label %49
49: ; preds = %48, %45
br label %50
50: ; preds = %49
br label %57
51: ; preds = %13
%52 = load i32, i32* @stderr
%53 = load i32, i32* %2
%54 = load i32, i32* %3
%55 = load i64, i64* %4
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %53, i32 %54, i64 %55)
store i64 -2, i64* %1
br label %58
57: ; preds = %50, %39, %33, %27, %21, %20
br label %5
58: ; preds = %51, %44, %18
%59 = load i64, i64* %1
ret i64 %59
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"...Device does not answer!\0A\00"
@.str.1 = [44 x i8] c"\0AError: readval, i = %i, j = %i, val = %li\0A\00"
define i64 @readval() {
%1 = tail call i32 @com_getc(i32 300)
%2 = icmp eq i32 %1, -1
br i1 %2, label %3, label %7
3: ; preds = %30, %0
%4 = load i32, i32* @stderr
%5 = tail call i32 (i32, i8*, ...) @fprintf(i32 %4, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %0, %30
%8 = phi i32 [ %33, %30 ], [ %1, %0 ]
%9 = phi i64 [ %32, %30 ], [ 0, %0 ]
%10 = phi i32 [ %31, %30 ], [ 257, %0 ]
switch i32 %10, label %27 [
i32 1, label %11
i32 2, label %13
i32 3, label %17
i32 4, label %21
i32 256, label %30
i32 257, label %25
]
11: ; preds = %7
%12 = icmp eq i32 %8, 170
br i1 %12, label %35, label %30
13: ; preds = %7
%14 = shl nsw i64 %9, 8
%15 = sext i32 %8 to i64
%16 = add nsw i64 %14, %15
br label %30
17: ; preds = %7
%18 = shl nsw i64 %9, 8
%19 = sext i32 %8 to i64
%20 = add nsw i64 %18, %19
br label %30
21: ; preds = %7
%22 = shl nsw i64 %9, 8
%23 = sext i32 %8 to i64
%24 = add nsw i64 %22, %23
br label %30
25: ; preds = %7
switch i32 %8, label %30 [
i32 171, label %35
i32 168, label %26
]
26: ; preds = %25
br label %30
27: ; preds = %7
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %10, i64 %9)
br label %35
30: ; preds = %25, %7, %26, %11, %21, %17, %13
%31 = phi i32 [ 256, %26 ], [ 3, %21 ], [ 2, %17 ], [ 1, %13 ], [ 1, %11 ], [ %8, %7 ], [ 257, %25 ]
%32 = phi i64 [ %9, %26 ], [ %24, %21 ], [ %20, %17 ], [ %16, %13 ], [ %9, %11 ], [ %9, %7 ], [ %9, %25 ]
%33 = tail call i32 @com_getc(i32 300)
%34 = icmp eq i32 %33, -1
br i1 %34, label %3, label %7
35: ; preds = %25, %11, %27
%36 = phi i64 [ -2, %27 ], [ -2, %25 ], [ %9, %11 ]
ret i64 %36
}