#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*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ strtol(char *, char **, int);
void read_csv(char filename[], int rows, int cols, int array[rows * cols]) {
  FILE *file = fopen(filename, "r");
  int i, j;
  char buffer[cols * 5], *ptr;
  for (i = 0; fgets(buffer, sizeof buffer, file); ++i)
    for (j = 0, ptr = buffer; j < cols; ++j, ++ptr)
      array[(i * cols) + j] = (int)strtol(ptr, &ptr, 10);
  fclose(file);
}


x86 assembly (clang, -O0):
<read_csv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe9f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%ecx
mov    %ecx,%eax
lea    (%rax,%rax,4),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     122d <read_csv+0xed>
mov    -0x48(%rbp),%rax
movl   $0x0,-0x28(%rbp)
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    121a <read_csv+0xda>
mov    -0x40(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,%edx
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
imul   -0x10(%rbp),%ecx
add    -0x28(%rbp),%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    11cc <read_csv+0x8c>
jmp    121f <read_csv+0xdf>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a0 <read_csv+0x60>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_csv>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%rbx
mov    %edx,%r14d
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rdx
mov    %r14,-0x38(%rbp)
lea    (%r14,%r14,4),%esi
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r13
sub    %rax,%r13
mov    %r13,%rsp
mov    %r13,%rdi
mov    %rsi,-0x50(%rbp)
mov    %rdx,-0x40(%rbp)
call   1040 <fgets@plt>
test   %rax,%rax
je     1208 <read_csv+0xc8>
mov    -0x38(%rbp),%rax
movslq %eax,%rcx
mov    %eax,%r12d
shl    $0x2,%rcx
mov    %rcx,-0x48(%rbp)
lea    -0x30(%rbp),%r14
jmp    11c9 <read_csv+0x89>
nopl   0x0(%rax)
mov    %r13,%rdi
mov    -0x50(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1040 <fgets@plt>
add    -0x48(%rbp),%rbx
test   %rax,%rax
je     1208 <read_csv+0xc8>
mov    %r13,-0x30(%rbp)
cmpl   $0x0,-0x38(%rbp)
jle    11b0 <read_csv+0x70>
xor    %r15d,%r15d
mov    %r13,%rdi
nopl   0x0(%rax)
mov    %r14,%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,(%rbx,%r15,4)
add    $0x1,%r15
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
mov    %rdi,-0x30(%rbp)
cmp    %r15,%r12
jne    11e0 <read_csv+0xa0>
jmp    11b0 <read_csv+0x70>
mov    -0x40(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define void @read_csv(i8* %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%15 = load i32, i32* %6
%16 = load i32, i32* %7
%17 = mul nsw i32 %15, %16
%18 = zext i32 %17 to i64
%19 = load i8*, i8** %5
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %9
%21 = load i32, i32* %7
%22 = mul nsw i32 %21, 5
%23 = zext i32 %22 to i64
%24 = call i8* @llvm.stacksave()
store i8* %24, i8** %12
%25 = alloca i8, i64 %23
store i64 %23, i64* %13
store i32 0, i32* %10
br label %26
26: ; preds = %54, %4
%27 = trunc i64 %23 to i32
%28 = load i32*, i32** %9
%29 = call i64 @fgets(i8* %25, i32 %27, i32* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %57
31: ; preds = %26
store i32 0, i32* %11
store i8* %25, i8** %14
br label %32
32: ; preds = %48, %31
%33 = load i32, i32* %11
%34 = load i32, i32* %7
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %53
36: ; preds = %32
%37 = load i8*, i8** %14
%38 = call i64 @strtol(i8* %37, i8** %14, i32 10)
%39 = trunc i64 %38 to i32
%40 = load i32*, i32** %8
%41 = load i32, i32* %10
%42 = load i32, i32* %7
%43 = mul nsw i32 %41, %42
%44 = load i32, i32* %11
%45 = add nsw i32 %43, %44
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %40, i64 %46
store i32 %39, i32* %47
br label %48
48: ; preds = %36
%49 = load i32, i32* %11
%50 = add nsw i32 %49, 1
store i32 %50, i32* %11
%51 = load i8*, i8** %14
%52 = getelementptr inbounds i8, i8* %51, i32 1
store i8* %52, i8** %14
br label %32
53: ; preds = %32
br label %54
54: ; preds = %53
%55 = load i32, i32* %10
%56 = add nsw i32 %55, 1
store i32 %56, i32* %10
br label %26
57: ; preds = %26
%58 = load i32*, i32** %9
%59 = call i32 @fclose(i32* %58)
%60 = load i8*, i8** %12
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define void @read_csv(i8* nocapture %0, i32 %1, i32 %2, i32* nocapture writeonly %3) {
%5 = alloca i8*
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = mul nsw i32 %2, 5
%8 = zext i32 %7 to i64
%9 = alloca i8, i64 %8
%10 = bitcast i8** %5 to i8*
%11 = call i64 @fgets(i8* %9, i32 %7, i32* %6)
%12 = icmp eq i64 %11, 0
br i1 %12, label %36, label %13
13: ; preds = %4
%14 = icmp sgt i32 %2, 0
%15 = sext i32 %2 to i64
%16 = zext i32 %2 to i64
br label %17
17: ; preds = %13, %32
%18 = phi i64 [ 0, %13 ], [ %33, %32 ]
store i8* %9, i8** %5
br i1 %14, label %19, label %32
19: ; preds = %17
%20 = mul nsw i64 %18, %15
br label %21
21: ; preds = %19, %21
%22 = phi i64 [ 0, %19 ], [ %28, %21 ]
%23 = phi i8* [ %9, %19 ], [ %30, %21 ]
%24 = call i64 @strtol(i8* %23, i8** %5, i32 10)
%25 = trunc i64 %24 to i32
%26 = add nsw i64 %22, %20
%27 = getelementptr inbounds i32, i32* %3, i64 %26
store i32 %25, i32* %27
%28 = add nuw nsw i64 %22, 1
%29 = load i8*, i8** %5
%30 = getelementptr inbounds i8, i8* %29, i64 1
store i8* %30, i8** %5
%31 = icmp eq i64 %28, %16
br i1 %31, label %32, label %21
32: ; preds = %21, %17
%33 = add nuw i64 %18, 1
%34 = call i64 @fgets(i8* %9, i32 %7, i32* %6)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %17
36: ; preds = %32, %4
%37 = call i32 @fclose(i32* %6)
ret void
}