#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 */
char EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int readPiecesFile(char *filename, short *piecesArray, int maxReadableSize) {
  FILE *f = NULL;
  f = fopen(filename, "r");
  if (f == NULL)
    return -1;
  int number;
  char ch;
  int sizeRead = 0;
  while (sizeRead < maxReadableSize) {
    ch = fgetc(f);
    if (ch == EOF)
      break;
    number = ch - '0';
    ch = fgetc(f);
    if (ch == EOF)
      break;
    piecesArray[sizeRead] = number;
    sizeRead++;
  }
  fclose(f);
  return sizeRead;
}


x86 assembly (clang, -O0):
<readPiecesFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeaa(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1176 <readPiecesFile+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <readPiecesFile+0xe0>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1201 <readPiecesFile+0xd1>
mov    -0x28(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x2d(%rbp)
movsbl -0x2d(%rbp),%eax
mov    0x2e48(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11b0 <readPiecesFile+0x80>
jmp    1201 <readPiecesFile+0xd1>
movsbl -0x2d(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x2d(%rbp)
movsbl -0x2d(%rbp),%eax
mov    0x2e17(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11e1 <readPiecesFile+0xb1>
jmp    1201 <readPiecesFile+0xd1>
mov    -0x2c(%rbp),%eax
mov    %ax,%dx
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %dx,(%rax,%rcx,2)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    117d <readPiecesFile+0x4d>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readPiecesFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r15
lea    0xeb9(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11a5 <readPiecesFile+0x75>
mov    %rax,%r14
test   %ebx,%ebx
jle    11ad <readPiecesFile+0x7d>
mov    %ebx,%r13d
xor    %r12d,%r12d
mov    0x2e83(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
call   1040 <fgetc@plt>
cmp    0x0(%rbp),%al
je     11b0 <readPiecesFile+0x80>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <fgetc@plt>
cmp    0x0(%rbp),%al
je     11b0 <readPiecesFile+0x80>
movsbl %bl,%eax
add    $0xffffffd0,%eax
mov    %ax,(%r15,%r12,2)
add    $0x1,%r12
cmp    %r12,%r13
jne    1170 <readPiecesFile+0x40>
mov    %r13d,%r12d
jmp    11b0 <readPiecesFile+0x80>
mov    $0xffffffff,%r12d
jmp    11b8 <readPiecesFile+0x88>
xor    %r12d,%r12d
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@EOF = global i8 0
define i32 @readPiecesFile(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 i8
%11 = alloca i32
store i8* %0, i8** %5
store i16* %1, i16** %6
store i32 %2, i32* %7
store i32* null, i32** %8
%12 = load i8*, i8** %5
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %8
%14 = load i32*, i32** %8
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %56
17: ; preds = %3
store i32 0, i32* %11
br label %18
18: ; preds = %43, %17
%19 = load i32, i32* %11
%20 = load i32, i32* %7
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %52
22: ; preds = %18
%23 = load i32*, i32** %8
%24 = call signext i8 @fgetc(i32* %23)
store i8 %24, i8* %10
%25 = load i8, i8* %10
%26 = sext i8 %25 to i32
%27 = load i8, i8* @EOF
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %26, %28
br i1 %29, label %30, label %31
30: ; preds = %22
br label %52
31: ; preds = %22
%32 = load i8, i8* %10
%33 = sext i8 %32 to i32
%34 = sub nsw i32 %33, 48
store i32 %34, i32* %9
%35 = load i32*, i32** %8
%36 = call signext i8 @fgetc(i32* %35)
store i8 %36, i8* %10
%37 = load i8, i8* %10
%38 = sext i8 %37 to i32
%39 = load i8, i8* @EOF
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %38, %40
br i1 %41, label %42, label %43
42: ; preds = %31
br label %52
43: ; preds = %31
%44 = load i32, i32* %9
%45 = trunc i32 %44 to i16
%46 = load i16*, i16** %6
%47 = load i32, i32* %11
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i16, i16* %46, i64 %48
store i16 %45, i16* %49
%50 = load i32, i32* %11
%51 = add nsw i32 %50, 1
store i32 %51, i32* %11
br label %18
52: ; preds = %42, %30, %18
%53 = load i32*, i32** %8
%54 = call i32 @fclose(i32* %53)
%55 = load i32, i32* %11
store i32 %55, i32* %4
br label %56
56: ; preds = %52, %16
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i8 0
define i32 @readPiecesFile(i8* nocapture %0, i16* nocapture writeonly %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %32, label %6
6: ; preds = %3
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %29
8: ; preds = %6
%9 = zext i32 %2 to i64
br label %10
10: ; preds = %8, %19
%11 = phi i64 [ 0, %8 ], [ %23, %19 ]
%12 = tail call signext i8 @fgetc(i32* %4)
%13 = load i8, i8* @EOF
%14 = icmp eq i8 %12, %13
br i1 %14, label %27, label %15
15: ; preds = %10
%16 = tail call signext i8 @fgetc(i32* %4)
%17 = load i8, i8* @EOF
%18 = icmp eq i8 %16, %17
br i1 %18, label %25, label %19
19: ; preds = %15
%20 = sext i8 %12 to i16
%21 = add nsw i16 %20, -48
%22 = getelementptr inbounds i16, i16* %1, i64 %11
store i16 %21, i16* %22
%23 = add nuw nsw i64 %11, 1
%24 = icmp eq i64 %23, %9
br i1 %24, label %29, label %10
25: ; preds = %15
%26 = trunc i64 %11 to i32
br label %29
27: ; preds = %10
%28 = trunc i64 %11 to i32
br label %29
29: ; preds = %19, %25, %27, %6
%30 = phi i32 [ 0, %6 ], [ %26, %25 ], [ %28, %27 ], [ %2, %19 ]
%31 = tail call i32 @fclose(i32* %4)
br label %32
32: ; preds = %3, %29
%33 = phi i32 [ %30, %29 ], [ -1, %3 ]
ret i32 %33
}