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

/* Variables and functions */
int /*<<< orphan*/ htons(int);
int *malloc(int);
int ntohl(int /*<<< orphan*/);
int /*<<< orphan*/ rand();
uint8_t *hk_ws_generate_frame_head(uint64_t data_length, int *head_length,
                                   int masked, uint8_t opcode) {
  uint8_t *write_position, *frame_data;
  uint64_t payload_lenth;
  int is_masked;
  union {
    uint32_t raw;
    uint8_t octets[4];
  } mask;
  payload_lenth = data_length;
  if (masked) {
    is_masked = 1;
    mask.raw = rand();
  } else {
    is_masked = 0;
  }
  *head_length = 2;
  if (payload_lenth < 126) {
  } else if (payload_lenth < 65536) {
    *head_length += 2;
  } else {
    *head_length += 8;
  }
  if (is_masked) {
    *head_length += 4;
  }
  frame_data = malloc(*head_length * sizeof(uint8_t));
  if (frame_data == NULL) {
    return NULL;
  }
  write_position = frame_data;
  *write_position = (opcode & 15) | 0b10000000;
  write_position += 1;
  if (payload_lenth < 126) {
    *write_position = (payload_lenth & 127) | (is_masked << 7);
  } else if (payload_lenth < 65536) {
    *write_position = 126 | (is_masked << 7);
    write_position += 1;
    *(uint16_t *)write_position = htons(payload_lenth);
    write_position += 2;
  } else {
    *write_position = 127 | (is_masked << 7);
    write_position += 1;
    *((uint64_t *)write_position) = ntohl((uint32_t)payload_lenth);
    *((uint64_t *)write_position) <<= 32;
    *((uint64_t *)write_position) +=
        ntohl(*(((uint32_t *)(&payload_lenth)) + 1));
    write_position += 8;
  }
  if (is_masked) {
    *((uint32_t *)write_position) = mask.raw;
    write_position += 4;
  }
  return frame_data;
}


