#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*/ uint32_t;
typedef int /*<<< orphan*/ uint16_t;
typedef int /*<<< orphan*/ tmp;
struct ns_connection {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(unsigned char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ ns_send(struct ns_connection *, unsigned char *, int);
void ns_send_ws_header(struct ns_connection *nc, int op, size_t len) {
  int header_len;
  unsigned char header[10];
  header[0] = 0x80 + (op & 0x0f);
  if (len < 126) {
    header[1] = len;
    header_len = 2;
  } else if (len < 65535) {
    uint16_t tmp = htons((uint16_t)len);
    header[1] = 126;
    memcpy(&header[2], &tmp, sizeof(tmp));
    header_len = 4;
  } else {
    uint32_t tmp;
    header[1] = 127;
    tmp = htonl((uint32_t)((uint64_t)len >> 32));
    memcpy(&header[2], &tmp, sizeof(tmp));
    tmp = htonl((uint32_t)(len & 0xffffffff));
    memcpy(&header[6], &tmp, sizeof(tmp));
    header_len = 10;
  }
  ns_send(nc, header, header_len);
}


x86 assembly (clang, -O0):
<ns_send_ws_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
and    $0xf,%eax
add    $0x80,%eax
mov    %al,-0x26(%rbp)
cmpq   $0x7e,-0x18(%rbp)
jae    117f <ns_send_ws_header+0x3f>
mov    -0x18(%rbp),%rax
mov    %al,-0x25(%rbp)
movl   $0x2,-0x1c(%rbp)
jmp    122a <ns_send_ws_header+0xea>
cmpq   $0xffff,-0x18(%rbp)
jae    11c1 <ns_send_ws_header+0x81>
mov    -0x18(%rbp),%rax
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
movb   $0x7e,-0x25(%rbp)
lea    -0x26(%rbp),%rdi
add    $0x2,%rdi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
movl   $0x4,-0x1c(%rbp)
jmp    1225 <ns_send_ws_header+0xe5>
movb   $0x7f,-0x25(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%edi
mov    $0x20,%ecx
sar    %cl,%edi
call   1040 <htonl@plt>
mov    %eax,-0x30(%rbp)
lea    -0x26(%rbp),%rdi
add    $0x2,%rdi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
movabs $0xffffffff,%rax
and    -0x18(%rbp),%rax
mov    %eax,%edi
call   1040 <htonl@plt>
mov    %eax,-0x30(%rbp)
lea    -0x26(%rbp),%rdi
add    $0x6,%rdi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
movl   $0xa,-0x1c(%rbp)
jmp    122a <ns_send_ws_header+0xea>
mov    -0x8(%rbp),%rdi
lea    -0x26(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <ns_send@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ns_send_ws_header>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rdi,%r14
and    $0xf,%sil
or     $0x80,%sil
mov    %sil,0x6(%rsp)
cmp    $0x7d,%rdx
ja     116d <ns_send_ws_header+0x2d>
mov    %bl,0x7(%rsp)
mov    $0x2,%ebx
jmp    11e1 <ns_send_ws_header+0xa1>
cmp    $0xfffe,%rbx
ja     119e <ns_send_ws_header+0x5e>
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
movb   $0x7e,0x7(%rsp)
lea    0x8(%rsp),%rdi
mov    %rsp,%rsi
mov    $0x4,%ebx
mov    $0x4,%edx
call   1060 <memcpy@plt>
jmp    11e1 <ns_send_ws_header+0xa1>
movb   $0x7f,0x7(%rsp)
call   1040 <htonl@plt>
mov    %eax,(%rsp)
lea    0x8(%rsp),%rdi
mov    %rsp,%r15
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    %ebx,%edi
call   1040 <htonl@plt>
mov    %eax,(%rsp)
lea    0xc(%rsp),%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    $0xa,%ebx
lea    0x6(%rsp),%rsi
mov    %r14,%rdi
mov    %ebx,%edx
call   1050 <ns_send@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.ns_connection = type { i32 }
define void @ns_send_ws_header(%struct.ns_connection* %0, i32 %1, i64 %2) {
%4 = alloca %struct.ns_connection*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca [10 x i8]
%9 = alloca i32
%10 = alloca i32
store %struct.ns_connection* %0, %struct.ns_connection** %4
store i32 %1, i32* %5
store i64 %2, i64* %6
%11 = load i32, i32* %5
%12 = and i32 %11, 15
%13 = add nsw i32 128, %12
%14 = trunc i32 %13 to i8
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
store i8 %14, i8* %15
%16 = load i64, i64* %6
%17 = icmp ult i64 %16, 126
br i1 %17, label %18, label %22
18: ; preds = %3
%19 = load i64, i64* %6
%20 = trunc i64 %19 to i8
%21 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 1
store i8 %20, i8* %21
store i32 2, i32* %7
br label %47
22: ; preds = %3
%23 = load i64, i64* %6
%24 = icmp ult i64 %23, 65535
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = load i64, i64* %6
%27 = trunc i64 %26 to i32
%28 = call i32 @htons(i32 %27)
store i32 %28, i32* %9
%29 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 1
store i8 126, i8* %29
%30 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 2
%31 = call i32 @memcpy(i8* %30, i32* %9, i32 4)
store i32 4, i32* %7
br label %46
32: ; preds = %22
%33 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 1
store i8 127, i8* %33
%34 = load i64, i64* %6
%35 = trunc i64 %34 to i32
%36 = ashr i32 %35, 32
%37 = call i32 @htonl(i32 %36)
store i32 %37, i32* %10
%38 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 2
%39 = call i32 @memcpy(i8* %38, i32* %10, i32 4)
%40 = load i64, i64* %6
%41 = and i64 %40, 4294967295
%42 = trunc i64 %41 to i32
%43 = call i32 @htonl(i32 %42)
store i32 %43, i32* %10
%44 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 6
%45 = call i32 @memcpy(i8* %44, i32* %10, i32 4)
store i32 10, i32* %7
br label %46
46: ; preds = %32, %25
br label %47
47: ; preds = %46, %18
%48 = load %struct.ns_connection*, %struct.ns_connection** %4
%49 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%50 = load i32, i32* %7
%51 = call i32 @ns_send(%struct.ns_connection* %48, i8* %49, i32 %50)
ret void
}

LLVM IR (-O2):
%struct.ns_connection = type { i32 }
define void @ns_send_ws_header(%struct.ns_connection* %0, i32 %1, i64 %2) {
%4 = alloca [10 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%8 = trunc i32 %1 to i8
%9 = and i8 %8, 15
%10 = or i8 %9, -128
store i8 %10, i8* %7
%11 = icmp ult i64 %2, 126
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = trunc i64 %2 to i8
%14 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 1
store i8 %13, i8* %14
br label %34
15: ; preds = %3
%16 = icmp ult i64 %2, 65535
br i1 %16, label %17, label %24
17: ; preds = %15
%18 = bitcast i32* %5 to i8*
%19 = trunc i64 %2 to i32
%20 = tail call i32 @htons(i32 %19)
store i32 %20, i32* %5
%21 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 1
store i8 126, i8* %21
%22 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 2
%23 = call i32 @memcpy(i8* %22, i32* %5, i32 4)
br label %34
24: ; preds = %15
%25 = bitcast i32* %6 to i8*
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 1
store i8 127, i8* %26
%27 = tail call i32 @htonl(i32 poison)
store i32 %27, i32* %6
%28 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 2
%29 = call i32 @memcpy(i8* %28, i32* %6, i32 4)
%30 = trunc i64 %2 to i32
%31 = call i32 @htonl(i32 %30)
store i32 %31, i32* %6
%32 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 6
%33 = call i32 @memcpy(i8* %32, i32* %6, i32 4)
br label %34
34: ; preds = %17, %24, %12
%35 = phi i32 [ 2, %12 ], [ 4, %17 ], [ 10, %24 ]
%36 = call i32 @ns_send(%struct.ns_connection* %0, i8* %7, i32 %35)
ret void
}