#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 */
unsigned char fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(unsigned int);
scalar_t__ read_byte(int /*<<< orphan*/ *, unsigned char *);
int read_counted_string(FILE *file, char **stringp) {
  unsigned char len;
  char *data;
  if (read_byte(file, &len) == 0)
    return 0;
  if (len == 0) {
    data = 0;
  } else {
    data = malloc((unsigned)len + 1);
    if (!data)
      return 0;
    if (fread(data, (int)sizeof(char), (int)len, file) != len) {
      free(data);
      return 0;
    }
    data[len] = '\0';
  }
  *stringp = data;
  return 1;
}


x86 assembly (clang, -O0):
<read_counted_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x19(%rbp),%rsi
call   1040 <read_byte@plt>
cmp    $0x0,%rax
jne    1173 <read_counted_string+0x33>
movl   $0x0,-0x4(%rbp)
jmp    120c <read_counted_string+0xcc>
movzbl -0x19(%rbp),%eax
cmp    $0x0,%eax
jne    118d <read_counted_string+0x4d>
movq   $0x0,-0x28(%rbp)
jmp    11fa <read_counted_string+0xba>
movzbl -0x19(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b4 <read_counted_string+0x74>
movl   $0x0,-0x4(%rbp)
jmp    120c <read_counted_string+0xcc>
mov    -0x28(%rbp),%rdi
movzbl -0x19(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
movzbl %al,%eax
movzbl -0x19(%rbp),%ecx
cmp    %ecx,%eax
je     11ee <read_counted_string+0xae>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    120c <read_counted_string+0xcc>
mov    -0x28(%rbp),%rax
movzbl -0x19(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_counted_string>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r13
lea    0xf(%rsp),%rsi
call   1040 <read_byte@plt>
test   %rax,%rax
je     11a0 <read_counted_string+0x60>
movzbl 0xf(%rsp),%ebx
xor    %r15d,%r15d
test   %ebx,%ebx
je     11a5 <read_counted_string+0x65>
lea    0x1(%rbx),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11bb <read_counted_string+0x7b>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %r13,%rcx
call   1050 <fread@plt>
cmp    0xf(%rsp),%al
jne    11b3 <read_counted_string+0x73>
movzbl %al,%eax
movb   $0x0,(%r12,%rax,1)
jmp    11a8 <read_counted_string+0x68>
xor    %r15d,%r15d
jmp    11bb <read_counted_string+0x7b>
xor    %r12d,%r12d
mov    %r12,(%r14)
mov    $0x1,%r15d
jmp    11bb <read_counted_string+0x7b>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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