#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 timespec {
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
scalar_t__ calloc(int, int);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, scalar_t__);
int /*<<< orphan*/ write(int, double *, int);
int main(int argc, char *argv[]) {
  char *file1, *file2;
  if (argc != 3) {
    file1 = "testfile1";
    file2 = "testfile2";
  } else {
    file1 = argv[1];
    file2 = argv[2];
  }
  int fd1 = open(file1, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
  int fd2 = open(file2, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
  double *data = (double *)calloc(1000 * 1000 * 100, sizeof(double));
  struct timespec t1, t2, t3;
  clock_gettime(CLOCK_MONOTONIC, &t1);
  for (int i = 0; i < 1000 * 1000 * 100; i++) {
    write(fd1, data + i, sizeof(double) * 1);
  }
  clock_gettime(CLOCK_MONOTONIC, &t2);
  write(fd2, data, sizeof(double) * 1000 * 1000 * 100);
  clock_gettime(CLOCK_MONOTONIC, &t3);
  printf("Writing one element at a time took %6li seconds\n",
         t2.tv_sec - t1.tv_sec);
  printf("Writing all elements at once took  %6li seconds\n",
         t3.tv_sec - t2.tv_sec);
  close(fd1);
  close(fd2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     119b <main+0x3b>
lea    0xe79(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe78(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11b3 <main+0x53>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    0x2e29(%rip),%rax
or     (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
mov    0x2dee(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    0x2df0(%rip),%rax
or     (%rax),%esi
mov    0x2def(%rip),%rax
or     (%rax),%esi
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
mov    0x2db5(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
mov    $0x5f5e100,%edi
mov    $0x8,%esi
call   1070 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
call   1030 <clock_gettime@plt>
movl   $0x0,-0x4c(%rbp)
cmpl   $0x5f5e100,-0x4c(%rbp)
jge    1288 <main+0x128>
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
movslq -0x4c(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    1251 <main+0xf1>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x28(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    $0x2faf0800,%edx
call   1040 <write@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
lea    -0x48(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x40(%rbp),%rsi
sub    -0x38(%rbp),%rsi
lea    0xd48(%rip),%rdi
call   1050 <printf@plt>
mov    -0x48(%rbp),%rsi
sub    -0x40(%rbp),%rsi
lea    0xd65(%rip),%rdi
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x3,%edi
jne    117d <main+0x1d>
mov    0x8(%rsi),%rdi
mov    0x10(%rsi),%rax
jmp    118b <main+0x2b>
lea    0xe7c(%rip),%rdi
lea    0xe7f(%rip),%rax
mov    %rax,0x8(%rsp)
mov    0x2e19(%rip),%r15
mov    0x2e52(%rip),%rbp
mov    0x0(%rbp),%esi
or     (%r15),%esi
mov    0x2e4d(%rip),%rbx
or     (%rbx),%esi
mov    0x2e0c(%rip),%r13
mov    0x2e15(%rip),%r14
mov    (%r14),%edx
or     0x0(%r13),%edx
call   1080 <open@plt>
mov    0x0(%rbp),%esi
or     (%r15),%esi
or     (%rbx),%esi
mov    %eax,%r12d
mov    (%r14),%edx
or     0x0(%r13),%edx
mov    0x8(%rsp),%rdi
call   1080 <open@plt>
mov    %eax,%r14d
mov    $0x5f5e100,%edi
mov    $0x8,%esi
call   1070 <calloc@plt>
mov    %rax,%rbp
mov    0x2dd9(%rip),%r15
mov    (%r15),%edi
lea    0x20(%rsp),%rsi
call   1030 <clock_gettime@plt>
xor    %ebx,%ebx
xchg   %ax,%ax
lea    (%rbx,%rbp,1),%rsi
mov    %r12d,%edi
mov    $0x8,%edx
call   1040 <write@plt>
add    $0x8,%rbx
cmp    $0x2faf0800,%rbx
jne    1210 <main+0xb0>
mov    (%r15),%edi
lea    0x10(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x2faf0800,%edx
call   1040 <write@plt>
mov    (%r15),%edi
lea    0x18(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    0x10(%rsp),%rsi
sub    0x20(%rsp),%rsi
lea    0xdab(%rip),%rdi
call   1050 <printf@plt>
mov    0x18(%rsp),%rsi
sub    0x10(%rsp),%rsi
lea    0xdc6(%rip),%rdi
call   1050 <printf@plt>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64 }
@.str = [10 x i8] c"testfile1\00"
@.str.1 = [10 x i8] c"testfile2\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@CLOCK_MONOTONIC = global i32 0
@.str.2 = [49 x i8] c"Writing one element at a time took %6li seconds\0A\00"
@.str.3 = [49 x i8] c"Writing all elements at once took %6li seconds\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca double*
%11 = alloca %struct.timespec
%12 = alloca %struct.timespec
%13 = alloca %struct.timespec
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 3
br i1 %16, label %17, label %18
17: ; preds = %2
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %6
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %7
br label %25
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %6
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
store i8* %24, i8** %7
br label %25
25: ; preds = %18, %17
%26 = load i8*, i8** %6
%27 = load i32, i32* @O_WRONLY
%28 = load i32, i32* @O_TRUNC
%29 = or i32 %27, %28
%30 = load i32, i32* @O_CREAT
%31 = or i32 %29, %30
%32 = load i32, i32* @S_IRUSR
%33 = load i32, i32* @S_IWUSR
%34 = or i32 %32, %33
%35 = call i32 @open(i8* %26, i32 %31, i32 %34)
store i32 %35, i32* %8
%36 = load i8*, i8** %7
%37 = load i32, i32* @O_WRONLY
%38 = load i32, i32* @O_TRUNC
%39 = or i32 %37, %38
%40 = load i32, i32* @O_CREAT
%41 = or i32 %39, %40
%42 = load i32, i32* @S_IRUSR
%43 = load i32, i32* @S_IWUSR
%44 = or i32 %42, %43
%45 = call i32 @open(i8* %36, i32 %41, i32 %44)
store i32 %45, i32* %9
%46 = call i64 @calloc(i32 100000000, i32 8)
%47 = inttoptr i64 %46 to double*
store double* %47, double** %10
%48 = load i32, i32* @CLOCK_MONOTONIC
%49 = call i32 @clock_gettime(i32 %48, %struct.timespec* %11)
store i32 0, i32* %14
br label %50
50: ; preds = %60, %25
%51 = load i32, i32* %14
%52 = icmp slt i32 %51, 100000000
br i1 %52, label %53, label %63
53: ; preds = %50
%54 = load i32, i32* %8
%55 = load double*, double** %10
%56 = load i32, i32* %14
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds double, double* %55, i64 %57
%59 = call i32 @write(i32 %54, double* %58, i32 8)
br label %60
60: ; preds = %53
%61 = load i32, i32* %14
%62 = add nsw i32 %61, 1
store i32 %62, i32* %14
br label %50
63: ; preds = %50
%64 = load i32, i32* @CLOCK_MONOTONIC
%65 = call i32 @clock_gettime(i32 %64, %struct.timespec* %12)
%66 = load i32, i32* %9
%67 = load double*, double** %10
%68 = call i32 @write(i32 %66, double* %67, i32 800000000)
%69 = load i32, i32* @CLOCK_MONOTONIC
%70 = call i32 @clock_gettime(i32 %69, %struct.timespec* %13)
%71 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 0
%72 = load i64, i64* %71
%73 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 0
%74 = load i64, i64* %73
%75 = sub nsw i64 %72, %74
%76 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i64 %75)
%77 = getelementptr inbounds %struct.timespec, %struct.timespec* %13, i32 0, i32 0
%78 = load i64, i64* %77
%79 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 0
%80 = load i64, i64* %79
%81 = sub nsw i64 %78, %80
%82 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i64 %81)
%83 = load i32, i32* %8
%84 = call i32 @close(i32 %83)
%85 = load i32, i32* %9
%86 = call i32 @close(i32 %85)
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i64 }
@.str = [10 x i8] c"testfile1\00"
@.str.1 = [10 x i8] c"testfile2\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@CLOCK_MONOTONIC = global i32 0
@.str.2 = [49 x i8] c"Writing one element at a time took %6li seconds\0A\00"
@.str.3 = [49 x i8] c"Writing all elements at once took %6li seconds\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = icmp eq i32 %0, 3
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
br label %12
12: ; preds = %2, %7
%13 = phi i8* [ %11, %7 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %2 ]
%14 = phi i8* [ %9, %7 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %2 ]
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %16, %15
%18 = load i32, i32* @O_CREAT
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IRUSR
%21 = load i32, i32* @S_IWUSR
%22 = or i32 %21, %20
%23 = tail call i32 @open(i8* %14, i32 %19, i32 %22)
%24 = load i32, i32* @O_WRONLY
%25 = load i32, i32* @O_TRUNC
%26 = or i32 %25, %24
%27 = load i32, i32* @O_CREAT
%28 = or i32 %26, %27
%29 = load i32, i32* @S_IRUSR
%30 = load i32, i32* @S_IWUSR
%31 = or i32 %30, %29
%32 = tail call i32 @open(i8* %13, i32 %28, i32 %31)
%33 = tail call i64 @calloc(i32 100000000, i32 8)
%34 = inttoptr i64 %33 to double*
%35 = bitcast %struct.timespec* %3 to i8*
%36 = bitcast %struct.timespec* %4 to i8*
%37 = bitcast %struct.timespec* %5 to i8*
%38 = load i32, i32* @CLOCK_MONOTONIC
%39 = call i32 @clock_gettime(i32 %38, %struct.timespec* %3)
br label %59
40: ; preds = %59
%41 = load i32, i32* @CLOCK_MONOTONIC
%42 = call i32 @clock_gettime(i32 %41, %struct.timespec* %4)
%43 = call i32 @write(i32 %32, double* %34, i32 800000000)
%44 = load i32, i32* @CLOCK_MONOTONIC
%45 = call i32 @clock_gettime(i32 %44, %struct.timespec* %5)
%46 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%47 = load i64, i64* %46
%48 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%49 = load i64, i64* %48
%50 = sub nsw i64 %47, %49
%51 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i64 %50)
%52 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
%53 = load i64, i64* %52
%54 = load i64, i64* %46
%55 = sub nsw i64 %53, %54
%56 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i64 %55)
%57 = call i32 @close(i32 %23)
%58 = call i32 @close(i32 %32)
ret i32 0
59: ; preds = %12, %59
%60 = phi i64 [ 0, %12 ], [ %63, %59 ]
%61 = getelementptr inbounds double, double* %34, i64 %60
%62 = call i32 @write(i32 %23, double* %61, i32 8)
%63 = add nuw nsw i64 %60, 1
%64 = icmp eq i64 %63, 100000000
br i1 %64, label %40, label %59
}