#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*/ infohelper_output(int /*<<< orphan*/, char *);
int serialport_read(unsigned char *, int);
int serialport_receive_slip(unsigned char *data, unsigned int size) {
  unsigned int received;
  unsigned char cur_byte;
  received = 0;
  while (received != size) {
    if (serialport_read(&cur_byte, 1) != 1) {
      infohelper_output(0, "failed reading byte");
      return 0;
    }
    if (cur_byte == 0xDB) {
      if (serialport_read(&cur_byte, 1) != 1) {
        infohelper_output(0, "failed reading byte for unslip");
        return 0;
      }
      if (cur_byte == 0xDC) {
        *data++ = 0xC0;
      } else if (cur_byte == 0xDD) {
        *data++ = 0xDB;
      } else {
        infohelper_output(0, "unslip sequence wrong");
        return 0;
      }
    } else {
      *data++ = cur_byte;
    }
    received++;
  }
  return received;
}


x86 assembly (clang, -O0):
<serialport_receive_slip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     1245 <serialport_receive_slip+0x125>
lea    -0x19(%rbp),%rdi
mov    $0x1,%esi
call   1030 <serialport_read@plt>
cmp    $0x1,%eax
je     1173 <serialport_receive_slip+0x53>
xor    %edi,%edi
lea    0xe9e(%rip),%rsi
call   1040 <infohelper_output@plt>
movl   $0x0,-0x4(%rbp)
jmp    124b <serialport_receive_slip+0x12b>
movzbl -0x19(%rbp),%eax
cmp    $0xdb,%eax
jne    1223 <serialport_receive_slip+0x103>
lea    -0x19(%rbp),%rdi
mov    $0x1,%esi
call   1030 <serialport_read@plt>
cmp    $0x1,%eax
je     11b3 <serialport_receive_slip+0x93>
xor    %edi,%edi
lea    0xe72(%rip),%rsi
call   1040 <infohelper_output@plt>
movl   $0x0,-0x4(%rbp)
jmp    124b <serialport_receive_slip+0x12b>
movzbl -0x19(%rbp),%eax
cmp    $0xdc,%eax
jne    11d9 <serialport_receive_slip+0xb9>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0xc0,(%rax)
jmp    121e <serialport_receive_slip+0xfe>
movzbl -0x19(%rbp),%eax
cmp    $0xdd,%eax
jne    11ff <serialport_receive_slip+0xdf>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0xdb,(%rax)
jmp    1219 <serialport_receive_slip+0xf9>
xor    %edi,%edi
lea    0xe2b(%rip),%rsi
call   1040 <infohelper_output@plt>
movl   $0x0,-0x4(%rbp)
jmp    124b <serialport_receive_slip+0x12b>
jmp    121e <serialport_receive_slip+0xfe>
jmp    1237 <serialport_receive_slip+0x117>
mov    -0x19(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1136 <serialport_receive_slip+0x16>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serialport_receive_slip>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
je     119f <serialport_receive_slip+0x7f>
mov    %rdi,%r15
mov    %esi,%r13d
xor    %ebx,%ebx
lea    0x7(%rsp),%r12
lea    0xebd(%rip),%r14
lea    0xeca(%rip),%rbp
jmp    115d <serialport_receive_slip+0x3d>
nopl   0x0(%rax)
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %ebx,%r13d
je     11bd <serialport_receive_slip+0x9d>
mov    %r12,%rdi
mov    $0x1,%esi
call   1030 <serialport_read@plt>
cmp    $0x1,%eax
jne    11b0 <serialport_receive_slip+0x90>
movzbl 0x7(%rsp),%eax
cmp    $0xdb,%al
jne    1150 <serialport_receive_slip+0x30>
mov    %r12,%rdi
mov    $0x1,%esi
call   1030 <serialport_read@plt>
cmp    $0x1,%eax
jne    11a4 <serialport_receive_slip+0x84>
mov    $0xc0,%al
movzbl 0x7(%rsp),%ecx
cmp    $0xdc,%cl
je     1150 <serialport_receive_slip+0x30>
cmp    $0xdd,%cl
jne    11a9 <serialport_receive_slip+0x89>
mov    $0xdb,%al
jmp    1150 <serialport_receive_slip+0x30>
xor    %r13d,%r13d
jmp    11bd <serialport_receive_slip+0x9d>
mov    %rbp,%r14
jmp    11b0 <serialport_receive_slip+0x90>
lea    0xe83(%rip),%r14
xor    %r13d,%r13d
xor    %edi,%edi
mov    %r14,%rsi
call   1040 <infohelper_output@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"failed reading byte\00"
@.str.1 = [31 x i8] c"failed reading byte for unslip\00"
@.str.2 = [22 x i8] c"unslip sequence wrong\00"
define i32 @serialport_receive_slip(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
br label %8
8: ; preds = %48, %2
%9 = load i32, i32* %6
%10 = load i32, i32* %5
%11 = icmp ne i32 %9, %10
br i1 %11, label %12, label %51
12: ; preds = %8
%13 = call i32 @serialport_read(i8* %7, i32 1)
%14 = icmp ne i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @infohelper_output(i32 0, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %53
17: ; preds = %12
%18 = load i8, i8* %7
%19 = zext i8 %18 to i32
%20 = icmp eq i32 %19, 219
br i1 %20, label %21, label %44
21: ; preds = %17
%22 = call i32 @serialport_read(i8* %7, i32 1)
%23 = icmp ne i32 %22, 1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @infohelper_output(i32 0, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %53
26: ; preds = %21
%27 = load i8, i8* %7
%28 = zext i8 %27 to i32
%29 = icmp eq i32 %28, 220
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %4
store i8 -64, i8* %31
br label %43
33: ; preds = %26
%34 = load i8, i8* %7
%35 = zext i8 %34 to i32
%36 = icmp eq i32 %35, 221
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i8*, i8** %4
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %4
store i8 -37, i8* %38
br label %42
40: ; preds = %33
%41 = call i32 @infohelper_output(i32 0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %53
42: ; preds = %37
br label %43
43: ; preds = %42, %30
br label %48
44: ; preds = %17
%45 = load i8, i8* %7
%46 = load i8*, i8** %4
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %4
store i8 %45, i8* %46
br label %48
48: ; preds = %44, %43
%49 = load i32, i32* %6
%50 = add i32 %49, 1
store i32 %50, i32* %6
br label %8
51: ; preds = %8
%52 = load i32, i32* %6
store i32 %52, i32* %3
br label %53
53: ; preds = %51, %40, %24, %15
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@.str = [20 x i8] c"failed reading byte\00"
@.str.1 = [31 x i8] c"failed reading byte for unslip\00"
@.str.2 = [22 x i8] c"unslip sequence wrong\00"
define i32 @serialport_receive_slip(i8* nocapture writeonly %0, i32 %1) {
%3 = alloca i8
%4 = icmp eq i32 %1, 0
br i1 %4, label %27, label %5
5: ; preds = %2, %19
%6 = phi i32 [ %22, %19 ], [ 0, %2 ]
%7 = phi i8* [ %21, %19 ], [ %0, %2 ]
%8 = call i32 @serialport_read(i8* %3, i32 1)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %24
10: ; preds = %5
%11 = load i8, i8* %3
%12 = icmp eq i8 %11, -37
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = call i32 @serialport_read(i8* %3, i32 1)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %24
16: ; preds = %13
%17 = load i8, i8* %3
switch i8 %17, label %24 [
i8 -36, label %19
i8 -35, label %18
]
18: ; preds = %16
br label %19
19: ; preds = %10, %16, %18
%20 = phi i8 [ -37, %18 ], [ -64, %16 ], [ %11, %10 ]
store i8 %20, i8* %7
%21 = getelementptr inbounds i8, i8* %7, i64 1
%22 = add nuw i32 %6, 1
%23 = icmp eq i32 %22, %1
br i1 %23, label %27, label %5
24: ; preds = %16, %13, %5
%25 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), %5 ], [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), %16 ]
%26 = call i32 @infohelper_output(i32 0, i8* %25)
br label %27
27: ; preds = %19, %24, %2
%28 = phi i32 [ 0, %2 ], [ 0, %24 ], [ %1, %19 ]
ret i32 %28
}