#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__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, double);
int /*<<< orphan*/ yap_sysquit(char *, char *, ...);
void write_dvec(char *dfile, int N, double *d) {
  int i;
  FILE *fp = fopen(dfile, "w");
  if (!fp)
    yap_sysquit("Cannot open file '%s' for write\n", dfile);
  for (i = 0; i < N; i++) {
    if (!fprintf(fp, "%lg\n", d[i])) {
      yap_sysquit("Cannot write to '%s' position %d\n", dfile, i);
    }
  }
  if (ferror(fp))
    yap_sysquit("Error on writing file '%s' ", dfile);
  fclose(fp);
}


x86 assembly (clang, -O0):
<write_dvec>:
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 <write_dvec+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 <write_dvec+0xa5>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xe64(%rip),%rsi
call   1050 <fprintf@plt>
cmp    $0x0,%eax
jne    11e2 <write_dvec+0x92>
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1070 <yap_sysquit@plt>
jmp    11e7 <write_dvec+0x97>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119b <write_dvec+0x4b>
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     121a <write_dvec+0xca>
mov    -0x8(%rbp),%rsi
lea    0xe37(%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):
<write_dvec>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xe96(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     117d <write_dvec+0x2d>
test   %ebp,%ebp
jg     1192 <write_dvec+0x42>
jmp    11d3 <write_dvec+0x83>
lea    0xe7e(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <yap_sysquit@plt>
test   %ebp,%ebp
jle    11d3 <write_dvec+0x83>
mov    %ebp,%r13d
lea    0xe87(%rip),%r12
xor    %ebp,%ebp
jmp    11a9 <write_dvec+0x59>
add    $0x1,%rbp
cmp    %rbp,%r13
je     11d3 <write_dvec+0x83>
movsd  (%r15,%rbp,8),%xmm0
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <fprintf@plt>
test   %eax,%eax
jne    11a0 <write_dvec+0x50>
lea    0xe63(%rip),%rdi
mov    %r14,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <yap_sysquit@plt>
jmp    11a0 <write_dvec+0x50>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11f1 <write_dvec+0xa1>
lea    0xe63(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <yap_sysquit@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>

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

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [33 x i8] c"Cannot open file '%s' for write\0A\00"
@.str.2 = [5 x i8] c"%lg\0A\00"
@.str.3 = [34 x i8] c"Cannot write to '%s' position %d\0A\00"
@.str.4 = [28 x i8] c"Error on writing file '%s' \00"
define void @write_dvec(i8* %0, i32 %1, double* nocapture %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 (i8*, i8*, ...) @yap_sysquit(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %8
8: ; preds = %6, %3
%9 = icmp sgt i32 %1, 0
br i1 %9, label %10, label %24
10: ; preds = %8
%11 = zext i32 %1 to i64
br label %12
12: ; preds = %10, %21
%13 = phi i64 [ 0, %10 ], [ %22, %21 ]
%14 = getelementptr inbounds double, double* %2, i64 %13
%15 = load double, double* %14
%16 = tail call i32 @fprintf(i32* %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), double %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = trunc i64 %13 to i32
%20 = tail call i32 (i8*, i8*, ...) @yap_sysquit(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %19)
br label %21
21: ; preds = %12, %18
%22 = add nuw nsw i64 %13, 1
%23 = icmp eq i64 %22, %11
br i1 %23, label %24, label %12
24: ; preds = %21, %8
%25 = tail call i64 @ferror(i32* %4)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = tail 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 %29
29: ; preds = %27, %24
%30 = tail call i32 @fclose(i32* %4)
ret void
}