#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*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, ...);
int /*<<< orphan*/ strreverse(char *, int);
scalar_t__ write(int, char *, int);
int SendMessage(int fd, char *payload, int payload_length) {
  char *buffer;
  int head_length;
  if (payload_length < 126) {
    buffer = (char *)malloc(2 + payload_length);
    buffer[0] = 0x81;
    buffer[1] = payload_length;
    head_length = 2;
  } else if (payload_length < 0xffff) {
    buffer = (char *)malloc(4 + payload_length);
    buffer[0] = 0x81;
    buffer[1] = 126;
    buffer[2] = (payload_length >> 8 & 0xff);
    buffer[3] = (payload_length & 0xff);
    head_length = 4;
  } else {
    buffer = (char *)malloc(12 + payload_length);
    buffer[0] = 0x81;
    buffer[1] = 127;
    memcpy(buffer + 2, &payload_length, sizeof(unsigned long long));
    strreverse(buffer + 2, sizeof(unsigned long long));
    head_length = 12;
  }
  memcpy(buffer + head_length, payload, payload_length);
  if (write(fd, buffer, head_length + payload_length) <= 0)
    return -1;
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<SendMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x7e,-0x14(%rbp)
jge    119a <SendMessage+0x4a>
mov    -0x14(%rbp),%edi
add    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x81,(%rax)
mov    -0x14(%rbp),%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,0x1(%rax)
movl   $0x2,-0x24(%rbp)
jmp    124a <SendMessage+0xfa>
cmpl   $0xffff,-0x14(%rbp)
jge    11f6 <SendMessage+0xa6>
mov    -0x14(%rbp),%edi
add    $0x4,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x81,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x7e,0x1(%rax)
mov    -0x14(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x14(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,0x3(%rax)
movl   $0x4,-0x24(%rbp)
jmp    1245 <SendMessage+0xf5>
mov    -0x14(%rbp),%edi
add    $0xc,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x81,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x7f,0x1(%rax)
mov    -0x20(%rbp),%rdi
add    $0x2,%rdi
xor    %eax,%eax
lea    -0x14(%rbp),%rsi
mov    $0x8,%edx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rdi
add    $0x2,%rdi
mov    $0x8,%esi
call   1050 <strreverse@plt>
movl   $0xc,-0x24(%rbp)
jmp    124a <SendMessage+0xfa>
mov    -0x20(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %eax,%eax
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
add    -0x14(%rbp),%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jg     128b <SendMessage+0x13b>
movl   $0xffffffff,-0x4(%rbp)
jmp    129b <SendMessage+0x14b>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SendMessage>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %edi,%r14d
mov    %edx,0xc(%rsp)
cmp    $0x7d,%edx
jg     1189 <SendMessage+0x39>
add    $0x2,%edx
mov    %edx,%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
movb   $0x81,(%rax)
mov    0xc(%rsp),%al
mov    %al,0x1(%rbp)
mov    $0x2,%ebx
jmp    11f4 <SendMessage+0xa4>
cmp    $0xfffe,%edx
ja     11b8 <SendMessage+0x68>
add    $0x4,%edx
mov    %edx,%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
movw   $0x7e81,(%rax)
mov    0xd(%rsp),%al
mov    %al,0x2(%rbp)
mov    0xc(%rsp),%al
mov    %al,0x3(%rbp)
mov    $0x4,%ebx
jmp    11f4 <SendMessage+0xa4>
add    $0xc,%edx
mov    %edx,%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
movw   $0x7f81,(%rax)
lea    0x2(%rax),%rbx
lea    0xc(%rsp),%rsi
mov    $0x8,%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <memcpy@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
call   1050 <strreverse@plt>
mov    $0xc,%ebx
mov    %ebx,%edi
add    %rbp,%rdi
mov    0xc(%rsp),%edx
xor    %r12d,%r12d
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <memcpy@plt>
add    0xc(%rsp),%ebx
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
test   %rax,%rax
jle    122a <SendMessage+0xda>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    1230 <SendMessage+0xe0>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @SendMessage(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 126
br i1 %11, label %12, label %23
12: ; preds = %3
%13 = load i32, i32* %7
%14 = add nsw i32 2, %13
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = getelementptr inbounds i8, i8* %17, i64 0
store i8 -127, i8* %18
%19 = load i32, i32* %7
%20 = trunc i32 %19 to i8
%21 = load i8*, i8** %8
%22 = getelementptr inbounds i8, i8* %21, i64 1
store i8 %20, i8* %22
store i32 2, i32* %9
br label %62
23: ; preds = %3
%24 = load i32, i32* %7
%25 = icmp slt i32 %24, 65535
br i1 %25, label %26, label %46
26: ; preds = %23
%27 = load i32, i32* %7
%28 = add nsw i32 4, %27
%29 = call i64 @malloc(i32 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %8
%31 = load i8*, i8** %8
%32 = getelementptr inbounds i8, i8* %31, i64 0
store i8 -127, i8* %32
%33 = load i8*, i8** %8
%34 = getelementptr inbounds i8, i8* %33, i64 1
store i8 126, i8* %34
%35 = load i32, i32* %7
%36 = ashr i32 %35, 8
%37 = and i32 %36, 255
%38 = trunc i32 %37 to i8
%39 = load i8*, i8** %8
%40 = getelementptr inbounds i8, i8* %39, i64 2
store i8 %38, i8* %40
%41 = load i32, i32* %7
%42 = and i32 %41, 255
%43 = trunc i32 %42 to i8
%44 = load i8*, i8** %8
%45 = getelementptr inbounds i8, i8* %44, i64 3
store i8 %43, i8* %45
store i32 4, i32* %9
br label %61
46: ; preds = %23
%47 = load i32, i32* %7
%48 = add nsw i32 12, %47
%49 = call i64 @malloc(i32 %48)
%50 = inttoptr i64 %49 to i8*
store i8* %50, i8** %8
%51 = load i8*, i8** %8
%52 = getelementptr inbounds i8, i8* %51, i64 0
store i8 -127, i8* %52
%53 = load i8*, i8** %8
%54 = getelementptr inbounds i8, i8* %53, i64 1
store i8 127, i8* %54
%55 = load i8*, i8** %8
%56 = getelementptr inbounds i8, i8* %55, i64 2
%57 = call i32 (i8*, ...) @memcpy(i8* %56, i32* %7, i64 8)
%58 = load i8*, i8** %8
%59 = getelementptr inbounds i8, i8* %58, i64 2
%60 = call i32 @strreverse(i8* %59, i32 8)
store i32 12, i32* %9
br label %61
61: ; preds = %46, %26
br label %62
62: ; preds = %61, %12
%63 = load i8*, i8** %8
%64 = load i32, i32* %9
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = load i8*, i8** %6
%68 = load i32, i32* %7
%69 = call i32 (i8*, ...) @memcpy(i8* %66, i8* %67, i32 %68)
%70 = load i32, i32* %5
%71 = load i8*, i8** %8
%72 = load i32, i32* %9
%73 = load i32, i32* %7
%74 = add nsw i32 %72, %73
%75 = call i64 @write(i32 %70, i8* %71, i32 %74)
%76 = icmp sle i64 %75, 0
br i1 %76, label %77, label %78
77: ; preds = %62
store i32 -1, i32* %4
br label %81
78: ; preds = %62
%79 = load i8*, i8** %8
%80 = call i32 @free(i8* %79)
store i32 0, i32* %4
br label %81
81: ; preds = %78, %77
%82 = load i32, i32* %4
ret i32 %82
}

LLVM IR (-O2):
define i32 @SendMessage(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
store i32 %2, i32* %4
%5 = icmp slt i32 %2, 126
br i1 %5, label %6, label %13
6: ; preds = %3
%7 = add nsw i32 %2, 2
%8 = tail call i64 @malloc(i32 %7)
%9 = inttoptr i64 %8 to i8*
store i8 -127, i8* %9
%10 = load i32, i32* %4
%11 = trunc i32 %10 to i8
%12 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %11, i8* %12
br label %35
13: ; preds = %3
%14 = icmp ult i32 %2, 65535
br i1 %14, label %15, label %27
15: ; preds = %13
%16 = add nuw nsw i32 %2, 4
%17 = tail call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
store i8 -127, i8* %18
%19 = getelementptr inbounds i8, i8* %18, i64 1
store i8 126, i8* %19
%20 = load i32, i32* %4
%21 = lshr i32 %20, 8
%22 = trunc i32 %21 to i8
%23 = getelementptr inbounds i8, i8* %18, i64 2
store i8 %22, i8* %23
%24 = load i32, i32* %4
%25 = trunc i32 %24 to i8
%26 = getelementptr inbounds i8, i8* %18, i64 3
store i8 %25, i8* %26
br label %35
27: ; preds = %13
%28 = add nuw nsw i32 %2, 12
%29 = tail call i64 @malloc(i32 %28)
%30 = inttoptr i64 %29 to i8*
store i8 -127, i8* %30
%31 = getelementptr inbounds i8, i8* %30, i64 1
store i8 127, i8* %31
%32 = getelementptr inbounds i8, i8* %30, i64 2
%33 = call i32 (i8*, ...) @memcpy(i8* %32, i32* %4, i64 8)
%34 = call i32 @strreverse(i8* %32, i32 8)
br label %35
35: ; preds = %15, %27, %6
%36 = phi i8* [ %9, %6 ], [ %18, %15 ], [ %30, %27 ]
%37 = phi i32 [ 2, %6 ], [ 4, %15 ], [ 12, %27 ]
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i32, i32* %4
%41 = call i32 (i8*, ...) @memcpy(i8* %39, i8* %1, i32 %40)
%42 = load i32, i32* %4
%43 = add nsw i32 %42, %37
%44 = call i64 @write(i32 %0, i8* %36, i32 %43)
%45 = icmp slt i64 %44, 1
br i1 %45, label %48, label %46
46: ; preds = %35
%47 = call i32 @free(i8* %36)
br label %48
48: ; preds = %35, %46
%49 = phi i32 [ 0, %46 ], [ -1, %35 ]
ret i32 %49
}