#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 uint64_t;
typedef int /*<<< orphan*/ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int ntohll(int);
int ws_write_frame_header(char *out, int type, uint64_t len) {
  out[0] = type | 0x80;
  if (len < 126) {
    out[1] = len;
    return 2;
  }
  void *p = &out[2];
  if (len <= 0xFFFF) {
    out[1] = 126;
    *(uint16_t *)p = htons((uint16_t)len);
    return 4;
  }
  if (len <= 0x7FFFFFFFFFFFFFFFULL) {
    out[1] = 127;
    *(uint64_t *)p = ntohll((uint64_t)len);
    return 10;
  }
  return -1;
}


x86 assembly (clang, -O0):
<ws_write_frame_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
cmpl   $0x7e,-0x18(%rbp)
jge    1164 <ws_write_frame_header+0x44>
mov    -0x18(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
movl   $0x2,-0x4(%rbp)
jmp    11e3 <ws_write_frame_header+0xc3>
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
cmpl   $0xffff,-0x18(%rbp)
jg     11a1 <ws_write_frame_header+0x81>
mov    -0x10(%rbp),%rax
movb   $0x7e,0x1(%rax)
mov    -0x18(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x4,-0x4(%rbp)
jmp    11e3 <ws_write_frame_header+0xc3>
movslq -0x18(%rbp),%rax
movabs $0x7fffffffffffffff,%rcx
ff ff 7f
cmp    %rcx,%rax
ja     11dc <ws_write_frame_header+0xbc>
mov    -0x10(%rbp),%rax
movb   $0x7f,0x1(%rax)
mov    -0x18(%rbp),%edi
call   1040 <ntohll@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0xa,-0x4(%rbp)
jmp    11e3 <ws_write_frame_header+0xc3>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ws_write_frame_header>:
push   %rbx
mov    %rdi,%rbx
or     $0x80,%sil
mov    %sil,(%rdi)
cmp    $0x7d,%edx
jg     113a <ws_write_frame_header+0x1a>
mov    %dl,0x1(%rbx)
mov    $0x2,%eax
pop    %rbx
ret
cmp    $0xffff,%edx
ja     1157 <ws_write_frame_header+0x37>
movb   $0x7e,0x1(%rbx)
mov    %edx,%edi
call   1030 <htons@plt>
mov    %eax,0x2(%rbx)
mov    $0x4,%eax
pop    %rbx
ret
movb   $0x7f,0x1(%rbx)
mov    %edx,%edi
call   1040 <ntohll@plt>
mov    %eax,0x2(%rbx)
mov    $0xa,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @ws_write_frame_header(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i32, i32* %6
%10 = or i32 %9, 128
%11 = trunc i32 %10 to i8
%12 = load i8*, i8** %5
%13 = getelementptr inbounds i8, i8* %12, i64 0
store i8 %11, i8* %13
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 126
br i1 %15, label %16, label %21
16: ; preds = %3
%17 = load i32, i32* %7
%18 = trunc i32 %17 to i8
%19 = load i8*, i8** %5
%20 = getelementptr inbounds i8, i8* %19, i64 1
store i8 %18, i8* %20
store i32 2, i32* %4
br label %45
21: ; preds = %3
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i64 2
store i8* %23, i8** %8
%24 = load i32, i32* %7
%25 = icmp sle i32 %24, 65535
br i1 %25, label %26, label %33
26: ; preds = %21
%27 = load i8*, i8** %5
%28 = getelementptr inbounds i8, i8* %27, i64 1
store i8 126, i8* %28
%29 = load i32, i32* %7
%30 = call i32 @htons(i32 %29)
%31 = load i8*, i8** %8
%32 = bitcast i8* %31 to i32*
store i32 %30, i32* %32
store i32 4, i32* %4
br label %45
33: ; preds = %21
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = icmp ule i64 %35, 9223372036854775807
br i1 %36, label %37, label %44
37: ; preds = %33
%38 = load i8*, i8** %5
%39 = getelementptr inbounds i8, i8* %38, i64 1
store i8 127, i8* %39
%40 = load i32, i32* %7
%41 = call i32 @ntohll(i32 %40)
%42 = load i8*, i8** %8
%43 = bitcast i8* %42 to i32*
store i32 %41, i32* %43
store i32 10, i32* %4
br label %45
44: ; preds = %33
store i32 -1, i32* %4
br label %45
45: ; preds = %44, %37, %26, %16
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
define i32 @ws_write_frame_header(i8* nocapture writeonly %0, i32 %1, i32 %2) {
%4 = trunc i32 %1 to i8
%5 = or i8 %4, -128
store i8 %5, i8* %0
%6 = icmp slt i32 %2, 126
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = trunc i32 %2 to i8
%9 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %8, i8* %9
br label %20
10: ; preds = %3
%11 = getelementptr inbounds i8, i8* %0, i64 2
%12 = icmp ult i32 %2, 65536
%13 = getelementptr inbounds i8, i8* %0, i64 1
br i1 %12, label %14, label %17
14: ; preds = %10
store i8 126, i8* %13
%15 = tail call i32 @htons(i32 %2)
%16 = bitcast i8* %11 to i32*
store i32 %15, i32* %16
br label %20
17: ; preds = %10
store i8 127, i8* %13
%18 = tail call i32 @ntohll(i32 %2)
%19 = bitcast i8* %11 to i32*
store i32 %18, i32* %19
br label %20
20: ; preds = %14, %17, %7
%21 = phi i32 [ 2, %7 ], [ 4, %14 ], [ 10, %17 ]
ret i32 %21
}