#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*/ uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ _flush_stream(int /*<<< orphan*/ *);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int _demultiplex_slip(FILE *stream, int iface_fd, int out_fd) {
  FILE *target;
  int escaped = 0;
  int byte;
  if ((byte = fgetc(stream)) == EOF) {
    _flush_stream(stream);
    return -1;
  }
  if (byte == 0x0aU) {
    if ((byte = fgetc(stream)) == EOF) {
      _flush_stream(stream);
      return -1;
    }
    target = fdopen(dup(out_fd), "w");
  } else {
    switch (byte & 0xf0U) {
    case 0x40U:
    case 0x60U:
      target = fdopen(dup(iface_fd), "w");
      break;
    default:
      fprintf(stderr, "Unknown packet type 0x%02x\n", byte);
      _flush_stream(stream);
      return 0;
    }
  }
  while (byte != 0xc0U) {
    switch ((uint8_t)byte) {
    case 0xdbU:
      escaped = 1;
      break;
    case 0xc0U:
      fflush(target);
      fclose(target);
      return 0;
    case 0xdcU:
      if (escaped) {
        byte = 0xc0U;
        escaped = 0;
      }
    case 0xddU:
      if (escaped) {
        byte = 0xdbU;
        escaped = 0;
      }
    default:
      fputc(byte, target);
      break;
    }
    if ((byte = fgetc(stream)) == EOF) {
      _flush_stream(stream);
      fclose(target);
      return -1;
    }
  }
  fclose(target);
  return 0;
}


