#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;

/* Variables and functions */
int checksum1(int *);
int checksum2(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(int *, void **, int);
uint8_t *serialize(void *msg, int size) {
  uint8_t *BUFFER = (uint8_t *)malloc((size + 5) * sizeof(uint8_t));
  BUFFER[0] = 0xFF;
  BUFFER[1] = 0xFF;
  BUFFER[2] = size + 3;
  memcpy((BUFFER + 3), &msg, size);
  BUFFER[size + 3] = checksum1(BUFFER);
  BUFFER[size + 4] = checksum2(BUFFER[size + 3]);
}


x86 assembly (clang, -O0):
<serialize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x5,%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movl   $0xff,(%rax)
mov    -0x20(%rbp),%rax
movl   $0xff,0x4(%rax)
mov    -0x14(%rbp),%ecx
add    $0x3,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x20(%rbp),%rdi
add    $0xc,%rdi
mov    -0x14(%rbp),%edx
lea    -0x10(%rbp),%rsi
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rdi
call   1060 <checksum1@plt>
mov    %eax,%edx
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x3,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x3,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edi
call   1030 <checksum2@plt>
mov    %eax,%edx
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x4,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serialize>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,(%rsp)
lea    0x14(,%r14,4),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
movabs $0xff000000ff,%rax
mov    %rax,(%rbx)
lea    0x3(%r14),%eax
mov    %eax,0x8(%rbx)
lea    0xc(%rbx),%rdi
mov    %rsp,%rsi
mov    %r14d,%edx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
call   1060 <checksum1@plt>
movslq %r14d,%r14
mov    %eax,0xc(%rbx,%r14,4)
mov    %eax,%edi
call   1030 <checksum2@plt>
mov    %eax,0x10(%rbx,%r14,4)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32* @serialize(i8* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = add nsw i32 %7, 5
%9 = sext i32 %8 to i64
%10 = mul i64 %9, 4
%11 = trunc i64 %10 to i32
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i32*
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = getelementptr inbounds i32, i32* %14, i64 0
store i32 255, i32* %15
%16 = load i32*, i32** %6
%17 = getelementptr inbounds i32, i32* %16, i64 1
store i32 255, i32* %17
%18 = load i32, i32* %5
%19 = add nsw i32 %18, 3
%20 = load i32*, i32** %6
%21 = getelementptr inbounds i32, i32* %20, i64 2
store i32 %19, i32* %21
%22 = load i32*, i32** %6
%23 = getelementptr inbounds i32, i32* %22, i64 3
%24 = load i32, i32* %5
%25 = call i32 @memcpy(i32* %23, i8** %4, i32 %24)
%26 = load i32*, i32** %6
%27 = call i32 @checksum1(i32* %26)
%28 = load i32*, i32** %6
%29 = load i32, i32* %5
%30 = add nsw i32 %29, 3
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %28, i64 %31
store i32 %27, i32* %32
%33 = load i32*, i32** %6
%34 = load i32, i32* %5
%35 = add nsw i32 %34, 3
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %33, i64 %36
%38 = load i32, i32* %37
%39 = call i32 @checksum2(i32 %38)
%40 = load i32*, i32** %6
%41 = load i32, i32* %5
%42 = add nsw i32 %41, 4
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %40, i64 %43
store i32 %39, i32* %44
%45 = load i32*, i32** %3
ret i32* %45
}

LLVM IR (-O2):
define noalias i32* @serialize(i8* %0, i32 %1) {
%3 = alloca i8*
store i8* %0, i8** %3
%4 = shl i32 %1, 2
%5 = add i32 %4, 20
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i32*
store i32 255, i32* %7
%8 = getelementptr inbounds i32, i32* %7, i64 1
store i32 255, i32* %8
%9 = add nsw i32 %1, 3
%10 = getelementptr inbounds i32, i32* %7, i64 2
store i32 %9, i32* %10
%11 = getelementptr inbounds i32, i32* %7, i64 3
%12 = call i32 @memcpy(i32* %11, i8** %3, i32 %1)
%13 = call i32 @checksum1(i32* %7)
%14 = sext i32 %9 to i64
%15 = getelementptr inbounds i32, i32* %7, i64 %14
store i32 %13, i32* %15
%16 = call i32 @checksum2(i32 %13)
%17 = add nsw i32 %1, 4
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %7, i64 %18
store i32 %16, i32* %19
ret i32* undef
}