#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 int /*<<< orphan*/ uint8_t;
typedef int /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ uint16_t;
typedef int /*<<< orphan*/ tmpbuf;
typedef int /*<<< orphan*/ payload_size;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ htonl(size_t);
int /*<<< orphan*/ htons(size_t);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
size_t websocket_frame_header(size_t payload_size, uint8_t *buf, size_t size) {
  uint8_t tmpbuf[(2 + 8 + 4)];
  uint8_t *orig_buf_ptr;
  if (buf) {
    assert(!buf || size >= (2 + 8 + 4));
    orig_buf_ptr = buf;
  } else {
    orig_buf_ptr = buf = tmpbuf;
    size = sizeof(tmpbuf);
  }
  if (!payload_size)
    return 0;
  struct ws_frame {
    enum {
      OP_CONTINUATION = 0x0,
      OP_TEXT_FRAME = 0x1,
      OP_BINARY_FRAME = 0x2,
      OP_CLOSE = 0x8,
      OP_PING = 0x9,
      OP_PONG = 0xA
    } opcode : 4;
    unsigned int rsvs : 3;
    unsigned int fin : 1;
  } first_byte = {.opcode = OP_TEXT_FRAME, .fin = 1};
  *buf++ = *(uint8_t *)&first_byte;
  if (payload_size <= 125) {
    *buf++ = payload_size;
  } else if (payload_size <= 65535) {
    *buf++ = 126;
    uint16_t network_order_size = htons(payload_size);
    memcpy(buf, &network_order_size, 2);
    buf += 2;
  } else if (sizeof(payload_size) <= sizeof(uint32_t)) {
    *buf++ = 127;
    memset(buf, 0, 4);
    buf += 4;
    uint32_t network_order_size = htonl(payload_size);
    memcpy(buf, &network_order_size, 4);
    buf += 4;
  } else {
    *buf++ = 127;
    uint32_t hi = htonl(payload_size >> 32);
    memcpy(buf, &hi, 4);
    buf += 4;
    uint32_t lo = htonl(payload_size & 0xffffffff);
    memcpy(buf, &lo, 4);
    buf += 4;
  }
  return buf - orig_buf_ptr;
}


