void kdp_serialize_packet(unsigned char *packet, unsigned int len,
                          void (*outFunc)(char)) {
  unsigned int index;
  outFunc(0xFA);
  for (index = 0; index < len; index++) {
    unsigned char byte = *packet++;
    if (byte == 0xFA || byte == 0xFB || byte == 0xFE || byte == '\n') {
      outFunc(0xFE);
      byte = ~byte;
    }
    outFunc(byte);
  }
  outFunc(0xFB);
}


x86 assembly (clang, -O0):
<kdp_serialize_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0xfffffffa,%edi
call   *-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    11a6 <kdp_serialize_packet+0xa6>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%al
mov    %al,-0x1d(%rbp)
movzbl -0x1d(%rbp),%eax
cmp    $0xfa,%eax
je     117c <kdp_serialize_packet+0x7c>
movzbl -0x1d(%rbp),%eax
cmp    $0xfb,%eax
je     117c <kdp_serialize_packet+0x7c>
movzbl -0x1d(%rbp),%eax
cmp    $0xfe,%eax
je     117c <kdp_serialize_packet+0x7c>
movzbl -0x1d(%rbp),%eax
cmp    $0xa,%eax
jne    118e <kdp_serialize_packet+0x8e>
mov    $0xfffffffe,%edi
call   *-0x18(%rbp)
movzbl -0x1d(%rbp),%eax
xor    $0xffffffff,%eax
mov    %al,-0x1d(%rbp)
mov    -0x18(%rbp),%rax
movsbl -0x1d(%rbp),%edi
call   *%rax
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1122 <kdp_serialize_packet+0x22>
mov    $0xfffffffb,%edi
call   *-0x18(%rbp)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kdp_serialize_packet>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r14
mov    $0xfffffffa,%edi
mov    %rdx,%r15
call   *%rdx
test   %ebp,%ebp
je     116c <kdp_serialize_packet+0x6c>
mov    %ebp,%r12d
xor    %ebx,%ebx
jmp    113f <kdp_serialize_packet+0x3f>
mov    %r15,%rax
nopl   0x0(%rax,%rax,1)
movsbl %bpl,%edi
call   *%rax
add    $0x1,%rbx
cmp    %ebx,%r12d
je     116c <kdp_serialize_packet+0x6c>
movzbl (%r14,%rbx,1),%ebp
lea    0x6(%rbp),%eax
cmp    $0x10,%al
ja     1125 <kdp_serialize_packet+0x25>
movzbl %al,%eax
mov    $0x10013,%ecx
bt     %rax,%rcx
jae    1125 <kdp_serialize_packet+0x25>
mov    $0xfffffffe,%edi
mov    %r15,%r13
call   *%r15
mov    %r15,%rax
not    %bpl
jmp    1130 <kdp_serialize_packet+0x30>
mov    $0xfffffffb,%edi
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    *%rax

LLVM IR (-O0):
define void @kdp_serialize_packet(i8* %0, i32 %1, void (i8)* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca void (i8)*
%7 = alloca i32
%8 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
store void (i8)* %2, void (i8)** %6
%9 = load void (i8)*, void (i8)** %6
store i32 0, i32* %7
br label %10
10: ; preds = %42, %3
%11 = load i32, i32* %7
%12 = load i32, i32* %5
%13 = icmp ult i32 %11, %12
br i1 %13, label %14, label %45
14: ; preds = %10
%15 = load i8*, i8** %4
%16 = getelementptr inbounds i8, i8* %15, i32 1
store i8* %16, i8** %4
%17 = load i8, i8* %15
store i8 %17, i8* %8
%18 = load i8, i8* %8
%19 = zext i8 %18 to i32
%20 = icmp eq i32 %19, 250
br i1 %20, label %33, label %21
21: ; preds = %14
%22 = load i8, i8* %8
%23 = zext i8 %22 to i32
%24 = icmp eq i32 %23, 251
br i1 %24, label %33, label %25
25: ; preds = %21
%26 = load i8, i8* %8
%27 = zext i8 %26 to i32
%28 = icmp eq i32 %27, 254
br i1 %28, label %33, label %29
29: ; preds = %25
%30 = load i8, i8* %8
%31 = zext i8 %30 to i32
%32 = icmp eq i32 %31, 10
br i1 %32, label %33, label %39
33: ; preds = %29, %25, %21, %14
%34 = load void (i8)*, void (i8)** %6
%35 = load i8, i8* %8
%36 = zext i8 %35 to i32
%37 = xor i32 %36, -1
%38 = trunc i32 %37 to i8
store i8 %38, i8* %8
br label %39
39: ; preds = %33, %29
%40 = load void (i8)*, void (i8)** %6
%41 = load i8, i8* %8
br label %42
42: ; preds = %39
%43 = load i32, i32* %7
%44 = add i32 %43, 1
store i32 %44, i32* %7
br label %10
45: ; preds = %10
%46 = load void (i8)*, void (i8)** %6
ret void
}

LLVM IR (-O2):
define void @kdp_serialize_packet(i8* nocapture %0, i32 %1, void (i8)* nocapture %2) {
tail call void %2(i8 signext -6)
%4 = icmp eq i32 %1, 0
br i1 %4, label %16, label %5
5: ; preds = %3, %12
%6 = phi i32 [ %14, %12 ], [ 0, %3 ]
%7 = phi i8* [ %8, %12 ], [ %0, %3 ]
%8 = getelementptr inbounds i8, i8* %7, i64 1
%9 = load i8, i8* %7
switch i8 %9, label %12 [
i8 -2, label %10
i8 -5, label %10
i8 -6, label %10
i8 10, label %10
]
10: ; preds = %5, %5, %5, %5
tail call void %2(i8 signext -2)
%11 = xor i8 %9, -1
br label %12
12: ; preds = %5, %10
%13 = phi i8 [ %11, %10 ], [ %9, %5 ]
tail call void %2(i8 signext %13)
%14 = add nuw i32 %6, 1
%15 = icmp eq i32 %14, %1
br i1 %15, label %16, label %5
16: ; preds = %12, %3
tail call void %2(i8 signext -5)
ret void
}