#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*/ matrix_mostly_sorted(size_t, size_t, int **);
int /*<<< orphan*/ print_matrix(size_t, size_t, int **);
int /*<<< orphan*/ printf(char *);
int scanf(char *, ...);
int main(void) {
  printf("Enter matrix dimensions followed by the elements\n");
  printf("> ");
  size_t rows, cols;
  while (scanf("%zu%zu", &rows, &cols) == 2) {
    int matrix[rows][cols];
    size_t i, j;
    for (i = 0; i < rows; i++) {
      for (j = 0; j < cols; j++) {
        scanf("%d", &matrix[i][j]);
      }
    }
    matrix_mostly_sorted(rows, cols, matrix);
    print_matrix(rows, cols, matrix);
    printf("> ");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
call   1040 <printf@plt>
lea    0xed0(%rip),%rdi
call   1040 <printf@plt>
lea    0xec7(%rip),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1050 <scanf@plt>
cmp    $0x2,%eax
jne    127b <main+0x13b>
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rdx
mov    %rdx,-0x20(%rbp)
mov    %rcx,%rdx
imul   %rax,%rdx
lea    0xf(,%rdx,4),%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x48(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x28(%rbp)
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1241 <main+0x101>
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    122b <main+0xeb>
mov    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%rax
imul   -0x38(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x40(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    11df <main+0x9f>
jmp    1230 <main+0xf0>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11c9 <main+0x89>
mov    -0x48(%rbp),%rdx
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <matrix_mostly_sorted@plt>
mov    -0x48(%rbp),%rdx
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <print_matrix@plt>
lea    0xdc8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
jmp    1167 <main+0x27>
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xea5(%rip),%rdi
call   1030 <puts@plt>
lea    0xe8c(%rip),%rdi
call   1050 <printf@plt>
lea    0xe83(%rip),%rdi
lea    -0x38(%rbp),%rsi
lea    -0x30(%rbp),%rdx
xor    %eax,%eax
call   1060 <scanf@plt>
cmp    $0x2,%eax
jne    129b <main+0x14b>
lea    0xe6b(%rip),%rbx
jmp    11fd <main+0xad>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    -0x40(%rbp),%r14
mov    %r14,%rdx
call   1040 <matrix_mostly_sorted@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %r14,%rdx
call   1070 <print_matrix@plt>
lea    0xe2b(%rip),%rdi
call   1050 <printf@plt>
mov    -0x48(%rbp),%rsp
lea    0xe1e(%rip),%rdi
lea    -0x38(%rbp),%rsi
lea    -0x30(%rbp),%rdx
xor    %eax,%eax
call   1060 <scanf@plt>
cmp    $0x2,%eax
jne    129b <main+0x14b>
mov    %rsp,%rcx
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rax
imul   %rdx,%rax
mov    %rsp,%r14
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
test   %rdi,%rdi
mov    %rcx,-0x48(%rbp)
mov    %r14,-0x40(%rbp)
je     11b0 <main+0x60>
shl    $0x2,%rdx
mov    %rsi,%rax
xor    %r12d,%r12d
mov    %rdx,-0x50(%rbp)
jmp    1262 <main+0x112>
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
add    $0x1,%r12
add    %rdx,%r14
cmp    %rdi,%r12
jae    11b2 <main+0x62>
test   %rax,%rax
je     1250 <main+0x100>
mov    %r14,%r15
xor    %r13d,%r13d
nopl   (%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
add    $0x1,%r13
mov    -0x30(%rbp),%rsi
add    $0x4,%r15
cmp    %rsi,%r13
jb     1270 <main+0x120>
mov    -0x38(%rbp),%rdi
mov    %rsi,%rax
mov    -0x50(%rbp),%rdx
jmp    1252 <main+0x102>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [50 x i8] c"Enter matrix dimensions followed by the elements\0A\00"
@.str.1 = [3 x i8] c"> \00"
@.str.2 = [7 x i8] c"%zu%zu\00"
@.str.3 = [3 x i8] c"%d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i32 0, i32* %1
%9 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %43, %0
%12 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i64* %2, i64* %3)
%13 = icmp eq i32 %12, 2
br i1 %13, label %14, label %54
14: ; preds = %11
%15 = load i64, i64* %2
%16 = load i64, i64* %3
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %4
%18 = mul nuw i64 %15, %16
%19 = alloca i32, i64 %18
store i64 %15, i64* %5
store i64 %16, i64* %6
store i64 0, i64* %7
br label %20
20: ; preds = %40, %14
%21 = load i64, i64* %7
%22 = load i64, i64* %2
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %43
24: ; preds = %20
store i64 0, i64* %8
br label %25
25: ; preds = %36, %24
%26 = load i64, i64* %8
%27 = load i64, i64* %3
%28 = icmp ult i64 %26, %27
br i1 %28, label %29, label %39
29: ; preds = %25
%30 = load i64, i64* %7
%31 = mul nsw i64 %30, %16
%32 = getelementptr inbounds i32, i32* %19, i64 %31
%33 = load i64, i64* %8
%34 = getelementptr inbounds i32, i32* %32, i64 %33
%35 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %34)
br label %36
36: ; preds = %29
%37 = load i64, i64* %8
%38 = add i64 %37, 1
store i64 %38, i64* %8
br label %25
39: ; preds = %25
br label %40
40: ; preds = %39
%41 = load i64, i64* %7
%42 = add i64 %41, 1
store i64 %42, i64* %7
br label %20
43: ; preds = %20
%44 = load i64, i64* %2
%45 = load i64, i64* %3
%46 = bitcast i32* %19 to i32**
%47 = call i32 @matrix_mostly_sorted(i64 %44, i64 %45, i32** %46)
%48 = load i64, i64* %2
%49 = load i64, i64* %3
%50 = bitcast i32* %19 to i32**
%51 = call i32 @print_matrix(i64 %48, i64 %49, i32** %50)
%52 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%53 = load i8*, i8** %4
br label %11
54: ; preds = %11
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"> \00"
@.str.2 = [7 x i8] c"%zu%zu\00"
@.str.3 = [3 x i8] c"%d\00"
@str = [49 x i8] c"Enter matrix dimensions followed by the elements\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca i64
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%5 = bitcast i64* %1 to i8*
%6 = bitcast i64* %2 to i8*
%7 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i64* %1, i64* %2)
%8 = icmp eq i32 %7, 2
br i1 %8, label %9, label %53
9: ; preds = %0, %42
%10 = load i64, i64* %1
%11 = load i64, i64* %2
%12 = call i8* @llvm.stacksave()
%13 = mul nuw i64 %11, %10
%14 = alloca i32, i64 %13
%15 = load i64, i64* %1
%16 = icmp eq i64 %15, 0
%17 = load i64, i64* %2
br i1 %16, label %42, label %18
18: ; preds = %9, %36
%19 = phi i64 [ %37, %36 ], [ %17, %9 ]
%20 = phi i64 [ %38, %36 ], [ %15, %9 ]
%21 = phi i64 [ %39, %36 ], [ %17, %9 ]
%22 = phi i64 [ %40, %36 ], [ 0, %9 ]
%23 = icmp eq i64 %21, 0
br i1 %23, label %36, label %24
24: ; preds = %18
%25 = mul nsw i64 %22, %11
%26 = getelementptr inbounds i32, i32* %14, i64 %25
br label %27
27: ; preds = %24, %27
%28 = phi i64 [ 0, %24 ], [ %31, %27 ]
%29 = getelementptr inbounds i32, i32* %26, i64 %28
%30 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %29)
%31 = add nuw i64 %28, 1
%32 = load i64, i64* %2
%33 = icmp ult i64 %31, %32
br i1 %33, label %27, label %34
34: ; preds = %27
%35 = load i64, i64* %1
br label %36
36: ; preds = %34, %18
%37 = phi i64 [ %32, %34 ], [ %19, %18 ]
%38 = phi i64 [ %35, %34 ], [ %20, %18 ]
%39 = phi i64 [ %32, %34 ], [ 0, %18 ]
%40 = add nuw i64 %22, 1
%41 = icmp ult i64 %40, %38
br i1 %41, label %18, label %42
42: ; preds = %36, %9
%43 = phi i64 [ %17, %9 ], [ %37, %36 ]
%44 = phi i64 [ 0, %9 ], [ %38, %36 ]
%45 = bitcast i32* %14 to i32**
%46 = call i32 @matrix_mostly_sorted(i64 %44, i64 %43, i32** %45)
%47 = load i64, i64* %1
%48 = load i64, i64* %2
%49 = call i32 @print_matrix(i64 %47, i64 %48, i32** %45)
%50 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%51 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i64* %1, i64* %2)
%52 = icmp eq i32 %51, 2
br i1 %52, label %9, label %53
53: ; preds = %42, %0
ret i32 0
}