char *construct_piece_message(int piece_index, int blockoffset, int piece_len,
                              char *piece) {
  char *msg = malloc(13 + piece_len);
  int *msgLen = malloc(4);
  *msgLen = 9 + piece_len;
  int *piece_id = malloc(4);
  int *block_off = malloc(4);
  char *msgID = malloc(1);
  *msgID = 7;
  *piece_id = piece_index;
  *block_off = blockoffset;
  memcpy(msg, msgLen, 4);
  memcpy(msg + 4, msgID, 1);
  memcpy(msg + 5, piece_id, 4);
  memcpy(msg + 9, block_off, 4);
  memcpy(msg + 13, piece, piece_len);
  free(msgLen);
  free(msgID);
  free(piece_id);
  free(block_off);
  return msg;
}


x86 assembly (clang, -O0):
<construct_piece_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0xd,%eax
movslq %eax,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%ecx
add    $0x9,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movb   $0x7,(%rax)
mov    -0x4(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    (%rcx),%cl
mov    %cl,0x4(%rax)
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x5(%rax)
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x9(%rax)
mov    -0x20(%rbp),%rdi
add    $0xd,%rdi
mov    -0x18(%rbp),%rsi
movslq -0xc(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<construct_piece_message>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %edx,%ebx
mov    %esi,%r15d
mov    %edi,%r12d
lea    0xd(%rbx),%eax
movslq %eax,%rdi
call   1040 <malloc@plt>
mov    %rax,%rbp
lea    0x9(%rbx),%eax
mov    %eax,0x0(%rbp)
movb   $0x7,0x4(%rbp)
mov    %r12d,0x5(%rbp)
mov    %r15d,0x9(%rbp)
lea    0xd(%rbp),%rdi
movslq %ebx,%rdx
mov    %r14,%rsi
call   1030 <memcpy@plt>
mov    %rbp,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @construct_piece_message(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%14 = load i32, i32* %7
%15 = add nsw i32 13, %14
%16 = sext i32 %15 to i64
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %9
%18 = call i8* @malloc(i64 4)
%19 = bitcast i8* %18 to i32*
store i32* %19, i32** %10
%20 = load i32, i32* %7
%21 = add nsw i32 9, %20
%22 = load i32*, i32** %10
store i32 %21, i32* %22
%23 = call i8* @malloc(i64 4)
%24 = bitcast i8* %23 to i32*
store i32* %24, i32** %11
%25 = call i8* @malloc(i64 4)
%26 = bitcast i8* %25 to i32*
store i32* %26, i32** %12
%27 = call i8* @malloc(i64 1)
store i8* %27, i8** %13
%28 = load i8*, i8** %13
store i8 7, i8* %28
%29 = load i32, i32* %5
%30 = load i32*, i32** %11
store i32 %29, i32* %30
%31 = load i32, i32* %6
%32 = load i32*, i32** %12
store i32 %31, i32* %32
%33 = load i8*, i8** %9
%34 = load i32*, i32** %10
%35 = bitcast i32* %34 to i8*
%36 = load i8*, i8** %9
%37 = getelementptr inbounds i8, i8* %36, i64 4
%38 = load i8*, i8** %13
%39 = load i8*, i8** %9
%40 = getelementptr inbounds i8, i8* %39, i64 5
%41 = load i32*, i32** %11
%42 = bitcast i32* %41 to i8*
%43 = load i8*, i8** %9
%44 = getelementptr inbounds i8, i8* %43, i64 9
%45 = load i32*, i32** %12
%46 = bitcast i32* %45 to i8*
%47 = load i8*, i8** %9
%48 = getelementptr inbounds i8, i8* %47, i64 13
%49 = load i8*, i8** %8
%50 = load i32, i32* %7
%51 = sext i32 %50 to i64
%52 = load i32*, i32** %10
%53 = bitcast i32* %52 to i8*
%54 = load i8*, i8** %13
%55 = load i32*, i32** %11
%56 = bitcast i32* %55 to i8*
%57 = load i32*, i32** %12
%58 = bitcast i32* %57 to i8*
%59 = load i8*, i8** %9
ret i8* %59
}

LLVM IR (-O2):
define noalias i8* @construct_piece_message(i32 %0, i32 %1, i32 %2, i8* nocapture %3) {
%5 = add nsw i32 %2, 13
%6 = sext i32 %5 to i64
%7 = tail call i8* @malloc(i64 %6)
%8 = add nsw i32 %2, 9
%9 = bitcast i8* %7 to i32*
store i32 %8, i32* %9
%10 = getelementptr inbounds i8, i8* %7, i64 4
store i8 7, i8* %10
%11 = getelementptr inbounds i8, i8* %7, i64 5
%12 = bitcast i8* %11 to i32*
store i32 %0, i32* %12
%13 = getelementptr inbounds i8, i8* %7, i64 9
%14 = bitcast i8* %13 to i32*
store i32 %1, i32* %14
%15 = getelementptr inbounds i8, i8* %7, i64 13
%16 = sext i32 %2 to i64
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %15, i8* align 1 %3, i64 %16, i1 false)
ret i8* %7
}