#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fread(char *, int, char, int /*<<< orphan*/ *);
char *malloc(int);
int parse_string(FILE *ptr, char **str) {
  int bytes = 0;
  char type;
  char len;
  char *text;
  fread(&type, sizeof(char), 1, ptr);
  bytes++;
  if (type == 0x00) {
    *str = NULL;
    return bytes;
  }
  fread(&len, sizeof(char), 1, ptr);
  bytes++;
  text = malloc(sizeof(char) * (len + 1));
  fread(text, sizeof(char), len, ptr);
  text[len] = '\0';
  bytes += len;
  *str = text;
  return bytes;
}


x86 assembly (clang, -O0):
<parse_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x1d(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movsbl -0x1d(%rbp),%eax
cmp    $0x0,%eax
jne    1177 <parse_string+0x57>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ec <parse_string+0xcc>
mov    -0x10(%rbp),%rcx
lea    -0x1e(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movsbl -0x1e(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1e(%rbp),%al
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
movsbl %al,%edx
call   1030 <fread@plt>
mov    -0x28(%rbp),%rax
movsbq -0x1e(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movsbl -0x1e(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_string>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x7(%rsp),%rdi
mov    $0x1,%ebp
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
cmpb   $0x0,0x7(%rsp)
je     1193 <parse_string+0x73>
lea    0x6(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
movsbq 0x6(%rsp),%rbp
lea    0x1(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
movb   $0x0,(%rbx,%rbp,1)
add    $0x2,%ebp
jmp    1195 <parse_string+0x75>
xor    %ebx,%ebx
mov    %rbx,(%r14)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @parse_string(i32* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8*
store i32* %0, i32** %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%10 = load i32*, i32** %4
%11 = call i32 @fread(i8* %7, i32 1, i8 signext 1, i32* %10)
%12 = load i32, i32* %6
%13 = add nsw i32 %12, 1
store i32 %13, i32* %6
%14 = load i8, i8* %7
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i8**, i8*** %5
store i8* null, i8** %18
%19 = load i32, i32* %6
store i32 %19, i32* %3
br label %47
20: ; preds = %2
%21 = load i32*, i32** %4
%22 = call i32 @fread(i8* %8, i32 1, i8 signext 1, i32* %21)
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
store i32 %24, i32* %6
%25 = load i8, i8* %8
%26 = sext i8 %25 to i32
%27 = add nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = mul i64 1, %28
%30 = trunc i64 %29 to i32
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %9
%32 = load i8*, i8** %9
%33 = load i8, i8* %8
%34 = load i32*, i32** %4
%35 = call i32 @fread(i8* %32, i32 1, i8 signext %33, i32* %34)
%36 = load i8*, i8** %9
%37 = load i8, i8* %8
%38 = sext i8 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 0, i8* %39
%40 = load i8, i8* %8
%41 = sext i8 %40 to i32
%42 = load i32, i32* %6
%43 = add nsw i32 %42, %41
store i32 %43, i32* %6
%44 = load i8*, i8** %9
%45 = load i8**, i8*** %5
store i8* %44, i8** %45
%46 = load i32, i32* %6
store i32 %46, i32* %3
br label %47
47: ; preds = %20, %17
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
define i32 @parse_string(i32* nocapture %0, i8** nocapture writeonly %1) {
%3 = alloca i8
%4 = alloca i8
%5 = call i32 @fread(i8* %3, i32 1, i8 signext 1, i32* %0)
%6 = load i8, i8* %3
%7 = icmp eq i8 %6, 0
br i1 %7, label %18, label %8
8: ; preds = %2
%9 = call i32 @fread(i8* %4, i32 1, i8 signext 1, i32* %0)
%10 = load i8, i8* %4
%11 = sext i8 %10 to i32
%12 = add nsw i32 %11, 1
%13 = tail call i8* @malloc(i32 %12)
%14 = tail call i32 @fread(i8* %13, i32 1, i8 signext %10, i32* %0)
%15 = sext i8 %10 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
store i8 0, i8* %16
%17 = add nsw i32 %11, 2
br label %18
18: ; preds = %2, %8
%19 = phi i8* [ %13, %8 ], [ null, %2 ]
%20 = phi i32 [ %17, %8 ], [ 1, %2 ]
store i8* %19, i8** %1
ret i32 %20
}