#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 */
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fwrite(double *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
void write_data(double *buff, char *path, int size) {
  FILE *fpw;
  double tmp;
  struct stat filestat;
  int ended;
  if ((fpw = fopen(path, "w")) == NULL) {
    printf("file open error\n");
    exit(1);
  }
  ended = 0;
  while (1) {
    ended += fwrite(buff + ended, 1, sizeof(double) * size - ended, fpw);
    if (ended >= sizeof(double) * size)
      break;
  }
  fclose(fpw);
}


x86 assembly (clang, -O0):
<write_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1197 <write_data+0x47>
lea    0xe7a(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0x34(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
movslq -0x34(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %rax,%rcx
movslq -0x34(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x34(%rbp)
movslq -0x34(%rbp),%rax
movslq -0x14(%rbp),%rcx
shl    $0x3,%rcx
cmp    %rcx,%rax
jb     11f3 <write_data+0xa3>
jmp    11f8 <write_data+0xa8>
jmp    119e <write_data+0x4e>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_data>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rax
mov    %rdi,%r14
lea    0xe9a(%rip),%rsi
mov    %rax,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11b5 <write_data+0x65>
mov    %rax,%r15
movslq %ebp,%rbp
shl    $0x3,%rbp
xor    %eax,%eax
nop
movslq %eax,%rbx
lea    (%r14,%rbx,8),%rdi
mov    %ebp,%edx
sub    %ebx,%edx
mov    $0x1,%esi
mov    %r15,%rcx
call   1070 <fwrite@plt>
add    %rbx,%rax
movslq %eax,%rcx
cmp    %rbp,%rcx
jb     1180 <write_data+0x30>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>
lea    0xe46(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"w\00"
@.str.1 = [17 x i8] c"file open error\0A\00"
define void @write_data(double* %0, i8* %1, i32 %2) {
%4 = alloca double*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca double
%9 = alloca %struct.stat
%10 = alloca i32
store double* %0, double** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i8*, i8** %5
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %7
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %3
store i32 0, i32* %10
br label %18
18: ; preds = %17, %43
%19 = load double*, double** %4
%20 = load i32, i32* %10
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds double, double* %19, i64 %21
%23 = load i32, i32* %6
%24 = sext i32 %23 to i64
%25 = mul i64 8, %24
%26 = load i32, i32* %10
%27 = sext i32 %26 to i64
%28 = sub i64 %25, %27
%29 = trunc i64 %28 to i32
%30 = load i32*, i32** %7
%31 = call i64 @fwrite(double* %22, i32 1, i32 %29, i32* %30)
%32 = load i32, i32* %10
%33 = sext i32 %32 to i64
%34 = add nsw i64 %33, %31
%35 = trunc i64 %34 to i32
store i32 %35, i32* %10
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = mul i64 8, %39
%41 = icmp uge i64 %37, %40
br i1 %41, label %42, label %43
42: ; preds = %18
br label %44
43: ; preds = %18
br label %18
44: ; preds = %42
%45 = load i32*, i32** %7
%46 = call i32 @fclose(i32* %45)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@str = [16 x i8] c"file open error\00"
define void @write_data(double* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %9, label %6
6: ; preds = %3
%7 = sext i32 %2 to i64
%8 = shl nsw i64 %7, 3
br label %12
9: ; preds = %3
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %6, %12
%13 = phi i64 [ %20, %12 ], [ 0, %6 ]
%14 = shl i64 %13, 32
%15 = ashr exact i64 %14, 32
%16 = getelementptr inbounds double, double* %0, i64 %15
%17 = sub i64 %8, %13
%18 = trunc i64 %17 to i32
%19 = tail call i64 @fwrite(double* %16, i32 1, i32 %18, i32* %4)
%20 = add nsw i64 %15, %19
%21 = shl i64 %20, 32
%22 = ashr exact i64 %21, 32
%23 = icmp ult i64 %22, %8
br i1 %23, label %12, label %24
24: ; preds = %12
%25 = tail call i32 @fclose(i32* %4)
ret void
}