#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*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, double);
void save_as_dat(char *fname, double *buff, int size) {
  int i;
  FILE *fd;
  fd = fopen(fname, "w");
  for (i = 0; i < size; i++) {
    fprintf(fd, "%d %f\n", i, buff[i]);
  }
  fclose(fd);
}


x86 assembly (clang, -O0):
<save_as_dat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeb2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1198 <save_as_dat+0x68>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xe7d(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    115e <save_as_dat+0x2e>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_as_dat>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r14
lea    0xebb(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r15
test   %ebx,%ebx
jle    117c <save_as_dat+0x4c>
mov    %ebx,%r13d
xor    %ebx,%ebx
lea    0xea5(%rip),%r12
nopl   (%rax)
movsd  (%r14,%rbx,8),%xmm0
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1040 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1160 <save_as_dat+0x30>
mov    %r15,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [7 x i8] c"%d %f\0A\00"
define void @save_as_dat(i8* %0, double* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca double*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %4
store double* %1, double** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %8
store i32 0, i32* %7
br label %11
11: ; preds = %24, %3
%12 = load i32, i32* %7
%13 = load i32, i32* %6
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %27
15: ; preds = %11
%16 = load i32*, i32** %8
%17 = load i32, i32* %7
%18 = load double*, double** %5
%19 = load i32, i32* %7
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds double, double* %18, i64 %20
%22 = load double, double* %21
%23 = call i32 @fprintf(i32* %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %17, double %22)
br label %24
24: ; preds = %15
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
store i32 %26, i32* %7
br label %11
27: ; preds = %11
%28 = load i32*, i32** %8
%29 = call i32 @fclose(i32* %28)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [7 x i8] c"%d %f\0A\00"
define void @save_as_dat(i8* nocapture %0, double* nocapture %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 sgt i32 %2, 0
br i1 %5, label %6, label %16
6: ; preds = %3
%7 = zext i32 %2 to i64
br label %8
8: ; preds = %6, %8
%9 = phi i64 [ 0, %6 ], [ %14, %8 ]
%10 = getelementptr inbounds double, double* %1, i64 %9
%11 = load double, double* %10
%12 = trunc i64 %9 to i32
%13 = tail call i32 @fprintf(i32* %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %12, double %11)
%14 = add nuw nsw i64 %9, 1
%15 = icmp eq i64 %14, %7
br i1 %15, label %16, label %8
16: ; preds = %8, %3
%17 = tail call i32 @fclose(i32* %4)
ret void
}