#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 */
struct mg_connection {
  int dummy;
};

/* Variables and functions */
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ mg_write(struct mg_connection *, ...);
int /*<<< orphan*/ printf(char *, size_t);
int websocket_data(struct mg_connection *conn, int flags, char *data,
                   size_t data_len) {
  unsigned char reply[4];
  int offset = 2;
  int ret = 1;
  if (data_len > 65536) {
    data_len = 65536;
  }
  printf("data_len=%d\n", data_len);
  int opcode = flags & 0x0f;
  if ((opcode == 0x08) || memcmp(data, ".close", 6) == 0) {
    reply[0] = 0x88;
    reply[1] = data_len;
    if (data_len == 0) {
      reply[2] = 3;
      reply[3] = 232;
    } else {
      reply[2] = data[0];
      reply[3] = data[1];
    }
    ret = 0;
  } else {
    reply[0] = 0x80 | opcode;
    if (data_len <= 125) {
      reply[1] = data_len;
    } else {
      reply[1] = 126;
      reply[2] = data_len / 256;
      reply[3] = data_len % 256;
      offset = 4;
    }
  }
  mg_write(conn, reply, offset);
  if (data_len > 0)
    mg_write(conn, data, data_len);
  return ret;
}


x86 assembly (clang, -O0):
<websocket_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x2,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
cmpq   $0x10000,-0x20(%rbp)
jbe    116b <websocket_data+0x3b>
movq   $0x10000,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe8a(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
and    $0xf,%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x8,-0x30(%rbp)
je     11ad <websocket_data+0x7d>
mov    -0x18(%rbp),%rdi
lea    0xe74(%rip),%rsi
mov    $0x6,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    11ef <websocket_data+0xbf>
movb   $0x88,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    %al,-0x23(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d0 <websocket_data+0xa0>
movb   $0x3,-0x22(%rbp)
movb   $0xe8,-0x21(%rbp)
jmp    11e3 <websocket_data+0xb3>
mov    -0x18(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x22(%rbp)
mov    -0x18(%rbp),%rax
mov    0x1(%rax),%al
mov    %al,-0x21(%rbp)
movl   $0x0,-0x2c(%rbp)
jmp    1239 <websocket_data+0x109>
mov    -0x30(%rbp),%eax
or     $0x80,%eax
mov    %al,-0x24(%rbp)
cmpq   $0x7d,-0x20(%rbp)
ja     1211 <websocket_data+0xe1>
mov    -0x20(%rbp),%rax
mov    %al,-0x23(%rbp)
jmp    1234 <websocket_data+0x104>
movb   $0x7e,-0x23(%rbp)
mov    -0x20(%rbp),%rax
shr    $0x8,%rax
mov    %al,-0x22(%rbp)
mov    -0x20(%rbp),%rax
and    $0xff,%rax
mov    %al,-0x21(%rbp)
movl   $0x4,-0x28(%rbp)
jmp    1239 <websocket_data+0x109>
mov    -0x8(%rbp),%rdi
lea    -0x24(%rbp),%rsi
mov    -0x28(%rbp),%edx
mov    $0x0,%al
call   1040 <mg_write@plt>
cmpq   $0x0,-0x20(%rbp)
jbe    1269 <websocket_data+0x139>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1040 <mg_write@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<websocket_data>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%r14
cmp    $0x10000,%rcx
mov    $0x10000,%ebx
cmovb  %rcx,%rbx
lea    0xeaa(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
and    $0xf,%ebp
cmp    $0x8,%ebp
je     11b2 <websocket_data+0x82>
lea    0xea0(%rip),%rsi
mov    %r15,%rdi
mov    $0x6,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     11b2 <websocket_data+0x82>
or     $0x80,%bpl
mov    %bpl,0x4(%rsp)
cmp    $0x7e,%rbx
jae    11f3 <websocket_data+0xc3>
mov    %bl,0x5(%rsp)
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
mov    $0x2,%edx
xor    %eax,%eax
call   1040 <mg_write@plt>
mov    $0x1,%ebp
test   %rbx,%rbx
jne    121b <websocket_data+0xeb>
jmp    122b <websocket_data+0xfb>
movb   $0x88,0x4(%rsp)
mov    %bl,0x5(%rsp)
test   %rbx,%rbx
je     11d4 <websocket_data+0xa4>
mov    (%r15),%al
mov    %al,0x6(%rsp)
mov    0x1(%r15),%al
xor    %ebp,%ebp
mov    $0x2,%edx
jmp    1208 <websocket_data+0xd8>
movw   $0xe803,0x6(%rsp)
xor    %ebp,%ebp
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
mov    $0x2,%edx
xor    %eax,%eax
call   1040 <mg_write@plt>
jmp    122b <websocket_data+0xfb>
movb   $0x7e,0x5(%rsp)
mov    %bh,0x6(%rsp)
mov    $0x1,%ebp
mov    $0x4,%edx
mov    %ebx,%eax
mov    %al,0x7(%rsp)
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <mg_write@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <mg_write@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.mg_connection = type { i32 }
@.str = [13 x i8] c"data_len=%d\0A\00"
@.str.1 = [7 x i8] c".close\00"
define i32 @websocket_data(%struct.mg_connection* %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca %struct.mg_connection*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca [4 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store %struct.mg_connection* %0, %struct.mg_connection** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i64 %3, i64* %8
store i32 2, i32* %10
store i32 1, i32* %11
%13 = load i64, i64* %8
%14 = icmp ugt i64 %13, 65536
br i1 %14, label %15, label %16
15: ; preds = %4
store i64 65536, i64* %8
br label %16
16: ; preds = %15, %4
%17 = load i64, i64* %8
%18 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 %17)
%19 = load i32, i32* %6
%20 = and i32 %19, 15
store i32 %20, i32* %12
%21 = load i32, i32* %12
%22 = icmp eq i32 %21, 8
br i1 %22, label %27, label %23
23: ; preds = %16
%24 = load i8*, i8** %7
%25 = call i64 @memcmp(i8* %24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %47
27: ; preds = %23, %16
%28 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
store i8 -120, i8* %28
%29 = load i64, i64* %8
%30 = trunc i64 %29 to i8
%31 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 1
store i8 %30, i8* %31
%32 = load i64, i64* %8
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 2
store i8 3, i8* %35
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 3
store i8 -24, i8* %36
br label %46
37: ; preds = %27
%38 = load i8*, i8** %7
%39 = getelementptr inbounds i8, i8* %38, i64 0
%40 = load i8, i8* %39
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 2
store i8 %40, i8* %41
%42 = load i8*, i8** %7
%43 = getelementptr inbounds i8, i8* %42, i64 1
%44 = load i8, i8* %43
%45 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 3
store i8 %44, i8* %45
br label %46
46: ; preds = %37, %34
store i32 0, i32* %11
br label %69
47: ; preds = %23
%48 = load i32, i32* %12
%49 = or i32 128, %48
%50 = trunc i32 %49 to i8
%51 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
store i8 %50, i8* %51
%52 = load i64, i64* %8
%53 = icmp ule i64 %52, 125
br i1 %53, label %54, label %58
54: ; preds = %47
%55 = load i64, i64* %8
%56 = trunc i64 %55 to i8
%57 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 1
store i8 %56, i8* %57
br label %68
58: ; preds = %47
%59 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 1
store i8 126, i8* %59
%60 = load i64, i64* %8
%61 = udiv i64 %60, 256
%62 = trunc i64 %61 to i8
%63 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 2
store i8 %62, i8* %63
%64 = load i64, i64* %8
%65 = urem i64 %64, 256
%66 = trunc i64 %65 to i8
%67 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 3
store i8 %66, i8* %67
store i32 4, i32* %10
br label %68
68: ; preds = %58, %54
br label %69
69: ; preds = %68, %46
%70 = load %struct.mg_connection*, %struct.mg_connection** %5
%71 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%72 = load i32, i32* %10
%73 = call i32 (%struct.mg_connection*, ...) @mg_write(%struct.mg_connection* %70, i8* %71, i32 %72)
%74 = load i64, i64* %8
%75 = icmp ugt i64 %74, 0
br i1 %75, label %76, label %81
76: ; preds = %69
%77 = load %struct.mg_connection*, %struct.mg_connection** %5
%78 = load i8*, i8** %7
%79 = load i64, i64* %8
%80 = call i32 (%struct.mg_connection*, ...) @mg_write(%struct.mg_connection* %77, i8* %78, i64 %79)
br label %81
81: ; preds = %76, %69
%82 = load i32, i32* %11
ret i32 %82
}

LLVM IR (-O2):
%struct.mg_connection = type { i32 }
@.str = [13 x i8] c"data_len=%d\0A\00"
@.str.1 = [7 x i8] c".close\00"
define i32 @websocket_data(%struct.mg_connection* %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca [4 x i8]
%6 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%7 = icmp ult i64 %3, 65536
%8 = select i1 %7, i64 %3, i64 65536
%9 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 %8)
%10 = and i32 %1, 15
%11 = icmp eq i32 %10, 8
br i1 %11, label %15, label %12
12: ; preds = %4
%13 = tail call i64 @memcmp(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %28
15: ; preds = %12, %4
store i8 -120, i8* %6
%16 = trunc i64 %8 to i8
%17 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 1
store i8 %16, i8* %17
%18 = icmp eq i64 %8, 0
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 2
store i8 3, i8* %20
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 3
store i8 -24, i8* %21
%22 = call i32 (%struct.mg_connection*, ...) @mg_write(%struct.mg_connection* %0, i8* %6, i32 2)
br label %52
23: ; preds = %15
%24 = load i8, i8* %2
%25 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 2
store i8 %24, i8* %25
%26 = getelementptr inbounds i8, i8* %2, i64 1
%27 = load i8, i8* %26
br label %38
28: ; preds = %12
%29 = trunc i32 %10 to i8
%30 = or i8 %29, -128
store i8 %30, i8* %6
%31 = icmp ult i64 %8, 126
br i1 %31, label %44, label %32
32: ; preds = %28
%33 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 1
store i8 126, i8* %33
%34 = lshr i64 %8, 8
%35 = trunc i64 %34 to i8
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 2
store i8 %35, i8* %36
%37 = trunc i64 %8 to i8
br label %38
38: ; preds = %23, %32
%39 = phi i8 [ %27, %23 ], [ %37, %32 ]
%40 = phi i32 [ 2, %23 ], [ 4, %32 ]
%41 = phi i32 [ 0, %23 ], [ 1, %32 ]
%42 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 3
store i8 %39, i8* %42
%43 = call i32 (%struct.mg_connection*, ...) @mg_write(%struct.mg_connection* %0, i8* %6, i32 %40)
br label %49
44: ; preds = %28
%45 = trunc i64 %8 to i8
%46 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 1
store i8 %45, i8* %46
%47 = call i32 (%struct.mg_connection*, ...) @mg_write(%struct.mg_connection* %0, i8* %6, i32 2)
%48 = icmp eq i64 %8, 0
br i1 %48, label %52, label %49
49: ; preds = %38, %44
%50 = phi i32 [ %41, %38 ], [ 1, %44 ]
%51 = call i32 (%struct.mg_connection*, ...) @mg_write(%struct.mg_connection* %0, i8* %2, i64 %8)
br label %52
52: ; preds = %19, %49, %44
%53 = phi i32 [ %50, %49 ], [ 1, %44 ], [ 0, %19 ]
ret i32 %53
}