#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SWAPL(int *);
int /*<<< orphan*/ SWAPW(short *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(unsigned int);
int open(char *, int /*<<< orphan*/, int);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int areadshort(char *file, short **data_ref, int *length_ref) {
  int fd;
  int length;
  int size;
  int offset;
  char *data;
  if ((fd = open(file, O_RDONLY, 0644)) < 0) {
    fprintf(stderr, "areadshort: %s: can't open\n", file);
    return -1;
  }
  if (read(fd, (char *)&length, 4) != 4) {
    fprintf(stderr, "areadshort: %s: can't read length (empty file?)\n", file);
    close(fd);
    return -1;
  }
  SWAPL(&length);
  size = length * sizeof(short);
  if (!(data = malloc((unsigned)size))) {
    fprintf(stderr, "areadshort: %s: can't alloc data\n", file);
    close(fd);
    return -1;
  }
  if (read(fd, data, size) != size) {
    fprintf(stderr, "areadshort: %s: can't read data\n", file);
    close(fd);
    free(data);
    return -1;
  }
  close(fd);
  *data_ref = (short *)data;
  for (offset = 0; offset < length; offset++)
    SWAPW(*data_ref + offset);
  *length_ref = length;
  return length;
}


x86 assembly (clang, -O0):
<areadshort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11dc <areadshort+0x5c>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe35(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1322 <areadshort+0x1a2>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x4,%eax
je     1223 <areadshort+0xa3>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe12(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1322 <areadshort+0x1a2>
lea    -0x28(%rbp),%rdi
call   10a0 <SWAPL@plt>
movslq -0x28(%rbp),%rax
shl    $0x1,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    127a <areadshort+0xfa>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdec(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1322 <areadshort+0x1a2>
mov    -0x24(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <read@plt>
cmp    -0x2c(%rbp),%eax
je     12c8 <areadshort+0x148>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdc9(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1322 <areadshort+0x1a2>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1313 <areadshort+0x193>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
movslq -0x30(%rbp),%rax
shl    $0x1,%rax
add    %rax,%rdi
call   1070 <SWAPW@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    12e2 <areadshort+0x162>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<areadshort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
test   %eax,%eax
js     1252 <areadshort+0xd2>
mov    %eax,%ebp
lea    0x4(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x4,%eax
jne    126c <areadshort+0xec>
lea    0x4(%rsp),%rdi
call   10a0 <SWAPL@plt>
mov    0x4(%rsp),%ebx
add    %ebx,%ebx
mov    %ebx,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     127e <areadshort+0xfe>
mov    %rax,%r12
mov    %ebp,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1050 <read@plt>
cmp    %ebx,%eax
jne    12b1 <areadshort+0x131>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r12,(%r15)
mov    0x4(%rsp),%eax
test   %eax,%eax
jle    124d <areadshort+0xcd>
mov    %r12,%rdi
call   1070 <SWAPW@plt>
mov    0x4(%rsp),%eax
cmp    $0x2,%eax
jl     124d <areadshort+0xcd>
mov    $0x1,%ebx
mov    $0x2,%ebp
nop
mov    (%r15),%rdi
add    %rbp,%rdi
call   1070 <SWAPW@plt>
add    $0x1,%rbx
movslq 0x4(%rsp),%rax
add    $0x2,%rbp
cmp    %rax,%rbx
jl     1230 <areadshort+0xb0>
mov    %eax,(%r14)
jmp    12a2 <areadshort+0x122>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xd9e(%rip),%rsi
mov    %r13,%rdx
call   1060 <fprintf@plt>
jmp    129d <areadshort+0x11d>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
jmp    128e <areadshort+0x10e>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
mov    %r13,%rdx
call   1060 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
mov    %r13,%rdx
call   1060 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    129d <areadshort+0x11d>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"areadshort: %s: can't open\0A\00"
@.str.1 = [49 x i8] c"areadshort: %s: can't read length (empty file?)\0A\00"
@.str.2 = [34 x i8] c"areadshort: %s: can't alloc data\0A\00"
@.str.3 = [33 x i8] c"areadshort: %s: can't read data\0A\00"
define i32 @areadshort(i8* %0, i16** %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i16**
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %5
store i16** %1, i16*** %6
store i32* %2, i32** %7
%13 = load i8*, i8** %5
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14, i32 420)
store i32 %15, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %5
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 -1, i32* %4
br label %86
21: ; preds = %3
%22 = load i32, i32* %8
%23 = bitcast i32* %9 to i8*
%24 = call i32 @read(i32 %22, i8* %23, i32 4)
%25 = icmp ne i32 %24, 4
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %5
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = load i32, i32* %8
%31 = call i32 @close(i32 %30)
store i32 -1, i32* %4
br label %86
32: ; preds = %21
%33 = call i32 @SWAPL(i32* %9)
%34 = load i32, i32* %9
%35 = sext i32 %34 to i64
%36 = mul i64 %35, 2
%37 = trunc i64 %36 to i32
store i32 %37, i32* %10
%38 = load i32, i32* %10
%39 = call i8* @malloc(i32 %38)
store i8* %39, i8** %12
%40 = icmp ne i8* %39, null
br i1 %40, label %47, label %41
41: ; preds = %32
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %5
%44 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %43)
%45 = load i32, i32* %8
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %4
br label %86
47: ; preds = %32
%48 = load i32, i32* %8
%49 = load i8*, i8** %12
%50 = load i32, i32* %10
%51 = call i32 @read(i32 %48, i8* %49, i32 %50)
%52 = load i32, i32* %10
%53 = icmp ne i32 %51, %52
br i1 %53, label %54, label %62
54: ; preds = %47
%55 = load i32, i32* @stderr
%56 = load i8*, i8** %5
%57 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %56)
%58 = load i32, i32* %8
%59 = call i32 @close(i32 %58)
%60 = load i8*, i8** %12
%61 = call i32 @free(i8* %60)
store i32 -1, i32* %4
br label %86
62: ; preds = %47
%63 = load i32, i32* %8
%64 = call i32 @close(i32 %63)
%65 = load i8*, i8** %12
%66 = bitcast i8* %65 to i16*
%67 = load i16**, i16*** %6
store i16* %66, i16** %67
store i32 0, i32* %11
br label %68
68: ; preds = %79, %62
%69 = load i32, i32* %11
%70 = load i32, i32* %9
%71 = icmp slt i32 %69, %70
br i1 %71, label %72, label %82
72: ; preds = %68
%73 = load i16**, i16*** %6
%74 = load i16*, i16** %73
%75 = load i32, i32* %11
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i16, i16* %74, i64 %76
%78 = call i32 @SWAPW(i16* %77)
br label %79
79: ; preds = %72
%80 = load i32, i32* %11
%81 = add nsw i32 %80, 1
store i32 %81, i32* %11
br label %68
82: ; preds = %68
%83 = load i32, i32* %9
%84 = load i32*, i32** %7
store i32 %83, i32* %84
%85 = load i32, i32* %9
store i32 %85, i32* %4
br label %86
86: ; preds = %82, %54, %41, %26, %17
%87 = load i32, i32* %4
ret i32 %87
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"areadshort: %s: can't open\0A\00"
@.str.1 = [49 x i8] c"areadshort: %s: can't read length (empty file?)\0A\00"
@.str.2 = [34 x i8] c"areadshort: %s: can't alloc data\0A\00"
@.str.3 = [33 x i8] c"areadshort: %s: can't read data\0A\00"
define i32 @areadshort(i8* %0, i16** nocapture %1, i32* nocapture writeonly %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6, i32 420)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %58
12: ; preds = %3
%13 = call i32 @read(i32 %7, i8* %5, i32 4)
%14 = icmp eq i32 %13, 4
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%18 = tail call i32 @close(i32 %7)
br label %58
19: ; preds = %12
%20 = call i32 @SWAPL(i32* %4)
%21 = load i32, i32* %4
%22 = shl i32 %21, 1
%23 = call i8* @malloc(i32 %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%28 = call i32 @close(i32 %7)
br label %58
29: ; preds = %19
%30 = call i32 @read(i32 %7, i8* %23, i32 %22)
%31 = icmp eq i32 %30, %22
br i1 %31, label %37, label %32
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%35 = call i32 @close(i32 %7)
%36 = call i32 @free(i8* %23)
br label %58
37: ; preds = %29
%38 = call i32 @close(i32 %7)
%39 = bitcast i16** %1 to i8**
store i8* %23, i8** %39
%40 = load i32, i32* %4
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %56
42: ; preds = %37
%43 = bitcast i8* %23 to i16*
%44 = call i32 @SWAPW(i16* %43)
%45 = load i32, i32* %4
%46 = icmp sgt i32 %45, 1
br i1 %46, label %47, label %56
47: ; preds = %42, %47
%48 = phi i64 [ %52, %47 ], [ 1, %42 ]
%49 = load i16*, i16** %1
%50 = getelementptr inbounds i16, i16* %49, i64 %48
%51 = call i32 @SWAPW(i16* %50)
%52 = add nuw nsw i64 %48, 1
%53 = load i32, i32* %4
%54 = sext i32 %53 to i64
%55 = icmp slt i64 %52, %54
br i1 %55, label %47, label %56
56: ; preds = %47, %42, %37
%57 = phi i32 [ %40, %37 ], [ %45, %42 ], [ %53, %47 ]
store i32 %57, i32* %2
br label %58
58: ; preds = %56, %32, %25, %15, %9
%59 = phi i32 [ -1, %9 ], [ -1, %15 ], [ -1, %32 ], [ %57, %56 ], [ -1, %25 ]
ret i32 %59
}