#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 */
unsigned long long htonl(int);
unsigned short htons(unsigned short);
int ILibWebServer_WebSocket_CreateHeader(char *header, unsigned short FLAGS,
                                         unsigned short OPCODE,
                                         int payloadLength) {
  unsigned short header1 = 0;
  int retVal;
  header1 = OPCODE << 8;
  header1 |= FLAGS;
  if (payloadLength < 126) {
    retVal = 2;
    header1 |= payloadLength;
    ((unsigned short *)header)[0] = htons(header1);
  } else if (payloadLength <= 0xFFFF) {
    retVal = 4;
    header1 |= 126;
    ((unsigned short *)header)[0] = htons(header1);
    ((unsigned short *)header)[1] = htons((unsigned short)payloadLength);
  } else {
    retVal = 10;
    header1 |= 127;
    ((unsigned short *)header)[0] = htons(header1);
    ((unsigned long long *)(header + 2))[0] = htonl((int)payloadLength);
  }
  return (retVal);
}


x86 assembly (clang, -O0):
<ILibWebServer_WebSocket_CreateHeader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dx,%ax
mov    %si,%dx
mov    %rdi,-0x8(%rbp)
mov    %dx,-0xa(%rbp)
mov    %ax,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
movw   $0x0,-0x12(%rbp)
movzwl -0xc(%rbp),%eax
shl    $0x8,%eax
mov    %ax,-0x12(%rbp)
movzwl -0xa(%rbp),%ecx
movzwl -0x12(%rbp),%eax
or     %ecx,%eax
mov    %ax,-0x12(%rbp)
cmpl   $0x7e,-0x10(%rbp)
jge    1192 <ILibWebServer_WebSocket_CreateHeader+0x72>
movl   $0x2,-0x18(%rbp)
mov    -0x10(%rbp),%ecx
movzwl -0x12(%rbp),%eax
or     %ecx,%eax
mov    %ax,-0x12(%rbp)
movzwl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %ax,%cx
mov    -0x8(%rbp),%rax
mov    %cx,(%rax)
jmp    121c <ILibWebServer_WebSocket_CreateHeader+0xfc>
cmpl   $0xffff,-0x10(%rbp)
jg     11df <ILibWebServer_WebSocket_CreateHeader+0xbf>
movl   $0x4,-0x18(%rbp)
movzwl -0x12(%rbp),%eax
or     $0x7e,%eax
mov    %ax,-0x12(%rbp)
movzwl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %ax,%cx
mov    -0x8(%rbp),%rax
mov    %cx,(%rax)
mov    -0x10(%rbp),%eax
movzwl %ax,%edi
call   1030 <htons@plt>
mov    %ax,%cx
mov    -0x8(%rbp),%rax
mov    %cx,0x2(%rax)
jmp    1217 <ILibWebServer_WebSocket_CreateHeader+0xf7>
movl   $0xa,-0x18(%rbp)
movzwl -0x12(%rbp),%eax
or     $0x7f,%eax
mov    %ax,-0x12(%rbp)
movzwl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %ax,%cx
mov    -0x8(%rbp),%rax
mov    %cx,(%rax)
mov    -0x10(%rbp),%edi
call   1040 <htonl@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,0x2(%rax)
jmp    121c <ILibWebServer_WebSocket_CreateHeader+0xfc>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ILibWebServer_WebSocket_CreateHeader>:
push   %rbp
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %rdi,%rbx
shl    $0x8,%edx
or     %esi,%edx
cmp    $0x7d,%ecx
jg     1146 <ILibWebServer_WebSocket_CreateHeader+0x26>
or     %ebp,%edx
movzwl %dx,%edi
call   1030 <htons@plt>
mov    %ax,(%rbx)
mov    $0x2,%eax
jmp    118d <ILibWebServer_WebSocket_CreateHeader+0x6d>
cmp    $0xffff,%ebp
ja     116f <ILibWebServer_WebSocket_CreateHeader+0x4f>
or     $0x7e,%edx
movzwl %dx,%edi
call   1030 <htons@plt>
mov    %ax,(%rbx)
movzwl %bp,%edi
call   1030 <htons@plt>
mov    %ax,0x2(%rbx)
mov    $0x4,%eax
jmp    118d <ILibWebServer_WebSocket_CreateHeader+0x6d>
or     $0x7f,%edx
movzwl %dx,%edi
call   1030 <htons@plt>
mov    %ax,(%rbx)
mov    %ebp,%edi
call   1040 <htonl@plt>
mov    %rax,0x2(%rbx)
mov    $0xa,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @ILibWebServer_WebSocket_CreateHeader(i8* nocapture writeonly %0, i16 zeroext %1, i16 zeroext %2, i32 %3) {
%5 = shl i16 %2, 8
%6 = or i16 %5, %1
%7 = icmp slt i32 %3, 126
br i1 %7, label %8, label %13
8: ; preds = %4
%9 = trunc i32 %3 to i16
%10 = or i16 %6, %9
%11 = tail call zeroext i16 @htons(i16 zeroext %10)
%12 = bitcast i8* %0 to i16*
store i16 %11, i16* %12
br label %30
13: ; preds = %4
%14 = icmp ult i32 %3, 65536
br i1 %14, label %15, label %23
15: ; preds = %13
%16 = or i16 %6, 126
%17 = tail call zeroext i16 @htons(i16 zeroext %16)
%18 = bitcast i8* %0 to i16*
store i16 %17, i16* %18
%19 = trunc i32 %3 to i16
%20 = tail call zeroext i16 @htons(i16 zeroext %19)
%21 = getelementptr inbounds i8, i8* %0, i64 2
%22 = bitcast i8* %21 to i16*
store i16 %20, i16* %22
br label %30
23: ; preds = %13
%24 = or i16 %6, 127
%25 = tail call zeroext i16 @htons(i16 zeroext %24)
%26 = bitcast i8* %0 to i16*
store i16 %25, i16* %26
%27 = tail call i64 @htonl(i32 %3)
%28 = getelementptr inbounds i8, i8* %0, i64 2
%29 = bitcast i8* %28 to i64*
store i64 %27, i64* %29
br label %30
30: ; preds = %15, %23, %8
%31 = phi i32 [ 2, %8 ], [ 4, %15 ], [ 10, %23 ]
ret i32 %31
}