char *construct_cancel_message(int piece_index, int blockoffset,
                               int blocklength) {
  char *msg = malloc(17);
  int *msgLen = malloc(4);
  *msgLen = 13;
  int *piece_id = malloc(4);
  int *block_off = malloc(4);
  int *block_len = malloc(4);
  char *msgID = malloc(1);
  *msgID = 8;
  *piece_id = piece_index;
  *block_off = blockoffset;
  *block_len = blocklength;
  memcpy(msg, msgLen, 4);
  memcpy(msg + 4, msgID, 1);
  memcpy(msg + 5, piece_id, 4);
  memcpy(msg + 9, block_off, 4);
  memcpy(msg + 13, block_len, 4);
  free(msgLen);
  free(msgID);
  free(piece_id);
  free(block_off);
  free(block_len);
  return msg;
}


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

x86 assembly (clang, -O2):
<construct_cancel_message>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %edi,%ebx
mov    $0x11,%edi
call   1030 <malloc@plt>
movl   $0xd,(%rax)
movb   $0x8,0x4(%rax)
mov    %ebx,0x5(%rax)
mov    %ebp,0x9(%rax)
mov    %r14d,0xd(%rax)
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define noalias i8* @construct_cancel_message(i32 %0, i32 %1, i32 %2) {
%4 = tail call dereferenceable_or_null(17) i8* @malloc(i64 17)
%5 = bitcast i8* %4 to i32*
store i32 13, i32* %5
%6 = getelementptr inbounds i8, i8* %4, i64 4
store i8 8, i8* %6
%7 = getelementptr inbounds i8, i8* %4, i64 5
%8 = bitcast i8* %7 to i32*
store i32 %0, i32* %8
%9 = getelementptr inbounds i8, i8* %4, i64 9
%10 = bitcast i8* %9 to i32*
store i32 %1, i32* %10
%11 = getelementptr inbounds i8, i8* %4, i64 13
%12 = bitcast i8* %11 to i32*
store i32 %2, i32* %12
ret i8* %4
}