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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int pppfcs16(int, unsigned char *, unsigned int);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
int decaps_hdlc(int fd, int (*cb)(int cl, void *pack, unsigned len), int cl) {
  static unsigned char buffer[4096], copy[4096];
  static unsigned start = 0, end = 0;
  static unsigned len = 0, escape = 0;
  int status;
  if (start >= end) {
    if ((status = read(fd, buffer, sizeof(buffer))) < 0)
      return status;
    end = status;
    start = 0;
  }
  while (1) {
    if (start >= end)
      return 0;
    while (buffer[start] != 0x7E) {
      if (len < 4096)
        copy[len] = buffer[start] ^ ((escape) ? 0x20 : 0x00);
      if (buffer[start] == 0x7D && !escape)
        escape = 1;
      else {
        len++;
        escape = 0;
      }
      start++;
      if (start >= end)
        return 0;
    }
    start++;
    if ((len < 4) || (escape == 1)) {
      len = 0;
      escape = 0;
      continue;
    }
    if (pppfcs16(0xffff, copy, len) != 0xf0b8)
      fprintf(stderr, "Bad FCS\n");
    len -= sizeof(u_int16_t);
    if ((status = cb(cl, copy, len)) < 0)
      return status;
    len = 0;
    escape = 0;
  }
}


