#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];
  size_t i = 0;
  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    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x2,-0x34(%rbp)
movl   $0x1,-0x38(%rbp)
cmpq   $0x10000,-0x20(%rbp)
jbe    1173 <websocket_data+0x43>
movq   $0x10000,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe82(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
and    $0xf,%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x8,-0x3c(%rbp)
je     11b5 <websocket_data+0x85>
mov    -0x18(%rbp),%rdi
lea    0xe6c(%rip),%rsi
mov    $0x6,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    11f7 <websocket_data+0xc7>
movb   $0x88,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    %al,-0x23(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d8 <websocket_data+0xa8>
movb   $0x3,-0x22(%rbp)
movb   $0xe8,-0x21(%rbp)
jmp    11eb <websocket_data+0xbb>
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,-0x38(%rbp)
jmp    1241 <websocket_data+0x111>
mov    -0x3c(%rbp),%eax
or     $0x80,%eax
mov    %al,-0x24(%rbp)
cmpq   $0x7d,-0x20(%rbp)
ja     1219 <websocket_data+0xe9>
mov    -0x20(%rbp),%rax
mov    %al,-0x23(%rbp)
jmp    123c <websocket_data+0x10c>
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,-0x34(%rbp)
jmp    1241 <websocket_data+0x111>
mov    -0x8(%rbp),%rdi
lea    -0x24(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    $0x0,%al
call   1040 <mg_write@plt>
cmpq   $0x0,-0x20(%rbp)
jbe    1271 <websocket_data+0x141>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1040 <mg_write@plt>
mov    -0x38(%rbp),%eax
add    $0x40,%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 i64
%11 = alloca i32
%12 = alloca i32
%13 = 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 i64 0, i64* %10
store i32 2, i32* %11
store i32 1, i32* %12
%14 = load i64, i64* %8
%15 = icmp ugt i64 %14, 65536
br i1 %15, label %16, label %17
16: ; preds = %4
store i64 65536, i64* %8
br label %17
17: ; preds = %16, %4
%18 = load i64, i64* %8
%19 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 %18)
%20 = load i32, i32* %6
%21 = and i32 %20, 15
store i32 %21, i32* %13
%22 = load i32, i32* %13
%23 = icmp eq i32 %22, 8
br i1 %23, label %28, label %24
24: ; preds = %17
%25 = load i8*, i8** %7
%26 = call i64 @memcmp(i8* %25, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %48
28: ; preds = %24, %17
%29 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
store i8 -120, i8* %29
%30 = load i64, i64* %8
%31 = trunc i64 %30 to i8
%32 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 1
store i8 %31, i8* %32
%33 = load i64, i64* %8
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 2
store i8 3, i8* %36
%37 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 3
store i8 -24, i8* %37
br label %47
38: ; preds = %28
%39 = load i8*, i8** %7
%40 = getelementptr inbounds i8, i8* %39, i64 0
%41 = load i8, i8* %40
%42 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 2
store i8 %41, i8* %42
%43 = load i8*, i8** %7
%44 = getelementptr inbounds i8, i8* %43, i64 1
%45 = load i8, i8* %44
%46 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 3
store i8 %45, i8* %46
br label %47
47: ; preds = %38, %35
store i32 0, i32* %12
br label %70
48: ; preds = %24
%49 = load i32, i32* %13
%50 = or i32 128, %49
%51 = trunc i32 %50 to i8
%52 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
store i8 %51, i8* %52
%53 = load i64, i64* %8
%54 = icmp ule i64 %53, 125
br i1 %54, label %55, label %59
55: ; preds = %48
%56 = load i64, i64* %8
%57 = trunc i64 %56 to i8
%58 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 1
store i8 %57, i8* %58
br label %69
59: ; preds = %48
%60 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 1
store i8 126, i8* %60
%61 = load i64, i64* %8
%62 = udiv i64 %61, 256
%63 = trunc i64 %62 to i8
%64 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 2
store i8 %63, i8* %64
%65 = load i64, i64* %8
%66 = urem i64 %65, 256
%67 = trunc i64 %66 to i8
%68 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 3
store i8 %67, i8* %68
store i32 4, i32* %11
br label %69
69: ; preds = %59, %55
br label %70
70: ; preds = %69, %47
%71 = load %struct.mg_connection*, %struct.mg_connection** %5
%72 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%73 = load i32, i32* %11
%74 = call i32 (%struct.mg_connection*, ...) @mg_write(%struct.mg_connection* %71, i8* %72, i32 %73)
%75 = load i64, i64* %8
%76 = icmp ugt i64 %75, 0
br i1 %76, label %77, label %82
77: ; preds = %70
%78 = load %struct.mg_connection*, %struct.mg_connection** %5
%79 = load i8*, i8** %7
%80 = load i64, i64* %8
%81 = call i32 (%struct.mg_connection*, ...) @mg_write(%struct.mg_connection* %78, i8* %79, i64 %80)
br label %82
82: ; preds = %77, %70
%83 = load i32, i32* %12
ret i32 %83
}

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
}