#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 atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
void *malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *strtok(char *, char *);
int **allocate_matrix(int rowMaxIndex, int columnMaxIndex, FILE *fp) {
  int idx;
  int **mat;
  int i, j;
  char part[1024];
  char *token;
  mat = malloc(sizeof(int *) * rowMaxIndex);
  if (mat == NULL) {
    printf("ERROR: out of memory\n");
  }
  for (idx = 0; idx < rowMaxIndex; idx++) {
    mat[idx] = malloc(sizeof(int *) * columnMaxIndex);
    if (mat[idx] == NULL) {
      printf("ERROR: out of memory\n");
      break;
    }
  }
  rewind(fp);
  i = j = 0;
  while (fgets(part, 1024, fp) != NULL) {
    token = NULL;
    while ((token = strtok((token == NULL) ? part : NULL, ",")) != NULL) {
      mat[i][j] = atoi(token);
      j = (j + 1) % columnMaxIndex;
    }
    i++;
  }
  fclose(fp);
  return mat;
}


x86 assembly (clang, -O0):
<allocate_matrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11af <allocate_matrix+0x3f>
lea    0xe56(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1217 <allocate_matrix+0xa7>
movslq -0x8(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    1204 <allocate_matrix+0x94>
lea    0xe06(%rip),%rdi
call   1040 <printf@plt>
jmp    1217 <allocate_matrix+0xa7>
jmp    1209 <allocate_matrix+0x99>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b6 <allocate_matrix+0x46>
mov    -0x10(%rbp),%rdi
call   1050 <rewind@plt>
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     12ec <allocate_matrix+0x17c>
movq   $0x0,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
jne    1279 <allocate_matrix+0x109>
lea    -0x430(%rbp),%rax
mov    %rax,-0x440(%rbp)
jmp    1287 <allocate_matrix+0x117>
xor    %eax,%eax
mov    %rax,-0x440(%rbp)
jmp    1287 <allocate_matrix+0x117>
mov    -0x440(%rbp),%rdi
lea    0xd81(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x438(%rbp)
cmp    $0x0,%rax
je     12de <allocate_matrix+0x16e>
mov    -0x438(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%edx
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x28(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
cltd
idivl  -0x8(%rbp)
mov    %edx,-0x28(%rbp)
jmp    1258 <allocate_matrix+0xe8>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    122e <allocate_matrix+0xbe>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate_matrix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdx,(%rsp)
mov    %esi,%ebx
mov    %edi,%r12d
lea    0x0(,%r12,8),%edi
call   1070 <malloc@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
jne    11ad <allocate_matrix+0x3d>
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
test   %r12d,%r12d
jle    11ed <allocate_matrix+0x7d>
lea    0x0(,%rbx,8),%r15d
mov    %r12d,%r14d
xor    %ebp,%ebp
nop
mov    %r15d,%edi
call   1070 <malloc@plt>
mov    0x8(%rsp),%rcx
mov    %rax,(%rcx,%rbp,8)
test   %rax,%rax
je     11e1 <allocate_matrix+0x71>
add    $0x1,%rbp
cmp    %rbp,%r14
jne    11c0 <allocate_matrix+0x50>
jmp    11ed <allocate_matrix+0x7d>
lea    0xe1a(%rip),%rdi
call   1030 <puts@plt>
mov    (%rsp),%rbp
mov    %rbp,%rdi
call   1050 <rewind@plt>
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
mov    %rbp,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     129a <allocate_matrix+0x12a>
xor    %r14d,%r14d
lea    0xde2(%rip),%r12
lea    0x10(%rsp),%r13
xor    %ebp,%ebp
jmp    124a <allocate_matrix+0xda>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r14
mov    %r13,%rdi
mov    $0x400,%esi
mov    (%rsp),%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     129a <allocate_matrix+0x12a>
mov    %r13,%rdi
mov    %r12,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
je     1230 <allocate_matrix+0xc0>
mov    0x8(%rsp),%rcx
mov    (%rcx,%r14,8),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rax,%rdi
call   1090 <atoi@plt>
movslq %ebp,%rcx
mov    %eax,(%r15,%rcx,4)
add    $0x1,%ecx
mov    %ecx,%eax
cltd
idiv   %ebx
mov    %edx,%ebp
xor    %edi,%edi
mov    %r12,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
jne    1270 <allocate_matrix+0x100>
jmp    1230 <allocate_matrix+0xc0>
mov    (%rsp),%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"ERROR: out of memory\0A\00"
@.str.1 = [2 x i8] c",\00"
define i32** @allocate_matrix(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32**
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1024 x i8]
%12 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%13 = load i32, i32* %4
%14 = sext i32 %13 to i64
%15 = mul i64 8, %14
%16 = trunc i64 %15 to i32
%17 = call i8* @malloc(i32 %16)
%18 = bitcast i8* %17 to i32**
store i32** %18, i32*** %8
%19 = load i32**, i32*** %8
%20 = icmp eq i32** %19, null
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %3
store i32 0, i32* %7
br label %24
24: ; preds = %48, %23
%25 = load i32, i32* %7
%26 = load i32, i32* %4
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %51
28: ; preds = %24
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = mul i64 8, %30
%32 = trunc i64 %31 to i32
%33 = call i8* @malloc(i32 %32)
%34 = bitcast i8* %33 to i32*
%35 = load i32**, i32*** %8
%36 = load i32, i32* %7
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32*, i32** %35, i64 %37
store i32* %34, i32** %38
%39 = load i32**, i32*** %8
%40 = load i32, i32* %7
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32*, i32** %39, i64 %41
%43 = load i32*, i32** %42
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %47
45: ; preds = %28
%46 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %51
47: ; preds = %28
br label %48
48: ; preds = %47
%49 = load i32, i32* %7
%50 = add nsw i32 %49, 1
store i32 %50, i32* %7
br label %24
51: ; preds = %45, %24
%52 = load i32*, i32** %6
%53 = call i32 @rewind(i32* %52)
store i32 0, i32* %10
store i32 0, i32* %9
br label %54
54: ; preds = %85, %51
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%56 = load i32*, i32** %6
%57 = call i32* @fgets(i8* %55, i32 1024, i32* %56)
%58 = icmp ne i32* %57, null
br i1 %58, label %59, label %88
59: ; preds = %54
store i8* null, i8** %12
br label %60
60: ; preds = %70, %59
%61 = load i8*, i8** %12
%62 = icmp eq i8* %61, null
br i1 %62, label %63, label %65
63: ; preds = %60
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
br label %66
65: ; preds = %60
br label %66
66: ; preds = %65, %63
%67 = phi i8* [ %64, %63 ], [ null, %65 ]
%68 = call i8* @strtok(i8* %67, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %68, i8** %12
%69 = icmp ne i8* %68, null
br i1 %69, label %70, label %85
70: ; preds = %66
%71 = load i8*, i8** %12
%72 = call i32 @atoi(i8* %71)
%73 = load i32**, i32*** %8
%74 = load i32, i32* %9
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i32*, i32** %73, i64 %75
%77 = load i32*, i32** %76
%78 = load i32, i32* %10
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i32, i32* %77, i64 %79
store i32 %72, i32* %80
%81 = load i32, i32* %10
%82 = add nsw i32 %81, 1
%83 = load i32, i32* %5
%84 = srem i32 %82, %83
store i32 %84, i32* %10
br label %60
85: ; preds = %66
%86 = load i32, i32* %9
%87 = add nsw i32 %86, 1
store i32 %87, i32* %9
br label %54
88: ; preds = %54
%89 = load i32*, i32** %6
%90 = call i32 @fclose(i32* %89)
%91 = load i32**, i32*** %8
ret i32** %91
}

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