#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 *);
void *malloc(int);
int /*<<< orphan*/ memcpy(char *, ...);
char *construct_bitfield_message(char *bitfield, int bitfieldLen) {
  char *msg = malloc(4 + 1 + bitfieldLen);
  int *msgLen = malloc(sizeof(int));
  *msgLen = bitfieldLen + 1;
  memcpy(msg, msgLen, 4);
  msg[4] = (char)5;
  memcpy(msg + 5, bitfield, bitfieldLen);
  free(msgLen);
  return msg;
}


x86 assembly (clang, -O0):
<construct_bitfield_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x5,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
movb   $0x5,0x4(%rax)
mov    -0x18(%rbp),%rdi
add    $0x5,%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<construct_bitfield_message>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x5(%r14),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
lea    0x1(%r14),%eax
mov    %eax,(%r12)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
movb   $0x5,0x4(%rbx)
lea    0x5(%rbx),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @construct_bitfield_message(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = add nsw i32 5, %7
%9 = call i8* @malloc(i32 %8)
store i8* %9, i8** %5
%10 = call i8* @malloc(i32 4)
%11 = bitcast i8* %10 to i32*
store i32* %11, i32** %6
%12 = load i32, i32* %4
%13 = add nsw i32 %12, 1
%14 = load i32*, i32** %6
store i32 %13, i32* %14
%15 = load i8*, i8** %5
%16 = load i32*, i32** %6
%17 = call i32 (i8*, ...) @memcpy(i8* %15, i32* %16, i32 4)
%18 = load i8*, i8** %5
%19 = getelementptr inbounds i8, i8* %18, i64 4
store i8 5, i8* %19
%20 = load i8*, i8** %5
%21 = getelementptr inbounds i8, i8* %20, i64 5
%22 = load i8*, i8** %3
%23 = load i32, i32* %4
%24 = call i32 (i8*, ...) @memcpy(i8* %21, i8* %22, i32 %23)
%25 = load i32*, i32** %6
%26 = call i32 @free(i32* %25)
%27 = load i8*, i8** %5
ret i8* %27
}

LLVM IR (-O2):
define i8* @construct_bitfield_message(i8* %0, i32 %1) {
%3 = add nsw i32 %1, 5
%4 = tail call i8* @malloc(i32 %3)
%5 = tail call dereferenceable_or_null(4) i8* @malloc(i32 4)
%6 = bitcast i8* %5 to i32*
%7 = add nsw i32 %1, 1
store i32 %7, i32* %6
%8 = tail call i32 (i8*, ...) @memcpy(i8* %4, i8* %5, i32 4)
%9 = getelementptr inbounds i8, i8* %4, i64 4
store i8 5, i8* %9
%10 = getelementptr inbounds i8, i8* %4, i64 5
%11 = tail call i32 (i8*, ...) @memcpy(i8* %10, i8* %0, i32 %1)
%12 = tail call i32 @free(i32* %6)
ret i8* %4
}