x86 assembly (clang, -O0):
<_demultiplex_slip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x28(%rbp)
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%eax
jne    11c9 <_demultiplex_slip+0x49>
mov    -0x10(%rbp),%rdi
call   1040 <_flush_stream@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1387 <_demultiplex_slip+0x207>
cmpl   $0xa,-0x28(%rbp)
jne    1222 <_demultiplex_slip+0xa2>
mov    -0x10(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x28(%rbp)
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%eax
jne    1203 <_demultiplex_slip+0x83>
mov    -0x10(%rbp),%rdi
call   1040 <_flush_stream@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1387 <_demultiplex_slip+0x207>
mov    -0x18(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,%edi
lea    0xe64(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x20(%rbp)
jmp    128c <_demultiplex_slip+0x10c>
mov    -0x28(%rbp),%eax
or     $0x20,%eax
and    $0xf0,%eax
sub    $0x60,%eax
je     123b <_demultiplex_slip+0xbb>
jmp    125a <_demultiplex_slip+0xda>
mov    -0x14(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,%edi
lea    0xe2c(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x20(%rbp)
jmp    1287 <_demultiplex_slip+0x107>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
lea    0xe0d(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <_flush_stream@plt>
movl   $0x0,-0x4(%rbp)
jmp    1387 <_demultiplex_slip+0x207>
jmp    128c <_demultiplex_slip+0x10c>
jmp    1291 <_demultiplex_slip+0x111>
cmpl   $0xc0,-0x28(%rbp)
je     1377 <_demultiplex_slip+0x1f7>
mov    -0x28(%rbp),%eax
add    $0xffffff40,%eax
mov    %eax,%ecx
mov    %rcx,-0x30(%rbp)
sub    $0x1d,%eax
ja     132d <_demultiplex_slip+0x1ad>
mov    -0x30(%rbp),%rax
lea    0xd40(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x1,-0x24(%rbp)
jmp    1339 <_demultiplex_slip+0x1b9>
mov    -0x20(%rbp),%rdi
call   1090 <fflush@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1387 <_demultiplex_slip+0x207>
cmpl   $0x0,-0x24(%rbp)
je     130b <_demultiplex_slip+0x18b>
movl   $0xc0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
jmp    1310 <_demultiplex_slip+0x190>
cmpl   $0x0,-0x24(%rbp)
je     1328 <_demultiplex_slip+0x1a8>
movl   $0xdb,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
jmp    132d <_demultiplex_slip+0x1ad>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1070 <fputc@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x28(%rbp)
mov    0x2c9c(%rip),%rcx
cmp    (%rcx),%eax
jne    1372 <_demultiplex_slip+0x1f2>
mov    -0x10(%rbp),%rdi
call   1040 <_flush_stream@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1387 <_demultiplex_slip+0x207>
jmp    1291 <_demultiplex_slip+0x111>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_demultiplex_slip>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %esi,%r15d
mov    %rdi,%r14
call   1060 <fgetc@plt>
mov    0x2e58(%rip),%rbp
cmp    0x0(%rbp),%eax
jne    11a7 <_demultiplex_slip+0x37>
mov    %r14,%rdi
call   1040 <_flush_stream@plt>
mov    $0xffffffff,%eax
jmp    12a8 <_demultiplex_slip+0x138>
mov    %eax,%ebx
cmp    $0xa,%eax
jne    11bf <_demultiplex_slip+0x4f>
mov    %r14,%rdi
call   1060 <fgetc@plt>
mov    %eax,%ebx
cmp    0x0(%rbp),%eax
je     1195 <_demultiplex_slip+0x25>
jmp    11d0 <_demultiplex_slip+0x60>
mov    %ebx,%eax
or     $0x20,%al
and    $0xf0,%al
cmp    $0x60,%al
jne    127d <_demultiplex_slip+0x10d>
mov    %r15d,%r12d
mov    %r12d,%edi
call   1050 <dup@plt>
lea    0xe99(%rip),%rsi
mov    %eax,%edi
call   1090 <fdopen@plt>
mov    %rax,%r15
xor    %r13d,%r13d
lea    0xe0d(%rip),%rbp
jmp    1238 <_demultiplex_slip+0xc8>
cs nopw 0x0(%rax,%rax,1)
nop
test   %r13d,%r13d
mov    $0xc0,%ebx
mov    $0xdc,%eax
cmove  %eax,%ebx
xor    %r13d,%r13d
mov    %ebx,%edi
mov    %r15,%rsi
call   1070 <fputc@plt>
mov    %r13d,%r12d
mov    %r14,%rdi
call   1060 <fgetc@plt>
mov    %eax,%ebx
mov    %r12d,%r13d
mov    0x2db4(%rip),%rax
cmp    (%rax),%ebx
je     1266 <_demultiplex_slip+0xf6>
lea    -0xc0(%rbx),%eax
cmp    $0x1d,%eax
ja     1213 <_demultiplex_slip+0xa3>
mov    $0x1,%r12d
movslq 0x0(%rbp,%rax,4),%rax
add    %rbp,%rax
jmp    *%rax
test   %r13d,%r13d
je     125f <_demultiplex_slip+0xef>
mov    $0xdb,%ebx
jmp    1210 <_demultiplex_slip+0xa0>
mov    $0xdd,%ebx
jmp    1210 <_demultiplex_slip+0xa0>
mov    %r14,%rdi
call   1040 <_flush_stream@plt>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    $0xffffffff,%eax
jmp    12a8 <_demultiplex_slip+0x138>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
mov    %ebx,%edx
call   1080 <fprintf@plt>
mov    %r14,%rdi
call   1040 <_flush_stream@plt>
jmp    12a6 <_demultiplex_slip+0x136>
mov    %r15,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"Unknown packet type 0x%02x\0A\00"
define i32 @_demultiplex_slip(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %9
%11 = load i32*, i32** %5
%12 = call i32 @fgetc(i32* %11)
store i32 %12, i32* %10
%13 = load i32, i32* @EOF
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i32*, i32** %5
%17 = call i32 @_flush_stream(i32* %16)
store i32 -1, i32* %4
br label %87
18: ; preds = %3
%19 = load i32, i32* %10
%20 = icmp eq i32 %19, 10
br i1 %20, label %21, label %33
21: ; preds = %18
%22 = load i32*, i32** %5
%23 = call i32 @fgetc(i32* %22)
store i32 %23, i32* %10
%24 = load i32, i32* @EOF
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32*, i32** %5
%28 = call i32 @_flush_stream(i32* %27)
store i32 -1, i32* %4
br label %87
29: ; preds = %21
%30 = load i32, i32* %7
%31 = call i32 @dup(i32 %30)
%32 = call i32* @fdopen(i32 %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %32, i32** %8
br label %47
33: ; preds = %18
%34 = load i32, i32* %10
%35 = and i32 %34, 240
switch i32 %35, label %40 [
i32 64, label %36
i32 96, label %36
]
36: ; preds = %33, %33
%37 = load i32, i32* %6
%38 = call i32 @dup(i32 %37)
%39 = call i32* @fdopen(i32 %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %39, i32** %8
br label %46
40: ; preds = %33
%41 = load i32, i32* @stderr
%42 = load i32, i32* %10
%43 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %42)
%44 = load i32*, i32** %5
%45 = call i32 @_flush_stream(i32* %44)
store i32 0, i32* %4
br label %87
46: ; preds = %36
br label %47
47: ; preds = %46, %29
br label %48
48: ; preds = %83, %47
%49 = load i32, i32* %10
%50 = icmp ne i32 %49, 192
br i1 %50, label %51, label %84
51: ; preds = %48
%52 = load i32, i32* %10
switch i32 %52, label %69 [
i32 219, label %53
i32 192, label %54
i32 220, label %59
i32 221, label %64
]
53: ; preds = %51
store i32 1, i32* %9
br label %73
54: ; preds = %51
%55 = load i32*, i32** %8
%56 = call i32 @fflush(i32* %55)
%57 = load i32*, i32** %8
%58 = call i32 @fclose(i32* %57)
store i32 0, i32* %4
br label %87
59: ; preds = %51
%60 = load i32, i32* %9
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %59
store i32 192, i32* %10
store i32 0, i32* %9
br label %63
63: ; preds = %62, %59
br label %64
64: ; preds = %51, %63
%65 = load i32, i32* %9
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %64
store i32 219, i32* %10
store i32 0, i32* %9
br label %68
68: ; preds = %67, %64
br label %69
69: ; preds = %51, %68
%70 = load i32, i32* %10
%71 = load i32*, i32** %8
%72 = call i32 @fputc(i32 %70, i32* %71)
br label %73
73: ; preds = %69, %53
%74 = load i32*, i32** %5
%75 = call i32 @fgetc(i32* %74)
store i32 %75, i32* %10
%76 = load i32, i32* @EOF
%77 = icmp eq i32 %75, %76
br i1 %77, label %78, label %83
78: ; preds = %73
%79 = load i32*, i32** %5
%80 = call i32 @_flush_stream(i32* %79)
%81 = load i32*, i32** %8
%82 = call i32 @fclose(i32* %81)
store i32 -1, i32* %4
br label %87
83: ; preds = %73
br label %48
84: ; preds = %48
%85 = load i32*, i32** %8
%86 = call i32 @fclose(i32* %85)
store i32 0, i32* %4
br label %87
87: ; preds = %84, %78, %54, %40, %26, %15
%88 = load i32, i32* %4
ret i32 %88
}

LLVM IR (-O2):
@EOF = global i32 0
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"Unknown packet type 0x%02x\0A\00"
define i32 @_demultiplex_slip(i32* %0, i32 %1, i32 %2) {
%4 = tail call i32 @fgetc(i32* %0)
%5 = load i32, i32* @EOF
%6 = icmp eq i32 %4, %5
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @_flush_stream(i32* %0)
br label %53
9: ; preds = %3
%10 = icmp eq i32 %4, 10
br i1 %10, label %11, label %17
11: ; preds = %9
%12 = tail call i32 @fgetc(i32* %0)
%13 = load i32, i32* @EOF
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %24
15: ; preds = %11
%16 = tail call i32 @_flush_stream(i32* %0)
br label %53
17: ; preds = %9
%18 = trunc i32 %4 to i8
%19 = and i8 %18, -16
switch i8 %19, label %20 [
i8 64, label %24
i8 96, label %24
]
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%23 = tail call i32 @_flush_stream(i32* %0)
br label %53
24: ; preds = %17, %17, %11
%25 = phi i32 [ %2, %11 ], [ %1, %17 ], [ %1, %17 ]
%26 = phi i32 [ %12, %11 ], [ %4, %17 ], [ %4, %17 ]
%27 = tail call i32 @dup(i32 %25)
%28 = tail call i32* @fdopen(i32 %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %43, %24
%30 = phi i32 [ 0, %24 ], [ %44, %43 ]
%31 = phi i32 [ %26, %24 ], [ %45, %43 ]
switch i32 %31, label %39 [
i32 192, label %51
i32 219, label %43
i32 221, label %35
i32 220, label %32
]
32: ; preds = %29
%33 = icmp eq i32 %30, 0
%34 = select i1 %33, i32 220, i32 192
br label %37
35: ; preds = %29
%36 = icmp eq i32 %30, 0
br i1 %36, label %37, label %39
37: ; preds = %32, %35
%38 = phi i32 [ %34, %32 ], [ 221, %35 ]
br label %39
39: ; preds = %37, %35, %29
%40 = phi i32 [ %30, %29 ], [ 0, %35 ], [ 0, %37 ]
%41 = phi i32 [ %31, %29 ], [ 219, %35 ], [ %38, %37 ]
%42 = tail call i32 @fputc(i32 %41, i32* %28)
br label %43
43: ; preds = %29, %39
%44 = phi i32 [ %40, %39 ], [ 1, %29 ]
%45 = tail call i32 @fgetc(i32* %0)
%46 = load i32, i32* @EOF
%47 = icmp eq i32 %45, %46
br i1 %47, label %48, label %29
48: ; preds = %43
%49 = tail call i32 @_flush_stream(i32* %0)
%50 = tail call i32 @fclose(i32* %28)
br label %53
51: ; preds = %29
%52 = tail call i32 @fclose(i32* %28)
br label %53
53: ; preds = %51, %48, %20, %15, %7
%54 = phi i32 [ -1, %7 ], [ -1, %15 ], [ -1, %48 ], [ 0, %51 ], [ 0, %20 ]
ret i32 %54
}