#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(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int *gf_read_matrix(FILE *f, int *rows, int *cols) {
  int i, r, c, *a;
  if (fscanf(f, "%d", &r) == 0) {
    fprintf(stderr, "ERROR reading file -- rows\n");
    exit(1);
  }
  if (fscanf(f, "%d", &c) == 0) {
    fprintf(stderr, "ERROR reading file -- cols\n");
    exit(1);
  }
  a = (int *)malloc(sizeof(int) * r * c);
  if (a == NULL) {
    perror("gf_read_matrix: malloc");
    exit(1);
  }
  for (i = 0; i < r * c; i++) {
    if (fscanf(f, "%d", a + i) == 0) {
      fprintf(stderr, "ERROR reading file -- element %d\n", i);
      exit(1);
    }
  }
  *rows = r;
  *cols = c;
  return a;
}


x86 assembly (clang, -O0):
<gf_read_matrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe91(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <fscanf@plt>
cmp    $0x0,%rax
jne    11a3 <gf_read_matrix+0x53>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe71(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    0xe52(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1030 <fscanf@plt>
cmp    $0x0,%rax
jne    11e2 <gf_read_matrix+0x92>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movslq -0x20(%rbp),%rax
shl    $0x2,%rax
movslq -0x24(%rbp),%rcx
imul   %rcx,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    121e <gf_read_matrix+0xce>
lea    0xe2c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%ecx
imul   -0x24(%rbp),%ecx
cmp    %ecx,%eax
jge    1297 <gf_read_matrix+0x147>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rdx
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xdaf(%rip),%rsi
call   1030 <fscanf@plt>
cmp    $0x0,%rax
jne    1284 <gf_read_matrix+0x134>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1289 <gf_read_matrix+0x139>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1225 <gf_read_matrix+0xd5>
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gf_read_matrix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r13
lea    0xe92(%rip),%rsi
lea    0xc(%rsp),%rdx
call   1030 <fscanf@plt>
test   %rax,%rax
je     124f <gf_read_matrix+0xff>
lea    0xe78(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %r13,%rdi
call   1030 <fscanf@plt>
test   %rax,%rax
je     1261 <gf_read_matrix+0x111>
mov    %rbx,0x10(%rsp)
mov    0xc(%rsp),%edi
imul   0x8(%rsp),%edi
shl    $0x2,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1282 <gf_read_matrix+0x132>
mov    %rax,%r14
mov    0xc(%rsp),%eax
mov    0x8(%rsp),%ecx
mov    %ecx,%edx
imul   %eax,%edx
test   %edx,%edx
jle    1210 <gf_read_matrix+0xc0>
xor    %ebx,%ebx
lea    0xe26(%rip),%rbp
mov    %r14,%r12
nopl   (%rax)
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
call   1030 <fscanf@plt>
test   %rax,%rax
je     122c <gf_read_matrix+0xdc>
add    $0x1,%rbx
mov    0xc(%rsp),%eax
mov    0x8(%rsp),%ecx
mov    %ecx,%edx
imul   %eax,%edx
movslq %edx,%rdx
add    $0x4,%r12
cmp    %rdx,%rbx
jl     11e0 <gf_read_matrix+0x90>
mov    %eax,(%r15)
mov    0x10(%rsp),%rax
mov    %ecx,(%rax)
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
jmp    1271 <gf_read_matrix+0x121>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xdb2(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"ERROR reading file -- rows\0A\00"
@.str.2 = [28 x i8] c"ERROR reading file -- cols\0A\00"
@.str.3 = [23 x i8] c"gf_read_matrix: malloc\00"
@.str.4 = [34 x i8] c"ERROR reading file -- element %d\0A\00"
define i32* @gf_read_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*
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%11 = load i32*, i32** %4
%12 = call i64 @fscanf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %8)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %3
%19 = load i32*, i32** %4
%20 = call i64 @fscanf(i32* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %9)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %18
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = mul i64 4, %28
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = mul i64 %29, %31
%33 = trunc i64 %32 to i32
%34 = call i64 @malloc(i32 %33)
%35 = inttoptr i64 %34 to i32*
store i32* %35, i32** %10
%36 = load i32*, i32** %10
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %41
38: ; preds = %26
%39 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %26
store i32 0, i32* %7
br label %42
42: ; preds = %62, %41
%43 = load i32, i32* %7
%44 = load i32, i32* %8
%45 = load i32, i32* %9
%46 = mul nsw i32 %44, %45
%47 = icmp slt i32 %43, %46
br i1 %47, label %48, label %65
48: ; preds = %42
%49 = load i32*, i32** %4
%50 = load i32*, i32** %10
%51 = load i32, i32* %7
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %50, i64 %52
%54 = call i64 @fscanf(i32* %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %53)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %48
%57 = load i32, i32* @stderr
%58 = load i32, i32* %7
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %58)
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %48
br label %62
62: ; preds = %61
%63 = load i32, i32* %7
%64 = add nsw i32 %63, 1
store i32 %64, i32* %7
br label %42
65: ; preds = %42
%66 = load i32, i32* %8
%67 = load i32*, i32** %5
store i32 %66, i32* %67
%68 = load i32, i32* %9
%69 = load i32*, i32** %6
store i32 %68, i32* %69
%70 = load i32*, i32** %10
ret i32* %70
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"ERROR reading file -- rows\0A\00"
@.str.2 = [28 x i8] c"ERROR reading file -- cols\0A\00"
@.str.3 = [23 x i8] c"gf_read_matrix: malloc\00"
@.str.4 = [34 x i8] c"ERROR reading file -- element %d\0A\00"
define i32* @gf_read_matrix(i32* nocapture %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = call i64 @fscanf(i32* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %3
%15 = call i64 @fscanf(i32* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %5)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = load i32, i32* %4
%23 = shl i32 %22, 2
%24 = load i32, i32* %5
%25 = mul i32 %23, %24
%26 = call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i32*
%28 = icmp eq i64 %26, 0
br i1 %28, label %34, label %29
29: ; preds = %21
%30 = load i32, i32* %4
%31 = load i32, i32* %5
%32 = mul nsw i32 %31, %30
%33 = icmp sgt i32 %32, 0
br i1 %33, label %37, label %54
34: ; preds = %21
%35 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %29, %47
%38 = phi i64 [ %48, %47 ], [ 0, %29 ]
%39 = getelementptr inbounds i32, i32* %27, i64 %38
%40 = call i64 @fscanf(i32* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = trunc i64 %38 to i32
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %43)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %37
%48 = add nuw nsw i64 %38, 1
%49 = load i32, i32* %4
%50 = load i32, i32* %5
%51 = mul nsw i32 %50, %49
%52 = sext i32 %51 to i64
%53 = icmp slt i64 %48, %52
br i1 %53, label %37, label %54
54: ; preds = %47, %29
%55 = phi i32 [ %30, %29 ], [ %49, %47 ]
%56 = phi i32 [ %31, %29 ], [ %50, %47 ]
store i32 %55, i32* %1
store i32 %56, i32* %2
ret i32* %27
}