#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*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ mergeSort(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ print1D(int *, int);
int /*<<< orphan*/ print2D(int *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int r, c;
  printf("Row: ");
  scanf(" %d", &r);
  printf("Col: ");
  scanf(" %d", &c);
  int *arr = (int *)malloc(sizeof(int) * r * c);
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      scanf(" %d", &arr[i * c + j]);
    }
  }
  printf("\nYour Matrix: \n");
  print2D(arr, r, c);
  mergeSort(arr, 0, r * c - 1);
  printf("\nSorted 1D array: \n");
  print1D(arr, r * c);
  free(arr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
call   1050 <printf@plt>
lea    0xe74(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1090 <scanf@plt>
lea    0xe68(%rip),%rdi
call   1050 <printf@plt>
lea    0xe58(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1090 <scanf@plt>
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
movslq -0xc(%rbp),%rcx
imul   %rcx,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    123c <main+0xcc>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1229 <main+0xb9>
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%eax
imul   -0xc(%rbp),%eax
add    -0x20(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
lea    0xdf0(%rip),%rdi
call   1090 <scanf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11ec <main+0x7c>
jmp    122e <main+0xbe>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d9 <main+0x69>
lea    0xdcd(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1060 <print2D@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%edx
imul   -0xc(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1070 <mergeSort@plt>
lea    0xdad(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%esi
imul   -0xc(%rbp),%esi
call   1040 <print1D@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xe71(%rip),%rdi
call   1060 <printf@plt>
lea    0xe6b(%rip),%rbx
lea    0x4(%rsp),%rsi
mov    %rbx,%rdi
call   10a0 <scanf@plt>
lea    0xe5b(%rip),%rdi
call   1060 <printf@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   10a0 <scanf@plt>
mov    0x4(%rsp),%edi
imul   (%rsp),%edi
shl    $0x2,%edi
call   1090 <malloc@plt>
mov    %rax,%r15
mov    0x4(%rsp),%ecx
test   %ecx,%ecx
jle    1226 <main+0xa6>
xor    %r12d,%r12d
mov    (%rsp),%eax
lea    0xe1f(%rip),%r14
jmp    11f9 <main+0x79>
nopl   0x0(%rax)
add    $0x1,%r12d
cmp    %ecx,%r12d
jge    1226 <main+0xa6>
test   %eax,%eax
jle    11f0 <main+0x70>
xor    %ebx,%ebx
nop
imul   %r12d,%eax
cltq
add    %rbx,%rax
lea    (%r15,%rax,4),%rsi
mov    %r14,%rdi
call   10a0 <scanf@plt>
mov    (%rsp),%eax
add    $0x1,%rbx
cmp    %eax,%ebx
jl     1200 <main+0x80>
mov    0x4(%rsp),%ecx
jmp    11f0 <main+0x70>
lea    0xde3(%rip),%rdi
call   1040 <puts@plt>
mov    0x4(%rsp),%esi
mov    (%rsp),%edx
mov    %r15,%rdi
call   1070 <print2D@plt>
mov    (%rsp),%edx
imul   0x4(%rsp),%edx
add    $0xffffffff,%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <mergeSort@plt>
lea    0xdc2(%rip),%rdi
call   1040 <puts@plt>
mov    (%rsp),%esi
imul   0x4(%rsp),%esi
mov    %r15,%rdi
call   1050 <print1D@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"Row: \00"
@.str.1 = [4 x i8] c" %d\00"
@.str.2 = [6 x i8] c"Col: \00"
@.str.3 = [16 x i8] c"\0AYour Matrix: \0A\00"
@.str.4 = [20 x i8] c"\0ASorted 1D array: \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%9 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%11 = load i32, i32* %2
%12 = sext i32 %11 to i64
%13 = mul i64 4, %12
%14 = load i32, i32* %3
%15 = sext i32 %14 to i64
%16 = mul i64 %13, %15
%17 = trunc i64 %16 to i32
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i32*
store i32* %19, i32** %4
store i32 0, i32* %5
br label %20
20: ; preds = %43, %0
%21 = load i32, i32* %5
%22 = load i32, i32* %2
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %46
24: ; preds = %20
store i32 0, i32* %6
br label %25
25: ; preds = %39, %24
%26 = load i32, i32* %6
%27 = load i32, i32* %3
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %42
29: ; preds = %25
%30 = load i32*, i32** %4
%31 = load i32, i32* %5
%32 = load i32, i32* %3
%33 = mul nsw i32 %31, %32
%34 = load i32, i32* %6
%35 = add nsw i32 %33, %34
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %30, i64 %36
%38 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %37)
br label %39
39: ; preds = %29
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
store i32 %41, i32* %6
br label %25
42: ; preds = %25
br label %43
43: ; preds = %42
%44 = load i32, i32* %5
%45 = add nsw i32 %44, 1
store i32 %45, i32* %5
br label %20
46: ; preds = %20
%47 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%48 = load i32*, i32** %4
%49 = load i32, i32* %2
%50 = load i32, i32* %3
%51 = call i32 @print2D(i32* %48, i32 %49, i32 %50)
%52 = load i32*, i32** %4
%53 = load i32, i32* %2
%54 = load i32, i32* %3
%55 = mul nsw i32 %53, %54
%56 = sub nsw i32 %55, 1
%57 = call i32 @mergeSort(i32* %52, i32 0, i32 %56)
%58 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%59 = load i32*, i32** %4
%60 = load i32, i32* %2
%61 = load i32, i32* %3
%62 = mul nsw i32 %60, %61
%63 = call i32 @print1D(i32* %59, i32 %62)
%64 = load i32*, i32** %4
%65 = call i32 @free(i32* %64)
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"Row: \00"
@.str.1 = [4 x i8] c" %d\00"
@.str.2 = [6 x i8] c"Col: \00"
@str = [15 x i8] c"\0AYour Matrix: \00"
@str.5 = [19 x i8] c"\0ASorted 1D array: \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%7 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%9 = load i32, i32* %1
%10 = shl i32 %9, 2
%11 = load i32, i32* %2
%12 = mul i32 %10, %11
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i32*
%15 = load i32, i32* %1
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %0
%18 = load i32, i32* %2
br label %19
19: ; preds = %17, %42
%20 = phi i32 [ %43, %42 ], [ %15, %17 ]
%21 = phi i32 [ %44, %42 ], [ %18, %17 ]
%22 = phi i32 [ %45, %42 ], [ 0, %17 ]
%23 = icmp sgt i32 %21, 0
br i1 %23, label %47, label %42
24: ; preds = %42, %0
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%26 = load i32, i32* %1
%27 = load i32, i32* %2
%28 = call i32 @print2D(i32* %14, i32 %26, i32 %27)
%29 = load i32, i32* %1
%30 = load i32, i32* %2
%31 = mul nsw i32 %30, %29
%32 = add nsw i32 %31, -1
%33 = call i32 @mergeSort(i32* %14, i32 0, i32 %32)
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
%35 = load i32, i32* %1
%36 = load i32, i32* %2
%37 = mul nsw i32 %36, %35
%38 = call i32 @print1D(i32* %14, i32 %37)
%39 = call i32 @free(i32* %14)
ret i32 0
40: ; preds = %47
%41 = load i32, i32* %1
br label %42
42: ; preds = %40, %19
%43 = phi i32 [ %41, %40 ], [ %20, %19 ]
%44 = phi i32 [ %56, %40 ], [ %21, %19 ]
%45 = add nuw nsw i32 %22, 1
%46 = icmp slt i32 %45, %43
br i1 %46, label %19, label %24
47: ; preds = %19, %47
%48 = phi i32 [ %56, %47 ], [ %21, %19 ]
%49 = phi i32 [ %55, %47 ], [ 0, %19 ]
%50 = mul nsw i32 %48, %22
%51 = add nsw i32 %50, %49
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %14, i64 %52
%54 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %53)
%55 = add nuw nsw i32 %49, 1
%56 = load i32, i32* %2
%57 = icmp slt i32 %55, %56
br i1 %57, label %47, label %40
}