x86 assembly (clang, -O0):
<decaps_hdlc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x4f08(%rip),%eax
cmp    0x4f06(%rip),%eax
jb     1192 <decaps_hdlc+0x62>
mov    -0x8(%rbp),%edi
lea    0x2ef2(%rip),%rsi
mov    $0x1000,%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    117f <decaps_hdlc+0x4f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1374 <decaps_hdlc+0x244>
mov    -0x18(%rbp),%eax
mov    %eax,0x4ecc(%rip)
movl   $0x0,0x4ebe(%rip)
jmp    1197 <decaps_hdlc+0x67>
mov    0x4eb3(%rip),%eax
cmp    0x4eb1(%rip),%eax
jb     11b5 <decaps_hdlc+0x85>
movl   $0x0,-0x4(%rbp)
jmp    1374 <decaps_hdlc+0x244>
jmp    11ba <decaps_hdlc+0x8a>
mov    0x4e90(%rip),%eax
mov    %eax,%ecx
lea    0x2e87(%rip),%rax
movzbl (%rax,%rcx,1),%eax
cmp    $0x7e,%eax
je     12a5 <decaps_hdlc+0x175>
cmpl   $0x1000,0x4e78(%rip)
jae    1222 <decaps_hdlc+0xf2>
mov    0x4e64(%rip),%eax
mov    %eax,%ecx
lea    0x2e5b(%rip),%rax
movzbl (%rax,%rcx,1),%eax
mov    0x4e5d(%rip),%esi
xor    %ecx,%ecx
mov    $0x20,%edx
cmp    $0x0,%esi
cmovne %edx,%ecx
xor    %ecx,%eax
mov    %al,%dl
mov    0x4e42(%rip),%eax
mov    %eax,%ecx
lea    0x3e31(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    0x4e28(%rip),%eax
mov    %eax,%ecx
lea    0x2e1f(%rip),%rax
movzbl (%rax,%rcx,1),%eax
cmp    $0x7d,%eax
jne    125a <decaps_hdlc+0x12a>
cmpl   $0x0,0x4e17(%rip)
jne    125a <decaps_hdlc+0x12a>
movl   $0x1,0x4e07(%rip)
jmp    1273 <decaps_hdlc+0x143>
mov    0x4df8(%rip),%eax
add    $0x1,%eax
mov    %eax,0x4def(%rip)
movl   $0x0,0x4de9(%rip)
mov    0x4dd7(%rip),%eax
add    $0x1,%eax
mov    %eax,0x4dce(%rip)
mov    0x4dc8(%rip),%eax
cmp    0x4dc6(%rip),%eax
jb     12a0 <decaps_hdlc+0x170>
movl   $0x0,-0x4(%rbp)
jmp    1374 <decaps_hdlc+0x244>
jmp    11ba <decaps_hdlc+0x8a>
mov    0x4da5(%rip),%eax
add    $0x1,%eax
mov    %eax,0x4d9c(%rip)
cmpl   $0x4,0x4d9d(%rip)
jb     12ce <decaps_hdlc+0x19e>
cmpl   $0x1,0x4d94(%rip)
jne    12e7 <decaps_hdlc+0x1b7>
movl   $0x0,0x4d80(%rip)
movl   $0x0,0x4d7a(%rip)
jmp    1197 <decaps_hdlc+0x67>
mov    0x4d6b(%rip),%edx
mov    $0xffff,%edi
lea    0x3d57(%rip),%rsi
call   1040 <pppfcs16@plt>
cmp    $0xf0b8,%eax
je     131e <decaps_hdlc+0x1ee>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
lea    0xce7(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x4d34(%rip),%eax
sub    $0x4,%rax
mov    %eax,0x4d2a(%rip)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%edi
mov    0x4d1d(%rip),%edx
lea    0x3d0e(%rip),%rsi
call   *%rax
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    135b <decaps_hdlc+0x22b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1374 <decaps_hdlc+0x244>
movl   $0x0,0x4cf3(%rip)
movl   $0x0,0x4ced(%rip)
jmp    1197 <decaps_hdlc+0x67>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decaps_hdlc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r12
mov    0x4f09(%rip),%esi
mov    0x4f07(%rip),%eax
cmp    %eax,%esi
jb     117c <decaps_hdlc+0x4c>
lea    0x2ef8(%rip),%rsi
mov    $0x1000,%edx
call   1030 <read@plt>
test   %eax,%eax
js     12ae <decaps_hdlc+0x17e>
mov    %eax,0x4ee4(%rip)
movl   $0x0,0x4ed6(%rip)
xor    %esi,%esi
cmp    %eax,%esi
jae    129b <decaps_hdlc+0x16b>
mov    0x4ece(%rip),%edx
mov    0x4ecb(%rip),%dil
lea    0x2eb8(%rip),%r13
lea    0x3eb1(%rip),%r15
mov    %esi,%esi
jmp    11d2 <decaps_hdlc+0xa2>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    $0x1,%bl
mov    %ebx,%ecx
and    $0x1,%cl
mov    %cl,0x4e9e(%rip)
add    $0x1,%rsi
mov    %esi,0x4e88(%rip)
mov    %ebx,%edi
cmp    %eax,%esi
jae    129b <decaps_hdlc+0x16b>
movzbl (%rsi,%r13,1),%ebx
cmp    $0x7e,%bl
je     1210 <decaps_hdlc+0xe0>
cmp    $0xfff,%edx
ja     11f5 <decaps_hdlc+0xc5>
mov    %edi,%ecx
and    $0x1,%cl
shl    $0x5,%cl
xor    %bl,%cl
mov    %edx,%ebp
mov    %cl,0x0(%rbp,%r15,1)
cmp    $0x7d,%bl
setne  %bl
or     %dil,%bl
test   $0x1,%bl
je     11b0 <decaps_hdlc+0x80>
add    $0x1,%edx
mov    %edx,0x4e4c(%rip)
jmp    11b0 <decaps_hdlc+0x80>
xchg   %ax,%ax
add    $0x1,%rsi
mov    %esi,0x4e36(%rip)
cmp    $0x4,%edx
setb   %cl
or     %dil,%cl
test   $0x1,%cl
jne    1279 <decaps_hdlc+0x149>
mov    $0xffff,%edi
mov    %r15,%rsi
call   1040 <pppfcs16@plt>
cmp    $0xf0b8,%eax
je     1251 <decaps_hdlc+0x121>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xdb4(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x4e01(%rip),%edx
add    $0xfffffffc,%edx
mov    %edx,0x4df8(%rip)
mov    %r14d,%edi
mov    %r15,%rsi
call   *%r12
test   %eax,%eax
js     12ae <decaps_hdlc+0x17e>
mov    0x4ddd(%rip),%esi
mov    0x4ddb(%rip),%eax
movl   $0x0,0x4dd5(%rip)
movb   $0x0,0x4dd2(%rip)
xor    %edi,%edi
mov    $0x0,%edx
cmp    %eax,%esi
jb     119f <decaps_hdlc+0x6f>
jmp    129d <decaps_hdlc+0x16d>
xor    %edi,%edi
mov    %edi,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%edi
jmp    129d <decaps_hdlc+0x16d>

LLVM IR (-O0):
@decaps_hdlc.buffer = internal global [4096 x i8] zeroinitializer
@decaps_hdlc.copy = internal global [4096 x i8] zeroinitializer
@decaps_hdlc.start = internal global i32 0
@decaps_hdlc.end = internal global i32 0
@decaps_hdlc.len = internal global i32 0
@decaps_hdlc.escape = internal global i32 0
@stderr = global i32 0
@.str = [9 x i8] c"Bad FCS\0A\00"
define i32 @decaps_hdlc(i32 %0, i32 (i32, i8*, i32)* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32 (i32, i8*, i32)*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i32 (i32, i8*, i32)* %1, i32 (i32, i8*, i32)** %6
store i32 %2, i32* %7
%9 = load i32, i32* @decaps_hdlc.start
%10 = load i32, i32* @decaps_hdlc.end
%11 = icmp uge i32 %9, %10
br i1 %11, label %12, label %20
12: ; preds = %3
%13 = load i32, i32* %5
%14 = call i32 @read(i32 %13, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @decaps_hdlc.buffer, i64 0, i64 0), i32 4096)
store i32 %14, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = load i32, i32* %8
store i32 %17, i32* %4
br label %103
18: ; preds = %12
%19 = load i32, i32* %8
store i32 %19, i32* @decaps_hdlc.end
store i32 0, i32* @decaps_hdlc.start
br label %20
20: ; preds = %18, %3
br label %21
21: ; preds = %20, %82, %102
%22 = load i32, i32* @decaps_hdlc.start
%23 = load i32, i32* @decaps_hdlc.end
%24 = icmp uge i32 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 0, i32* %4
br label %103
26: ; preds = %21
br label %27
27: ; preds = %73, %26
%28 = load i32, i32* @decaps_hdlc.start
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* @decaps_hdlc.buffer, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
%33 = icmp ne i32 %32, 126
br i1 %33, label %34, label %74
34: ; preds = %27
%35 = load i32, i32* @decaps_hdlc.len
%36 = icmp ult i32 %35, 4096
br i1 %36, label %37, label %52
37: ; preds = %34
%38 = load i32, i32* @decaps_hdlc.start
%39 = zext i32 %38 to i64
%40 = getelementptr inbounds [4096 x i8], [4096 x i8]* @decaps_hdlc.buffer, i64 0, i64 %39
%41 = load i8, i8* %40
%42 = zext i8 %41 to i32
%43 = load i32, i32* @decaps_hdlc.escape
%44 = icmp ne i32 %43, 0
%45 = zext i1 %44 to i64
%46 = select i1 %44, i32 32, i32 0
%47 = xor i32 %42, %46
%48 = trunc i32 %47 to i8
%49 = load i32, i32* @decaps_hdlc.len
%50 = zext i32 %49 to i64
%51 = getelementptr inbounds [4096 x i8], [4096 x i8]* @decaps_hdlc.copy, i64 0, i64 %50
store i8 %48, i8* %51
br label %52
52: ; preds = %37, %34
%53 = load i32, i32* @decaps_hdlc.start
%54 = zext i32 %53 to i64
%55 = getelementptr inbounds [4096 x i8], [4096 x i8]* @decaps_hdlc.buffer, i64 0, i64 %54
%56 = load i8, i8* %55
%57 = zext i8 %56 to i32
%58 = icmp eq i32 %57, 125
br i1 %58, label %59, label %63
59: ; preds = %52
%60 = load i32, i32* @decaps_hdlc.escape
%61 = icmp ne i32 %60, 0
br i1 %61, label %63, label %62
62: ; preds = %59
store i32 1, i32* @decaps_hdlc.escape
br label %66
63: ; preds = %59, %52
%64 = load i32, i32* @decaps_hdlc.len
%65 = add i32 %64, 1
store i32 %65, i32* @decaps_hdlc.len
store i32 0, i32* @decaps_hdlc.escape
br label %66
66: ; preds = %63, %62
%67 = load i32, i32* @decaps_hdlc.start
%68 = add i32 %67, 1
store i32 %68, i32* @decaps_hdlc.start
%69 = load i32, i32* @decaps_hdlc.start
%70 = load i32, i32* @decaps_hdlc.end
%71 = icmp uge i32 %69, %70
br i1 %71, label %72, label %73
72: ; preds = %66
store i32 0, i32* %4
br label %103
73: ; preds = %66
br label %27
74: ; preds = %27
%75 = load i32, i32* @decaps_hdlc.start
%76 = add i32 %75, 1
store i32 %76, i32* @decaps_hdlc.start
%77 = load i32, i32* @decaps_hdlc.len
%78 = icmp ult i32 %77, 4
br i1 %78, label %82, label %79
79: ; preds = %74
%80 = load i32, i32* @decaps_hdlc.escape
%81 = icmp eq i32 %80, 1
br i1 %81, label %82, label %83
82: ; preds = %79, %74
store i32 0, i32* @decaps_hdlc.len
store i32 0, i32* @decaps_hdlc.escape
br label %21
83: ; preds = %79
%84 = load i32, i32* @decaps_hdlc.len
%85 = call i32 @pppfcs16(i32 65535, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @decaps_hdlc.copy, i64 0, i64 0), i32 %84)
%86 = icmp ne i32 %85, 61624
br i1 %86, label %87, label %90
87: ; preds = %83
%88 = load i32, i32* @stderr
%89 = call i32 @fprintf(i32 %88, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %90
90: ; preds = %87, %83
%91 = load i32, i32* @decaps_hdlc.len
%92 = zext i32 %91 to i64
%93 = sub i64 %92, 4
%94 = trunc i64 %93 to i32
store i32 %94, i32* @decaps_hdlc.len
%95 = load i32 (i32, i8*, i32)*, i32 (i32, i8*, i32)** %6
%96 = load i32, i32* %7
%97 = load i32, i32* @decaps_hdlc.len
%98 = call i32 %95(i32 %96, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @decaps_hdlc.copy, i64 0, i64 0), i32 %97)
store i32 %98, i32* %8
%99 = icmp slt i32 %98, 0
br i1 %99, label %100, label %102
100: ; preds = %90
%101 = load i32, i32* %8
store i32 %101, i32* %4
br label %103
102: ; preds = %90
store i32 0, i32* @decaps_hdlc.len
store i32 0, i32* @decaps_hdlc.escape
br label %21
103: ; preds = %100, %72, %25, %16
%104 = load i32, i32* %4
ret i32 %104
}

LLVM IR (-O2):
@decaps_hdlc.buffer = internal global [4096 x i8] zeroinitializer
@decaps_hdlc.copy = internal global [4096 x i8] zeroinitializer
@decaps_hdlc.start = internal unnamed_addr global i32 0
@decaps_hdlc.end = internal unnamed_addr global i32 0
@decaps_hdlc.len = internal unnamed_addr global i32 0
@decaps_hdlc.escape = internal unnamed_addr global i1 false
@stderr = global i32 0
@.str = [9 x i8] c"Bad FCS\0A\00"
define i32 @decaps_hdlc(i32 %0, i32 (i32, i8*, i32)* nocapture %1, i32 %2) {
%4 = load i32, i32* @decaps_hdlc.start
%5 = load i32, i32* @decaps_hdlc.end
%6 = icmp ult i32 %4, %5
br i1 %6, label %11, label %7
7: ; preds = %3
%8 = tail call i32 @read(i32 %0, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @decaps_hdlc.buffer, i64 0, i64 0), i32 4096)
%9 = icmp slt i32 %8, 0
br i1 %9, label %70, label %10
10: ; preds = %7
store i32 %8, i32* @decaps_hdlc.end
store i32 0, i32* @decaps_hdlc.start
br label %11
11: ; preds = %10, %3
%12 = phi i32 [ %8, %10 ], [ %5, %3 ]
%13 = phi i32 [ 0, %10 ], [ %4, %3 ]
%14 = icmp ult i32 %13, %12
br i1 %14, label %15, label %70
15: ; preds = %11
%16 = load i32, i32* @decaps_hdlc.len
%17 = load i1, i1* @decaps_hdlc.escape
br label %18
18: ; preds = %15, %52
%19 = phi i32 [ %53, %52 ], [ %12, %15 ]
%20 = phi i1 [ false, %52 ], [ %17, %15 ]
%21 = phi i32 [ 0, %52 ], [ %16, %15 ]
%22 = phi i32 [ %54, %52 ], [ %13, %15 ]
br label %23
23: ; preds = %18, %43
%24 = phi i1 [ %20, %18 ], [ %45, %43 ]
%25 = phi i32 [ %21, %18 ], [ %44, %43 ]
%26 = phi i32 [ %22, %18 ], [ %46, %43 ]
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* @decaps_hdlc.buffer, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 126
br i1 %30, label %48, label %31
31: ; preds = %23
%32 = icmp ult i32 %25, 4096
br i1 %32, label %33, label %38
33: ; preds = %31
%34 = select i1 %24, i8 32, i8 0
%35 = xor i8 %34, %29
%36 = zext i32 %25 to i64
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* @decaps_hdlc.copy, i64 0, i64 %36
store i8 %35, i8* %37
br label %38
38: ; preds = %33, %31
%39 = icmp ne i8 %29, 125
%40 = select i1 %39, i1 true, i1 %24
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = add i32 %25, 1
store i32 %42, i32* @decaps_hdlc.len
br label %43
43: ; preds = %38, %41
%44 = phi i32 [ %25, %38 ], [ %42, %41 ]
%45 = xor i1 %40, true
store i1 %45, i1* @decaps_hdlc.escape
%46 = add nuw i32 %26, 1
store i32 %46, i32* @decaps_hdlc.start
%47 = icmp ult i32 %46, %19
br i1 %47, label %23, label %70
48: ; preds = %23
%49 = add nuw i32 %26, 1
store i32 %49, i32* @decaps_hdlc.start
%50 = icmp ult i32 %25, 4
%51 = select i1 %50, i1 true, i1 %24
br i1 %51, label %52, label %56
52: ; preds = %48, %67
%53 = phi i32 [ %69, %67 ], [ %19, %48 ]
%54 = phi i32 [ %68, %67 ], [ %49, %48 ]
store i32 0, i32* @decaps_hdlc.len
store i1 false, i1* @decaps_hdlc.escape
%55 = icmp ult i32 %54, %53
br i1 %55, label %18, label %70
56: ; preds = %48
%57 = tail call i32 @pppfcs16(i32 65535, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @decaps_hdlc.copy, i64 0, i64 0), i32 %25)
%58 = icmp eq i32 %57, 61624
br i1 %58, label %62, label %59
59: ; preds = %56
%60 = load i32, i32* @stderr
%61 = tail call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %62
62: ; preds = %59, %56
%63 = load i32, i32* @decaps_hdlc.len
%64 = add i32 %63, -4
store i32 %64, i32* @decaps_hdlc.len
%65 = tail call i32 %1(i32 %2, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @decaps_hdlc.copy, i64 0, i64 0), i32 %64)
%66 = icmp slt i32 %65, 0
br i1 %66, label %70, label %67
67: ; preds = %62
%68 = load i32, i32* @decaps_hdlc.start
%69 = load i32, i32* @decaps_hdlc.end
br label %52
70: ; preds = %52, %62, %43, %11, %7
%71 = phi i32 [ %8, %7 ], [ 0, %11 ], [ 0, %43 ], [ 0, %52 ], [ %65, %62 ]
ret i32 %71
}