#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 */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int fscanf(int /*<<< orphan*/ *, char *, float *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int fvecs_read_txt(const char *fname, int d, int n, float *v) {
  int i, ret;
  FILE *f = fopen(fname, "r");
  if (!f) {
    fprintf(stderr, "fvecs_read_txt: could not open %s\n", fname);
    perror("");
    return -1;
  }
  for (i = 0; i < n * d; i++) {
    ret = fscanf(f, "%f", v + i);
    if (ret != 1) {
      if (feof(f))
        break;
      else {
        perror("fvecs_read_txt error 1");
        return -1;
      }
    }
  }
  fclose(f);
  return i / d;
}


x86 assembly (clang, -O0):
<fvecs_read_txt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe7f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c6 <fvecs_read_txt+0x66>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe59(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe6f(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <fvecs_read_txt+0x101>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x18(%rbp),%ecx
imul   -0x14(%rbp),%ecx
cmp    %ecx,%eax
jge    124e <fvecs_read_txt+0xee>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe2c(%rip),%rsi
call   1040 <fscanf@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x1,-0x28(%rbp)
je     123b <fvecs_read_txt+0xdb>
mov    -0x30(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     1223 <fvecs_read_txt+0xc3>
jmp    124e <fvecs_read_txt+0xee>
lea    0xdfe(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <fvecs_read_txt+0x101>
jmp    1240 <fvecs_read_txt+0xe0>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11cd <fvecs_read_txt+0x6d>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x24(%rbp),%eax
cltd
idivl  -0x14(%rbp)
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fvecs_read_txt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbp
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%r12
lea    0xe83(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1208 <fvecs_read_txt+0xa8>
mov    %rax,%r13
imul   %ebx,%r15d
test   %r15d,%r15d
jle    11d2 <fvecs_read_txt+0x72>
mov    %ebx,0x4(%rsp)
mov    %r15d,%r14d
xor    %ebx,%ebx
lea    0xe7e(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jne    11d7 <fvecs_read_txt+0x77>
add    $0x1,%rbx
add    $0x4,%rbp
cmp    %rbx,%r14
jne    11b0 <fvecs_read_txt+0x50>
jmp    11e7 <fvecs_read_txt+0x87>
xor    %r15d,%r15d
jmp    11eb <fvecs_read_txt+0x8b>
mov    %r13,%rdi
call   1060 <feof@plt>
test   %rax,%rax
je     1229 <fvecs_read_txt+0xc9>
mov    %ebx,%r15d
mov    0x4(%rsp),%ebx
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    %r15d,%eax
cltd
idiv   %ebx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
mov    %r12,%rdx
call   1050 <fprintf@plt>
lea    0xdfd(%rip),%rdi
jmp    1230 <fvecs_read_txt+0xd0>
lea    0xdf8(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    11f9 <fvecs_read_txt+0x99>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"fvecs_read_txt: could not open %s\0A\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [3 x i8] c"%f\00"
@.str.4 = [23 x i8] c"fvecs_read_txt error 1\00"
define i32 @fvecs_read_txt(i8* %0, i32 %1, i32 %2, float* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca float*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store float* %3, float** %9
%13 = load i8*, i8** %6
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %12
%15 = load i32*, i32** %12
%16 = icmp ne i32* %15, null
br i1 %16, label %22, label %17
17: ; preds = %4
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %6
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %5
br label %55
22: ; preds = %4
store i32 0, i32* %10
br label %23
23: ; preds = %46, %22
%24 = load i32, i32* %10
%25 = load i32, i32* %8
%26 = load i32, i32* %7
%27 = mul nsw i32 %25, %26
%28 = icmp slt i32 %24, %27
br i1 %28, label %29, label %49
29: ; preds = %23
%30 = load i32*, i32** %12
%31 = load float*, float** %9
%32 = load i32, i32* %10
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds float, float* %31, i64 %33
%35 = call i32 @fscanf(i32* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), float* %34)
store i32 %35, i32* %11
%36 = load i32, i32* %11
%37 = icmp ne i32 %36, 1
br i1 %37, label %38, label %45
38: ; preds = %29
%39 = load i32*, i32** %12
%40 = call i64 @feof(i32* %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %38
br label %49
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %5
br label %55
45: ; preds = %29
br label %46
46: ; preds = %45
%47 = load i32, i32* %10
%48 = add nsw i32 %47, 1
store i32 %48, i32* %10
br label %23
49: ; preds = %42, %23
%50 = load i32*, i32** %12
%51 = call i32 @fclose(i32* %50)
%52 = load i32, i32* %10
%53 = load i32, i32* %7
%54 = sdiv i32 %52, %53
store i32 %54, i32* %5
br label %55
55: ; preds = %49, %43, %17
%56 = load i32, i32* %5
ret i32 %56
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"fvecs_read_txt: could not open %s\0A\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [3 x i8] c"%f\00"
@.str.4 = [23 x i8] c"fvecs_read_txt error 1\00"
define i32 @fvecs_read_txt(i8* %0, i32 %1, i32 %2, float* %3) {
%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 %12, label %7
7: ; preds = %4
%8 = mul i32 %2, %1
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %30
10: ; preds = %7
%11 = zext i32 %8 to i64
br label %16
12: ; preds = %4
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%15 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %34
16: ; preds = %10, %27
%17 = phi i64 [ 0, %10 ], [ %28, %27 ]
%18 = getelementptr inbounds float, float* %3, i64 %17
%19 = tail call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), float* %18)
%20 = icmp eq i32 %19, 1
br i1 %20, label %27, label %21
21: ; preds = %16
%22 = trunc i64 %17 to i32
%23 = tail call i64 @feof(i32* %5)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %34
27: ; preds = %16
%28 = add nuw nsw i64 %17, 1
%29 = icmp eq i64 %28, %11
br i1 %29, label %30, label %16
30: ; preds = %27, %7, %21
%31 = phi i32 [ %22, %21 ], [ 0, %7 ], [ %8, %27 ]
%32 = tail call i32 @fclose(i32* %5)
%33 = sdiv i32 %31, %1
br label %34
34: ; preds = %30, %25, %12
%35 = phi i32 [ %33, %30 ], [ -1, %25 ], [ -1, %12 ]
ret i32 %35
}