#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(scalar_t__);
scalar_t__ snprintf(int /*<<< orphan*/ *, int /*<<< orphan*/, char *, int,
                    char *);
int /*<<< orphan*/ sprintf(char *, char *, int, char *);
int /*<<< orphan*/ stderr;
char *strdup(char *);
scalar_t__ strlen(char *);
char *serialize_string(char *str) {
  if (str == NULL) {
    return strdup("0");
  }
  char *ret = malloc(snprintf(NULL, 0, "%d %s", (int)strlen(str), str) + 1);
  if (ret == NULL) {
    fprintf(stderr, "[ERROR] "
                    "Failed to allocate memory."
                    "\n");
    exit(-1);
  }
  sprintf(ret, "%d %s", (int)strlen(str), str);
  return ret;
}


x86 assembly (clang, -O0):
<serialize_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119c <serialize_string+0x2c>
lea    0xe72(%rip),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1226 <serialize_string+0xb6>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
lea    0xe4a(%rip),%rdx
call   1040 <snprintf@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f7 <serialize_string+0x87>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%edx
mov    -0x10(%rbp),%rcx
lea    0xde9(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serialize_string>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     11ce <serialize_string+0x5e>
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    0xe7a(%rip),%rdx
xor    %edi,%edi
xor    %esi,%esi
mov    %eax,%ecx
mov    %rbx,%r8
call   1040 <snprintf@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11e1 <serialize_string+0x71>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0xe4c(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
mov    %rbx,%rcx
call   1070 <sprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe2b(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1090 <strdup@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"0\00"
@.str.1 = [6 x i8] c"%d %s\00"
@stderr = global i32 0
@.str.2 = [36 x i8] c"[ERROR] Failed to allocate memory.\0A\00"
define i8* @serialize_string(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %2
br label %31
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i64 @strlen(i8* %10)
%12 = trunc i64 %11 to i32
%13 = load i8*, i8** %3
%14 = call i64 @snprintf(i32* null, i32 0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %12, i8* %13)
%15 = add nsw i64 %14, 1
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %4
%17 = load i8*, i8** %4
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %9
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %9
%24 = load i8*, i8** %4
%25 = load i8*, i8** %3
%26 = call i64 @strlen(i8* %25)
%27 = trunc i64 %26 to i32
%28 = load i8*, i8** %3
%29 = call i32 @sprintf(i8* %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %27, i8* %28)
%30 = load i8*, i8** %4
store i8* %30, i8** %2
br label %31
31: ; preds = %23, %7
%32 = load i8*, i8** %2
ret i8* %32
}

LLVM IR (-O2):
@.str = [2 x i8] c"0\00"
@.str.1 = [6 x i8] c"%d %s\00"
@stderr = global i32 0
@.str.2 = [36 x i8] c"[ERROR] Failed to allocate memory.\0A\00"
define noalias i8* @serialize_string(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call dereferenceable_or_null(2) i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %20
5: ; preds = %1
%6 = tail call i64 @strlen(i8* %0)
%7 = trunc i64 %6 to i32
%8 = tail call i64 @snprintf(i32* null, i32 0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %7, i8* %0)
%9 = add nsw i64 %8, 1
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %5
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %5
%17 = tail call i64 @strlen(i8* %0)
%18 = trunc i64 %17 to i32
%19 = tail call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %18, i8* %0)
br label %20
20: ; preds = %16, %3
%21 = phi i8* [ %4, %3 ], [ %10, %16 ]
ret i8* %21
}