#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 */
scalar_t__ calloc(int, scalar_t__);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, scalar_t__);
scalar_t__ strlen(char *);
unsigned char *encode_string(unsigned char *array) {
  unsigned char *string = (unsigned char *)calloc(1, strlen((char *)array) + 2);
  memcpy(&string[2], array, strlen((char *)array));
  string[0] = 0x4;
  string[1] = (unsigned char)(strlen((char *)array));
  return string;
}


x86 assembly (clang, -O0):
<encode_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rsi
add    $0x2,%rsi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    %rax,%rdx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rax
movb   $0x4,(%rax)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_string>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
lea    0x2(%rax),%rsi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,%rbx
lea    0x2(%rax),%r15
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1050 <memcpy@plt>
movb   $0x4,(%rbx)
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %al,0x1(%rbx)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @encode_string(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i64 @strlen(i8* %4)
%6 = add nsw i64 %5, 2
%7 = call i64 @calloc(i32 1, i64 %6)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %3
%9 = load i8*, i8** %3
%10 = getelementptr inbounds i8, i8* %9, i64 2
%11 = load i8*, i8** %2
%12 = load i8*, i8** %2
%13 = call i64 @strlen(i8* %12)
%14 = call i32 @memcpy(i8* %10, i8* %11, i64 %13)
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8, i8* %15, i64 0
store i8 4, i8* %16
%17 = load i8*, i8** %2
%18 = call i64 @strlen(i8* %17)
%19 = trunc i64 %18 to i8
%20 = load i8*, i8** %3
%21 = getelementptr inbounds i8, i8* %20, i64 1
store i8 %19, i8* %21
%22 = load i8*, i8** %3
ret i8* %22
}

LLVM IR (-O2):
define i8* @encode_string(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add nsw i64 %2, 2
%4 = tail call i64 @calloc(i32 1, i64 %3)
%5 = inttoptr i64 %4 to i8*
%6 = getelementptr inbounds i8, i8* %5, i64 2
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = tail call i32 @memcpy(i8* %6, i8* %0, i64 %7)
store i8 4, i8* %5
%9 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%10 = trunc i64 %9 to i8
%11 = getelementptr inbounds i8, i8* %5, i64 1
store i8 %10, i8* %11
ret i8* %5
}