#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 char uint16_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char htons(int);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
void *sendMessage(int sock, char *s, int len) {
  int frameCount;
  uint16_t len16;
  char frame[10];
  char *reply = malloc(sizeof(char) * (len + 8));
  frame[0] = '\x81';
  if (len <= 125) {
    frame[1] = len;
    frameCount = 2;
  } else if (len >= 126 && len <= 65535) {
    frame[1] = 126;
    len16 = htons(len);
    memcpy(frame + 2, &len16, 2);
    frameCount = 4;
  } else {
    frame[1] = 127;
    frame[7] = (char)(((char)len >> 16) & (char)255);
    frame[8] = (char)(((char)len >> 8) & (char)255);
    frame[9] = (char)(((char)len) & (char)255);
    frameCount = 10;
  }
  memcpy(reply, frame, frameCount);
  memcpy(reply + frameCount, s, len);
  if (write(sock, reply, strlen(reply)) <= 0) {
    printf("\n\nWE ARE NOT WRITING!!\n\n");
  } else {
  }
  memset(reply, '\0', sizeof(char) * (len + 8));
  free(reply);
  reply = NULL;
  return NULL;
}


x86 assembly (clang, -O0):
<sendMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x8,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
movb   $0x81,-0x23(%rbp)
cmpl   $0x7d,-0x14(%rbp)
jg     11c9 <sendMessage+0x49>
mov    -0x14(%rbp),%eax
mov    %al,-0x22(%rbp)
movl   $0x2,-0x18(%rbp)
jmp    124b <sendMessage+0xcb>
cmpl   $0x7e,-0x14(%rbp)
jl     1211 <sendMessage+0x91>
cmpl   $0xffff,-0x14(%rbp)
jg     1211 <sendMessage+0x91>
movb   $0x7e,-0x22(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <htons@plt>
mov    %al,-0x19(%rbp)
lea    -0x23(%rbp),%rdi
add    $0x2,%rdi
lea    -0x19(%rbp),%rsi
mov    $0x2,%edx
call   1090 <memcpy@plt>
movl   $0x4,-0x18(%rbp)
jmp    1246 <sendMessage+0xc6>
movb   $0x7f,-0x22(%rbp)
mov    -0x14(%rbp),%eax
movsbl %al,%eax
sar    $0x10,%eax
and    $0xffffffff,%eax
mov    %al,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
movsbl %al,%eax
sar    $0x8,%eax
and    $0xffffffff,%eax
mov    %al,-0x1b(%rbp)
mov    -0x14(%rbp),%eax
movsbl %al,%eax
and    $0xffffffff,%eax
mov    %al,-0x1a(%rbp)
movl   $0xa,-0x18(%rbp)
jmp    124b <sendMessage+0xcb>
mov    -0x30(%rbp),%rdi
lea    -0x23(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1090 <memcpy@plt>
mov    -0x30(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1090 <memcpy@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jg     12b2 <sendMessage+0x132>
lea    0xd58(%rip),%rdi
call   1070 <printf@plt>
jmp    12b7 <sendMessage+0x137>
jmp    12b7 <sendMessage+0x137>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%eax
add    $0x8,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1080 <memset@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x30(%rbp)
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendMessage>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r13d
mov    %rsi,%r12
mov    %edi,%r14d
lea    0x8(%r13),%r15d
mov    %r15d,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
movb   $0x81,0xe(%rsp)
cmp    $0x7d,%r13d
jg     11bd <sendMessage+0x3d>
mov    %r13b,0xf(%rsp)
mov    $0x2,%ebp
jmp    1211 <sendMessage+0x91>
cmp    $0xffff,%r13d
ja     11f2 <sendMessage+0x72>
movb   $0x7e,0xf(%rsp)
mov    %r13d,%edi
call   1070 <htons@plt>
mov    %al,0xd(%rsp)
lea    0x10(%rsp),%rdi
lea    0xd(%rsp),%rsi
mov    $0x2,%edx
call   1090 <memcpy@plt>
mov    $0x4,%ebp
jmp    1211 <sendMessage+0x91>
movb   $0x7f,0xf(%rsp)
movsbl %r13b,%eax
mov    %eax,%ecx
shr    $0x7,%ecx
mov    %cl,0x15(%rsp)
mov    %cl,0x16(%rsp)
mov    %al,0x17(%rsp)
mov    $0xa,%ebp
lea    0xe(%rsp),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1090 <memcpy@plt>
mov    %ebp,%eax
mov    %rbx,%rdi
add    %rax,%rdi
mov    %r12,%rsi
mov    %r13d,%edx
call   1090 <memcpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
test   %rax,%rax
jg     1259 <sendMessage+0xd9>
lea    0xdac(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1080 <memset@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"\0A\0AWE ARE NOT WRITING!!\0A\0A\00"
define i8* @sendMessage(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca [10 x i8]
%10 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i32, i32* %6
%12 = add nsw i32 %11, 8
%13 = sext i32 %12 to i64
%14 = mul i64 1, %13
%15 = trunc i64 %14 to i32
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %10
%17 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
store i8 -127, i8* %17
%18 = load i32, i32* %6
%19 = icmp sle i32 %18, 125
br i1 %19, label %20, label %24
20: ; preds = %3
%21 = load i32, i32* %6
%22 = trunc i32 %21 to i8
%23 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 1
store i8 %22, i8* %23
store i32 2, i32* %7
br label %60
24: ; preds = %3
%25 = load i32, i32* %6
%26 = icmp sge i32 %25, 126
br i1 %26, label %27, label %37
27: ; preds = %24
%28 = load i32, i32* %6
%29 = icmp sle i32 %28, 65535
br i1 %29, label %30, label %37
30: ; preds = %27
%31 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 1
store i8 126, i8* %31
%32 = load i32, i32* %6
%33 = call signext i8 @htons(i32 %32)
store i8 %33, i8* %8
%34 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%35 = getelementptr inbounds i8, i8* %34, i64 2
%36 = call i32 @memcpy(i8* %35, i8* %8, i32 2)
store i32 4, i32* %7
br label %59
37: ; preds = %27, %24
%38 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 1
store i8 127, i8* %38
%39 = load i32, i32* %6
%40 = trunc i32 %39 to i8
%41 = sext i8 %40 to i32
%42 = ashr i32 %41, 16
%43 = and i32 %42, -1
%44 = trunc i32 %43 to i8
%45 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 7
store i8 %44, i8* %45
%46 = load i32, i32* %6
%47 = trunc i32 %46 to i8
%48 = sext i8 %47 to i32
%49 = ashr i32 %48, 8
%50 = and i32 %49, -1
%51 = trunc i32 %50 to i8
%52 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 8
store i8 %51, i8* %52
%53 = load i32, i32* %6
%54 = trunc i32 %53 to i8
%55 = sext i8 %54 to i32
%56 = and i32 %55, -1
%57 = trunc i32 %56 to i8
%58 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 9
store i8 %57, i8* %58
store i32 10, i32* %7
br label %59
59: ; preds = %37, %30
br label %60
60: ; preds = %59, %20
%61 = load i8*, i8** %10
%62 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%63 = load i32, i32* %7
%64 = call i32 @memcpy(i8* %61, i8* %62, i32 %63)
%65 = load i8*, i8** %10
%66 = load i32, i32* %7
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %65, i64 %67
%69 = load i8*, i8** %5
%70 = load i32, i32* %6
%71 = call i32 @memcpy(i8* %68, i8* %69, i32 %70)
%72 = load i32, i32* %4
%73 = load i8*, i8** %10
%74 = load i8*, i8** %10
%75 = call i32 @strlen(i8* %74)
%76 = call i64 @write(i32 %72, i8* %73, i32 %75)
%77 = icmp sle i64 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %60
%79 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %81
80: ; preds = %60
br label %81
81: ; preds = %80, %78
%82 = load i8*, i8** %10
%83 = load i32, i32* %6
%84 = add nsw i32 %83, 8
%85 = sext i32 %84 to i64
%86 = mul i64 1, %85
%87 = trunc i64 %86 to i32
%88 = call i32 @memset(i8* %82, i8 signext 0, i32 %87)
%89 = load i8*, i8** %10
%90 = call i32 @free(i8* %89)
store i8* null, i8** %10
ret i8* null
}

LLVM IR (-O2):
@str = [24 x i8] c"\0A\0AWE ARE NOT WRITING!!\0A\00"
define noalias i8* @sendMessage(i32 %0, i8* %1, i32 %2) {
%4 = alloca i8
%5 = alloca [10 x i8]
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%7 = add nsw i32 %2, 8
%8 = tail call i8* @malloc(i32 %7)
store i8 -127, i8* %6
%9 = icmp slt i32 %2, 126
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = trunc i32 %2 to i8
%12 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 1
store i8 %11, i8* %12
br label %28
13: ; preds = %3
%14 = icmp ult i32 %2, 65536
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 1
br i1 %14, label %16, label %20
16: ; preds = %13
store i8 126, i8* %15
%17 = tail call signext i8 @htons(i32 %2)
store i8 %17, i8* %4
%18 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 2
%19 = call i32 @memcpy(i8* %18, i8* %4, i32 2)
br label %28
20: ; preds = %13
store i8 127, i8* %15
%21 = trunc i32 %2 to i8
%22 = shl i32 %2, 24
%23 = ashr i32 %22, 31
%24 = trunc i32 %23 to i8
%25 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 7
store i8 %24, i8* %25
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 8
store i8 %24, i8* %26
%27 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 9
store i8 %21, i8* %27
br label %28
28: ; preds = %16, %20, %10
%29 = phi i32 [ 2, %10 ], [ 4, %16 ], [ 10, %20 ]
%30 = call i32 @memcpy(i8* %8, i8* %6, i32 %29)
%31 = zext i32 %29 to i64
%32 = getelementptr inbounds i8, i8* %8, i64 %31
%33 = call i32 @memcpy(i8* %32, i8* %1, i32 %2)
%34 = call i32 @strlen(i8* %8)
%35 = call i64 @write(i32 %0, i8* %8, i32 %34)
%36 = icmp slt i64 %35, 1
br i1 %36, label %37, label %39
37: ; preds = %28
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %39
39: ; preds = %28, %37
%40 = call i32 @memset(i8* %8, i8 signext 0, i32 %7)
%41 = call i32 @free(i8* %8)
ret i8* null
}