#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 */
int /*<<< orphan*/ free(int *);
int *malloc(unsigned int);
int /*<<< orphan*/ memcpy(int *, void *, unsigned int);
int ntohl(int);
int process_control_message(unsigned int, int, void *);
int process_event_message(unsigned int, int, void *);
int process_protocol_message(unsigned int, int, void *);
int process_wmanager_message(unsigned int, int, void *);
int process_message(unsigned int len, int type, void *message) {
  int *ip;
  int i;
  ip = malloc(len);
  memcpy(ip, message, len);
  for (i = 0; i < (len / sizeof(int)); i++)
    ip[i] = ntohl(ip[i]);
  if (type < 0x1000) {
    i = process_protocol_message(len, type, (void *)ip);
    free(ip);
    return i;
  }
  if (type < 0x2000) {
    i = process_wmanager_message(len, type, (void *)ip);
    free(ip);
    return i;
  }
  if (type < 0x3000) {
    i = process_event_message(len, type, (void *)ip);
    free(ip);
    return i;
  }
  if (type < 0x4000) {
    i = process_control_message(len, type, (void *)ip);
    free(ip);
    return i;
  }
  free(ip);
  return -4;
}


x86 assembly (clang, -O0):
<process_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1080 <memcpy@plt>
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
mov    -0x8(%rbp),%ecx
shr    $0x2,%rcx
cmp    %rcx,%rax
jae    11f4 <process_message+0x74>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   10a0 <ntohl@plt>
mov    %eax,%edx
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b5 <process_message+0x35>
cmpl   $0x1000,-0xc(%rbp)
jge    1227 <process_message+0xa7>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1070 <process_protocol_message@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d0 <process_message+0x150>
cmpl   $0x2000,-0xc(%rbp)
jge    125a <process_message+0xda>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1050 <process_wmanager_message@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d0 <process_message+0x150>
cmpl   $0x3000,-0xc(%rbp)
jge    128d <process_message+0x10d>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1040 <process_event_message@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d0 <process_message+0x150>
cmpl   $0x4000,-0xc(%rbp)
jge    12c0 <process_message+0x140>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1060 <process_control_message@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d0 <process_message+0x150>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xfffffffc,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_message>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%r14d
mov    %edi,%r12d
call   1090 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1080 <memcpy@plt>
cmp    $0x4,%r12d
jb     11d8 <process_message+0x58>
mov    %r12d,%ebp
shr    $0x2,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13,%rbx,4),%edi
call   10a0 <ntohl@plt>
mov    %eax,0x0(%r13,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11c0 <process_message+0x40>
cmp    $0xfff,%r14d
jg     11f1 <process_message+0x71>
mov    %r12d,%edi
mov    %r14d,%esi
mov    %r13,%rdx
call   1070 <process_protocol_message@plt>
jmp    1240 <process_message+0xc0>
cmp    $0x1fff,%r14d
ja     120a <process_message+0x8a>
mov    %r12d,%edi
mov    %r14d,%esi
mov    %r13,%rdx
call   1050 <process_wmanager_message@plt>
jmp    1240 <process_message+0xc0>
cmp    $0x2fff,%r14d
ja     1223 <process_message+0xa3>
mov    %r12d,%edi
mov    %r14d,%esi
mov    %r13,%rdx
call   1040 <process_event_message@plt>
jmp    1240 <process_message+0xc0>
mov    $0xfffffffc,%r15d
cmp    $0x3fff,%r14d
ja     1243 <process_message+0xc3>
mov    %r12d,%edi
mov    %r14d,%esi
mov    %r13,%rdx
call   1060 <process_control_message@plt>
mov    %eax,%r15d
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @process_message(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%10 = load i32, i32* %5
%11 = call i32* @malloc(i32 %10)
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = load i8*, i8** %7
%14 = load i32, i32* %5
%15 = call i32 @memcpy(i32* %12, i8* %13, i32 %14)
store i32 0, i32* %9
br label %16
16: ; preds = %34, %3
%17 = load i32, i32* %9
%18 = sext i32 %17 to i64
%19 = load i32, i32* %5
%20 = zext i32 %19 to i64
%21 = udiv i64 %20, 4
%22 = icmp ult i64 %18, %21
br i1 %22, label %23, label %37
23: ; preds = %16
%24 = load i32*, i32** %8
%25 = load i32, i32* %9
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
%28 = load i32, i32* %27
%29 = call i32 @ntohl(i32 %28)
%30 = load i32*, i32** %8
%31 = load i32, i32* %9
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %30, i64 %32
store i32 %29, i32* %33
br label %34
34: ; preds = %23
%35 = load i32, i32* %9
%36 = add nsw i32 %35, 1
store i32 %36, i32* %9
br label %16
37: ; preds = %16
%38 = load i32, i32* %6
%39 = icmp slt i32 %38, 4096
br i1 %39, label %40, label %49
40: ; preds = %37
%41 = load i32, i32* %5
%42 = load i32, i32* %6
%43 = load i32*, i32** %8
%44 = bitcast i32* %43 to i8*
%45 = call i32 @process_protocol_message(i32 %41, i32 %42, i8* %44)
store i32 %45, i32* %9
%46 = load i32*, i32** %8
%47 = call i32 @free(i32* %46)
%48 = load i32, i32* %9
store i32 %48, i32* %4
br label %88
49: ; preds = %37
%50 = load i32, i32* %6
%51 = icmp slt i32 %50, 8192
br i1 %51, label %52, label %61
52: ; preds = %49
%53 = load i32, i32* %5
%54 = load i32, i32* %6
%55 = load i32*, i32** %8
%56 = bitcast i32* %55 to i8*
%57 = call i32 @process_wmanager_message(i32 %53, i32 %54, i8* %56)
store i32 %57, i32* %9
%58 = load i32*, i32** %8
%59 = call i32 @free(i32* %58)
%60 = load i32, i32* %9
store i32 %60, i32* %4
br label %88
61: ; preds = %49
%62 = load i32, i32* %6
%63 = icmp slt i32 %62, 12288
br i1 %63, label %64, label %73
64: ; preds = %61
%65 = load i32, i32* %5
%66 = load i32, i32* %6
%67 = load i32*, i32** %8
%68 = bitcast i32* %67 to i8*
%69 = call i32 @process_event_message(i32 %65, i32 %66, i8* %68)
store i32 %69, i32* %9
%70 = load i32*, i32** %8
%71 = call i32 @free(i32* %70)
%72 = load i32, i32* %9
store i32 %72, i32* %4
br label %88
73: ; preds = %61
%74 = load i32, i32* %6
%75 = icmp slt i32 %74, 16384
br i1 %75, label %76, label %85
76: ; preds = %73
%77 = load i32, i32* %5
%78 = load i32, i32* %6
%79 = load i32*, i32** %8
%80 = bitcast i32* %79 to i8*
%81 = call i32 @process_control_message(i32 %77, i32 %78, i8* %80)
store i32 %81, i32* %9
%82 = load i32*, i32** %8
%83 = call i32 @free(i32* %82)
%84 = load i32, i32* %9
store i32 %84, i32* %4
br label %88
85: ; preds = %73
%86 = load i32*, i32** %8
%87 = call i32 @free(i32* %86)
store i32 -4, i32* %4
br label %88
88: ; preds = %85, %76, %64, %52, %40
%89 = load i32, i32* %4
ret i32 %89
}

LLVM IR (-O2):
define i32 @process_message(i32 %0, i32 %1, i8* %2) {
%4 = tail call i32* @malloc(i32 %0)
%5 = tail call i32 @memcpy(i32* %4, i8* %2, i32 %0)
%6 = icmp ult i32 %0, 4
br i1 %6, label %17, label %7
7: ; preds = %3
%8 = lshr i32 %0, 2
%9 = zext i32 %8 to i64
br label %10
10: ; preds = %7, %10
%11 = phi i64 [ 0, %7 ], [ %15, %10 ]
%12 = getelementptr inbounds i32, i32* %4, i64 %11
%13 = load i32, i32* %12
%14 = tail call i32 @ntohl(i32 %13)
store i32 %14, i32* %12
%15 = add nuw nsw i64 %11, 1
%16 = icmp eq i64 %15, %9
br i1 %16, label %17, label %10
17: ; preds = %10, %3
%18 = icmp slt i32 %1, 4096
br i1 %18, label %19, label %22
19: ; preds = %17
%20 = bitcast i32* %4 to i8*
%21 = tail call i32 @process_protocol_message(i32 %0, i32 %1, i8* %20)
br label %37
22: ; preds = %17
%23 = icmp ult i32 %1, 8192
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = bitcast i32* %4 to i8*
%26 = tail call i32 @process_wmanager_message(i32 %0, i32 %1, i8* %25)
br label %37
27: ; preds = %22
%28 = icmp ult i32 %1, 12288
br i1 %28, label %29, label %32
29: ; preds = %27
%30 = bitcast i32* %4 to i8*
%31 = tail call i32 @process_event_message(i32 %0, i32 %1, i8* %30)
br label %37
32: ; preds = %27
%33 = icmp ult i32 %1, 16384
br i1 %33, label %34, label %37
34: ; preds = %32
%35 = bitcast i32* %4 to i8*
%36 = tail call i32 @process_control_message(i32 %0, i32 %1, i8* %35)
br label %37
37: ; preds = %32, %34, %29, %24, %19
%38 = phi i32 [ %21, %19 ], [ %26, %24 ], [ %31, %29 ], [ %36, %34 ], [ -4, %32 ]
%39 = tail call i32 @free(i32* %4)
ret i32 %38
}