#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 */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(int **);
int /*<<< orphan*/ printf(char *, ...);
int scanf(char *, size_t *, size_t *, ...);
int /*<<< orphan*/ stderr;
int **two_d_alloc(size_t, size_t);
int main(void) {
  printf("Enter the array dimensions\n");
  size_t rows, cols;
  scanf("%zu%zu", &rows, &cols);
  int **arr = two_d_alloc(rows, cols);
  if (arr == NULL) {
    fprintf(stderr, "Out of memory.\n");
    return 127;
  }
  printf("Available commands are:\n"
         "W i j n - Write the value `n` into position (i, j)\n"
         "R i j - Read and print the value in position (i, j)\n"
         "Q - Quit\n");
  while (1) {
    size_t row, col;
    int val;
    if (scanf(" W %zu%zu%d", &row, &col, &val) == 3) {
      arr[row][col] = val;
      printf("Stored %d in (%zu, %zu)\n", val, row, col);
    } else if (scanf(" R %zu%zu", &row, &col) == 2) {
      printf("(%zu, %zu) = %d\n", row, col, arr[row][col]);
    } else {
      break;
    }
  }
  free(arr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xf69(%rip),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1060 <scanf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <two_d_alloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c0 <main+0x70>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x7f,-0x4(%rbp)
jmp    1291 <main+0x141>
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xee0(%rip),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x34(%rbp),%rcx
mov    $0x0,%al
call   1060 <scanf@plt>
cmp    $0x3,%eax
jne    1225 <main+0xd5>
mov    -0x34(%rbp),%edx
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x30(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x34(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
lea    0xea8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    127c <main+0x12c>
lea    0xeae(%rip),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
mov    $0x0,%al
call   1060 <scanf@plt>
cmp    $0x2,%eax
jne    1272 <main+0x122>
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x30(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1277 <main+0x127>
jmp    1281 <main+0x131>
jmp    127c <main+0x12c>
jmp    11ce <main+0x7e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xedb(%rip),%rdi
call   1040 <puts@plt>
lea    0xeb7(%rip),%rdi
lea    0x20(%rsp),%rsi
lea    0x18(%rsp),%rdx
xor    %eax,%eax
call   1070 <scanf@plt>
mov    0x20(%rsp),%rdi
mov    0x18(%rsp),%rsi
call   1080 <two_d_alloc@plt>
test   %rax,%rax
je     1261 <main+0x101>
mov    %rax,%rbx
lea    0xeb7(%rip),%rdi
call   1040 <puts@plt>
lea    0xe50(%rip),%r14
lea    0x10(%rsp),%r15
lea    0x8(%rsp),%r12
lea    0x4(%rsp),%r13
lea    0xe5f(%rip),%rbp
jmp    1207 <main+0xa7>
nopl   0x0(%rax,%rax,1)
mov    0x4(%rsp),%eax
mov    0x10(%rsp),%rdx
mov    (%rbx,%rdx,8),%rsi
mov    0x8(%rsp),%rcx
mov    %eax,(%rsi,%rcx,4)
mov    0x4(%rsp),%esi
lea    0xe1c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1070 <scanf@plt>
cmp    $0x3,%eax
je     11e0 <main+0x80>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <scanf@plt>
cmp    $0x2,%eax
jne    1255 <main+0xf5>
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%rdx
mov    (%rbx,%rsi,8),%rax
mov    (%rax,%rdx,4),%ecx
lea    0xdf3(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1207 <main+0xa7>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    127b <main+0x11b>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xd8f(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x7f,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Enter the array dimensions\0A\00"
@.str.1 = [7 x i8] c"%zu%zu\00"
@stderr = global i32 0
@.str.2 = [16 x i8] c"Out of memory.\0A\00"
@.str.3 = [137 x i8] c"Available commands are:\0AW i j n - Write the value `n` into position (i, j)\0AR i j - Read and print the value in position (i, j)\0AQ - Quit\0A\00"
@.str.4 = [12 x i8] c" W %zu%zu%d\00"
@.str.5 = [25 x i8] c"Stored %d in (%zu, %zu)\0A\00"
@.str.6 = [10 x i8] c" R %zu%zu\00"
@.str.7 = [17 x i8] c"(%zu, %zu) = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32**
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%9 = call i32 (i8*, i64*, i64*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64* %2, i64* %3)
%10 = load i64, i64* %2
%11 = load i64, i64* %3
%12 = call i32** @two_d_alloc(i64 %10, i64 %11)
store i32** %12, i32*** %4
%13 = load i32**, i32*** %4
%14 = icmp eq i32** %13, null
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 127, i32* %1
br label %55
18: ; preds = %0
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([137 x i8], [137 x i8]* @.str.3, i64 0, i64 0))
br label %20
20: ; preds = %18, %51
%21 = call i32 (i8*, i64*, i64*, ...) @scanf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i64* %5, i64* %6, i32* %7)
%22 = icmp eq i32 %21, 3
br i1 %22, label %23, label %35
23: ; preds = %20
%24 = load i32, i32* %7
%25 = load i32**, i32*** %4
%26 = load i64, i64* %5
%27 = getelementptr inbounds i32*, i32** %25, i64 %26
%28 = load i32*, i32** %27
%29 = load i64, i64* %6
%30 = getelementptr inbounds i32, i32* %28, i64 %29
store i32 %24, i32* %30
%31 = load i32, i32* %7
%32 = load i64, i64* %5
%33 = load i64, i64* %6
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %31, i64 %32, i64 %33)
br label %51
35: ; preds = %20
%36 = call i32 (i8*, i64*, i64*, ...) @scanf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i64* %5, i64* %6)
%37 = icmp eq i32 %36, 2
br i1 %37, label %38, label %49
38: ; preds = %35
%39 = load i64, i64* %5
%40 = load i64, i64* %6
%41 = load i32**, i32*** %4
%42 = load i64, i64* %5
%43 = getelementptr inbounds i32*, i32** %41, i64 %42
%44 = load i32*, i32** %43
%45 = load i64, i64* %6
%46 = getelementptr inbounds i32, i32* %44, i64 %45
%47 = load i32, i32* %46
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i64 %39, i64 %40, i32 %47)
br label %50
49: ; preds = %35
br label %52
50: ; preds = %38
br label %51
51: ; preds = %50, %23
br label %20
52: ; preds = %49
%53 = load i32**, i32*** %4
%54 = call i32 @free(i32** %53)
store i32 0, i32* %1
br label %55
55: ; preds = %52, %15
%56 = load i32, i32* %1
ret i32 %56
}

LLVM IR (-O2):
@.str.1 = [7 x i8] c"%zu%zu\00"
@stderr = global i32 0
@.str.2 = [16 x i8] c"Out of memory.\0A\00"
@.str.4 = [12 x i8] c" W %zu%zu%d\00"
@.str.5 = [25 x i8] c"Stored %d in (%zu, %zu)\0A\00"
@.str.6 = [10 x i8] c" R %zu%zu\00"
@.str.7 = [17 x i8] c"(%zu, %zu) = %d\0A\00"
@str = [27 x i8] c"Enter the array dimensions\00"
@str.8 = [136 x i8] c"Available commands are:\0AW i j n - Write the value `n` into position (i, j)\0AR i j - Read and print the value in position (i, j)\0AQ - Quit\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%7 = bitcast i64* %1 to i8*
%8 = bitcast i64* %2 to i8*
%9 = call i32 (i8*, i64*, i64*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64* %1, i64* %2)
%10 = load i64, i64* %1
%11 = load i64, i64* %2
%12 = call i32** @two_d_alloc(i64 %10, i64 %11)
%13 = icmp eq i32** %12, null
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %48
17: ; preds = %0
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([136 x i8], [136 x i8]* @str.8, i64 0, i64 0))
%19 = bitcast i64* %3 to i8*
%20 = bitcast i64* %4 to i8*
%21 = bitcast i32* %5 to i8*
br label %22
22: ; preds = %45, %17
%23 = call i32 (i8*, i64*, i64*, ...) @scanf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i64* %3, i64* %4, i32* %5)
%24 = icmp eq i32 %23, 3
br i1 %24, label %25, label %34
25: ; preds = %22
%26 = load i32, i32* %5
%27 = load i64, i64* %3
%28 = getelementptr inbounds i32*, i32** %12, i64 %27
%29 = load i32*, i32** %28
%30 = load i64, i64* %4
%31 = getelementptr inbounds i32, i32* %29, i64 %30
store i32 %26, i32* %31
%32 = load i32, i32* %5
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %32, i64 %27, i64 %30)
br label %45
34: ; preds = %22
%35 = call i32 (i8*, i64*, i64*, ...) @scanf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i64* %3, i64* %4)
%36 = icmp eq i32 %35, 2
br i1 %36, label %37, label %46
37: ; preds = %34
%38 = load i64, i64* %3
%39 = load i64, i64* %4
%40 = getelementptr inbounds i32*, i32** %12, i64 %38
%41 = load i32*, i32** %40
%42 = getelementptr inbounds i32, i32* %41, i64 %39
%43 = load i32, i32* %42
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i64 %38, i64 %39, i32 %43)
br label %45
45: ; preds = %25, %37
br label %22
46: ; preds = %34
%47 = call i32 @free(i32** %12)
br label %48
48: ; preds = %46, %14
%49 = phi i32 [ 127, %14 ], [ 0, %46 ]
ret i32 %49
}