#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*/ fwrite(double *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
void save_d(double *data, char *file, int N, int M) {
  int i = 0, j = 0;
  FILE *outfile;
  printf("Saving data to file: %s\n", file);
  if ((outfile = fopen(file, "wb")) == NULL) {
    printf("Can't open output file");
  }
  fwrite(data, sizeof(double), N * M, outfile);
  fclose(outfile);
}


x86 assembly (clang, -O0):
<save_d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe98(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11a2 <save_d+0x62>
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
imul   -0x18(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x8,%esi
call   1060 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_d>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0xea7(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeb2(%rip),%rsi
mov    %rbp,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1185 <save_d+0x45>
lea    0xe9e(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
imul   %r15d,%ebx
mov    %r14,%rdi
mov    $0x8,%esi
mov    %ebx,%edx
mov    %rbp,%rcx
call   1060 <fwrite@plt>
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"Saving data to file: %s\0A\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [23 x i8] c"Can't open output file\00"
define void @save_d(double* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca double*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
store double* %0, double** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
%12 = load i8*, i8** %6
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i8*, i8** %6
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %11
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %4
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %4
%20 = load double*, double** %5
%21 = load i32, i32* %7
%22 = load i32, i32* %8
%23 = mul nsw i32 %21, %22
%24 = load i32*, i32** %11
%25 = call i32 @fwrite(double* %20, i32 8, i32 %23, i32* %24)
%26 = load i32*, i32** %11
%27 = call i32 @fclose(i32* %26)
ret void
}

LLVM IR (-O2):
@.str = [25 x i8] c"Saving data to file: %s\0A\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [23 x i8] c"Can't open output file\00"
define void @save_d(double* nocapture %0, i8* %1, i32 %2, i32 %3) {
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %1)
%6 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %10
10: ; preds = %8, %4
%11 = mul nsw i32 %3, %2
%12 = tail call i32 @fwrite(double* %0, i32 8, i32 %11, i32* %6)
%13 = tail call i32 @fclose(i32* %6)
ret void
}