#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ B2400;
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ decode_msg(unsigned char *);
int /*<<< orphan*/ errno;
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned char *, int);
int /*<<< orphan*/ set_interface_attribs(int, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int main() {
  char *portname = "/dev/ttyUSB0";
  int fd;
  printf("Data Logging interface for PeakMeter MS8236 USB Multimeter.\n");
  printf("If logging does not start make sure USB lead is connected,\n");
  printf("then press and hold USB button on meter for two seconds.\n");
  fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
  if (fd < 0) {
    printf("Error opening %s: %s\n", portname, strerror(errno));
    return -1;
  }
  set_interface_attribs(fd, B2400);
  int msg_index = 0;
  unsigned char raw_msg[80];
  do {
    unsigned char buf[80];
    int rdlen;
    rdlen = read(fd, buf, sizeof(buf) - 1);
    if (rdlen > 0) {
      unsigned char *p;
      for (p = buf; rdlen-- > 0; p++) {
        if (*p == 0xAA) {
          msg_index = 0;
        }
        raw_msg[msg_index++] = *p;
        if (msg_index >= 80)
          msg_index--;
        if (msg_index == 22) {
          decode_msg(raw_msg);
          msg_index = 0;
        }
      }
    } else if (rdlen < 0) {
      printf("Error from read: %d: %s\n", rdlen, strerror(errno));
    }
  } while (1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xeb8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xee6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
or     (%rax),%esi
mov    0x2e1c(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1215 <main+0xb5>
mov    -0x10(%rbp),%rax
mov    %rax,-0xd8(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0xd8(%rbp),%rsi
mov    %rax,%rdx
lea    0xebe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1357 <main+0x1f7>
mov    -0x14(%rbp),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
call   1040 <set_interface_attribs@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0xc0(%rbp),%rsi
mov    $0x4f,%edx
call   1060 <read@plt>
mov    %eax,-0xc4(%rbp)
cmpl   $0x0,-0xc4(%rbp)
jle    1300 <main+0x1a0>
lea    -0xc0(%rbp),%rax
mov    %rax,-0xd0(%rbp)
mov    -0xc4(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc4(%rbp)
cmp    $0x0,%eax
jle    12fb <main+0x19b>
mov    -0xd0(%rbp),%rax
movzbl (%rax),%eax
cmp    $0xaa,%eax
jne    1298 <main+0x138>
movl   $0x0,-0x18(%rbp)
mov    -0xd0(%rbp),%rax
mov    (%rax),%cl
mov    -0x18(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
cltq
mov    %cl,-0x70(%rbp,%rax,1)
cmpl   $0x50,-0x18(%rbp)
jl     12c5 <main+0x165>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x16,-0x18(%rbp)
jne    12df <main+0x17f>
lea    -0x70(%rbp),%rdi
call   1030 <decode_msg@plt>
movl   $0x0,-0x18(%rbp)
jmp    12e4 <main+0x184>
mov    -0xd0(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xd0(%rbp)
jmp    1262 <main+0x102>
jmp    1343 <main+0x1e3>
cmpl   $0x0,-0xc4(%rbp)
jge    133e <main+0x1de>
mov    -0xc4(%rbp),%eax
mov    %eax,-0xdc(%rbp)
mov    0x2ca0(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0xdc(%rbp),%esi
mov    %rax,%rdx
lea    0xd9f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1343 <main+0x1e3>
jmp    1348 <main+0x1e8>
mov    $0x1,%al
test   $0x1,%al
jne    122d <main+0xcd>
jmp    1357 <main+0x1f7>
mov    -0x4(%rbp),%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
lea    0xeb4(%rip),%rdi
call   1030 <puts@plt>
lea    0xee4(%rip),%rdi
call   1030 <puts@plt>
lea    0xf13(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e3c(%rip),%rax
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e22(%rip),%rax
or     (%rax),%esi
lea    0xe39(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1291 <main+0x121>
mov    %eax,%r12d
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1050 <set_interface_attribs@plt>
xor    %ebx,%ebx
lea    0x50(%rsp),%r13
xor    %r15d,%r15d
jmp    1202 <main+0x92>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
js     126b <main+0xfb>
mov    %r12d,%edi
mov    %rsp,%rsi
mov    $0x4f,%edx
call   1070 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1200 <main+0x90>
add    $0xffffffff,%ebp
add    $0x1,%rbp
xor    %r14d,%r14d
jmp    1239 <main+0xc9>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r14
cmp    %r14,%rbp
je     1202 <main+0x92>
movzbl (%rsp,%r14,1),%eax
cmp    $0xaa,%al
cmove  %ebx,%r15d
movslq %r15d,%rcx
mov    %al,0x50(%rsp,%rcx,1)
xor    %r15d,%r15d
cmp    $0x4f,%ecx
setl   %r15b
add    %ecx,%r15d
cmp    $0x16,%r15d
jne    1230 <main+0xc0>
mov    %r13,%rdi
call   1040 <decode_msg@plt>
xor    %r15d,%r15d
jmp    1230 <main+0xc0>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xda3(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1202 <main+0x92>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd67(%rip),%rdi
lea    0xd53(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/ttyUSB0\00"
@.str.1 = [61 x i8] c"Data Logging interface for PeakMeter MS8236 USB Multimeter.\0A\00"
@.str.2 = [60 x i8] c"If logging does not start make sure USB lead is connected,\0A\00"
@.str.3 = [58 x i8] c"then press and hold USB button on meter for two seconds.\0A\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str.4 = [22 x i8] c"Error opening %s: %s\0A\00"
@errno = global i32 0
@B2400 = global i32 0
@.str.5 = [25 x i8] c"Error from read: %d: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [80 x i8]
%6 = alloca [80 x i8]
%7 = alloca i32
%8 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0))
%12 = load i8*, i8** %2
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_NOCTTY
%15 = or i32 %13, %14
%16 = load i32, i32* @O_SYNC
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %12, i32 %17)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %0
%22 = load i8*, i8** %2
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %22, i8* %24)
store i32 -1, i32* %1
br label %82
26: ; preds = %0
%27 = load i32, i32* %3
%28 = load i32, i32* @B2400
%29 = call i32 @set_interface_attribs(i32 %27, i32 %28)
store i32 0, i32* %4
br label %30
30: ; preds = %81, %26
%31 = load i32, i32* %3
%32 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%33 = call i32 @read(i32 %31, i8* %32, i32 79)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %71
36: ; preds = %30
%37 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
store i8* %37, i8** %8
br label %38
38: ; preds = %67, %36
%39 = load i32, i32* %7
%40 = add nsw i32 %39, -1
store i32 %40, i32* %7
%41 = icmp sgt i32 %39, 0
br i1 %41, label %42, label %70
42: ; preds = %38
%43 = load i8*, i8** %8
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = icmp eq i32 %45, 170
br i1 %46, label %47, label %48
47: ; preds = %42
store i32 0, i32* %4
br label %48
48: ; preds = %47, %42
%49 = load i8*, i8** %8
%50 = load i8, i8* %49
%51 = load i32, i32* %4
%52 = add nsw i32 %51, 1
store i32 %52, i32* %4
%53 = sext i32 %51 to i64
%54 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 %53
store i8 %50, i8* %54
%55 = load i32, i32* %4
%56 = icmp sge i32 %55, 80
br i1 %56, label %57, label %60
57: ; preds = %48
%58 = load i32, i32* %4
%59 = add nsw i32 %58, -1
store i32 %59, i32* %4
br label %60
60: ; preds = %57, %48
%61 = load i32, i32* %4
%62 = icmp eq i32 %61, 22
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%65 = call i32 @decode_msg(i8* %64)
store i32 0, i32* %4
br label %66
66: ; preds = %63, %60
br label %67
67: ; preds = %66
%68 = load i8*, i8** %8
%69 = getelementptr inbounds i8, i8* %68, i32 1
store i8* %69, i8** %8
br label %38
70: ; preds = %38
br label %80
71: ; preds = %30
%72 = load i32, i32* %7
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %79
74: ; preds = %71
%75 = load i32, i32* %7
%76 = load i32, i32* @errno
%77 = call i8* @strerror(i32 %76)
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %75, i8* %77)
br label %79
79: ; preds = %74, %71
br label %80
80: ; preds = %79, %70
br label %81
81: ; preds = %80
br i1 true, label %30, label %82
82: ; preds = %21, %81
%83 = load i32, i32* %1
ret i32 %83
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/ttyUSB0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str.4 = [22 x i8] c"Error opening %s: %s\0A\00"
@errno = global i32 0
@B2400 = global i32 0
@.str.5 = [25 x i8] c"Error from read: %d: %s\0A\00"
@str = [60 x i8] c"Data Logging interface for PeakMeter MS8236 USB Multimeter.\00"
@str.6 = [59 x i8] c"If logging does not start make sure USB lead is connected,\00"
@str.7 = [57 x i8] c"then press and hold USB button on meter for two seconds.\00"
define i32 @main() {
%1 = alloca [80 x i8]
%2 = alloca [80 x i8]
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @str.6, i64 0, i64 0))
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str.7, i64 0, i64 0))
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %7, %6
%9 = load i32, i32* @O_SYNC
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %0
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %15)
ret i32 -1
17: ; preds = %0
%18 = load i32, i32* @B2400
%19 = tail call i32 @set_interface_attribs(i32 %11, i32 %18)
%20 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%21 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
br label %22
22: ; preds = %54, %17
%23 = phi i32 [ 0, %17 ], [ %55, %54 ]
%24 = call i32 @read(i32 %11, i8* %21, i32 79)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %48
26: ; preds = %22
%27 = add nsw i32 %24, -1
%28 = zext i32 %27 to i64
%29 = getelementptr [80 x i8], [80 x i8]* %2, i64 0, i64 %28
br label %30
30: ; preds = %26, %44
%31 = phi i8* [ %21, %26 ], [ %46, %44 ]
%32 = phi i32 [ %23, %26 ], [ %45, %44 ]
%33 = load i8, i8* %31
%34 = icmp eq i8 %33, -86
%35 = select i1 %34, i32 0, i32 %32
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 %36
store i8 %33, i8* %37
%38 = icmp slt i32 %35, 79
%39 = zext i1 %38 to i32
%40 = add nsw i32 %35, %39
%41 = icmp eq i32 %40, 22
br i1 %41, label %42, label %44
42: ; preds = %30
%43 = call i32 @decode_msg(i8* %20)
br label %44
44: ; preds = %30, %42
%45 = phi i32 [ 0, %42 ], [ %40, %30 ]
%46 = getelementptr inbounds i8, i8* %31, i64 1
%47 = icmp eq i8* %31, %29
br i1 %47, label %54, label %30
48: ; preds = %22
%49 = icmp slt i32 %24, 0
br i1 %49, label %50, label %54
50: ; preds = %48
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %24, i8* %52)
br label %54
54: ; preds = %44, %48, %50
%55 = phi i32 [ %23, %50 ], [ %23, %48 ], [ %45, %44 ]
br label %22
}