#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 */
size_t make_websocket_header(unsigned char buffer[10], unsigned char opcode,
                             int payload_len, int fin) {
  buffer[0] = (fin ? 0x80 : 0x00) | opcode;
  if (payload_len < 126) {
    buffer[1] = (unsigned char)(payload_len & 0x7f);
    return 2;
  } else if (payload_len < 65536) {
    buffer[1] = 126;
    buffer[2] = (unsigned char)(payload_len >> 8);
    buffer[3] = (unsigned char)payload_len;
    return 4;
  } else {
    buffer[1] = 127;
    buffer[2] = buffer[3] = buffer[4] = buffer[5] = 0;
    buffer[6] = (unsigned char)(payload_len >> 24);
    buffer[7] = (unsigned char)(payload_len >> 16);
    buffer[8] = (unsigned char)(payload_len >> 8);
    buffer[9] = (unsigned char)payload_len;
    return 10;
  }
}


x86 assembly (clang, -O0):
<make_websocket_header>:
push   %rbp
mov    %rsp,%rbp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x1c(%rbp),%edx
xor    %eax,%eax
mov    $0x80,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
movzbl -0x11(%rbp),%ecx
or     %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
cmpl   $0x7e,-0x18(%rbp)
jge    1158 <make_websocket_header+0x58>
mov    -0x18(%rbp),%eax
and    $0x7f,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
movq   $0x2,-0x8(%rbp)
jmp    11fe <make_websocket_header+0xfe>
cmpl   $0x10000,-0x18(%rbp)
jge    1195 <make_websocket_header+0x95>
mov    -0x10(%rbp),%rax
movb   $0x7e,0x1(%rax)
mov    -0x18(%rbp),%eax
sar    $0x8,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x18(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x3(%rax)
movq   $0x4,-0x8(%rbp)
jmp    11fe <make_websocket_header+0xfe>
mov    -0x10(%rbp),%rax
movb   $0x7f,0x1(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x5(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x4(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x3(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x2(%rax)
mov    -0x18(%rbp),%eax
sar    $0x18,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x6(%rax)
mov    -0x18(%rbp),%eax
sar    $0x10,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x7(%rax)
mov    -0x18(%rbp),%eax
sar    $0x8,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x8(%rax)
mov    -0x18(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x9(%rax)
movq   $0xa,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_websocket_header>:
test   %ecx,%ecx
setne  %al
shl    $0x7,%al
or     %sil,%al
mov    %al,(%rdi)
cmp    $0x7d,%edx
jg     1123 <make_websocket_header+0x23>
and    $0x7f,%dl
mov    $0x2,%eax
mov    $0x1,%ecx
mov    %dl,(%rdi,%rcx,1)
ret
cmp    $0xffff,%edx
ja     1140 <make_websocket_header+0x40>
movb   $0x7e,0x1(%rdi)
mov    %dh,0x2(%rdi)
mov    $0x4,%eax
mov    $0x3,%ecx
mov    %dl,(%rdi,%rcx,1)
ret
movb   $0x7f,0x1(%rdi)
mov    %edx,%eax
shr    $0x18,%eax
movl   $0x0,0x2(%rdi)
mov    %al,0x6(%rdi)
mov    %edx,%eax
shr    $0x10,%eax
mov    %al,0x7(%rdi)
mov    %dh,0x8(%rdi)
mov    $0xa,%eax
mov    $0x9,%ecx
mov    %dl,(%rdi,%rcx,1)
ret

LLVM IR (-O0):
define i64 @make_websocket_header(i8* %0, i8 zeroext %1, i32 %2, i32 %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %6
store i8 %1, i8* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%10 = load i32, i32* %9
%11 = icmp ne i32 %10, 0
%12 = zext i1 %11 to i64
%13 = select i1 %11, i32 128, i32 0
%14 = load i8, i8* %7
%15 = zext i8 %14 to i32
%16 = or i32 %13, %15
%17 = trunc i32 %16 to i8
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i64 0
store i8 %17, i8* %19
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 126
br i1 %21, label %22, label %28
22: ; preds = %4
%23 = load i32, i32* %8
%24 = and i32 %23, 127
%25 = trunc i32 %24 to i8
%26 = load i8*, i8** %6
%27 = getelementptr inbounds i8, i8* %26, i64 1
store i8 %25, i8* %27
store i64 2, i64* %5
br label %73
28: ; preds = %4
%29 = load i32, i32* %8
%30 = icmp slt i32 %29, 65536
br i1 %30, label %31, label %43
31: ; preds = %28
%32 = load i8*, i8** %6
%33 = getelementptr inbounds i8, i8* %32, i64 1
store i8 126, i8* %33
%34 = load i32, i32* %8
%35 = ashr i32 %34, 8
%36 = trunc i32 %35 to i8
%37 = load i8*, i8** %6
%38 = getelementptr inbounds i8, i8* %37, i64 2
store i8 %36, i8* %38
%39 = load i32, i32* %8
%40 = trunc i32 %39 to i8
%41 = load i8*, i8** %6
%42 = getelementptr inbounds i8, i8* %41, i64 3
store i8 %40, i8* %42
store i64 4, i64* %5
br label %73
43: ; preds = %28
%44 = load i8*, i8** %6
%45 = getelementptr inbounds i8, i8* %44, i64 1
store i8 127, i8* %45
%46 = load i8*, i8** %6
%47 = getelementptr inbounds i8, i8* %46, i64 5
store i8 0, i8* %47
%48 = load i8*, i8** %6
%49 = getelementptr inbounds i8, i8* %48, i64 4
store i8 0, i8* %49
%50 = load i8*, i8** %6
%51 = getelementptr inbounds i8, i8* %50, i64 3
store i8 0, i8* %51
%52 = load i8*, i8** %6
%53 = getelementptr inbounds i8, i8* %52, i64 2
store i8 0, i8* %53
%54 = load i32, i32* %8
%55 = ashr i32 %54, 24
%56 = trunc i32 %55 to i8
%57 = load i8*, i8** %6
%58 = getelementptr inbounds i8, i8* %57, i64 6
store i8 %56, i8* %58
%59 = load i32, i32* %8
%60 = ashr i32 %59, 16
%61 = trunc i32 %60 to i8
%62 = load i8*, i8** %6
%63 = getelementptr inbounds i8, i8* %62, i64 7
store i8 %61, i8* %63
%64 = load i32, i32* %8
%65 = ashr i32 %64, 8
%66 = trunc i32 %65 to i8
%67 = load i8*, i8** %6
%68 = getelementptr inbounds i8, i8* %67, i64 8
store i8 %66, i8* %68
%69 = load i32, i32* %8
%70 = trunc i32 %69 to i8
%71 = load i8*, i8** %6
%72 = getelementptr inbounds i8, i8* %71, i64 9
store i8 %70, i8* %72
store i64 10, i64* %5
br label %73
73: ; preds = %43, %31, %22
%74 = load i64, i64* %5
ret i64 %74
}

LLVM IR (-O2):
define i64 @make_websocket_header(i8* nocapture writeonly %0, i8 zeroext %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %3, 0
%6 = select i1 %5, i8 0, i8 -128
%7 = or i8 %6, %1
store i8 %7, i8* %0
%8 = icmp slt i32 %2, 126
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = trunc i32 %2 to i8
%11 = and i8 %10, 127
br label %33
12: ; preds = %4
%13 = icmp ult i32 %2, 65536
%14 = getelementptr inbounds i8, i8* %0, i64 1
br i1 %13, label %15, label %20
15: ; preds = %12
store i8 126, i8* %14
%16 = lshr i32 %2, 8
%17 = trunc i32 %16 to i8
%18 = getelementptr inbounds i8, i8* %0, i64 2
store i8 %17, i8* %18
%19 = trunc i32 %2 to i8
br label %33
20: ; preds = %12
store i8 127, i8* %14
%21 = getelementptr inbounds i8, i8* %0, i64 2
%22 = lshr i32 %2, 24
%23 = trunc i32 %22 to i8
%24 = getelementptr inbounds i8, i8* %0, i64 6
%25 = bitcast i8* %21 to i32*
store i32 0, i32* %25
store i8 %23, i8* %24
%26 = lshr i32 %2, 16
%27 = trunc i32 %26 to i8
%28 = getelementptr inbounds i8, i8* %0, i64 7
store i8 %27, i8* %28
%29 = lshr i32 %2, 8
%30 = trunc i32 %29 to i8
%31 = getelementptr inbounds i8, i8* %0, i64 8
store i8 %30, i8* %31
%32 = trunc i32 %2 to i8
br label %33
33: ; preds = %20, %15, %9
%34 = phi i64 [ 9, %20 ], [ 3, %15 ], [ 1, %9 ]
%35 = phi i8 [ %32, %20 ], [ %19, %15 ], [ %11, %9 ]
%36 = phi i64 [ 10, %20 ], [ 4, %15 ], [ 2, %9 ]
%37 = getelementptr inbounds i8, i8* %0, i64 %34
store i8 %35, i8* %37
ret i64 %36
}