#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*/ bzero(char *, unsigned short);
unsigned short fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(unsigned int);
scalar_t__ read_short(unsigned short *, int /*<<< orphan*/ *);
int read_counted_string(unsigned short *countp, char **stringp, FILE *file) {
  unsigned short len;
  char *data;
  if (read_short(&len, file) == 0)
    return 0;
  if (len == 0) {
    data = 0;
  } else {
    data = malloc((unsigned)len);
    if (!data)
      return 0;
    if (fread(data, (int)sizeof(char), (int)len, file) != len) {
      bzero(data, len);
      free(data);
      return 0;
    }
  }
  *stringp = data;
  *countp = len;
  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    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    -0x22(%rbp),%rdi
call   1050 <read_short@plt>
cmp    $0x0,%rax
jne    1187 <read_counted_string+0x37>
movl   $0x0,-0x4(%rbp)
jmp    122e <read_counted_string+0xde>
movzwl -0x22(%rbp),%eax
cmp    $0x0,%eax
jne    11a1 <read_counted_string+0x51>
movq   $0x0,-0x30(%rbp)
jmp    1211 <read_counted_string+0xc1>
movzwl -0x22(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c5 <read_counted_string+0x75>
movl   $0x0,-0x4(%rbp)
jmp    122e <read_counted_string+0xde>
mov    -0x30(%rbp),%rdi
movzwl -0x22(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
movzwl %ax,%eax
movzwl -0x22(%rbp),%ecx
cmp    %ecx,%eax
je     120c <read_counted_string+0xbc>
mov    -0x30(%rbp),%rdi
movzwl -0x22(%rbp),%esi
call   1070 <bzero@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    122e <read_counted_string+0xde>
jmp    1211 <read_counted_string+0xc1>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x22(%rbp),%cx
mov    -0x10(%rbp),%rax
mov    %cx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_counted_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x6(%rsp),%rdi
mov    %rdx,%rsi
call   1050 <read_short@plt>
test   %rax,%rax
je     11c2 <read_counted_string+0x72>
movzwl 0x6(%rsp),%ebp
xor    %r13d,%r13d
test   %ebp,%ebp
je     11c7 <read_counted_string+0x77>
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11d9 <read_counted_string+0x89>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
movzwl 0x6(%rsp),%ecx
cmp    %cx,%ax
je     11cc <read_counted_string+0x7c>
movzwl %cx,%esi
mov    %r12,%rdi
call   1070 <bzero@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    11d9 <read_counted_string+0x89>
xor    %r13d,%r13d
jmp    11d9 <read_counted_string+0x89>
xor    %eax,%eax
xor    %r12d,%r12d
mov    %r12,(%r14)
mov    %ax,(%r15)
mov    $0x1,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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