#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*/ quicksort(int *, size_t);
int /*<<< orphan*/ swap_arrays(int *, int *, size_t);
void matrix_mostly_sorted(size_t rows, size_t cols, int matrix[rows][cols]) {
  quicksort(&matrix[0][0], rows * cols);
  size_t i;
  for (i = 0; i < rows / 2; i++) {
    swap_arrays(&matrix[i][0], &matrix[rows - (i + 1)][0], cols);
  }
}


x86 assembly (clang, -O0):
<matrix_mostly_sorted>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
imul   $0x0,%rax,%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
imul   -0x10(%rbp),%rsi
call   1030 <quicksort@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
shr    $0x1,%rcx
cmp    %rcx,%rax
jae    11c5 <matrix_mostly_sorted+0xa5>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rdi
mov    %rcx,%rax
imul   -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
sub    %rdx,%rax
imul   %rcx,%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x10(%rbp),%rdx
call   1040 <swap_arrays@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1161 <matrix_mostly_sorted+0x41>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<matrix_mostly_sorted>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
imul   %rdi,%rsi
mov    %rdx,%rdi
call   1030 <quicksort@plt>
cmp    $0x2,%rbx
jb     118a <matrix_mostly_sorted+0x6a>
mov    %rbx,%r12
shr    %r12
add    $0xffffffffffffffff,%rbx
imul   %r14,%rbx
lea    (%r15,%rbx,4),%rbx
lea    0x0(,%r14,4),%r13
mov    %r13,%rbp
neg    %rbp
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <swap_arrays@plt>
add    %rbp,%rbx
add    %r13,%r15
add    $0xffffffffffffffff,%r12
jne    1170 <matrix_mostly_sorted+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @matrix_mostly_sorted(i64 %0, i64 %1, i32* %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i64
store i64 %0, i64* %4
store i64 %1, i64* %5
store i32* %2, i32** %6
%8 = load i64, i64* %4
%9 = load i64, i64* %5
%10 = load i32*, i32** %6
%11 = mul nsw i64 0, %9
%12 = getelementptr inbounds i32, i32* %10, i64 %11
%13 = getelementptr inbounds i32, i32* %12, i64 0
%14 = load i64, i64* %4
%15 = load i64, i64* %5
%16 = mul i64 %14, %15
%17 = call i32 @quicksort(i32* %13, i64 %16)
store i64 0, i64* %7
br label %18
18: ; preds = %39, %3
%19 = load i64, i64* %7
%20 = load i64, i64* %4
%21 = udiv i64 %20, 2
%22 = icmp ult i64 %19, %21
br i1 %22, label %23, label %42
23: ; preds = %18
%24 = load i32*, i32** %6
%25 = load i64, i64* %7
%26 = mul nsw i64 %25, %9
%27 = getelementptr inbounds i32, i32* %24, i64 %26
%28 = getelementptr inbounds i32, i32* %27, i64 0
%29 = load i32*, i32** %6
%30 = load i64, i64* %4
%31 = load i64, i64* %7
%32 = add i64 %31, 1
%33 = sub i64 %30, %32
%34 = mul nsw i64 %33, %9
%35 = getelementptr inbounds i32, i32* %29, i64 %34
%36 = getelementptr inbounds i32, i32* %35, i64 0
%37 = load i64, i64* %5
%38 = call i32 @swap_arrays(i32* %28, i32* %36, i64 %37)
br label %39
39: ; preds = %23
%40 = load i64, i64* %7
%41 = add i64 %40, 1
store i64 %41, i64* %7
br label %18
42: ; preds = %18
ret void
}

LLVM IR (-O2):
define void @matrix_mostly_sorted(i64 %0, i64 %1, i32* %2) {
%4 = mul i64 %1, %0
%5 = tail call i32 @quicksort(i32* %2, i64 %4)
%6 = icmp ult i64 %0, 2
br i1 %6, label %19, label %7
7: ; preds = %3
%8 = lshr i64 %0, 1
br label %9
9: ; preds = %7, %9
%10 = phi i64 [ %13, %9 ], [ 0, %7 ]
%11 = mul nsw i64 %10, %1
%12 = getelementptr inbounds i32, i32* %2, i64 %11
%13 = add nuw nsw i64 %10, 1
%14 = sub i64 %0, %13
%15 = mul nsw i64 %14, %1
%16 = getelementptr inbounds i32, i32* %2, i64 %15
%17 = tail call i32 @swap_arrays(i32* %12, i32* %16, i64 %1)
%18 = icmp eq i64 %13, %8
br i1 %18, label %19, label %9
19: ; preds = %9, %3
ret void
}