#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*/ EXIT_FAILURE;
double atof(char *);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ stderr;
char *strdup(char *);
char *strsep(char **, char *);
void read_dataset(double ***matrix, int row, int col, char *filename) {
  FILE *dataset = fopen(filename, "r");
  int bufflen = row * 15;
  char *line = (char *)calloc(bufflen, sizeof(char));
  int i = 0;
  while (fgets(line, bufflen, dataset)) {
    char *dup = strdup(line);
    char *ptr = dup;
    char *token;
    int j = 0;
    while ((token = strsep(&dup, ","))) {
      (*matrix)[i][j] = atof(token);
      j++;
      if (j > col) {
        fprintf(stderr, "ERROR: Too few allocated columns!\n");
        exit(EXIT_FAILURE);
      }
    }
    free(ptr);
    dup = NULL;
    i++;
    if (i > row) {
      fprintf(stderr, "ERROR: Too few allocated rows!\n");
      exit(EXIT_FAILURE);
    }
  }
  free(line);
  fclose(dataset);
}


x86 assembly (clang, -O0):
<read_dataset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
imul   $0xf,-0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     12e9 <read_dataset+0x149>
mov    -0x30(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x54(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xdd8(%rip),%rsi
call   1090 <strsep@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
je     129b <read_dataset+0xfb>
mov    -0x50(%rbp),%rdi
call   1040 <atof@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x54(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    1296 <read_dataset+0xf6>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
lea    0xd81(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    121f <read_dataset+0x7f>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x40(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    12e4 <read_dataset+0x144>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
lea    0xd56(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    11e9 <read_dataset+0x49>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_dataset>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xe43(%rip),%rsi
mov    %rcx,%rdi
call   10a0 <fopen@plt>
mov    %rax,%r12
mov    %rbp,%r14
lea    0x0(%rbp,%rbp,4),%eax
lea    (%rax,%rax,2),%ebp
mov    %ebp,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %ebp,0xc(%rsp)
mov    %ebp,%esi
mov    %r12,0x10(%rsp)
mov    %r12,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12bd <read_dataset+0x11d>
xor    %ebp,%ebp
test   %r15d,%r15d
cmovle %ebp,%r15d
add    $0x1,%r15d
test   %r14d,%r14d
cmovle %ebp,%r14d
mov    %r14,0x28(%rsp)
lea    0xde2(%rip),%r12
lea    0x18(%rsp),%r14
mov    %r13,0x20(%rsp)
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   10c0 <strdup@plt>
mov    %rax,0x30(%rsp)
mov    %rax,0x18(%rsp)
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    %r12,%rsi
call   1090 <strsep@plt>
test   %rax,%rax
je     1280 <read_dataset+0xe0>
mov    %rax,%rdi
call   1040 <atof@plt>
mov    (%rbx),%rax
mov    (%rax,%rbp,8),%rax
movsd  %xmm0,(%rax,%r13,8)
add    $0x1,%r13
cmp    %r13,%r15
jne    1250 <read_dataset+0xb0>
jmp    12de <read_dataset+0x13e>
mov    0x30(%rsp),%rdi
call   1030 <free@plt>
movq   $0x0,0x18(%rsp)
cmp    0x28(%rsp),%rbp
je     1301 <read_dataset+0x161>
add    $0x1,%rbp
mov    0x20(%rsp),%r13
mov    %r13,%rdi
mov    0xc(%rsp),%esi
mov    0x10(%rsp),%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    1230 <read_dataset+0x90>
mov    %r13,%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%rdi
call   1050 <fclose@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
lea    0xd16(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
lea    0xd16(%rip),%rsi
jmp    12ee <read_dataset+0x14e>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c",\00"
@stderr = global i32 0
@.str.2 = [35 x i8] c"ERROR: Too few allocated columns!\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [32 x i8] c"ERROR: Too few allocated rows!\0A\00"
define void @read_dataset(double*** %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca double***
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i32
store double*** %0, double**** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%17 = load i8*, i8** %8
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %9
%19 = load i32, i32* %6
%20 = mul nsw i32 %19, 15
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = call i64 @calloc(i32 %21, i32 1)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %11
store i32 0, i32* %12
br label %24
24: ; preds = %73, %4
%25 = load i8*, i8** %11
%26 = load i32, i32* %10
%27 = load i32*, i32** %9
%28 = call i64 @fgets(i8* %25, i32 %26, i32* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %74
30: ; preds = %24
%31 = load i8*, i8** %11
%32 = call i8* @strdup(i8* %31)
store i8* %32, i8** %13
%33 = load i8*, i8** %13
store i8* %33, i8** %14
store i32 0, i32* %16
br label %34
34: ; preds = %59, %30
%35 = call i8* @strsep(i8** %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %35, i8** %15
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %60
37: ; preds = %34
%38 = load i8*, i8** %15
%39 = call double @atof(i8* %38)
%40 = load double***, double**** %5
%41 = load double**, double*** %40
%42 = load i32, i32* %12
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds double*, double** %41, i64 %43
%45 = load double*, double** %44
%46 = load i32, i32* %16
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds double, double* %45, i64 %47
store double %39, double* %48
%49 = load i32, i32* %16
%50 = add nsw i32 %49, 1
store i32 %50, i32* %16
%51 = load i32, i32* %16
%52 = load i32, i32* %7
%53 = icmp sgt i32 %51, %52
br i1 %53, label %54, label %59
54: ; preds = %37
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %37
br label %34
60: ; preds = %34
%61 = load i8*, i8** %14
%62 = call i32 @free(i8* %61)
store i8* null, i8** %13
%63 = load i32, i32* %12
%64 = add nsw i32 %63, 1
store i32 %64, i32* %12
%65 = load i32, i32* %12
%66 = load i32, i32* %6
%67 = icmp sgt i32 %65, %66
br i1 %67, label %68, label %73
68: ; preds = %60
%69 = load i32, i32* @stderr
%70 = call i32 @fprintf(i32 %69, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%71 = load i32, i32* @EXIT_FAILURE
%72 = call i32 @exit(i32 %71)
unreachable
73: ; preds = %60
br label %24
74: ; preds = %24
%75 = load i8*, i8** %11
%76 = call i32 @free(i8* %75)
%77 = load i32*, i32** %9
%78 = call i32 @fclose(i32* %77)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c",\00"
@stderr = global i32 0
@.str.2 = [35 x i8] c"ERROR: Too few allocated columns!\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [32 x i8] c"ERROR: Too few allocated rows!\0A\00"
define void @read_dataset(double*** nocapture %0, i32 %1, i32 %2, i8* nocapture %3) {
%5 = alloca i8*
%6 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = mul nsw i32 %1, 15
%8 = tail call i64 @calloc(i32 %7, i32 1)
%9 = inttoptr i64 %8 to i8*
%10 = call i64 @fgets(i8* %9, i32 %7, i32* %6)
%11 = icmp eq i64 %10, 0
br i1 %11, label %51, label %12
12: ; preds = %4
%13 = bitcast i8** %5 to i8*
%14 = call i32 @llvm.smax.i32(i32 %2, i32 0)
%15 = add nuw i32 %14, 1
%16 = call i32 @llvm.smax.i32(i32 %1, i32 0)
%17 = zext i32 %16 to i64
%18 = zext i32 %15 to i64
br label %19
19: ; preds = %12, %47
%20 = phi i64 [ 0, %12 ], [ %48, %47 ]
%21 = call i8* @strdup(i8* %9)
store i8* %21, i8** %5
br label %22
22: ; preds = %26, %19
%23 = phi i64 [ %32, %26 ], [ 0, %19 ]
%24 = call i8* @strsep(i8** %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i8* %24, null
br i1 %25, label %39, label %26
26: ; preds = %22
%27 = call double @atof(i8* %24)
%28 = load double**, double*** %0
%29 = getelementptr inbounds double*, double** %28, i64 %20
%30 = load double*, double** %29
%31 = getelementptr inbounds double, double* %30, i64 %23
store double %27, double* %31
%32 = add nuw nsw i64 %23, 1
%33 = icmp eq i64 %32, %18
br i1 %33, label %34, label %22
34: ; preds = %26
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %22
%40 = call i32 @free(i8* %21)
store i8* null, i8** %5
%41 = icmp eq i64 %20, %17
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %39
%48 = add nuw nsw i64 %20, 1
%49 = call i64 @fgets(i8* %9, i32 %7, i32* %6)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %19
51: ; preds = %47, %4
%52 = call i32 @free(i8* %9)
%53 = call i32 @fclose(i32* %6)
ret void
}