#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int ReadN(int fd, short *v, int n) {
  int r;
  do {
    r = read(fd, (char *)(v), n * sizeof(short));
  } while (r == -1 && errno == EINTR);
  if (r == -1 || r % sizeof(short)) {
    perror("Error reading input");
    exit(-1);
  }
  return r / sizeof(short);
}


x86 assembly (clang, -O0):
<ReadN>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x18(%rbp)
mov    %al,-0x19(%rbp)
jne    1184 <ReadN+0x54>
mov    0x2e6f(%rip),%rax
mov    (%rax),%rax
mov    0x2e5d(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1142 <ReadN+0x12>
cmpl   $0xffffffff,-0x18(%rbp)
je     11ab <ReadN+0x7b>
movslq -0x18(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     11c1 <ReadN+0x91>
lea    0xe4e(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
movslq -0x18(%rbp),%rax
shr    $0x1,%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadN>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
add    %edx,%ebx
mov    0x2e98(%rip),%r15
mov    0x2e89(%rip),%r12
nop
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    116a <ReadN+0x3a>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1150 <ReadN+0x20>
test   $0x1,%al
jne    1179 <ReadN+0x49>
sar    %eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe80(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [20 x i8] c"Error reading input\00"
define i32 @ReadN(i32 %0, i16* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i16*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i16* %1, i16** %5
store i32 %2, i32* %6
br label %8
8: ; preds = %24, %3
%9 = load i32, i32* %4
%10 = load i16*, i16** %5
%11 = bitcast i16* %10 to i8*
%12 = load i32, i32* %6
%13 = sext i32 %12 to i64
%14 = mul i64 %13, 2
%15 = trunc i64 %14 to i32
%16 = call i32 @read(i32 %9, i8* %11, i32 %15)
store i32 %16, i32* %7
br label %17
17: ; preds = %8
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br label %24
24: ; preds = %20, %17
%25 = phi i1 [ false, %17 ], [ %23, %20 ]
br i1 %25, label %8, label %26
26: ; preds = %24
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, -1
br i1 %28, label %34, label %29
29: ; preds = %26
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = urem i64 %31, 2
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %29, %26
%35 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %29
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = udiv i64 %39, 2
%41 = trunc i64 %40 to i32
ret i32 %41
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [20 x i8] c"Error reading input\00"
define i32 @ReadN(i32 %0, i16* nocapture %1, i32 %2) {
%4 = bitcast i16* %1 to i8*
%5 = shl i32 %2, 1
br label %6
6: ; preds = %6, %3
%7 = tail call i32 @read(i32 %0, i8* %4, i32 %5)
%8 = icmp eq i32 %7, -1
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp eq i64 %9, %10
%12 = select i1 %8, i1 %11, i1 false
br i1 %12, label %6, label %13
13: ; preds = %6
%14 = and i32 %7, 1
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32 @exit(i32 -1)
unreachable
19: ; preds = %13
%20 = ashr i32 %7, 1
ret i32 %20
}