#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_matrix2(double **);
int /*<<< orphan*/ matrix_copy(double **, double **, int, int);
int /*<<< orphan*/ memcpy(double *, double *, int);
double **new_matrix2(int, int);
void reorder_rows(double **Y, double **X, int *idx, int n, int m) {
  int i;
  double **Y2 = (X == Y ? new_matrix2(n, m) : Y);
  for (i = 0; i < n; i++)
    memcpy(Y2[i], X[idx[i]], m * sizeof(double));
  if (X == Y) {
    matrix_copy(Y, Y2, n, m);
    free_matrix2(Y2);
  }
}


x86 assembly (clang, -O0):
<reorder_rows>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    117d <reorder_rows+0x3d>
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1060 <new_matrix2@plt>
mov    %rax,-0x38(%rbp)
jmp    1185 <reorder_rows+0x45>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11dd <reorder_rows+0x9d>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq -0x24(%rbp),%rdx
movslq (%rcx,%rdx,4),%rcx
mov    (%rax,%rcx,8),%rsi
movslq -0x20(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1194 <reorder_rows+0x54>
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    1207 <reorder_rows+0xc7>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1030 <matrix_copy@plt>
mov    -0x30(%rbp),%rdi
call   1050 <free_matrix2@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reorder_rows>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,0x8(%rsp)
mov    %ecx,%r14d
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    %rdi,0x10(%rsp)
cmp    %rdi,%rsi
jne    1179 <reorder_rows+0x39>
mov    %r14d,%edi
mov    0x8(%rsp),%rsi
call   1060 <new_matrix2@plt>
mov    %rax,%rbp
mov    %r14d,0x4(%rsp)
test   %r14d,%r14d
jle    11be <reorder_rows+0x7e>
mov    0x8(%rsp),%rax
lea    0x0(,%rax,8),%r14d
mov    0x4(%rsp),%r15d
xor    %r13d,%r13d
nopl   0x0(%rax,%rax,1)
mov    0x0(%rbp,%r13,8),%rdi
movslq (%r12,%r13,4),%rax
mov    (%rbx,%rax,8),%rsi
mov    %r14d,%edx
call   1040 <memcpy@plt>
add    $0x1,%r13
cmp    %r13,%r15
jne    11a0 <reorder_rows+0x60>
cmp    0x10(%rsp),%rbx
je     11d4 <reorder_rows+0x94>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    0x4(%rsp),%edx
mov    0x8(%rsp),%rcx
call   1030 <matrix_copy@plt>
mov    %rbp,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <free_matrix2@plt>

LLVM IR (-O0):
define void @reorder_rows(double** %0, double** %1, i32* %2, i32 %3, i32 %4) {
%6 = alloca double**
%7 = alloca double**
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca double**
store double** %0, double*** %6
store double** %1, double*** %7
store i32* %2, i32** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%13 = load double**, double*** %7
%14 = load double**, double*** %6
%15 = icmp eq double** %13, %14
br i1 %15, label %16, label %20
16: ; preds = %5
%17 = load i32, i32* %9
%18 = load i32, i32* %10
%19 = call double** @new_matrix2(i32 %17, i32 %18)
br label %22
20: ; preds = %5
%21 = load double**, double*** %6
br label %22
22: ; preds = %20, %16
%23 = phi double** [ %19, %16 ], [ %21, %20 ]
store double** %23, double*** %12
store i32 0, i32* %11
br label %24
24: ; preds = %48, %22
%25 = load i32, i32* %11
%26 = load i32, i32* %9
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %51
28: ; preds = %24
%29 = load double**, double*** %12
%30 = load i32, i32* %11
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds double*, double** %29, i64 %31
%33 = load double*, double** %32
%34 = load double**, double*** %7
%35 = load i32*, i32** %8
%36 = load i32, i32* %11
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
%39 = load i32, i32* %38
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds double*, double** %34, i64 %40
%42 = load double*, double** %41
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = mul i64 %44, 8
%46 = trunc i64 %45 to i32
%47 = call i32 @memcpy(double* %33, double* %42, i32 %46)
br label %48
48: ; preds = %28
%49 = load i32, i32* %11
%50 = add nsw i32 %49, 1
store i32 %50, i32* %11
br label %24
51: ; preds = %24
%52 = load double**, double*** %7
%53 = load double**, double*** %6
%54 = icmp eq double** %52, %53
br i1 %54, label %55, label %63
55: ; preds = %51
%56 = load double**, double*** %6
%57 = load double**, double*** %12
%58 = load i32, i32* %9
%59 = load i32, i32* %10
%60 = call i32 @matrix_copy(double** %56, double** %57, i32 %58, i32 %59)
%61 = load double**, double*** %12
%62 = call i32 @free_matrix2(double** %61)
br label %63
63: ; preds = %55, %51
ret void
}

LLVM IR (-O2):
define void @reorder_rows(double** %0, double** %1, i32* nocapture %2, i32 %3, i32 %4) {
%6 = icmp eq double** %1, %0
br i1 %6, label %7, label %9
7: ; preds = %5
%8 = tail call double** @new_matrix2(i32 %3, i32 %4)
br label %9
9: ; preds = %5, %7
%10 = phi double** [ %8, %7 ], [ %0, %5 ]
%11 = icmp sgt i32 %3, 0
br i1 %11, label %12, label %27
12: ; preds = %9
%13 = shl i32 %4, 3
%14 = zext i32 %3 to i64
br label %15
15: ; preds = %12, %15
%16 = phi i64 [ 0, %12 ], [ %25, %15 ]
%17 = getelementptr inbounds double*, double** %10, i64 %16
%18 = load double*, double** %17
%19 = getelementptr inbounds i32, i32* %2, i64 %16
%20 = load i32, i32* %19
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds double*, double** %1, i64 %21
%23 = load double*, double** %22
%24 = tail call i32 @memcpy(double* %18, double* %23, i32 %13)
%25 = add nuw nsw i64 %16, 1
%26 = icmp eq i64 %25, %14
br i1 %26, label %27, label %15
27: ; preds = %15, %9
br i1 %6, label %28, label %31
28: ; preds = %27
%29 = tail call i32 @matrix_copy(double** %1, double** %10, i32 %3, i32 %4)
%30 = tail call i32 @free_matrix2(double** %10)
br label %31
31: ; preds = %28, %27
ret void
}