#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 /*<<< orphan*/ exit(int);
scalar_t__ send(int, unsigned char *, int, int /*<<< orphan*/);
void negotiate(int sock, unsigned char *buf, int len) {
  int i;
  if (buf[1] == 0xfd && buf[2] == 31) {
    unsigned char tmp1[10] = {255, 251, 31};
    if (send(sock, tmp1, 3, 0) < 0)
      exit(1);
    unsigned char tmp2[10] = {255, 250, 31, 0, 80, 0, 24, 255, 240};
    if (send(sock, tmp2, 9, 0) < 0)
      exit(1);
    return;
  }
  for (i = 0; i < len; i++) {
    if (buf[i] == 0xfd)
      buf[i] = 0xfc;
    else if (buf[i] == 0xfb)
      buf[i] = 0xfd;
  }
  if (send(sock, buf, len, 0) < 0)
    exit(1);
}


x86 assembly (clang, -O0):
<negotiate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%eax
cmp    $0xfd,%eax
jne    11d5 <negotiate+0xb5>
mov    -0x10(%rbp),%rax
movzbl 0x2(%rax),%eax
cmp    $0x1f,%eax
jne    11d5 <negotiate+0xb5>
mov    0xea3(%rip),%rax
mov    %rax,-0x22(%rbp)
mov    0xea0(%rip),%ax
mov    %ax,-0x1a(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0x3,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
cmp    $0x0,%rax
jge    1193 <negotiate+0x73>
mov    $0x1,%edi
call   1040 <exit@plt>
mov    0xe70(%rip),%rax
mov    %rax,-0x2c(%rbp)
mov    0xe6d(%rip),%ax
mov    %ax,-0x24(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x9,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
cmp    $0x0,%rax
jge    11d0 <negotiate+0xb0>
mov    $0x1,%edi
call   1040 <exit@plt>
jmp    1270 <negotiate+0x150>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    124b <negotiate+0x12b>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0xfd,%eax
jne    1210 <negotiate+0xf0>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0xfc,(%rax,%rcx,1)
jmp    1238 <negotiate+0x118>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0xfb,%eax
jne    1233 <negotiate+0x113>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0xfd,(%rax,%rcx,1)
jmp    1238 <negotiate+0x118>
jmp    123d <negotiate+0x11d>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11dc <negotiate+0xbc>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
cmp    $0x0,%rax
jge    1270 <negotiate+0x150>
mov    $0x1,%edi
call   1040 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<negotiate>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
cmpb   $0xfd,0x1(%rsi)
jne    1183 <negotiate+0x63>
cmpb   $0x1f,0x2(%rsi)
jne    1183 <negotiate+0x63>
movw   $0x0,0x18(%rsp)
movq   $0x1ffbff,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
test   %rax,%rax
js     1221 <negotiate+0x101>
movabs $0xff180050001ffaff,%rax
mov    %rax,(%rsp)
movw   $0xf0,0x8(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x9,%edx
jmp    120f <negotiate+0xef>
test   %edx,%edx
jle    120d <negotiate+0xed>
mov    %edx,%r8d
cmp    $0x1,%edx
jne    11ac <negotiate+0x8c>
xor    %eax,%eax
test   $0x1,%r8b
je     120d <negotiate+0xed>
mov    (%rsi,%rax,1),%cl
cmp    $0xfd,%cl
je     1208 <negotiate+0xe8>
cmp    $0xfb,%cl
jne    120d <negotiate+0xed>
mov    $0xfd,%cl
jmp    120a <negotiate+0xea>
mov    %r8d,%edi
and    $0xfffffffe,%edi
xor    %eax,%eax
jmp    11cf <negotiate+0xaf>
cs nopw 0x0(%rax,%rax,1)
mov    $0xfc,%cl
mov    %cl,0x1(%rsi,%rax,1)
add    $0x2,%rax
cmp    %rax,%rdi
je     1195 <negotiate+0x75>
movzbl (%rsi,%rax,1),%ecx
cmp    $0xfd,%cl
je     11f0 <negotiate+0xd0>
cmp    $0xfb,%cl
jne    11f5 <negotiate+0xd5>
mov    $0xfd,%cl
jmp    11f2 <negotiate+0xd2>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0xfc,%cl
mov    %cl,(%rsi,%rax,1)
movzbl 0x1(%rsi,%rax,1),%ecx
cmp    $0xfd,%cl
je     11c0 <negotiate+0xa0>
cmp    $0xfb,%cl
jne    11c6 <negotiate+0xa6>
mov    $0xfd,%cl
jmp    11c2 <negotiate+0xa2>
mov    $0xfc,%cl
mov    %cl,(%rsi,%rax,1)
mov    %ebx,%edi
xor    %ecx,%ecx
call   1030 <send@plt>
test   %rax,%rax
js     1221 <negotiate+0x101>
add    $0x20,%rsp
pop    %rbx
ret
mov    $0x1,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@__const.negotiate.tmp1 = [10 x i8] c"\FF\FB\1F\00\00\00\00\00\00\00"
@__const.negotiate.tmp2 = [10 x i8] c"\FF\FA\1F\00P\00\18\FF\F0\00"
define void @negotiate(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [10 x i8]
%9 = alloca [10 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %5
%11 = getelementptr inbounds i8, i8* %10, i64 1
%12 = load i8, i8* %11
%13 = zext i8 %12 to i32
%14 = icmp eq i32 %13, 253
br i1 %14, label %15, label %38
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = getelementptr inbounds i8, i8* %16, i64 2
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = icmp eq i32 %19, 31
br i1 %20, label %21, label %38
21: ; preds = %15
%22 = bitcast [10 x i8]* %8 to i8*
%23 = load i32, i32* %4
%24 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%25 = call i64 @send(i32 %23, i8* %24, i32 3, i32 0)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = bitcast [10 x i8]* %9 to i8*
%31 = load i32, i32* %4
%32 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%33 = call i64 @send(i32 %31, i8* %32, i32 9, i32 0)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %29
br label %82
38: ; preds = %15, %3
store i32 0, i32* %7
br label %39
39: ; preds = %71, %38
%40 = load i32, i32* %7
%41 = load i32, i32* %6
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %74
43: ; preds = %39
%44 = load i8*, i8** %5
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = load i8, i8* %47
%49 = zext i8 %48 to i32
%50 = icmp eq i32 %49, 253
br i1 %50, label %51, label %56
51: ; preds = %43
%52 = load i8*, i8** %5
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
store i8 -4, i8* %55
br label %70
56: ; preds = %43
%57 = load i8*, i8** %5
%58 = load i32, i32* %7
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = load i8, i8* %60
%62 = zext i8 %61 to i32
%63 = icmp eq i32 %62, 251
br i1 %63, label %64, label %69
64: ; preds = %56
%65 = load i8*, i8** %5
%66 = load i32, i32* %7
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %65, i64 %67
store i8 -3, i8* %68
br label %69
69: ; preds = %64, %56
br label %70
70: ; preds = %69, %51
br label %71
71: ; preds = %70
%72 = load i32, i32* %7
%73 = add nsw i32 %72, 1
store i32 %73, i32* %7
br label %39
74: ; preds = %39
%75 = load i32, i32* %4
%76 = load i8*, i8** %5
%77 = load i32, i32* %6
%78 = call i64 @send(i32 %75, i8* %76, i32 %77, i32 0)
%79 = icmp slt i64 %78, 0
br i1 %79, label %80, label %82
80: ; preds = %74
%81 = call i32 @exit(i32 1)
unreachable
82: ; preds = %37, %74
ret void
}

LLVM IR (-O2):
@__const.negotiate.tmp1 = [10 x i8] c"\FF\FB\1F\00\00\00\00\00\00\00"
@__const.negotiate.tmp2 = [10 x i8] c"\FF\FA\1F\00P\00\18\FF\F0\00"
define void @negotiate(i32 %0, i8* %1, i32 %2) {
%4 = alloca [10 x i8]
%5 = alloca [10 x i8]
%6 = getelementptr inbounds i8, i8* %1, i64 1
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, -3
br i1 %8, label %9, label %26
9: ; preds = %3
%10 = getelementptr inbounds i8, i8* %1, i64 2
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 31
br i1 %12, label %13, label %26
13: ; preds = %9
%14 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%15 = call i64 @send(i32 %0, i8* %14, i32 3, i32 0)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%21 = call i64 @send(i32 %0, i8* %20, i32 9, i32 0)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %19
br label %67
26: ; preds = %9, %3
%27 = icmp sgt i32 %2, 0
br i1 %27, label %28, label %62
28: ; preds = %26
%29 = zext i32 %2 to i64
%30 = and i64 %29, 1
%31 = icmp eq i32 %2, 1
br i1 %31, label %53, label %32
32: ; preds = %28
%33 = and i64 %29, 4294967294
br label %34
34: ; preds = %49, %32
%35 = phi i64 [ 0, %32 ], [ %50, %49 ]
%36 = phi i64 [ 0, %32 ], [ %51, %49 ]
%37 = getelementptr inbounds i8, i8* %1, i64 %35
%38 = load i8, i8* %37
switch i8 %38, label %42 [
i8 -3, label %40
i8 -5, label %39
]
39: ; preds = %34
br label %40
40: ; preds = %34, %39
%41 = phi i8 [ -3, %39 ], [ -4, %34 ]
store i8 %41, i8* %37
br label %42
42: ; preds = %40, %34
%43 = or i64 %35, 1
%44 = getelementptr inbounds i8, i8* %1, i64 %43
%45 = load i8, i8* %44
switch i8 %45, label %49 [
i8 -3, label %47
i8 -5, label %46
]
46: ; preds = %42
br label %47
47: ; preds = %46, %42
%48 = phi i8 [ -3, %46 ], [ -4, %42 ]
store i8 %48, i8* %44
br label %49
49: ; preds = %47, %42
%50 = add nuw nsw i64 %35, 2
%51 = add i64 %36, 2
%52 = icmp eq i64 %51, %33
br i1 %52, label %53, label %34
53: ; preds = %49, %28
%54 = phi i64 [ 0, %28 ], [ %50, %49 ]
%55 = icmp eq i64 %30, 0
br i1 %55, label %62, label %56
56: ; preds = %53
%57 = getelementptr inbounds i8, i8* %1, i64 %54
%58 = load i8, i8* %57
switch i8 %58, label %62 [
i8 -3, label %60
i8 -5, label %59
]
59: ; preds = %56
br label %60
60: ; preds = %59, %56
%61 = phi i8 [ -3, %59 ], [ -4, %56 ]
store i8 %61, i8* %57
br label %62
62: ; preds = %53, %60, %56, %26
%63 = tail call i64 @send(i32 %0, i8* %1, i32 %2, i32 0)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %62
%66 = tail call i32 @exit(i32 1)
unreachable
67: ; preds = %62, %25
ret void
}