#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 unsigned int uint16_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, unsigned int *);
int /*<<< orphan*/ yap_sysquit(char *, char *, ...);
void read_u16vec(char *dfile, int N, uint16_t *d) {
  int i;
  FILE *fp = fopen(dfile, "r");
  if (!fp)
    yap_sysquit("Cannot open file '%s' for read\n", dfile);
  for (i = 0; i < N; i++) {
    unsigned u;
    if (!fscanf(fp, " %u", &u)) {
      yap_sysquit("Cannot read from '%s' position %d\n", dfile, i);
    }
    d[i] = u;
  }
  if (ferror(fp))
    yap_sysquit("Error on reading file '%s' ", dfile);
  fclose(fp);
}


x86 assembly (clang, -O0):
<read_u16vec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1194 <read_u16vec+0x44>
mov    -0x8(%rbp),%rsi
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1070 <yap_sysquit@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f5 <read_u16vec+0xa5>
mov    -0x28(%rbp),%rdi
lea    0xe70(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1050 <fscanf@plt>
cmp    $0x0,%eax
jne    11d9 <read_u16vec+0x89>
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1070 <yap_sysquit@plt>
mov    -0x2c(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119b <read_u16vec+0x4b>
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     121a <read_u16vec+0xca>
mov    -0x8(%rbp),%rsi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1070 <yap_sysquit@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_u16vec>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %esi,%ebp
lea    0xe96(%rip),%rsi
mov    %rdi,0x8(%rsp)
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1182 <read_u16vec+0x32>
test   %ebp,%ebp
jg     1199 <read_u16vec+0x49>
jmp    11ea <read_u16vec+0x9a>
lea    0xe79(%rip),%rdi
mov    0x8(%rsp),%rsi
xor    %eax,%eax
call   1070 <yap_sysquit@plt>
test   %ebp,%ebp
jle    11ea <read_u16vec+0x9a>
mov    %ebp,%r14d
lea    0xe7f(%rip),%r12
lea    0x14(%rsp),%r13
xor    %ebp,%ebp
jmp    11c1 <read_u16vec+0x71>
nopl   0x0(%rax)
mov    0x14(%rsp),%eax
mov    %eax,(%r15,%rbp,4)
add    $0x1,%rbp
cmp    %rbp,%r14
je     11ea <read_u16vec+0x9a>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1050 <fscanf@plt>
test   %eax,%eax
jne    11b0 <read_u16vec+0x60>
lea    0xe4c(%rip),%rdi
mov    0x8(%rsp),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <yap_sysquit@plt>
jmp    11b0 <read_u16vec+0x60>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     120a <read_u16vec+0xba>
lea    0xe4b(%rip),%rdi
mov    0x8(%rsp),%rsi
xor    %eax,%eax
call   1070 <yap_sysquit@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [32 x i8] c"Cannot open file '%s' for read\0A\00"
@.str.2 = [4 x i8] c" %u\00"
@.str.3 = [35 x i8] c"Cannot read from '%s' position %d\0A\00"
@.str.4 = [28 x i8] c"Error on reading file '%s' \00"
define void @read_u16vec(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp ne i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = call i32 (i8*, i8*, ...) @yap_sysquit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %14, %3
store i32 0, i32* %7
br label %18
18: ; preds = %36, %17
%19 = load i32, i32* %7
%20 = load i32, i32* %5
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %39
22: ; preds = %18
%23 = load i32*, i32** %8
%24 = call i32 @fscanf(i32* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %9)
%25 = icmp ne i32 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = load i8*, i8** %4
%28 = load i32, i32* %7
%29 = call i32 (i8*, i8*, ...) @yap_sysquit(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %27, i32 %28)
br label %30
30: ; preds = %26, %22
%31 = load i32, i32* %9
%32 = load i32*, i32** %6
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %32, i64 %34
store i32 %31, i32* %35
br label %36
36: ; preds = %30
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
br label %18
39: ; preds = %18
%40 = load i32*, i32** %8
%41 = call i64 @ferror(i32* %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i8*, i8** %4
%45 = call i32 (i8*, i8*, ...) @yap_sysquit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %43, %39
%47 = load i32*, i32** %8
%48 = call i32 @fclose(i32* %47)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [32 x i8] c"Cannot open file '%s' for read\0A\00"
@.str.2 = [4 x i8] c" %u\00"
@.str.3 = [35 x i8] c"Cannot read from '%s' position %d\0A\00"
@.str.4 = [28 x i8] c"Error on reading file '%s' \00"
define void @read_u16vec(i8* %0, i32 %1, i32* nocapture writeonly %2) {
%4 = alloca i32
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 (i8*, i8*, ...) @yap_sysquit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %9
9: ; preds = %7, %3
%10 = icmp sgt i32 %1, 0
br i1 %10, label %11, label %26
11: ; preds = %9
%12 = bitcast i32* %4 to i8*
%13 = zext i32 %1 to i64
br label %14
14: ; preds = %11, %21
%15 = phi i64 [ 0, %11 ], [ %24, %21 ]
%16 = call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = trunc i64 %15 to i32
%20 = call i32 (i8*, i8*, ...) @yap_sysquit(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %19)
br label %21
21: ; preds = %18, %14
%22 = load i32, i32* %4
%23 = getelementptr inbounds i32, i32* %2, i64 %15
store i32 %22, i32* %23
%24 = add nuw nsw i64 %15, 1
%25 = icmp eq i64 %24, %13
br i1 %25, label %26, label %14
26: ; preds = %21, %9
%27 = call i64 @ferror(i32* %5)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = call i32 (i8*, i8*, ...) @yap_sysquit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %31
31: ; preds = %29, %26
%32 = call i32 @fclose(i32* %5)
ret void
}