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


x86 assembly (clang, -O0):
<readval>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
movl   $0x101,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x3,%esi
call   1030 <com_getc@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    116f <readval+0x4f>
lea    0xea5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0xfffffffffffffffd,-0x8(%rbp)
ff
jmp    1294 <readval+0x174>
cmpl   $0xfffffffe,-0x10(%rbp)
jne    1194 <readval+0x74>
lea    0xea5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0xfffffffffffffffc,-0x8(%rbp)
ff
jmp    1294 <readval+0x174>
jmp    1199 <readval+0x79>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0x1,%eax
je     11e7 <readval+0xc7>
jmp    11ad <readval+0x8d>
mov    -0x24(%rbp),%eax
add    $0xfffffffe,%eax
sub    $0x3,%eax
jb     1206 <readval+0xe6>
jmp    11c1 <readval+0xa1>
mov    -0x24(%rbp),%eax
sub    $0x100,%eax
je     1227 <readval+0x107>
jmp    11d4 <readval+0xb4>
mov    -0x24(%rbp),%eax
sub    $0x101,%eax
je     1232 <readval+0x112>
jmp    126a <readval+0x14a>
cmpl   $0xaa,-0x10(%rbp)
jne    1201 <readval+0xe1>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1294 <readval+0x174>
jmp    128f <readval+0x16f>
mov    -0x20(%rbp),%rax
shl    $0x8,%rax
movslq -0x10(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    128f <readval+0x16f>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    128f <readval+0x16f>
cmpl   $0xab,-0x10(%rbp)
jne    124c <readval+0x12c>
movq   $0xfffffffffffffffe,-0x8(%rbp)
ff
jmp    1294 <readval+0x174>
cmpl   $0xa8,-0x10(%rbp)
jne    1260 <readval+0x140>
movl   $0x100,-0x14(%rbp)
jmp    1265 <readval+0x145>
jmp    128f <readval+0x16f>
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0xfffffffffffffffe,-0x8(%rbp)
ff
jmp    1294 <readval+0x174>
jmp    113a <readval+0x1a>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readval>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
mov    $0x101,%eax
xor    %r14d,%r14d
jmp    1150 <readval+0x20>
mov    $0x100,%eax
cs nopw 0x0(%rax,%rax,1)
nop
mov    %eax,%ebx
mov    %ebp,%edi
mov    $0x3,%esi
call   1030 <com_getc@plt>
cmp    $0xfffffffe,%eax
je     11cb <readval+0x9b>
cmp    $0xffffffff,%eax
je     11e0 <readval+0xb0>
cmp    $0xff,%ebx
jle    11a0 <readval+0x70>
cmp    $0x100,%ebx
je     1150 <readval+0x20>
cmp    $0x101,%ebx
jne    11f5 <readval+0xc5>
cmp    $0xa8,%eax
je     1140 <readval+0x10>
cmp    $0xab,%eax
mov    $0x101,%eax
jne    1150 <readval+0x20>
jmp    120a <readval+0xda>
cs nopw 0x0(%rax,%rax,1)
nop
cmp    $0x1,%ebx
jne    11b3 <readval+0x83>
cmp    $0xaa,%eax
mov    $0x1,%eax
jne    1150 <readval+0x20>
jmp    1211 <readval+0xe1>
lea    -0x2(%rbx),%ecx
cmp    $0x3,%ecx
jae    11f5 <readval+0xc5>
shl    $0x8,%r14
cltq
add    %rax,%r14
add    $0xffffffff,%ebx
mov    %ebx,%eax
jmp    1150 <readval+0x20>
lea    0xe5a(%rip),%rdi
call   1040 <puts@plt>
mov    $0xfffffffffffffffc,%r14
jmp    1211 <readval+0xe1>
lea    0xe66(%rip),%rdi
call   1040 <puts@plt>
mov    $0xfffffffffffffffd,%r14
jmp    1211 <readval+0xe1>
lea    0xe04(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xfffffffffffffffe,%r14
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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