#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_write_byte(int);
int send_byte_escape(int val) {
  if (val == 0x100)
    return Serial_write_byte(0x55);
  else {
    if (val == 0x55) {
      if (Serial_write_byte(0xAA))
        return -1;
      return Serial_write_byte(0x01);
    } else if (val == 0xAA) {
      if (Serial_write_byte(0xAA))
        return -1;
      return Serial_write_byte(0x02);
    } else {
      return Serial_write_byte(val);
    }
  }
}


x86 assembly (clang, -O0):
<send_byte_escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x100,-0x8(%rbp)
jne    113a <send_byte_escape+0x2a>
mov    $0x55,%edi
call   1030 <Serial_write_byte@plt>
mov    %eax,-0x4(%rbp)
jmp    11be <send_byte_escape+0xae>
cmpl   $0x55,-0x8(%rbp)
jne    1175 <send_byte_escape+0x65>
mov    $0xaa,%edi
call   1030 <Serial_write_byte@plt>
cmp    $0x0,%eax
je     1163 <send_byte_escape+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <send_byte_escape+0xae>
mov    $0x1,%edi
call   1030 <Serial_write_byte@plt>
mov    %eax,-0x4(%rbp)
jmp    11be <send_byte_escape+0xae>
cmpl   $0xaa,-0x8(%rbp)
jne    11b3 <send_byte_escape+0xa3>
mov    $0xaa,%edi
call   1030 <Serial_write_byte@plt>
cmp    $0x0,%eax
je     11a1 <send_byte_escape+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <send_byte_escape+0xae>
mov    $0x2,%edi
call   1030 <Serial_write_byte@plt>
mov    %eax,-0x4(%rbp)
jmp    11be <send_byte_escape+0xae>
mov    -0x8(%rbp),%edi
call   1030 <Serial_write_byte@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_byte_escape>:
push   %rax
cmp    $0x100,%edi
je     113f <send_byte_escape+0x2f>
cmp    $0xaa,%edi
je     114a <send_byte_escape+0x3a>
cmp    $0x55,%edi
jne    1164 <send_byte_escape+0x54>
mov    $0xaa,%edi
call   1030 <Serial_write_byte@plt>
test   %eax,%eax
jne    1158 <send_byte_escape+0x48>
mov    $0x1,%edi
pop    %rax
jmp    1030 <Serial_write_byte@plt>
mov    $0x55,%edi
pop    %rax
jmp    1030 <Serial_write_byte@plt>
mov    $0xaa,%edi
call   1030 <Serial_write_byte@plt>
test   %eax,%eax
je     115f <send_byte_escape+0x4f>
mov    $0xffffffff,%eax
pop    %rcx
ret
mov    $0x2,%edi
pop    %rax
jmp    1030 <Serial_write_byte@plt>

LLVM IR (-O0):
define i32 @send_byte_escape(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = icmp eq i32 %4, 256
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = call i32 @Serial_write_byte(i32 85)
store i32 %7, i32* %2
br label %29
8: ; preds = %1
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, 85
br i1 %10, label %11, label %17
11: ; preds = %8
%12 = call i32 @Serial_write_byte(i32 170)
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %11
store i32 -1, i32* %2
br label %29
15: ; preds = %11
%16 = call i32 @Serial_write_byte(i32 1)
store i32 %16, i32* %2
br label %29
17: ; preds = %8
%18 = load i32, i32* %3
%19 = icmp eq i32 %18, 170
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = call i32 @Serial_write_byte(i32 170)
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 -1, i32* %2
br label %29
24: ; preds = %20
%25 = call i32 @Serial_write_byte(i32 2)
store i32 %25, i32* %2
br label %29
26: ; preds = %17
%27 = load i32, i32* %3
%28 = call i32 @Serial_write_byte(i32 %27)
store i32 %28, i32* %2
br label %29
29: ; preds = %26, %24, %23, %15, %14, %6
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
define i32 @send_byte_escape(i32 %0) {
switch i32 %0, label %8 [
i32 256, label %9
i32 85, label %2
i32 170, label %5
]
2: ; preds = %1
%3 = tail call i32 @Serial_write_byte(i32 170)
%4 = icmp eq i32 %3, 0
br i1 %4, label %9, label %12
5: ; preds = %1
%6 = tail call i32 @Serial_write_byte(i32 170)
%7 = icmp eq i32 %6, 0
br i1 %7, label %9, label %12
8: ; preds = %1
br label %9
9: ; preds = %5, %2, %1, %8
%10 = phi i32 [ %0, %8 ], [ 85, %1 ], [ 1, %2 ], [ 2, %5 ]
%11 = tail call i32 @Serial_write_byte(i32 %10)
br label %12
12: ; preds = %9, %5, %2
%13 = phi i32 [ -1, %2 ], [ -1, %5 ], [ %11, %9 ]
ret i32 %13
}