#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int read(int, unsigned char *, int);
int xserial_port_read_packet(int serline, unsigned char *buffer) {
  unsigned char c;
  int err, i = 0;
  buffer[i] = 0x7e;
  while (1) {
    err = read(serline, &c, 1);
    if (err < 0) {
      perror("error reading from serial port");
      exit(2);
    }
    if (err == 1) {
      if (++i > 255)
        return i;
      buffer[i] = c;
      if (c == 0x7e)
        return i;
    }
  }
}


x86 assembly (clang, -O0):
<xserial_port_read_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x7e,(%rax,%rcx,1)
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1186 <xserial_port_read_packet+0x56>
lea    0xe89(%rip),%rdi
call   1040 <perror@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
cmpl   $0x1,-0x18(%rbp)
jne    11da <xserial_port_read_packet+0xaa>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmp    $0xff,%eax
jle    11af <xserial_port_read_packet+0x7f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11df <xserial_port_read_packet+0xaf>
mov    -0x11(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
movzbl -0x11(%rbp),%eax
cmp    $0x7e,%eax
jne    11d5 <xserial_port_read_packet+0xa5>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11df <xserial_port_read_packet+0xaf>
jmp    11da <xserial_port_read_packet+0xaa>
jmp    1152 <xserial_port_read_packet+0x22>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xserial_port_read_packet>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
movb   $0x7e,(%rsi)
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
js     11a0 <xserial_port_read_packet+0x70>
xor    %ebx,%ebx
lea    0x7(%rsp),%r15
jmp    1173 <xserial_port_read_packet+0x43>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
js     11a0 <xserial_port_read_packet+0x70>
cmp    $0x1,%eax
jne    1160 <xserial_port_read_packet+0x30>
movslq %ebx,%rax
lea    0x1(%rax),%rbx
cmp    $0xfe,%eax
jg     1193 <xserial_port_read_packet+0x63>
movzbl 0x7(%rsp),%eax
mov    %al,(%r14,%rbx,1)
cmp    $0x7e,%al
jne    1160 <xserial_port_read_packet+0x30>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe59(%rip),%rdi
call   1040 <perror@plt>
mov    $0x2,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [31 x i8] c"error reading from serial port\00"
define i32 @xserial_port_read_packet(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %8
%9 = load i8*, i8** %5
%10 = load i32, i32* %8
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
store i8 126, i8* %12
br label %13
13: ; preds = %2, %42
%14 = load i32, i32* %4
%15 = call i32 @read(i32 %14, i8* %6, i32 1)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 2)
unreachable
21: ; preds = %13
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %42
24: ; preds = %21
%25 = load i32, i32* %8
%26 = add nsw i32 %25, 1
store i32 %26, i32* %8
%27 = icmp sgt i32 %26, 255
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = load i32, i32* %8
store i32 %29, i32* %3
br label %43
30: ; preds = %24
%31 = load i8, i8* %6
%32 = load i8*, i8** %5
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 %31, i8* %35
%36 = load i8, i8* %6
%37 = zext i8 %36 to i32
%38 = icmp eq i32 %37, 126
br i1 %38, label %39, label %41
39: ; preds = %30
%40 = load i32, i32* %8
store i32 %40, i32* %3
br label %43
41: ; preds = %30
br label %42
42: ; preds = %41, %21
br label %13
43: ; preds = %39, %28
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@.str = [31 x i8] c"error reading from serial port\00"
define i32 @xserial_port_read_packet(i32 %0, i8* nocapture writeonly %1) {
%3 = alloca i8
store i8 126, i8* %1
%4 = call i32 @read(i32 %0, i8* %3, i32 1)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %21, %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 2)
unreachable
9: ; preds = %2, %21
%10 = phi i32 [ %23, %21 ], [ %4, %2 ]
%11 = phi i32 [ %22, %21 ], [ 0, %2 ]
%12 = icmp eq i32 %10, 1
br i1 %12, label %13, label %21
13: ; preds = %9
%14 = add nsw i32 %11, 1
%15 = icmp sgt i32 %11, 254
br i1 %15, label %25, label %16
16: ; preds = %13
%17 = load i8, i8* %3
%18 = sext i32 %14 to i64
%19 = getelementptr inbounds i8, i8* %1, i64 %18
store i8 %17, i8* %19
%20 = icmp eq i8 %17, 126
br i1 %20, label %25, label %21
21: ; preds = %16, %9
%22 = phi i32 [ %14, %16 ], [ %11, %9 ]
%23 = call i32 @read(i32 %0, i8* %3, i32 1)
%24 = icmp slt i32 %23, 0
br i1 %24, label %6, label %9
25: ; preds = %16, %13
ret i32 %14
}