#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 */
scalar_t__ EOF;
int /*<<< orphan*/ EXIT_FAILURE;
char *calloc(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, char *);
char getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void getRowsAndCols(const char *filename, int *rows, int *cols) {
  FILE *gridfile;
  char *row = calloc(2, sizeof(char));
  int i = 0;
  if ((row) && (filename)) {
    gridfile = fopen(filename, "r");
    if (!gridfile) {
      fprintf(stderr, "Something went wrong when reading %s", filename);
      exit(EXIT_FAILURE);
    }
    *rows = 0;
    *cols = 0;
    while (fscanf(gridfile, "%s", row) != EOF) {
      i += 1;
      if ((getc(gridfile) == '\n')) {
        *rows += 1;
      }
    }
  } else {
    fprintf(stderr, "Something went wrong...");
    exit(EXIT_FAILURE);
  }
  *cols = i / *rows;
  fclose(gridfile);
  free(row);
}


x86 assembly (clang, -O0):
<getRowsAndCols>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x2,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1277 <getRowsAndCols+0xf7>
cmpq   $0x0,-0x8(%rbp)
je     1277 <getRowsAndCols+0xf7>
mov    -0x8(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120c <getRowsAndCols+0x8c>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe0b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xdf5(%rip),%rsi
call   1050 <fscanf@plt>
mov    0x2dad(%rip),%rcx
cmp    (%rcx),%rax
je     1272 <getRowsAndCols+0xf2>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   10a0 <getc@plt>
movsbl %al,%eax
cmp    $0xa,%eax
jne    126d <getRowsAndCols+0xed>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    1220 <getRowsAndCols+0xa0>
jmp    129c <getRowsAndCols+0x11c>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x2c(%rbp),%eax
mov    -0x10(%rbp),%rcx
cltd
idivl  (%rcx)
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getRowsAndCols>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %rdi,%r14
mov    $0x2,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
test   %r14,%r14
je     1268 <getRowsAndCols+0xe8>
mov    %rax,%r12
test   %rax,%rax
je     1268 <getRowsAndCols+0xe8>
lea    0xe41(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1281 <getRowsAndCols+0x101>
mov    %rax,%rbx
movl   $0x0,(%r15)
mov    %rbp,(%rsp)
movl   $0x0,0x0(%rbp)
lea    0xe38(%rip),%rsi
mov    %rax,%rdi
mov    %r12,%rdx
call   1050 <fscanf@plt>
mov    0x2dea(%rip),%r14
xor    %ebp,%ebp
cmp    (%r14),%rax
jne    122f <getRowsAndCols+0xaf>
mov    %ebp,%eax
cltd
idivl  (%r15)
mov    (%rsp),%rcx
mov    %eax,(%rcx)
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
xor    %ebp,%ebp
lea    0xdec(%rip),%r13
jmp    1256 <getRowsAndCols+0xd6>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1050 <fscanf@plt>
add    $0x1,%ebp
cmp    (%r14),%rax
je     1205 <getRowsAndCols+0x85>
mov    %rbx,%rdi
call   10a0 <getc@plt>
cmp    $0xa,%al
jne    1240 <getRowsAndCols+0xc0>
addl   $0x1,(%r15)
jmp    1240 <getRowsAndCols+0xc0>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    129b <getRowsAndCols+0x11b>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
lea    0xd71(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"Something went wrong when reading %s\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [3 x i8] c"%s\00"
@EOF = global i64 0
@.str.3 = [24 x i8] c"Something went wrong...\00"
define void @getRowsAndCols(i8* %0, i32* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%10 = call i8* @calloc(i32 2, i32 1)
store i8* %10, i8** %8
store i32 0, i32* %9
%11 = load i8*, i8** %8
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %49
13: ; preds = %3
%14 = load i8*, i8** %4
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %49
16: ; preds = %13
%17 = load i8*, i8** %4
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %7
%19 = load i32*, i32** %7
%20 = icmp ne i32* %19, null
br i1 %20, label %27, label %21
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %4
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %16
%28 = load i32*, i32** %5
store i32 0, i32* %28
%29 = load i32*, i32** %6
store i32 0, i32* %29
br label %30
30: ; preds = %47, %27
%31 = load i32*, i32** %7
%32 = load i8*, i8** %8
%33 = call i64 @fscanf(i32* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = load i64, i64* @EOF
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %48
36: ; preds = %30
%37 = load i32, i32* %9
%38 = add nsw i32 %37, 1
store i32 %38, i32* %9
%39 = load i32*, i32** %7
%40 = call signext i8 @getc(i32* %39)
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 10
br i1 %42, label %43, label %47
43: ; preds = %36
%44 = load i32*, i32** %5
%45 = load i32, i32* %44
%46 = add nsw i32 %45, 1
store i32 %46, i32* %44
br label %47
47: ; preds = %43, %36
br label %30
48: ; preds = %30
br label %54
49: ; preds = %13, %3
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %48
%55 = load i32, i32* %9
%56 = load i32*, i32** %5
%57 = load i32, i32* %56
%58 = sdiv i32 %55, %57
%59 = load i32*, i32** %6
store i32 %58, i32* %59
%60 = load i32*, i32** %7
%61 = call i32 @fclose(i32* %60)
%62 = load i8*, i8** %8
%63 = call i32 @free(i8* %62)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"Something went wrong when reading %s\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [3 x i8] c"%s\00"
@EOF = global i64 0
@.str.3 = [24 x i8] c"Something went wrong...\00"
define void @getRowsAndCols(i8* %0, i32* nocapture %1, i32* nocapture writeonly %2) {
%4 = tail call dereferenceable_or_null(2) i8* @calloc(i32 2, i32 1)
%5 = icmp ne i8* %4, null
%6 = icmp ne i8* %0, null
%7 = and i1 %6, %5
br i1 %7, label %8, label %32
8: ; preds = %3
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %16
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %8
store i32 0, i32* %1
store i32 0, i32* %2
%17 = tail call i64 @fscanf(i32* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%18 = load i64, i64* @EOF
%19 = icmp eq i64 %17, %18
br i1 %19, label %37, label %20
20: ; preds = %16, %28
%21 = phi i32 [ %22, %28 ], [ 0, %16 ]
%22 = add nuw nsw i32 %21, 1
%23 = tail call signext i8 @getc(i32* %9)
%24 = icmp eq i8 %23, 10
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i32, i32* %1
%27 = add nsw i32 %26, 1
store i32 %27, i32* %1
br label %28
28: ; preds = %25, %20
%29 = tail call i64 @fscanf(i32* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%30 = load i64, i64* @EOF
%31 = icmp eq i64 %29, %30
br i1 %31, label %37, label %20
32: ; preds = %3
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %28, %16
%38 = phi i32 [ 0, %16 ], [ %22, %28 ]
%39 = load i32, i32* %1
%40 = sdiv i32 %38, %39
store i32 %40, i32* %2
%41 = tail call i32 @fclose(i32* %9)
%42 = tail call i32 @free(i8* %4)
ret void
}