#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 */

/* Variables and functions */
void *malloc(int);
int scan_columns(char *);
int scan_rows(char *);
char **grid_setup(char *str) {
  int rows;
  int columns;
  char **grid;
  int i;
  rows = scan_rows(str);
  columns = scan_columns(str);
  grid = malloc(sizeof(char *) * rows);
  i = 0;
  while (i < rows) {
    grid[i] = malloc(sizeof(char) * columns);
    i++;
  }
  return (grid);
}


x86 assembly (clang, -O0):
<grid_setup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <scan_rows@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <scan_columns@plt>
mov    %eax,-0x10(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11a6 <grid_setup+0x76>
movslq -0x10(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116e <grid_setup+0x3e>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grid_setup>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
call   1050 <scan_rows@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1030 <scan_columns@plt>
mov    %eax,%r15d
lea    0x0(,%r14,8),%edi
call   1040 <malloc@plt>
mov    %rax,%r12
test   %r14d,%r14d
jle    1185 <grid_setup+0x55>
mov    %r14d,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
call   1040 <malloc@plt>
mov    %rax,(%r12,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1170 <grid_setup+0x40>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8** @grid_setup(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @scan_rows(i8* %7)
store i32 %8, i32* %3
%9 = load i8*, i8** %2
%10 = call i32 @scan_columns(i8* %9)
store i32 %10, i32* %4
%11 = load i32, i32* %3
%12 = sext i32 %11 to i64
%13 = mul i64 8, %12
%14 = trunc i64 %13 to i32
%15 = call i8* @malloc(i32 %14)
%16 = bitcast i8* %15 to i8**
store i8** %16, i8*** %5
store i32 0, i32* %6
br label %17
17: ; preds = %21, %1
%18 = load i32, i32* %6
%19 = load i32, i32* %3
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %33
21: ; preds = %17
%22 = load i32, i32* %4
%23 = sext i32 %22 to i64
%24 = mul i64 1, %23
%25 = trunc i64 %24 to i32
%26 = call i8* @malloc(i32 %25)
%27 = load i8**, i8*** %5
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %27, i64 %29
store i8* %26, i8** %30
%31 = load i32, i32* %6
%32 = add nsw i32 %31, 1
store i32 %32, i32* %6
br label %17
33: ; preds = %17
%34 = load i8**, i8*** %5
ret i8** %34
}

LLVM IR (-O2):
define noalias i8** @grid_setup(i8* %0) {
%2 = tail call i32 @scan_rows(i8* %0)
%3 = tail call i32 @scan_columns(i8* %0)
%4 = shl i32 %2, 3
%5 = tail call i8* @malloc(i32 %4)
%6 = bitcast i8* %5 to i8**
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %16
8: ; preds = %1
%9 = zext i32 %2 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %14, %10 ]
%12 = tail call i8* @malloc(i32 %3)
%13 = getelementptr inbounds i8*, i8** %6, i64 %11
store i8* %12, i8** %13
%14 = add nuw nsw i64 %11, 1
%15 = icmp eq i64 %14, %9
br i1 %15, label %16, label %10
16: ; preds = %10, %1
ret i8** %6
}