#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*/ fwrite(short *, int, int, int /*<<< orphan*/ *);
short *malloc(int);
int workloop(FILE *theinfile, FILE *theoutfile,
             int (*work)(short *buffer, int length)) {
  short *buffer;
  int length, nowlength;
  length = 50000;
  if ((buffer = malloc(sizeof(short) * length)) == NULL)
    fatalperror("");
  while ((0 == 0)) {
    nowlength = fread(buffer, sizeof(short), length, theinfile);
    if (ferror(theinfile) != 0)
      fatalperror("Error reading input file");
    if (nowlength == 0)
      break;
    if (!work(buffer, nowlength))
      return (0 == 1);
    if (!fwrite(buffer, sizeof(short), nowlength, theoutfile)) {
      return (0 == 1);
    }
    if (ferror(theoutfile) != 0)
      fatalperror("Error writing to output file");
  }
  return (0 == 0);
}


x86 assembly (clang, -O0):
<workloop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0xc350,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1194 <workloop+0x44>
lea    0xe89(%rip),%rdi
call   1070 <fatalperror@plt>
jmp    1199 <workloop+0x49>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x2,%esi
call   1040 <fread@plt>
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11d0 <workloop+0x80>
lea    0xe35(%rip),%rdi
call   1070 <fatalperror@plt>
cmpl   $0x0,-0x30(%rbp)
jne    11df <workloop+0x8f>
jmp    124f <workloop+0xff>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%esi
call   *%rax
cmp    $0x0,%eax
jne    1201 <workloop+0xb1>
movl   $0x0,-0x4(%rbp)
jmp    1256 <workloop+0x106>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x2,%esi
call   1060 <fwrite@plt>
cmp    $0x0,%eax
jne    122b <workloop+0xdb>
movl   $0x0,-0x4(%rbp)
jmp    1256 <workloop+0x106>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     124a <workloop+0xfa>
lea    0xdd4(%rip),%rdi
call   1070 <fatalperror@plt>
jmp    1199 <workloop+0x49>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<workloop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    $0x186a0,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
lea    0xea0(%rip),%rdi
test   %rax,%rax
jne    1190 <workloop+0x40>
call   1070 <fatalperror@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x2,%esi
mov    $0xc350,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %eax,%r13d
mov    %r12,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11c1 <workloop+0x71>
lea    0xe44(%rip),%rdi
call   1070 <fatalperror@plt>
mov    $0x1,%ebp
test   %r13d,%r13d
je     120a <workloop+0xba>
mov    %rbx,%rdi
mov    %r13d,%esi
call   *%r14
xor    %ebp,%ebp
test   %eax,%eax
je     120a <workloop+0xba>
mov    %rbx,%rdi
mov    $0x2,%esi
mov    %r13d,%edx
mov    %r15,%rcx
call   1060 <fwrite@plt>
test   %eax,%eax
je     120a <workloop+0xba>
mov    %r15,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1190 <workloop+0x40>
lea    0xe14(%rip),%rdi
jmp    117d <workloop+0x2d>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [25 x i8] c"Error reading input file\00"
@.str.2 = [29 x i8] c"Error writing to output file\00"
define i32 @workloop(i32* %0, i32* %1, i32 (i16*, i32)* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32 (i16*, i32)*
%8 = alloca i16*
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32 (i16*, i32)* %2, i32 (i16*, i32)** %7
store i32 50000, i32* %9
%11 = load i32, i32* %9
%12 = sext i32 %11 to i64
%13 = mul i64 2, %12
%14 = trunc i64 %13 to i32
%15 = call i16* @malloc(i32 %14)
store i16* %15, i16** %8
%16 = icmp eq i16* %15, null
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @fatalperror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %3
br label %20
20: ; preds = %19, %54
%21 = load i16*, i16** %8
%22 = load i32, i32* %9
%23 = load i32*, i32** %5
%24 = call i32 @fread(i16* %21, i32 2, i32 %22, i32* %23)
store i32 %24, i32* %10
%25 = load i32*, i32** %5
%26 = call i64 @ferror(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = call i32 @fatalperror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %20
%31 = load i32, i32* %10
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
br label %55
34: ; preds = %30
%35 = load i32 (i16*, i32)*, i32 (i16*, i32)** %7
%36 = load i16*, i16** %8
%37 = load i32, i32* %10
%38 = call i32 %35(i16* %36, i32 %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %41, label %40
40: ; preds = %34
store i32 0, i32* %4
br label %56
41: ; preds = %34
%42 = load i16*, i16** %8
%43 = load i32, i32* %10
%44 = load i32*, i32** %6
%45 = call i32 @fwrite(i16* %42, i32 2, i32 %43, i32* %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %48, label %47
47: ; preds = %41
store i32 0, i32* %4
br label %56
48: ; preds = %41
%49 = load i32*, i32** %6
%50 = call i64 @ferror(i32* %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @fatalperror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %54
54: ; preds = %52, %48
br label %20
55: ; preds = %33
store i32 1, i32* %4
br label %56
56: ; preds = %55, %47, %40
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [25 x i8] c"Error reading input file\00"
@.str.2 = [29 x i8] c"Error writing to output file\00"
define i32 @workloop(i32* nocapture %0, i32* nocapture %1, i32 (i16*, i32)* nocapture %2) {
%4 = tail call i16* @malloc(i32 100000)
%5 = icmp eq i16* %4, null
br i1 %5, label %6, label %9
6: ; preds = %24, %3
%7 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), %24 ]
%8 = tail call i32 @fatalperror(i8* %7)
br label %9
9: ; preds = %3, %6
br label %10
10: ; preds = %9, %24
%11 = tail call i32 @fread(i16* %4, i32 2, i32 50000, i32* %0)
%12 = tail call i64 @ferror(i32* %0)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 @fatalperror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = icmp eq i32 %11, 0
br i1 %17, label %27, label %18
18: ; preds = %16
%19 = tail call i32 %2(i16* %4, i32 %11)
%20 = icmp eq i32 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %18
%22 = tail call i32 @fwrite(i16* %4, i32 2, i32 %11, i32* %1)
%23 = icmp eq i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = tail call i64 @ferror(i32* %1)
%26 = icmp eq i64 %25, 0
br i1 %26, label %10, label %6
27: ; preds = %16, %21, %18
%28 = phi i32 [ 0, %18 ], [ 0, %21 ], [ 1, %16 ]
ret i32 %28
}