#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 u_int8_t;

/* Variables and functions */
int /*<<< orphan*/ slip_send(int, int);
void write_to_serial(int outfd, void *inbuf, int len) {
  u_int8_t *p = inbuf;
  int i;
  for (i = 0; i < len; i++) {
    switch (p[i]) {
    case 0300:
      slip_send(outfd, 0333);
      slip_send(outfd, 0334);
      break;
    case 0333:
      slip_send(outfd, 0333);
      slip_send(outfd, 0335);
      break;
    default:
      slip_send(outfd, p[i]);
      break;
    }
  }
  slip_send(outfd, 0300);
  do {
  } while (0);
}


x86 assembly (clang, -O0):
<write_to_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11d2 <write_to_serial+0xc2>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x28(%rbp)
sub    $0xc0,%eax
je     116e <write_to_serial+0x5e>
jmp    115b <write_to_serial+0x4b>
mov    -0x28(%rbp),%eax
sub    $0xdb,%eax
je     118d <write_to_serial+0x7d>
jmp    11ac <write_to_serial+0x9c>
mov    -0x4(%rbp),%edi
mov    $0xdb,%esi
call   1030 <slip_send@plt>
mov    -0x4(%rbp),%edi
mov    $0xdc,%esi
call   1030 <slip_send@plt>
jmp    11bf <write_to_serial+0xaf>
mov    -0x4(%rbp),%edi
mov    $0xdb,%esi
call   1030 <slip_send@plt>
mov    -0x4(%rbp),%edi
mov    $0xdd,%esi
call   1030 <slip_send@plt>
jmp    11bf <write_to_serial+0xaf>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
call   1030 <slip_send@plt>
jmp    11c4 <write_to_serial+0xb4>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1131 <write_to_serial+0x21>
mov    -0x4(%rbp),%edi
mov    $0xc0,%esi
call   1030 <slip_send@plt>
jmp    11e4 <write_to_serial+0xd4>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_serial>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r12d
test   %edx,%edx
jle    1170 <write_to_serial+0x60>
mov    %rsi,%r14
mov    %edx,%r15d
xor    %ebx,%ebx
jmp    1155 <write_to_serial+0x45>
nopl   0x0(%rax)
mov    $0xdc,%ebp
mov    %r12d,%edi
mov    $0xdb,%esi
call   1030 <slip_send@plt>
mov    %r12d,%edi
mov    %ebp,%esi
call   1030 <slip_send@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
je     1170 <write_to_serial+0x60>
mov    (%r14,%rbx,4),%ebp
cmp    $0xc0,%ebp
je     1130 <write_to_serial+0x20>
cmp    $0xdb,%ebp
jne    1142 <write_to_serial+0x32>
mov    $0xdd,%ebp
jmp    1135 <write_to_serial+0x25>
mov    %r12d,%edi
mov    $0xc0,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <slip_send@plt>

LLVM IR (-O0):
define void @write_to_serial(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %5
%10 = bitcast i8* %9 to i32*
store i32* %10, i32** %7
store i32 0, i32* %8
br label %11
11: ; preds = %40, %3
%12 = load i32, i32* %8
%13 = load i32, i32* %6
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %43
15: ; preds = %11
%16 = load i32*, i32** %7
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %16, i64 %18
%20 = load i32, i32* %19
switch i32 %20, label %31 [
i32 192, label %21
i32 219, label %26
]
21: ; preds = %15
%22 = load i32, i32* %4
%23 = call i32 @slip_send(i32 %22, i32 219)
%24 = load i32, i32* %4
%25 = call i32 @slip_send(i32 %24, i32 220)
br label %39
26: ; preds = %15
%27 = load i32, i32* %4
%28 = call i32 @slip_send(i32 %27, i32 219)
%29 = load i32, i32* %4
%30 = call i32 @slip_send(i32 %29, i32 221)
br label %39
31: ; preds = %15
%32 = load i32, i32* %4
%33 = load i32*, i32** %7
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %33, i64 %35
%37 = load i32, i32* %36
%38 = call i32 @slip_send(i32 %32, i32 %37)
br label %39
39: ; preds = %31, %26, %21
br label %40
40: ; preds = %39
%41 = load i32, i32* %8
%42 = add nsw i32 %41, 1
store i32 %42, i32* %8
br label %11
43: ; preds = %11
%44 = load i32, i32* %4
%45 = call i32 @slip_send(i32 %44, i32 192)
br label %46
46: ; preds = %43
br label %47
47: ; preds = %46
ret void
}

LLVM IR (-O2):
define void @write_to_serial(i32 %0, i8* nocapture %1, i32 %2) {
%4 = bitcast i8* %1 to i32*
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %21
6: ; preds = %3
%7 = zext i32 %2 to i64
br label %8
8: ; preds = %6, %16
%9 = phi i64 [ 0, %6 ], [ %19, %16 ]
%10 = getelementptr inbounds i32, i32* %4, i64 %9
%11 = load i32, i32* %10
switch i32 %11, label %16 [
i32 192, label %13
i32 219, label %12
]
12: ; preds = %8
br label %13
13: ; preds = %8, %12
%14 = phi i32 [ 221, %12 ], [ 220, %8 ]
%15 = tail call i32 @slip_send(i32 %0, i32 219)
br label %16
16: ; preds = %13, %8
%17 = phi i32 [ %11, %8 ], [ %14, %13 ]
%18 = tail call i32 @slip_send(i32 %0, i32 %17)
%19 = add nuw nsw i64 %9, 1
%20 = icmp eq i64 %19, %7
br i1 %20, label %21, label %8
21: ; preds = %16, %3
%22 = tail call i32 @slip_send(i32 %0, i32 192)
ret void
}