x86 assembly (clang, -O0):
<hk_ws_generate_frame_head>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     117b <hk_ws_generate_frame_head+0x3b>
movl   $0x1,-0x38(%rbp)
mov    $0x0,%al
call   1060 <rand@plt>
mov    %eax,-0x48(%rbp)
jmp    1182 <hk_ws_generate_frame_head+0x42>
movl   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x2,(%rax)
cmpl   $0x7e,-0x34(%rbp)
jge    119b <hk_ws_generate_frame_head+0x5b>
jmp    11c8 <hk_ws_generate_frame_head+0x88>
cmpl   $0x10000,-0x34(%rbp)
jge    11b8 <hk_ws_generate_frame_head+0x78>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x2,%ecx
mov    %ecx,(%rax)
jmp    11c3 <hk_ws_generate_frame_head+0x83>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x8,%ecx
mov    %ecx,(%rax)
jmp    11c8 <hk_ws_generate_frame_head+0x88>
cmpl   $0x0,-0x38(%rbp)
je     11dd <hk_ws_generate_frame_head+0x9d>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x4,%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    120b <hk_ws_generate_frame_head+0xcb>
movq   $0x0,-0x8(%rbp)
jmp    1323 <hk_ws_generate_frame_head+0x1e3>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%ecx
and    $0xf,%ecx
or     $0x80,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x7e,-0x34(%rbp)
jge    1254 <hk_ws_generate_frame_head+0x114>
mov    -0x34(%rbp),%ecx
and    $0x7f,%ecx
mov    -0x38(%rbp),%eax
shl    $0x7,%eax
or     %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    12fc <hk_ws_generate_frame_head+0x1bc>
cmpl   $0x10000,-0x34(%rbp)
jge    129d <hk_ws_generate_frame_head+0x15d>
mov    -0x38(%rbp),%ecx
shl    $0x7,%ecx
or     $0x7e,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x34(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
jmp    12f7 <hk_ws_generate_frame_head+0x1b7>
mov    -0x38(%rbp),%ecx
shl    $0x7,%ecx
or     $0x7f,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x34(%rbp),%edi
call   1050 <ntohl@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
mov    $0x20,%ecx
shl    %cl,%edx
mov    %edx,%ecx
mov    %ecx,(%rax)
mov    -0x30(%rbp),%edi
call   1050 <ntohl@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x28(%rbp)
jmp    12fc <hk_ws_generate_frame_head+0x1bc>
cmpl   $0x0,-0x38(%rbp)
je     131b <hk_ws_generate_frame_head+0x1db>
mov    -0x48(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hk_ws_generate_frame_head>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r12d
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%ebp
test   %edx,%edx
je     116c <hk_ws_generate_frame_head+0x2c>
xor    %eax,%eax
call   1060 <rand@plt>
mov    %eax,%r14d
mov    $0x80,%r13d
jmp    116f <hk_ws_generate_frame_head+0x2f>
xor    %r13d,%r13d
cmp    $0x10000,%ebp
mov    $0x4,%eax
mov    $0xa,%ecx
cmovb  %eax,%ecx
cmp    $0x7e,%ebp
mov    $0x2,%eax
cmovge %ecx,%eax
lea    0x4(%rax),%edi
test   %r15d,%r15d
cmove  %eax,%edi
mov    %edi,(%rbx)
shl    $0x2,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     121d <hk_ws_generate_frame_head+0xdd>
and    $0xf,%r12d
or     $0x80,%r12d
mov    %r12d,(%rbx)
cmp    $0x7d,%ebp
jg     11d2 <hk_ws_generate_frame_head+0x92>
mov    %rbx,%rax
add    $0x4,%rax
and    $0x7f,%ebp
or     %ebp,%r13d
mov    %r13d,(%rax)
test   %r15d,%r15d
jne    121a <hk_ws_generate_frame_head+0xda>
jmp    121d <hk_ws_generate_frame_head+0xdd>
cmp    $0xffff,%ebp
ja     11fa <hk_ws_generate_frame_head+0xba>
or     $0x7e,%r13d
mov    %r13d,0x4(%rbx)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rbx)
mov    %rbx,%rax
add    $0x10,%rax
test   %r15d,%r15d
jne    121a <hk_ws_generate_frame_head+0xda>
jmp    121d <hk_ws_generate_frame_head+0xdd>
or     $0x7f,%r13d
mov    %r13d,0x4(%rbx)
mov    %ebp,%edi
call   1050 <ntohl@plt>
call   1050 <ntohl@plt>
mov    %rbx,%rax
add    $0x28,%rax
test   %r15d,%r15d
je     121d <hk_ws_generate_frame_head+0xdd>
mov    %r14d,(%rax)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%union.anon = type { [4 x i32] }
define i32* @hk_ws_generate_frame_head(i32 %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca %union.anon
store i32 %0, i32* %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%15 = load i32, i32* %6
store i32 %15, i32* %12
%16 = load i32, i32* %8
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %4
store i32 1, i32* %13
%19 = call i32 (...) @rand()
%20 = bitcast %union.anon* %14 to i32*
store i32 %19, i32* %20
br label %22
21: ; preds = %4
store i32 0, i32* %13
br label %22
22: ; preds = %21, %18
%23 = load i32*, i32** %7
store i32 2, i32* %23
%24 = load i32, i32* %12
%25 = icmp slt i32 %24, 126
br i1 %25, label %26, label %27
26: ; preds = %22
br label %39
27: ; preds = %22
%28 = load i32, i32* %12
%29 = icmp slt i32 %28, 65536
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32*, i32** %7
%32 = load i32, i32* %31
%33 = add nsw i32 %32, 2
store i32 %33, i32* %31
br label %38
34: ; preds = %27
%35 = load i32*, i32** %7
%36 = load i32, i32* %35
%37 = add nsw i32 %36, 8
store i32 %37, i32* %35
br label %38
38: ; preds = %34, %30
br label %39
39: ; preds = %38, %26
%40 = load i32, i32* %13
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = load i32*, i32** %7
%44 = load i32, i32* %43
%45 = add nsw i32 %44, 4
store i32 %45, i32* %43
br label %46
46: ; preds = %42, %39
%47 = load i32*, i32** %7
%48 = load i32, i32* %47
%49 = sext i32 %48 to i64
%50 = mul i64 %49, 4
%51 = trunc i64 %50 to i32
%52 = call i32* @malloc(i32 %51)
store i32* %52, i32** %11
%53 = load i32*, i32** %11
%54 = icmp eq i32* %53, null
br i1 %54, label %55, label %56
55: ; preds = %46
store i32* null, i32** %5
br label %121
56: ; preds = %46
%57 = load i32*, i32** %11
store i32* %57, i32** %10
%58 = load i32, i32* %9
%59 = and i32 %58, 15
%60 = or i32 %59, 128
%61 = load i32*, i32** %10
store i32 %60, i32* %61
%62 = load i32*, i32** %10
%63 = getelementptr inbounds i32, i32* %62, i64 1
store i32* %63, i32** %10
%64 = load i32, i32* %12
%65 = icmp slt i32 %64, 126
br i1 %65, label %66, label %73
66: ; preds = %56
%67 = load i32, i32* %12
%68 = and i32 %67, 127
%69 = load i32, i32* %13
%70 = shl i32 %69, 7
%71 = or i32 %68, %70
%72 = load i32*, i32** %10
store i32 %71, i32* %72
br label %110
73: ; preds = %56
%74 = load i32, i32* %12
%75 = icmp slt i32 %74, 65536
br i1 %75, label %76, label %88
76: ; preds = %73
%77 = load i32, i32* %13
%78 = shl i32 %77, 7
%79 = or i32 126, %78
%80 = load i32*, i32** %10
store i32 %79, i32* %80
%81 = load i32*, i32** %10
%82 = getelementptr inbounds i32, i32* %81, i64 1
store i32* %82, i32** %10
%83 = load i32, i32* %12
%84 = call i32 @htons(i32 %83)
%85 = load i32*, i32** %10
store i32 %84, i32* %85
%86 = load i32*, i32** %10
%87 = getelementptr inbounds i32, i32* %86, i64 2
store i32* %87, i32** %10
br label %109
88: ; preds = %73
%89 = load i32, i32* %13
%90 = shl i32 %89, 7
%91 = or i32 127, %90
%92 = load i32*, i32** %10
store i32 %91, i32* %92
%93 = load i32*, i32** %10
%94 = getelementptr inbounds i32, i32* %93, i64 1
store i32* %94, i32** %10
%95 = load i32, i32* %12
%96 = call i32 @ntohl(i32 %95)
%97 = load i32*, i32** %10
store i32 %96, i32* %97
%98 = load i32*, i32** %10
%99 = load i32, i32* %98
%100 = shl i32 %99, 32
store i32 %100, i32* %98
%101 = getelementptr inbounds i32, i32* %12, i64 1
%102 = load i32, i32* %101
%103 = call i32 @ntohl(i32 %102)
%104 = load i32*, i32** %10
%105 = load i32, i32* %104
%106 = add nsw i32 %105, %103
store i32 %106, i32* %104
%107 = load i32*, i32** %10
%108 = getelementptr inbounds i32, i32* %107, i64 8
store i32* %108, i32** %10
br label %109
109: ; preds = %88, %76
br label %110
110: ; preds = %109, %66
%111 = load i32, i32* %13
%112 = icmp ne i32 %111, 0
br i1 %112, label %113, label %119
113: ; preds = %110
%114 = bitcast %union.anon* %14 to i32*
%115 = load i32, i32* %114
%116 = load i32*, i32** %10
store i32 %115, i32* %116
%117 = load i32*, i32** %10
%118 = getelementptr inbounds i32, i32* %117, i64 4
store i32* %118, i32** %10
br label %119
119: ; preds = %113, %110
%120 = load i32*, i32** %11
store i32* %120, i32** %5
br label %121
121: ; preds = %119, %55
%122 = load i32*, i32** %5
ret i32* %122
}

LLVM IR (-O2):
define noalias i32* @hk_ws_generate_frame_head(i32 %0, i32* nocapture %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %2, 0
br i1 %5, label %8, label %6
6: ; preds = %4
%7 = tail call i32 (...) @rand()
br label %8
8: ; preds = %4, %6
%9 = phi i32 [ 128, %6 ], [ 0, %4 ]
%10 = phi i32 [ %7, %6 ], [ undef, %4 ]
%11 = icmp slt i32 %0, 126
%12 = icmp ult i32 %0, 65536
%13 = select i1 %12, i32 4, i32 10
%14 = select i1 %11, i32 2, i32 %13
%15 = add nuw nsw i32 %14, 4
%16 = select i1 %5, i32 %14, i32 %15
store i32 %16, i32* %1
%17 = shl nuw nsw i32 %16, 2
%18 = tail call i32* @malloc(i32 %17)
%19 = icmp eq i32* %18, null
br i1 %19, label %42, label %20
20: ; preds = %8
%21 = and i32 %3, 15
%22 = or i32 %21, 128
store i32 %22, i32* %18
%23 = getelementptr inbounds i32, i32* %18, i64 1
br i1 %11, label %24, label %27
24: ; preds = %20
%25 = and i32 %0, 127
%26 = or i32 %9, %25
store i32 %26, i32* %23
br label %39
27: ; preds = %20
%28 = icmp ult i32 %0, 65536
br i1 %28, label %29, label %34
29: ; preds = %27
%30 = or i32 %9, 126
store i32 %30, i32* %23
%31 = getelementptr inbounds i32, i32* %18, i64 2
%32 = tail call i32 @htons(i32 %0)
store i32 %32, i32* %31
%33 = getelementptr inbounds i32, i32* %18, i64 4
br label %39
34: ; preds = %27
%35 = or i32 %9, 127
store i32 %35, i32* %23
%36 = tail call i32 @ntohl(i32 %0)
%37 = tail call i32 @ntohl(i32 poison)
%38 = getelementptr inbounds i32, i32* %18, i64 10
br label %39
39: ; preds = %29, %34, %24
%40 = phi i32* [ %23, %24 ], [ %33, %29 ], [ %38, %34 ]
br i1 %5, label %42, label %41
41: ; preds = %39
store i32 %10, i32* %40
br label %42
42: ; preds = %39, %41, %8
ret i32* %18
}