#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*/ MSG_NOSIGNAL;
int /*<<< orphan*/ send(int, unsigned char *, int, int /*<<< orphan*/);
int negotiate(int sock, unsigned char *buf, int len) {
  unsigned char c;
  switch (buf[1]) {
  case 255:
    return 0;
  case 251:
  case 252:
  case 253:
  case 254:
    c = 255;
    send(sock, &c, 1, MSG_NOSIGNAL);
    if (252 == buf[1])
      c = 254;
    else if (254 == buf[1])
      c = 252;
    else if (3 == buf[1])
      c = (buf[1] == 253 ? 251 : 253);
    else
      c = (buf[1] == 253 ? 252 : 254);
    send(sock, &c, 1, MSG_NOSIGNAL);
    send(sock, &(buf[2]), 1, MSG_NOSIGNAL);
    break;
  default:
    break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<negotiate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%eax
mov    %eax,-0x1c(%rbp)
add    $0xffffff05,%eax
sub    $0x4,%eax
jb     115f <negotiate+0x4f>
jmp    1140 <negotiate+0x30>
mov    -0x1c(%rbp),%eax
sub    $0xff,%eax
jne    1256 <negotiate+0x146>
jmp    1153 <negotiate+0x43>
movl   $0x0,-0x4(%rbp)
jmp    1262 <negotiate+0x152>
movb   $0xff,-0x15(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%ecx
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <send@plt>
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%ecx
mov    $0xfc,%eax
cmp    %ecx,%eax
jne    119b <negotiate+0x8b>
movb   $0xfe,-0x15(%rbp)
jmp    1219 <negotiate+0x109>
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%ecx
mov    $0xfe,%eax
cmp    %ecx,%eax
jne    11b9 <negotiate+0xa9>
movb   $0xfc,-0x15(%rbp)
jmp    1214 <negotiate+0x104>
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%ecx
mov    $0x3,%eax
cmp    %ecx,%eax
jne    11f1 <negotiate+0xe1>
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%edx
mov    $0xfd,%eax
mov    $0xfb,%ecx
cmp    $0xfd,%edx
cmove  %ecx,%eax
mov    %al,-0x15(%rbp)
jmp    120f <negotiate+0xff>
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%edx
mov    $0xfe,%eax
mov    $0xfc,%ecx
cmp    $0xfd,%edx
cmove  %ecx,%eax
mov    %al,-0x15(%rbp)
jmp    1214 <negotiate+0x104>
jmp    1219 <negotiate+0x109>
mov    -0x8(%rbp),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%ecx
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <send@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    $0x2,%rsi
mov    0x2d9b(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
call   1030 <send@plt>
jmp    125b <negotiate+0x14b>
jmp    125b <negotiate+0x14b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<negotiate>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x1(%rsi),%al
add    $0x5,%al
cmp    $0x3,%al
ja     119f <negotiate+0x8f>
mov    %rsi,%rbx
mov    %edi,%ebp
movb   $0xff,0xf(%rsp)
mov    0x2eae(%rip),%r14
mov    (%r14),%ecx
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <send@plt>
mov    0x1(%rbx),%al
cmp    $0x3,%al
je     115a <negotiate+0x4a>
cmp    $0xfe,%al
je     1161 <negotiate+0x51>
cmp    $0xfc,%al
jne    1168 <negotiate+0x58>
movb   $0xfe,0xf(%rsp)
jmp    1175 <negotiate+0x65>
movb   $0xfd,0xf(%rsp)
jmp    1175 <negotiate+0x65>
movb   $0xfc,0xf(%rsp)
jmp    1175 <negotiate+0x65>
cmp    $0xfd,%al
setne  %al
add    %al,%al
or     $0xfc,%al
mov    %al,0xf(%rsp)
mov    (%r14),%ecx
lea    0xf(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <send@plt>
add    $0x2,%rbx
mov    (%r14),%ecx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <send@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@MSG_NOSIGNAL = global i32 0
define i32 @negotiate(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %6
%10 = getelementptr inbounds i8, i8* %9, i64 1
%11 = load i8, i8* %10
%12 = zext i8 %11 to i32
switch i32 %12, label %66 [
i32 255, label %13
i32 251, label %14
i32 252, label %14
i32 253, label %14
i32 254, label %14
]
13: ; preds = %3
store i32 0, i32* %4
br label %68
14: ; preds = %3, %3, %3, %3
store i8 -1, i8* %8
%15 = load i32, i32* %5
%16 = load i32, i32* @MSG_NOSIGNAL
%17 = call i32 @send(i32 %15, i8* %8, i32 1, i32 %16)
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = icmp eq i32 252, %21
br i1 %22, label %23, label %24
23: ; preds = %14
store i8 -2, i8* %8
br label %57
24: ; preds = %14
%25 = load i8*, i8** %6
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = icmp eq i32 254, %28
br i1 %29, label %30, label %31
30: ; preds = %24
store i8 -4, i8* %8
br label %56
31: ; preds = %24
%32 = load i8*, i8** %6
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = icmp eq i32 3, %35
br i1 %36, label %37, label %46
37: ; preds = %31
%38 = load i8*, i8** %6
%39 = getelementptr inbounds i8, i8* %38, i64 1
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = icmp eq i32 %41, 253
%43 = zext i1 %42 to i64
%44 = select i1 %42, i32 251, i32 253
%45 = trunc i32 %44 to i8
store i8 %45, i8* %8
br label %55
46: ; preds = %31
%47 = load i8*, i8** %6
%48 = getelementptr inbounds i8, i8* %47, i64 1
%49 = load i8, i8* %48
%50 = zext i8 %49 to i32
%51 = icmp eq i32 %50, 253
%52 = zext i1 %51 to i64
%53 = select i1 %51, i32 252, i32 254
%54 = trunc i32 %53 to i8
store i8 %54, i8* %8
br label %55
55: ; preds = %46, %37
br label %56
56: ; preds = %55, %30
br label %57
57: ; preds = %56, %23
%58 = load i32, i32* %5
%59 = load i32, i32* @MSG_NOSIGNAL
%60 = call i32 @send(i32 %58, i8* %8, i32 1, i32 %59)
%61 = load i32, i32* %5
%62 = load i8*, i8** %6
%63 = getelementptr inbounds i8, i8* %62, i64 2
%64 = load i32, i32* @MSG_NOSIGNAL
%65 = call i32 @send(i32 %61, i8* %63, i32 1, i32 %64)
br label %67
66: ; preds = %3
br label %67
67: ; preds = %66, %57
store i32 0, i32* %4
br label %68
68: ; preds = %67, %13
%69 = load i32, i32* %4
ret i32 %69
}

LLVM IR (-O2):
@MSG_NOSIGNAL = global i32 0
define i32 @negotiate(i32 %0, i8* %1, i32 %2) {
%4 = alloca i8
%5 = getelementptr inbounds i8, i8* %1, i64 1
%6 = load i8, i8* %5
%7 = add i8 %6, 5
%8 = icmp ult i8 %7, 4
br i1 %8, label %9, label %25
9: ; preds = %3
store i8 -1, i8* %4
%10 = load i32, i32* @MSG_NOSIGNAL
%11 = call i32 @send(i32 %0, i8* %4, i32 1, i32 %10)
%12 = load i8, i8* %5
switch i8 %12, label %16 [
i8 -4, label %13
i8 -2, label %14
i8 3, label %15
]
13: ; preds = %9
store i8 -2, i8* %4
br label %19
14: ; preds = %9
store i8 -4, i8* %4
br label %19
15: ; preds = %9
store i8 -3, i8* %4
br label %19
16: ; preds = %9
%17 = icmp eq i8 %12, -3
%18 = select i1 %17, i8 -4, i8 -2
store i8 %18, i8* %4
br label %19
19: ; preds = %14, %16, %15, %13
%20 = load i32, i32* @MSG_NOSIGNAL
%21 = call i32 @send(i32 %0, i8* %4, i32 1, i32 %20)
%22 = getelementptr inbounds i8, i8* %1, i64 2
%23 = load i32, i32* @MSG_NOSIGNAL
%24 = call i32 @send(i32 %0, i8* %22, i32 1, i32 %23)
br label %25
25: ; preds = %3, %19
ret i32 0
}