#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 */
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, short);
char serializer_read_char(char *, int *);
short serializer_read_short(char *, int *);
char *serializer_read_string(char *src, int *pointer) {
  short string_length = serializer_read_short(src, pointer);
  if (string_length > 0) {
    char *return_value = malloc(sizeof(char) * string_length + 1);
    memset(return_value, 0, string_length);
    short i;
    for (i = 0; i < string_length; i++) {
      char read_char = serializer_read_char(src, pointer);
      return_value[i] = read_char;
      if (i == (string_length - 1))
        return_value[string_length] = '\0';
    }
    (*pointer)++;
    return return_value;
  } else
    return NULL;
}


x86 assembly (clang, -O0):
<serializer_read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <serializer_read_short@plt>
mov    %ax,-0x1a(%rbp)
movswl -0x1a(%rbp),%eax
cmp    $0x0,%eax
jle    121b <serializer_read_string+0xdb>
movswq -0x1a(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1a(%rbp),%ax
movswl %ax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
movw   $0x0,-0x2a(%rbp)
movswl -0x2a(%rbp),%eax
movswl -0x1a(%rbp),%ecx
cmp    %ecx,%eax
jge    1203 <serializer_read_string+0xc3>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <serializer_read_char@plt>
mov    %al,-0x2b(%rbp)
mov    -0x2b(%rbp),%dl
mov    -0x28(%rbp),%rax
movswq -0x2a(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
movswl -0x2a(%rbp),%eax
movswl -0x1a(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11ed <serializer_read_string+0xad>
mov    -0x28(%rbp),%rax
movswq -0x1a(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11f2 <serializer_read_string+0xb2>
mov    -0x2a(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x2a(%rbp)
jmp    119e <serializer_read_string+0x5e>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1223 <serializer_read_string+0xe3>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serializer_read_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
call   1030 <serializer_read_short@plt>
test   %ax,%ax
jle    11b9 <serializer_read_string+0x79>
movswl %ax,%r12d
movzwl %r12w,%r13d
lea    0x1(%r12),%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1050 <memset@plt>
add    $0xffffffff,%r12d
jmp    1199 <serializer_read_string+0x59>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%rbp
cmp    %rbp,%r13
je     11b3 <serializer_read_string+0x73>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <serializer_read_char@plt>
mov    %al,(%rbx,%rbp,1)
cmp    %rbp,%r12
jne    1190 <serializer_read_string+0x50>
movb   $0x0,(%rbx,%r13,1)
jmp    1190 <serializer_read_string+0x50>
addl   $0x1,(%r15)
jmp    11bb <serializer_read_string+0x7b>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @serializer_read_string(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i16
%7 = alloca i8*
%8 = alloca i16
%9 = alloca i8
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = load i8*, i8** %4
%11 = load i32*, i32** %5
%12 = call signext i16 @serializer_read_short(i8* %10, i32* %11)
store i16 %12, i16* %6
%13 = load i16, i16* %6
%14 = sext i16 %13 to i32
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %61
16: ; preds = %2
%17 = load i16, i16* %6
%18 = sext i16 %17 to i64
%19 = mul i64 1, %18
%20 = add i64 %19, 1
%21 = trunc i64 %20 to i32
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = load i16, i16* %6
%25 = call i32 @memset(i8* %23, i32 0, i16 signext %24)
store i16 0, i16* %8
br label %26
26: ; preds = %53, %16
%27 = load i16, i16* %8
%28 = sext i16 %27 to i32
%29 = load i16, i16* %6
%30 = sext i16 %29 to i32
%31 = icmp slt i32 %28, %30
br i1 %31, label %32, label %56
32: ; preds = %26
%33 = load i8*, i8** %4
%34 = load i32*, i32** %5
%35 = call signext i8 @serializer_read_char(i8* %33, i32* %34)
store i8 %35, i8* %9
%36 = load i8, i8* %9
%37 = load i8*, i8** %7
%38 = load i16, i16* %8
%39 = sext i16 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 %36, i8* %40
%41 = load i16, i16* %8
%42 = sext i16 %41 to i32
%43 = load i16, i16* %6
%44 = sext i16 %43 to i32
%45 = sub nsw i32 %44, 1
%46 = icmp eq i32 %42, %45
br i1 %46, label %47, label %52
47: ; preds = %32
%48 = load i8*, i8** %7
%49 = load i16, i16* %6
%50 = sext i16 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 0, i8* %51
br label %52
52: ; preds = %47, %32
br label %53
53: ; preds = %52
%54 = load i16, i16* %8
%55 = add i16 %54, 1
store i16 %55, i16* %8
br label %26
56: ; preds = %26
%57 = load i32*, i32** %5
%58 = load i32, i32* %57
%59 = add nsw i32 %58, 1
store i32 %59, i32* %57
%60 = load i8*, i8** %7
store i8* %60, i8** %3
br label %62
61: ; preds = %2
store i8* null, i8** %3
br label %62
62: ; preds = %61, %56
%63 = load i8*, i8** %3
ret i8* %63
}

LLVM IR (-O2):
define i8* @serializer_read_string(i8* %0, i32* %1) {
%3 = tail call signext i16 @serializer_read_short(i8* %0, i32* %1)
%4 = sext i16 %3 to i32
%5 = icmp sgt i16 %3, 0
br i1 %5, label %6, label %26
6: ; preds = %2
%7 = zext i16 %3 to i64
%8 = add nsw i32 %4, 1
%9 = tail call i8* @malloc(i32 %8)
%10 = tail call i32 @memset(i8* %9, i32 0, i16 signext %3)
%11 = add nsw i32 %4, -1
%12 = getelementptr inbounds i8, i8* %9, i64 %7
%13 = zext i32 %11 to i64
br label %14
14: ; preds = %6, %20
%15 = phi i64 [ 0, %6 ], [ %21, %20 ]
%16 = tail call signext i8 @serializer_read_char(i8* %0, i32* %1)
%17 = getelementptr inbounds i8, i8* %9, i64 %15
store i8 %16, i8* %17
%18 = icmp eq i64 %15, %13
br i1 %18, label %19, label %20
19: ; preds = %14
store i8 0, i8* %12
br label %20
20: ; preds = %19, %14
%21 = add nuw nsw i64 %15, 1
%22 = icmp eq i64 %21, %7
br i1 %22, label %23, label %14
23: ; preds = %20
%24 = load i32, i32* %1
%25 = add nsw i32 %24, 1
store i32 %25, i32* %1
br label %26
26: ; preds = %2, %23
%27 = phi i8* [ %9, %23 ], [ null, %2 ]
ret i8* %27
}