x86 assembly (clang, -O0):
<websocket_frame_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1197 <websocket_frame_header+0x57>
mov    $0x1,%al
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x7d(%rbp)
je     117d <websocket_frame_header+0x3d>
cmpq   $0xe,-0x20(%rbp)
setae  %al
mov    %al,-0x7d(%rbp)
mov    -0x7d(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x68(%rbp)
jmp    11ab <websocket_frame_header+0x6b>
lea    -0x60(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    %rax,-0x68(%rbp)
movq   $0x38,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c3 <websocket_frame_header+0x83>
movq   $0x0,-0x8(%rbp)
jmp    12e8 <websocket_frame_header+0x1a8>
mov    0xe37(%rip),%eax
mov    %eax,-0x70(%rbp)
mov    -0x70(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x18(%rbp)
mov    %ecx,(%rax)
cmpq   $0x7d,-0x10(%rbp)
ja     1207 <websocket_frame_header+0xc7>
mov    -0x10(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x18(%rbp)
mov    %ecx,(%rax)
jmp    12d5 <websocket_frame_header+0x195>
cmpq   $0xffff,-0x10(%rbp)
ja     1259 <websocket_frame_header+0x119>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x18(%rbp)
movl   $0x7e,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <htons@plt>
mov    %eax,-0x74(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x74(%rbp),%rsi
mov    $0x2,%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
jmp    12d0 <websocket_frame_header+0x190>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x18(%rbp)
movl   $0x7f,(%rax)
mov    -0x10(%rbp),%rdi
shr    $0x20,%rdi
call   1040 <htonl@plt>
mov    %eax,-0x78(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x78(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x18(%rbp)
movabs $0xffffffff,%rdi
and    -0x10(%rbp),%rdi
call   1040 <htonl@plt>
mov    %eax,-0x7c(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x7c(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x18(%rbp)
jmp    12d5 <websocket_frame_header+0x195>
mov    -0x18(%rbp),%rax
mov    -0x68(%rbp),%rcx
sub    %rcx,%rax
sar    $0x2,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<websocket_frame_header>:
push   %r15
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %rdi,%rbx
test   %rsi,%rsi
je     1182 <websocket_frame_header+0x42>
mov    %rsi,%r14
xor    %edi,%edi
cmp    $0xe,%rdx
setae  %dil
call   1050 <assert@plt>
test   %rbx,%rbx
je     118c <websocket_frame_header+0x4c>
movl   $0x81,(%r14)
cmp    $0x7d,%rbx
ja     1193 <websocket_frame_header+0x53>
lea    0x8(%r14),%rax
mov    %ebx,0x4(%r14)
jmp    121b <websocket_frame_header+0xdb>
lea    0x10(%rsp),%r14
test   %rbx,%rbx
jne    1168 <websocket_frame_header+0x28>
xor    %eax,%eax
jmp    1222 <websocket_frame_header+0xe2>
lea    0x8(%r14),%r15
cmp    $0xffff,%rbx
ja     11cc <websocket_frame_header+0x8c>
movl   $0x7e,0x4(%r14)
mov    %rbx,%rdi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
mov    $0x2,%edx
call   1060 <memcpy@plt>
lea    0x10(%r14),%rax
jmp    121b <websocket_frame_header+0xdb>
movl   $0x7f,0x4(%r14)
mov    %rbx,%rdi
shr    $0x20,%rdi
call   1040 <htonl@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
mov    $0x4,%edx
call   1060 <memcpy@plt>
lea    0x18(%r14),%r15
mov    %ebx,%edi
call   1040 <htonl@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    %r15,%rdi
mov    $0x4,%edx
call   1060 <memcpy@plt>
lea    0x28(%r14),%rax
sub    %r14,%rax
sar    $0x2,%rax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.ws_frame = type { i8, [3 x i8] }
@__const.websocket_frame_header.first_byte = %struct.ws_frame { i8 -127, [3 x i8] undef }
define i64 @websocket_frame_header(i64 %0, i32* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i64
%8 = alloca [14 x i32]
%9 = alloca i32*
%10 = alloca %struct.ws_frame
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i64 %0, i64* %5
store i32* %1, i32** %6
store i64 %2, i64* %7
%14 = load i32*, i32** %6
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %27
16: ; preds = %3
%17 = load i32*, i32** %6
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i64, i64* %7
%21 = icmp uge i64 %20, 14
br label %22
22: ; preds = %19, %16
%23 = phi i1 [ true, %16 ], [ %21, %19 ]
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = load i32*, i32** %6
store i32* %26, i32** %9
br label %29
27: ; preds = %3
%28 = getelementptr inbounds [14 x i32], [14 x i32]* %8, i64 0, i64 0
store i32* %28, i32** %6
store i32* %28, i32** %9
store i64 56, i64* %7
br label %29
29: ; preds = %27, %22
%30 = load i64, i64* %5
%31 = icmp ne i64 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %29
store i64 0, i64* %4
br label %83
33: ; preds = %29
%34 = bitcast %struct.ws_frame* %10 to i8*
%35 = bitcast %struct.ws_frame* %10 to i32*
%36 = load i32, i32* %35
%37 = load i32*, i32** %6
%38 = getelementptr inbounds i32, i32* %37, i32 1
store i32* %38, i32** %6
store i32 %36, i32* %37
%39 = load i64, i64* %5
%40 = icmp ule i64 %39, 125
br i1 %40, label %41, label %46
41: ; preds = %33
%42 = load i64, i64* %5
%43 = trunc i64 %42 to i32
%44 = load i32*, i32** %6
%45 = getelementptr inbounds i32, i32* %44, i32 1
store i32* %45, i32** %6
store i32 %43, i32* %44
br label %76
46: ; preds = %33
%47 = load i64, i64* %5
%48 = icmp ule i64 %47, 65535
br i1 %48, label %49, label %58
49: ; preds = %46
%50 = load i32*, i32** %6
%51 = getelementptr inbounds i32, i32* %50, i32 1
store i32* %51, i32** %6
store i32 126, i32* %50
%52 = load i64, i64* %5
%53 = call i32 @htons(i64 %52)
store i32 %53, i32* %11
%54 = load i32*, i32** %6
%55 = call i32 @memcpy(i32* %54, i32* %11, i32 2)
%56 = load i32*, i32** %6
%57 = getelementptr inbounds i32, i32* %56, i64 2
store i32* %57, i32** %6
br label %75
58: ; preds = %46
%59 = load i32*, i32** %6
%60 = getelementptr inbounds i32, i32* %59, i32 1
store i32* %60, i32** %6
store i32 127, i32* %59
%61 = load i64, i64* %5
%62 = lshr i64 %61, 32
%63 = call i32 @htonl(i64 %62)
store i32 %63, i32* %12
%64 = load i32*, i32** %6
%65 = call i32 @memcpy(i32* %64, i32* %12, i32 4)
%66 = load i32*, i32** %6
%67 = getelementptr inbounds i32, i32* %66, i64 4
store i32* %67, i32** %6
%68 = load i64, i64* %5
%69 = and i64 %68, 4294967295
%70 = call i32 @htonl(i64 %69)
store i32 %70, i32* %13
%71 = load i32*, i32** %6
%72 = call i32 @memcpy(i32* %71, i32* %13, i32 4)
%73 = load i32*, i32** %6
%74 = getelementptr inbounds i32, i32* %73, i64 4
store i32* %74, i32** %6
br label %75
75: ; preds = %58, %49
br label %76
76: ; preds = %75, %41
%77 = load i32*, i32** %6
%78 = load i32*, i32** %9
%79 = ptrtoint i32* %77 to i64
%80 = ptrtoint i32* %78 to i64
%81 = sub i64 %79, %80
%82 = sdiv exact i64 %81, 4
store i64 %82, i64* %4
br label %83
83: ; preds = %76, %32
%84 = load i64, i64* %4
ret i64 %84
}

LLVM IR (-O2):
define i64 @websocket_frame_header(i64 %0, i32* %1, i64 %2) {
%4 = alloca [14 x i32]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = bitcast [14 x i32]* %4 to i8*
%9 = icmp eq i32* %1, null
br i1 %9, label %14, label %10
10: ; preds = %3
%11 = icmp ugt i64 %2, 13
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
br label %16
14: ; preds = %3
%15 = getelementptr inbounds [14 x i32], [14 x i32]* %4, i64 0, i64 0
br label %16
16: ; preds = %14, %10
%17 = phi i32* [ %1, %10 ], [ %15, %14 ]
%18 = icmp eq i64 %0, 0
br i1 %18, label %50, label %19
19: ; preds = %16
%20 = getelementptr inbounds i32, i32* %17, i64 1
store i32 129, i32* %17
%21 = icmp ult i64 %0, 126
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = trunc i64 %0 to i32
%24 = getelementptr inbounds i32, i32* %17, i64 2
store i32 %23, i32* %20
br label %44
25: ; preds = %19
%26 = icmp ult i64 %0, 65536
%27 = getelementptr inbounds i32, i32* %17, i64 2
br i1 %26, label %28, label %33
28: ; preds = %25
store i32 126, i32* %20
%29 = bitcast i32* %5 to i8*
%30 = tail call i32 @htons(i64 %0)
store i32 %30, i32* %5
%31 = call i32 @memcpy(i32* %27, i32* %5, i32 2)
%32 = getelementptr inbounds i32, i32* %17, i64 4
br label %44
33: ; preds = %25
store i32 127, i32* %20
%34 = bitcast i32* %6 to i8*
%35 = lshr i64 %0, 32
%36 = tail call i32 @htonl(i64 %35)
store i32 %36, i32* %6
%37 = call i32 @memcpy(i32* %27, i32* %6, i32 4)
%38 = getelementptr inbounds i32, i32* %17, i64 6
%39 = bitcast i32* %7 to i8*
%40 = and i64 %0, 4294967295
%41 = call i32 @htonl(i64 %40)
store i32 %41, i32* %7
%42 = call i32 @memcpy(i32* %38, i32* %7, i32 4)
%43 = getelementptr inbounds i32, i32* %17, i64 10
br label %44
44: ; preds = %28, %33, %22
%45 = phi i32* [ %24, %22 ], [ %32, %28 ], [ %43, %33 ]
%46 = ptrtoint i32* %45 to i64
%47 = ptrtoint i32* %17 to i64
%48 = sub i64 %46, %47
%49 = ashr exact i64 %48, 2
br label %50
50: ; preds = %16, %44
%51 = phi i64 [ %49, %44 ], [ 0, %16 ]
ret i64 %51
}