#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*/ 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 areadint(char *file, int **data_ref, int *length_ref) {
  int fd;
  int length;
  int size, k;
  int offset;
  char *data;
  fd = open(file, O_RDONLY, 0644);
  if (fd < 0) {
    fprintf(stderr, "areadint: %s: can't open\n", file);
    return -1;
  }
  if (read(fd, (char *)&length, 4) != 4) {
    fprintf(stderr, "areadint: %s: can't read length (empty file?)\n", file);
    close(fd);
    return -1;
  }
  SWAPL(&length);
  size = length * sizeof(int);
  if (!(data = malloc((unsigned)size))) {
    fprintf(stderr, "areadint: %s: can't alloc data\n", file);
    close(fd);
    return -1;
  }
  if ((k = read(fd, data, size)) != size) {
    fprintf(stderr,
            "areadint: %s: Expected size (%d) different from size read(%d)\n",
            file, size, k);
    close(fd);
    free(data);
    return -1;
  }
  close(fd);
  *data_ref = (int *)data;
  for (offset = 0; offset < length; offset++)
    SWAPL(*data_ref + offset);
  *length_ref = length;
  return length;
}


x86 assembly (clang, -O0):
<areadint>:
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    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11cf <areadint+0x5f>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1325 <areadint+0x1b5>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x4,%eax
je     1218 <areadint+0xa8>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe1d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1325 <areadint+0x1b5>
lea    -0x28(%rbp),%rdi
call   1090 <SWAPL@plt>
movslq -0x28(%rbp),%rax
shl    $0x2,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    1271 <areadint+0x101>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdf3(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1325 <areadint+0x1b5>
mov    -0x24(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x30(%rbp)
cmp    -0x2c(%rbp),%eax
je     12cb <areadint+0x15b>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
mov    -0x30(%rbp),%r8d
lea    0xdc2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1325 <areadint+0x1b5>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1316 <areadint+0x1a6>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
movslq -0x34(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
call   1090 <SWAPL@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    12e5 <areadint+0x175>
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):
<areadint>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
test   %eax,%eax
js     1242 <areadint+0xd2>
mov    %eax,%ebp
lea    0x4(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x4,%eax
jne    125e <areadint+0xee>
lea    0x4(%rsp),%rdi
call   1090 <SWAPL@plt>
mov    0x4(%rsp),%ebx
shl    $0x2,%ebx
mov    %ebx,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1270 <areadint+0x100>
mov    %rax,%r12
mov    %ebp,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1050 <read@plt>
cmp    %ebx,%eax
jne    12a5 <areadint+0x135>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r12,(%r15)
mov    0x4(%rsp),%eax
test   %eax,%eax
jle    123d <areadint+0xcd>
mov    %r12,%rdi
call   1090 <SWAPL@plt>
mov    0x4(%rsp),%eax
cmp    $0x2,%eax
jl     123d <areadint+0xcd>
mov    $0x1,%ebx
mov    $0x4,%ebp
mov    (%r15),%rdi
add    %rbp,%rdi
call   1090 <SWAPL@plt>
add    $0x1,%rbx
movslq 0x4(%rsp),%rax
add    $0x4,%rbp
cmp    %rax,%rbx
jl     1220 <areadint+0xb0>
mov    %eax,(%r14)
jmp    1296 <areadint+0x126>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1291 <areadint+0x121>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xdac(%rip),%rsi
jmp    1280 <areadint+0x110>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
mov    %r13,%rdx
xor    %eax,%eax
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    0x2d2c(%rip),%rcx
mov    (%rcx),%edi
lea    0xdb4(%rip),%rsi
mov    %r13,%rdx
mov    %ebx,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    1291 <areadint+0x121>

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"areadint: %s: can't open\0A\00"
@.str.1 = [47 x i8] c"areadint: %s: can't read length (empty file?)\0A\00"
@.str.2 = [32 x i8] c"areadint: %s: can't alloc data\0A\00"
@.str.3 = [63 x i8] c"areadint: %s: Expected size (%d) different from size read(%d)\0A\00"
define i32 @areadint(i8* %0, i32** 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 (i32, i8*, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([26 x i8], [26 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 (i32, i8*, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([47 x i8], [47 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, 2
%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 (i32, i8*, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([32 x i8], [32 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 (i32, i8*, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %22, i32 %30)
%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 i32** %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 i32*
%44 = call i32 @SWAPL(i32* %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 i32*, i32** %1
%50 = getelementptr inbounds i32, i32* %49, i64 %48
%51 = call i32 @SWAPL(i32* %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
}