#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 serial_read(int, char *, int);
int read_data(int fd, char *buf, int Onum) {
  char c;
  int n;
  if ((n = serial_read(fd, &c, 1) < 1) || c != 0x02) {
    return -1;
  }
  if ((n = serial_read(fd, buf, Onum)) < Onum) {
    return -1;
  }
  if (serial_read(fd, &c, 1) < 1 || c != 0x03) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <serial_read@plt>
cmp    $0x1,%eax
setl   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x1c(%rbp)
test   $0x1,%al
jne    1159 <read_data+0x49>
movsbl -0x15(%rbp),%eax
cmp    $0x2,%eax
je     1165 <read_data+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <read_data+0xb6>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <serial_read@plt>
mov    %eax,-0x1c(%rbp)
cmp    -0x14(%rbp),%eax
jge    118c <read_data+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <read_data+0xb6>
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <serial_read@plt>
cmp    $0x1,%eax
jl     11b3 <read_data+0xa3>
movsbl -0x15(%rbp),%eax
cmp    $0x3,%eax
je     11bf <read_data+0xaf>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <read_data+0xb6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_data>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebx
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1030 <serial_read@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jle    1175 <read_data+0x65>
cmpb   $0x2,0x7(%rsp)
jne    1175 <read_data+0x65>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <serial_read@plt>
cmp    %r14d,%eax
jl     1175 <read_data+0x65>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <serial_read@plt>
test   %eax,%eax
setle  %al
cmpb   $0x3,0x7(%rsp)
setne  %cl
or     %al,%cl
movzbl %cl,%ebp
neg    %ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_data(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = call i32 @serial_read(i32 %10, i8* %8, i32 1)
%12 = icmp slt i32 %11, 1
%13 = zext i1 %12 to i32
store i32 %13, i32* %9
br i1 %12, label %18, label %14
14: ; preds = %3
%15 = load i8, i8* %8
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 2
br i1 %17, label %18, label %19
18: ; preds = %14, %3
store i32 -1, i32* %4
br label %37
19: ; preds = %14
%20 = load i32, i32* %5
%21 = load i8*, i8** %6
%22 = load i32, i32* %7
%23 = call i32 @serial_read(i32 %20, i8* %21, i32 %22)
store i32 %23, i32* %9
%24 = load i32, i32* %7
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %19
store i32 -1, i32* %4
br label %37
27: ; preds = %19
%28 = load i32, i32* %5
%29 = call i32 @serial_read(i32 %28, i8* %8, i32 1)
%30 = icmp slt i32 %29, 1
br i1 %30, label %35, label %31
31: ; preds = %27
%32 = load i8, i8* %8
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %33, 3
br i1 %34, label %35, label %36
35: ; preds = %31, %27
store i32 -1, i32* %4
br label %37
36: ; preds = %31
store i32 0, i32* %4
br label %37
37: ; preds = %36, %35, %26, %18
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
define i32 @read_data(i32 %0, i8* %1, i32 %2) {
%4 = alloca i8
%5 = call i32 @serial_read(i32 %0, i8* %4, i32 1)
%6 = icmp slt i32 %5, 1
%7 = load i8, i8* %4
%8 = icmp ne i8 %7, 2
%9 = select i1 %6, i1 true, i1 %8
br i1 %9, label %20, label %10
10: ; preds = %3
%11 = call i32 @serial_read(i32 %0, i8* %1, i32 %2)
%12 = icmp slt i32 %11, %2
br i1 %12, label %20, label %13
13: ; preds = %10
%14 = call i32 @serial_read(i32 %0, i8* %4, i32 1)
%15 = icmp slt i32 %14, 1
%16 = load i8, i8* %4
%17 = icmp ne i8 %16, 3
%18 = select i1 %15, i1 true, i1 %17
%19 = sext i1 %18 to i32
br label %20
20: ; preds = %13, %10, %3
%21 = phi i32 [ -1, %3 ], [ -1, %10 ], [ %19, %13 ]
ret i32 %21
}