#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*/ fatalperror(char *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int fread(short *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(short *);
short *malloc(int);
void readawaysamples(FILE *input, size_t size) {
  short *buffer;
  int samplesread, count;
  buffer = malloc(sizeof(*buffer) * 50000);
  if (buffer == NULL)
    fatalperror("Couldn't allocate buffer");
  while (size > 0) {
    if (size > 50000)
      count = 50000;
    else
      count = size;
    samplesread = fread(buffer, sizeof(*buffer), count, input);
    if (ferror(input) != 0)
      fatalperror("Error reading input file");
    size -= samplesread;
  }
  free(buffer);
}


x86 assembly (clang, -O0):
<readawaysamples>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x186a0,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1185 <readawaysamples+0x35>
lea    0xe80(%rip),%rdi
call   1070 <fatalperror@plt>
jmp    118a <readawaysamples+0x3a>
cmpq   $0x0,-0x10(%rbp)
jbe    1201 <readawaysamples+0xb1>
cmpq   $0xc350,-0x10(%rbp)
jbe    11af <readawaysamples+0x5f>
movl   $0xc350,-0x20(%rbp)
jmp    11b6 <readawaysamples+0x66>
mov    -0x10(%rbp),%rax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    $0x2,%esi
call   1050 <fread@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     11ed <readawaysamples+0x9d>
lea    0xe31(%rip),%rdi
call   1070 <fatalperror@plt>
movslq -0x1c(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    118a <readawaysamples+0x3a>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readawaysamples>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    $0x186a0,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     1177 <readawaysamples+0x27>
test   %rbx,%rbx
jne    1188 <readawaysamples+0x38>
jmp    11e0 <readawaysamples+0x90>
lea    0xe82(%rip),%rdi
call   1070 <fatalperror@plt>
test   %rbx,%rbx
je     11e0 <readawaysamples+0x90>
lea    0xe8a(%rip),%r12
jmp    11a8 <readawaysamples+0x58>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %ebp,%rax
sub    %rax,%rbx
je     11e0 <readawaysamples+0x90>
cmp    $0xc350,%rbx
mov    $0xc350,%edx
cmovb  %ebx,%edx
mov    %r15,%rdi
mov    $0x2,%esi
mov    %r14,%rcx
call   1050 <fread@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     11a0 <readawaysamples+0x50>
mov    %r12,%rdi
call   1070 <fatalperror@plt>
jmp    11a0 <readawaysamples+0x50>
mov    %r15,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"Couldn't allocate buffer\00"
@.str.1 = [25 x i8] c"Error reading input file\00"
define void @readawaysamples(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i16*
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
store i64 %1, i64* %4
%8 = call i16* @malloc(i32 100000)
store i16* %8, i16** %5
%9 = load i16*, i16** %5
%10 = icmp eq i16* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @fatalperror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
br label %14
14: ; preds = %34, %13
%15 = load i64, i64* %4
%16 = icmp ugt i64 %15, 0
br i1 %16, label %17, label %39
17: ; preds = %14
%18 = load i64, i64* %4
%19 = icmp ugt i64 %18, 50000
br i1 %19, label %20, label %21
20: ; preds = %17
store i32 50000, i32* %7
br label %24
21: ; preds = %17
%22 = load i64, i64* %4
%23 = trunc i64 %22 to i32
store i32 %23, i32* %7
br label %24
24: ; preds = %21, %20
%25 = load i16*, i16** %5
%26 = load i32, i32* %7
%27 = load i32*, i32** %3
%28 = call i32 @fread(i16* %25, i32 2, i32 %26, i32* %27)
store i32 %28, i32* %6
%29 = load i32*, i32** %3
%30 = call i64 @ferror(i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %24
%33 = call i32 @fatalperror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %24
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = load i64, i64* %4
%38 = sub i64 %37, %36
store i64 %38, i64* %4
br label %14
39: ; preds = %14
%40 = load i16*, i16** %5
%41 = call i32 @free(i16* %40)
ret void
}

LLVM IR (-O2):
@.str = [25 x i8] c"Couldn't allocate buffer\00"
@.str.1 = [25 x i8] c"Error reading input file\00"
define void @readawaysamples(i32* nocapture %0, i64 %1) {
%3 = tail call i16* @malloc(i32 100000)
%4 = icmp eq i16* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @fatalperror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = icmp eq i64 %1, 0
br i1 %8, label %23, label %9
9: ; preds = %7, %19
%10 = phi i64 [ %21, %19 ], [ %1, %7 ]
%11 = icmp ult i64 %10, 50000
%12 = select i1 %11, i64 %10, i64 50000
%13 = trunc i64 %12 to i32
%14 = tail call i32 @fread(i16* %3, i32 2, i32 %13, i32* %0)
%15 = tail call i64 @ferror(i32* %0)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %9
%18 = tail call i32 @fatalperror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %9
%20 = sext i32 %14 to i64
%21 = sub i64 %10, %20
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %9
23: ; preds = %19, %7
%24 = tail call i32 @free(i16* %3)
ret void
}