#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 */
double atof(char const *);
int /*<<< orphan*/ double_buffersize(int *, void **);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
char *strtok(char *, char *);
double *read_csv_doubles(FILE *handle, int *valuesRead) {
  int bufferSize = (4 * 1024);
  double *buffer_ptr = (double *)malloc(bufferSize);
  if (buffer_ptr == NULL) {
    return NULL;
  }
  int i = 0;
  char line[1024];
  while (fgets(line, 1024, handle)) {
    const char *tok;
    tok = strtok(line, " ,\n");
    while (tok != NULL) {
      buffer_ptr[i] = atof(tok);
      i++;
      if (sizeof(double) * i >= bufferSize) {
        if (!double_buffersize(&bufferSize, (void **)&buffer_ptr)) {
          return NULL;
        }
      }
      tok = strtok(NULL, " ,\n");
    }
  }
  *valuesRead = i;
  return buffer_ptr;
}


x86 assembly (clang, -O0):
<read_csv_doubles>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x1000,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118e <read_csv_doubles+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    126d <read_csv_doubles+0x11d>
movl   $0x0,-0x2c(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     125c <read_csv_doubles+0x10c>
lea    -0x430(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
je     1257 <read_csv_doubles+0x107>
mov    -0x438(%rbp),%rdi
call   1040 <atof@plt>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
shl    $0x3,%rax
movslq -0x1c(%rbp),%rcx
cmp    %rcx,%rax
jb     123b <read_csv_doubles+0xeb>
lea    -0x28(%rbp),%rsi
lea    -0x1c(%rbp),%rdi
call   1030 <double_buffersize@plt>
cmp    $0x0,%eax
jne    1236 <read_csv_doubles+0xe6>
movq   $0x0,-0x8(%rbp)
jmp    126d <read_csv_doubles+0x11d>
jmp    123b <read_csv_doubles+0xeb>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdba(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x438(%rbp)
jmp    11ce <read_csv_doubles+0x7e>
jmp    1195 <read_csv_doubles+0x45>
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_csv_doubles>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rsi,%rbx
mov    %rdi,%r15
movl   $0x1000,0x4(%rsp)
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,0x8(%rsp)
xor    %r13d,%r13d
test   %rax,%rax
je     1254 <read_csv_doubles+0x104>
mov    %rax,%rbp
lea    0x20(%rsp),%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1262 <read_csv_doubles+0x112>
mov    %rbx,0x10(%rsp)
lea    0xe4c(%rip),%rbp
lea    0x20(%rsp),%rbx
lea    0x4(%rsp),%r12
lea    0x8(%rsp),%r14
xor    %r13d,%r13d
mov    %r15,0x18(%rsp)
jmp    11ec <read_csv_doubles+0x9c>
nopl   (%rax)
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    0x18(%rsp),%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1258 <read_csv_doubles+0x108>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <strtok@plt>
test   %rax,%rax
je     11d0 <read_csv_doubles+0x80>
movslq %r13d,%rcx
lea    0x8(,%rcx,8),%r15
jmp    1227 <read_csv_doubles+0xd7>
nopl   0x0(%rax)
xor    %edi,%edi
mov    %rbp,%rsi
call   1070 <strtok@plt>
add    $0x8,%r15
add    $0x1,%r13d
test   %rax,%rax
je     11d0 <read_csv_doubles+0x80>
mov    %rax,%rdi
call   1040 <atof@plt>
mov    0x8(%rsp),%rax
movsd  %xmm0,-0x8(%rax,%r15,1)
movslq 0x4(%rsp),%rax
cmp    %rax,%r15
jb     1210 <read_csv_doubles+0xc0>
mov    %r12,%rdi
mov    %r14,%rsi
call   1030 <double_buffersize@plt>
test   %eax,%eax
jne    1210 <read_csv_doubles+0xc0>
xor    %ebp,%ebp
jmp    1265 <read_csv_doubles+0x115>
mov    0x8(%rsp),%rbp
mov    0x10(%rsp),%rbx
mov    %r13d,(%rbx)
mov    %rbp,%rax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c" ,\0A\00"
define double* @read_csv_doubles(i32* %0, i32* %1) {
%3 = alloca double*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca double*
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca i8*
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 4096, i32* %6
%11 = load i32, i32* %6
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to double*
store double* %13, double** %7
%14 = load double*, double** %7
%15 = icmp eq double* %14, null
br i1 %15, label %16, label %17
16: ; preds = %2
store double* null, double** %3
br label %57
17: ; preds = %2
store i32 0, i32* %8
br label %18
18: ; preds = %52, %17
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%20 = load i32*, i32** %4
%21 = call i64 @fgets(i8* %19, i32 1024, i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %53
23: ; preds = %18
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%25 = call i8* @strtok(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %25, i8** %10
br label %26
26: ; preds = %50, %23
%27 = load i8*, i8** %10
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %52
29: ; preds = %26
%30 = load i8*, i8** %10
%31 = call double @atof(i8* %30)
%32 = load double*, double** %7
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds double, double* %32, i64 %34
store double %31, double* %35
%36 = load i32, i32* %8
%37 = add nsw i32 %36, 1
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = mul i64 8, %39
%41 = load i32, i32* %6
%42 = sext i32 %41 to i64
%43 = icmp uge i64 %40, %42
br i1 %43, label %44, label %50
44: ; preds = %29
%45 = bitcast double** %7 to i8**
%46 = call i32 @double_buffersize(i32* %6, i8** %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %44
store double* null, double** %3
br label %57
49: ; preds = %44
br label %50
50: ; preds = %49, %29
%51 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %51, i8** %10
br label %26
52: ; preds = %26
br label %18
53: ; preds = %18
%54 = load i32, i32* %8
%55 = load i32*, i32** %5
store i32 %54, i32* %55
%56 = load double*, double** %7
store double* %56, double** %3
br label %57
57: ; preds = %53, %48, %16
%58 = load double*, double** %3
ret double* %58
}

LLVM IR (-O2):
@.str = [4 x i8] c" ,\0A\00"
define double* @read_csv_doubles(i32* nocapture %0, i32* nocapture writeonly %1) {
%3 = alloca i32
%4 = alloca double*
%5 = alloca [1024 x i8]
%6 = bitcast i32* %3 to i8*
store i32 4096, i32* %3
%7 = bitcast double** %4 to i8*
%8 = tail call i64 @malloc(i32 4096)
%9 = inttoptr i64 %8 to double*
store double* %9, double** %4
%10 = icmp eq i64 %8, 0
br i1 %10, label %53, label %11
11: ; preds = %2
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%13 = call i64 @fgets(i8* %12, i32 1024, i32* %0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %48, label %15
15: ; preds = %11
%16 = bitcast double** %4 to i8**
br label %23
17: ; preds = %43
%18 = trunc i64 %35 to i32
br label %19
19: ; preds = %17, %23
%20 = phi i32 [ %24, %23 ], [ %18, %17 ]
%21 = call i64 @fgets(i8* %12, i32 1024, i32* %0)
%22 = icmp eq i64 %21, 0
br i1 %22, label %46, label %23
23: ; preds = %15, %19
%24 = phi i32 [ 0, %15 ], [ %20, %19 ]
%25 = call i8* @strtok(i8* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%26 = icmp eq i8* %25, null
br i1 %26, label %19, label %27
27: ; preds = %23
%28 = sext i32 %24 to i64
br label %29
29: ; preds = %27, %43
%30 = phi i64 [ %28, %27 ], [ %35, %43 ]
%31 = phi i8* [ %25, %27 ], [ %44, %43 ]
%32 = call double @atof(i8* %31)
%33 = load double*, double** %4
%34 = getelementptr inbounds double, double* %33, i64 %30
store double %32, double* %34
%35 = add i64 %30, 1
%36 = shl nsw i64 %35, 3
%37 = load i32, i32* %3
%38 = sext i32 %37 to i64
%39 = icmp ult i64 %36, %38
br i1 %39, label %43, label %40
40: ; preds = %29
%41 = call i32 @double_buffersize(i32* %3, i8** %16)
%42 = icmp eq i32 %41, 0
br i1 %42, label %51, label %43
43: ; preds = %40, %29
%44 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%45 = icmp eq i8* %44, null
br i1 %45, label %17, label %29
46: ; preds = %19
%47 = load double*, double** %4
br label %48
48: ; preds = %46, %11
%49 = phi double* [ %9, %11 ], [ %47, %46 ]
%50 = phi i32 [ 0, %11 ], [ %20, %46 ]
store i32 %50, i32* %1
br label %51
51: ; preds = %40, %48
%52 = phi double* [ %49, %48 ], [ null, %40 ]
br label %53
53: ; preds = %2, %51
%54 = phi double* [ %52, %51 ], [ null, %2 ]
ret double* %54
}