#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, unsigned short, 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 = NULL;
  } else {
    data = malloc((unsigned)len);
    if (!data)
      return 0;
    if (fread(data, sizeof(char), 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    1231 <read_counted_string+0xe1>
movzwl -0x22(%rbp),%eax
cmp    $0x0,%eax
jne    11a1 <read_counted_string+0x51>
movq   $0x0,-0x30(%rbp)
jmp    1214 <read_counted_string+0xc4>
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    1231 <read_counted_string+0xe1>
mov    -0x30(%rbp),%rdi
mov    -0x22(%rbp),%ax
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
movzwl %ax,%edx
call   1040 <fread@plt>
movzwl %ax,%eax
movzwl -0x22(%rbp),%ecx
cmp    %ecx,%eax
je     120f <read_counted_string+0xbf>
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    1231 <read_counted_string+0xe1>
jmp    1214 <read_counted_string+0xc4>
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 %47
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 %42
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 %47
26: ; preds = %19
%27 = load i8*, i8** %9
%28 = load i16, i16* %8
%29 = load i32*, i32** %7
%30 = call zeroext i16 @fread(i8* %27, i32 1, i16 zeroext %28, i32* %29)
%31 = zext i16 %30 to i32
%32 = load i16, i16* %8
%33 = zext i16 %32 to i32
%34 = icmp ne i32 %31, %33
br i1 %34, label %35, label %41
35: ; preds = %26
%36 = load i8*, i8** %9
%37 = load i16, i16* %8
%38 = call i32 @bzero(i8* %36, i16 zeroext %37)
%39 = load i8*, i8** %9
%40 = call i32 @free(i8* %39)
store i32 0, i32* %4
br label %47
41: ; preds = %26
br label %42
42: ; preds = %41, %18
%43 = load i8*, i8** %9
%44 = load i8**, i8*** %6
store i8* %43, i8** %44
%45 = load i16, i16* %8
%46 = load i16*, i16** %5
store i16 %45, i16* %46
store i32 1, i32* %4
br label %47
47: ; preds = %42, %35, %25, %13
%48 = load i32, i32* %4
ret i32 %48
}

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, i16 zeroext %9, 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
}