#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*/ *fopen(char const *, char *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ perror(char *);
int fvecs_write_txt(const char *fname, int d, int n, const float *vf) {
  int i, j, ret = 0;
  FILE *fo = fopen(fname, "w");
  if (!fo) {
    perror("fvecs_write_txt: cannot open file");
    return -1;
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < d; j++)
      fprintf(fo, "%f ", vf[i * d + j]);
    ret += fprintf(fo, "\n");
  }
  return ret;
}


x86 assembly (clang, -O0):
<fvecs_write_txt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea8(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1184 <fvecs_write_txt+0x54>
lea    0xe8f(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <fvecs_write_txt+0xe7>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1211 <fvecs_write_txt+0xe1>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11e4 <fvecs_write_txt+0xb4>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
imul   -0x14(%rbp),%ecx
add    -0x28(%rbp),%ecx
movslq %ecx,%rcx
movss  (%rax,%rcx,4),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xe55(%rip),%rsi
mov    $0x1,%al
call   1030 <fprintf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    119e <fvecs_write_txt+0x6e>
mov    -0x38(%rbp),%rdi
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    %rax,%rcx
movslq -0x2c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    118b <fvecs_write_txt+0x5b>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fvecs_write_txt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbx
mov    %edx,%r15d
mov    %esi,(%rsp)
lea    0xeb2(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     1203 <fvecs_write_txt+0xd3>
test   %r15d,%r15d
jle    11f2 <fvecs_write_txt+0xc2>
mov    %rax,%rbp
movslq (%rsp),%rax
mov    %r15d,%ecx
mov    %rcx,0x8(%rsp)
mov    %eax,%r14d
shl    $0x2,%rax
mov    %rax,0x10(%rsp)
lea    0xe9d(%rip),%r12
xor    %r13d,%r13d
xor    %eax,%eax
jmp    11b9 <fvecs_write_txt+0x89>
xchg   %ax,%ax
mov    %rbp,%rdi
lea    0xe8e(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x4(%rsp),%ecx
add    %eax,%ecx
mov    %ecx,%eax
add    $0x1,%r13
add    0x10(%rsp),%rbx
cmp    0x8(%rsp),%r13
je     11f4 <fvecs_write_txt+0xc4>
mov    %eax,0x4(%rsp)
cmpl   $0x0,(%rsp)
jle    1190 <fvecs_write_txt+0x60>
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
movss  (%rbx,%r15,4),%xmm0
cvtss2sd %xmm0,%xmm0
mov    %rbp,%rdi
mov    %r12,%rsi
mov    $0x1,%al
call   1030 <fprintf@plt>
add    $0x1,%r15
cmp    %r15,%r14
jne    11d0 <fvecs_write_txt+0xa0>
jmp    1190 <fvecs_write_txt+0x60>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf8(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    11f4 <fvecs_write_txt+0xc4>

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

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [34 x i8] c"fvecs_write_txt: cannot open file\00"
@.str.2 = [4 x i8] c"%f \00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @fvecs_write_txt(i8* nocapture %0, i32 %1, i32 %2, float* nocapture %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 %14, label %7
7: ; preds = %4
%8 = icmp sgt i32 %2, 0
br i1 %8, label %9, label %36
9: ; preds = %7
%10 = icmp sgt i32 %1, 0
%11 = sext i32 %1 to i64
%12 = zext i32 %2 to i64
%13 = zext i32 %1 to i64
br label %16
14: ; preds = %4
%15 = tail call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %36
16: ; preds = %9, %30
%17 = phi i64 [ 0, %9 ], [ %34, %30 ]
%18 = phi i32 [ 0, %9 ], [ %33, %30 ]
br i1 %10, label %19, label %30
19: ; preds = %16
%20 = mul nsw i64 %17, %11
br label %21
21: ; preds = %19, %21
%22 = phi i64 [ 0, %19 ], [ %28, %21 ]
%23 = add nsw i64 %22, %20
%24 = getelementptr inbounds float, float* %3, i64 %23
%25 = load float, float* %24
%26 = fpext float %25 to double
%27 = tail call i64 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), double %26)
%28 = add nuw nsw i64 %22, 1
%29 = icmp eq i64 %28, %13
br i1 %29, label %30, label %21
30: ; preds = %21, %16
%31 = tail call i64 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%32 = trunc i64 %31 to i32
%33 = add i32 %18, %32
%34 = add nuw nsw i64 %17, 1
%35 = icmp eq i64 %34, %12
br i1 %35, label %36, label %16
36: ; preds = %30, %7, %14
%37 = phi i32 [ -1, %14 ], [ 0, %7 ], [ %33, %30 ]
ret i32 %37
}