int copy(double *A, double **B, int rows, int cols) {
  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
      (*B)[i * cols + j] = A[i * cols + j];
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1188 <copy+0x88>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1175 <copy+0x75>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
imul   -0x18(%rbp),%ecx
add    -0x20(%rbp),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x1c(%rbp),%ecx
imul   -0x18(%rbp),%ecx
add    -0x20(%rbp),%ecx
movslq %ecx,%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    112c <copy+0x2c>
jmp    117a <copy+0x7a>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1119 <copy+0x19>
xor    %eax,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %ecx,-0x5c(%rsp)
mov    %rsi,-0x18(%rsp)
mov    %rdi,-0x40(%rsp)
test   %edx,%edx
jle    13b4 <copy+0x2b4>
movslq -0x5c(%rsp),%r13
mov    %edx,%eax
mov    %rax,-0x10(%rsp)
mov    %r13d,%edx
mov    %edx,%eax
and    $0xfffffffc,%eax
mov    %rax,-0x20(%rsp)
add    $0xfffffffffffffffc,%rax
mov    %rax,-0x28(%rsp)
shr    $0x2,%rax
add    $0x1,%rax
mov    %eax,%r15d
and    $0x3,%r15d
and    $0xfffffffffffffffc,%rax
mov    %rax,-0x38(%rsp)
mov    %edx,%r12d
and    $0x3,%r12d
mov    -0x40(%rsp),%rdi
lea    0x70(%rdi),%rbx
lea    0x0(,%r13,8),%rax
mov    %rax,-0x8(%rsp)
lea    0x10(%rdi),%rax
mov    %r15,-0x30(%rsp)
shl    $0x5,%r15
mov    $0xe,%r10d
mov    $0x2,%r14d
xor    %r11d,%r11d
xor    %r9d,%r9d
mov    %r12,-0x48(%rsp)
mov    %r13,-0x50(%rsp)
jmp    11d6 <copy+0xd6>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%r9
add    %r13,%r10
mov    -0x8(%rsp),%rcx
add    %rcx,%rbx
add    %r13,%r14
add    %rcx,%rax
add    %r13,%r11
add    %rcx,%rdi
cmp    -0x10(%rsp),%r9
je     13b4 <copy+0x2b4>
cmpl   $0x0,-0x5c(%rsp)
jle    11b0 <copy+0xb0>
mov    -0x18(%rsp),%rcx
mov    (%rcx),%rbp
cmpl   $0x4,-0x5c(%rsp)
mov    %rbp,-0x58(%rsp)
jae    1200 <copy+0x100>
xor    %r8d,%r8d
jmp    1320 <copy+0x220>
nopl   0x0(%rax)
mov    %r13,%rsi
mov    %r9,%rcx
imul   %r13,%rcx
lea    (%rcx,%rdx,1),%rsi
mov    -0x40(%rsp),%r12
lea    (%r12,%rsi,8),%r8
mov    -0x58(%rsp),%rbp
lea    0x0(%rbp,%rcx,8),%rbp
cmp    %r8,%rbp
mov    -0x58(%rsp),%r8
jae    124a <copy+0x14a>
lea    (%r12,%rcx,8),%rcx
lea    (%r8,%rsi,8),%rsi
cmp    %rsi,%rcx
jae    124a <copy+0x14a>
xor    %r8d,%r8d
mov    -0x50(%rsp),%r13
mov    -0x48(%rsp),%r12
jmp    1320 <copy+0x220>
cmpq   $0xc,-0x28(%rsp)
jae    125b <copy+0x15b>
xor    %esi,%esi
mov    -0x50(%rsp),%r13
jmp    12c9 <copy+0x1c9>
lea    (%r8,%r10,8),%rbp
mov    -0x38(%rsp),%rcx
xor    %esi,%esi
mov    -0x50(%rsp),%r13
nopl   0x0(%rax,%rax,1)
movups -0x70(%rbx,%rsi,8),%xmm0
movups -0x60(%rbx,%rsi,8),%xmm1
movups %xmm0,-0x70(%rbp,%rsi,8)
movups %xmm1,-0x60(%rbp,%rsi,8)
movups -0x50(%rbx,%rsi,8),%xmm0
movups -0x40(%rbx,%rsi,8),%xmm1
movups %xmm0,-0x50(%rbp,%rsi,8)
movups %xmm1,-0x40(%rbp,%rsi,8)
movups -0x30(%rbx,%rsi,8),%xmm0
movups -0x20(%rbx,%rsi,8),%xmm1
movups %xmm0,-0x30(%rbp,%rsi,8)
movups %xmm1,-0x20(%rbp,%rsi,8)
movups -0x10(%rbx,%rsi,8),%xmm0
movups (%rbx,%rsi,8),%xmm1
movups %xmm0,-0x10(%rbp,%rsi,8)
movups %xmm1,0x0(%rbp,%rsi,8)
add    $0x10,%rsi
add    $0xfffffffffffffffc,%rcx
jne    1270 <copy+0x170>
cmpq   $0x0,-0x30(%rsp)
je     12fb <copy+0x1fb>
lea    (%rsi,%r14,1),%rcx
lea    (%r8,%rcx,8),%rcx
lea    (%rax,%rsi,8),%rsi
xor    %ebp,%ebp
nop
movups -0x10(%rsi,%rbp,1),%xmm0
movups (%rsi,%rbp,1),%xmm1
movups %xmm0,-0x10(%rcx,%rbp,1)
movups %xmm1,(%rcx,%rbp,1)
add    $0x20,%rbp
cmp    %rbp,%r15
jne    12e0 <copy+0x1e0>
mov    -0x20(%rsp),%rcx
mov    %rcx,%r8
cmp    %rdx,%rcx
mov    -0x48(%rsp),%r12
je     11b0 <copy+0xb0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r8,%rcx
not    %rcx
add    %rdx,%rcx
test   %r12,%r12
je     1356 <copy+0x256>
mov    -0x58(%rsp),%rsi
lea    (%rsi,%r11,8),%rsi
nopw   0x0(%rax,%rax,1)
movsd  (%rdi,%r8,8),%xmm0
movsd  %xmm0,(%rsi,%r8,8)
add    $0x1,%r8
add    $0xffffffffffffffff,%r12
jne    1340 <copy+0x240>
cmp    $0x3,%rcx
mov    -0x48(%rsp),%r12
jb     11b0 <copy+0xb0>
mov    -0x58(%rsp),%rcx
lea    (%rcx,%r14,8),%rcx
xchg   %ax,%ax
movsd  (%rdi,%r8,8),%xmm0
movsd  %xmm0,-0x10(%rcx,%r8,8)
movsd  0x8(%rdi,%r8,8),%xmm0
movsd  %xmm0,-0x8(%rcx,%r8,8)
movsd  0x10(%rdi,%r8,8),%xmm0
movsd  %xmm0,(%rcx,%r8,8)
movsd  0x18(%rdi,%r8,8),%xmm0
movsd  %xmm0,0x8(%rcx,%r8,8)
add    $0x4,%r8
cmp    %r8,%rdx
jne    1370 <copy+0x270>
jmp    11b0 <copy+0xb0>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @copy(double* nocapture %0, double** nocapture %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %130
6: ; preds = %4
%7 = icmp sgt i32 %3, 0
%8 = sext i32 %3 to i64
%9 = zext i32 %2 to i64
%10 = zext i32 %3 to i64
%11 = and i64 %10, 4294967292
%12 = add nsw i64 %11, -4
%13 = lshr exact i64 %12, 2
%14 = add nuw nsw i64 %13, 1
%15 = icmp ult i32 %3, 4
%16 = and i64 %10, 4294967292
%17 = and i64 %14, 3
%18 = icmp ult i64 %12, 12
%19 = and i64 %14, 9223372036854775804
%20 = icmp eq i64 %17, 0
%21 = icmp eq i64 %16, %10
%22 = and i64 %10, 3
%23 = icmp eq i64 %22, 0
br label %24
24: ; preds = %6, %131
%25 = phi i64 [ 0, %6 ], [ %132, %131 ]
%26 = mul i64 %25, %8
%27 = add i64 %26, %10
%28 = getelementptr double, double* %0, i64 %26
%29 = getelementptr double, double* %0, i64 %27
br i1 %7, label %30, label %131
30: ; preds = %24
%31 = mul nsw i64 %25, %8
%32 = load double*, double** %1
br i1 %15, label %113, label %33
33: ; preds = %30
%34 = getelementptr double, double* %32, i64 %26
%35 = getelementptr double, double* %32, i64 %27
%36 = icmp ult double* %34, %29
%37 = icmp ult double* %28, %35
%38 = and i1 %36, %37
br i1 %38, label %113, label %39
39: ; preds = %33
br i1 %18, label %93, label %40
40: ; preds = %39, %40
%41 = phi i64 [ %90, %40 ], [ 0, %39 ]
%42 = phi i64 [ %91, %40 ], [ 0, %39 ]
%43 = add nsw i64 %41, %31
%44 = getelementptr inbounds double, double* %0, i64 %43
%45 = bitcast double* %44 to <2 x double>*
%46 = load <2 x double>, <2 x double>* %45
%47 = getelementptr inbounds double, double* %44, i64 2
%48 = bitcast double* %47 to <2 x double>*
%49 = load <2 x double>, <2 x double>* %48
%50 = getelementptr inbounds double, double* %32, i64 %43
%51 = bitcast double* %50 to <2 x double>*
store <2 x double> %46, <2 x double>* %51
%52 = getelementptr inbounds double, double* %50, i64 2
%53 = bitcast double* %52 to <2 x double>*
store <2 x double> %49, <2 x double>* %53
%54 = or i64 %41, 4
%55 = add nsw i64 %54, %31
%56 = getelementptr inbounds double, double* %0, i64 %55
%57 = bitcast double* %56 to <2 x double>*
%58 = load <2 x double>, <2 x double>* %57
%59 = getelementptr inbounds double, double* %56, i64 2
%60 = bitcast double* %59 to <2 x double>*
%61 = load <2 x double>, <2 x double>* %60
%62 = getelementptr inbounds double, double* %32, i64 %55
%63 = bitcast double* %62 to <2 x double>*
store <2 x double> %58, <2 x double>* %63
%64 = getelementptr inbounds double, double* %62, i64 2
%65 = bitcast double* %64 to <2 x double>*
store <2 x double> %61, <2 x double>* %65
%66 = or i64 %41, 8
%67 = add nsw i64 %66, %31
%68 = getelementptr inbounds double, double* %0, i64 %67
%69 = bitcast double* %68 to <2 x double>*
%70 = load <2 x double>, <2 x double>* %69
%71 = getelementptr inbounds double, double* %68, i64 2
%72 = bitcast double* %71 to <2 x double>*
%73 = load <2 x double>, <2 x double>* %72
%74 = getelementptr inbounds double, double* %32, i64 %67
%75 = bitcast double* %74 to <2 x double>*
store <2 x double> %70, <2 x double>* %75
%76 = getelementptr inbounds double, double* %74, i64 2
%77 = bitcast double* %76 to <2 x double>*
store <2 x double> %73, <2 x double>* %77
%78 = or i64 %41, 12
%79 = add nsw i64 %78, %31
%80 = getelementptr inbounds double, double* %0, i64 %79
%81 = bitcast double* %80 to <2 x double>*
%82 = load <2 x double>, <2 x double>* %81
%83 = getelementptr inbounds double, double* %80, i64 2
%84 = bitcast double* %83 to <2 x double>*
%85 = load <2 x double>, <2 x double>* %84
%86 = getelementptr inbounds double, double* %32, i64 %79
%87 = bitcast double* %86 to <2 x double>*
store <2 x double> %82, <2 x double>* %87
%88 = getelementptr inbounds double, double* %86, i64 2
%89 = bitcast double* %88 to <2 x double>*
store <2 x double> %85, <2 x double>* %89
%90 = add nuw i64 %41, 16
%91 = add nuw i64 %42, 4
%92 = icmp eq i64 %91, %19
br i1 %92, label %93, label %40
93: ; preds = %40, %39
%94 = phi i64 [ 0, %39 ], [ %90, %40 ]
br i1 %20, label %112, label %95
95: ; preds = %93, %95
%96 = phi i64 [ %109, %95 ], [ %94, %93 ]
%97 = phi i64 [ %110, %95 ], [ 0, %93 ]
%98 = add nsw i64 %96, %31
%99 = getelementptr inbounds double, double* %0, i64 %98
%100 = bitcast double* %99 to <2 x double>*
%101 = load <2 x double>, <2 x double>* %100
%102 = getelementptr inbounds double, double* %99, i64 2
%103 = bitcast double* %102 to <2 x double>*
%104 = load <2 x double>, <2 x double>* %103
%105 = getelementptr inbounds double, double* %32, i64 %98
%106 = bitcast double* %105 to <2 x double>*
store <2 x double> %101, <2 x double>* %106
%107 = getelementptr inbounds double, double* %105, i64 2
%108 = bitcast double* %107 to <2 x double>*
store <2 x double> %104, <2 x double>* %108
%109 = add nuw i64 %96, 4
%110 = add i64 %97, 1
%111 = icmp eq i64 %110, %17
br i1 %111, label %112, label %95
112: ; preds = %95, %93
br i1 %21, label %131, label %113
113: ; preds = %33, %30, %112
%114 = phi i64 [ 0, %33 ], [ 0, %30 ], [ %16, %112 ]
%115 = xor i64 %114, -1
%116 = add nsw i64 %115, %10
br i1 %23, label %127, label %117
117: ; preds = %113, %117
%118 = phi i64 [ %124, %117 ], [ %114, %113 ]
%119 = phi i64 [ %125, %117 ], [ 0, %113 ]
%120 = add nsw i64 %118, %31
%121 = getelementptr inbounds double, double* %0, i64 %120
%122 = load double, double* %121
%123 = getelementptr inbounds double, double* %32, i64 %120
store double %122, double* %123
%124 = add nuw nsw i64 %118, 1
%125 = add i64 %119, 1
%126 = icmp eq i64 %125, %22
br i1 %126, label %127, label %117
127: ; preds = %117, %113
%128 = phi i64 [ %114, %113 ], [ %124, %117 ]
%129 = icmp ult i64 %116, 3
br i1 %129, label %131, label %134
130: ; preds = %131, %4
ret i32 0
131: ; preds = %127, %134, %112, %24
%132 = add nuw nsw i64 %25, 1
%133 = icmp eq i64 %132, %9
br i1 %133, label %130, label %24
134: ; preds = %127, %134
%135 = phi i64 [ %155, %134 ], [ %128, %127 ]
%136 = add nsw i64 %135, %31
%137 = getelementptr inbounds double, double* %0, i64 %136
%138 = load double, double* %137
%139 = getelementptr inbounds double, double* %32, i64 %136
store double %138, double* %139
%140 = add nuw nsw i64 %135, 1
%141 = add nsw i64 %140, %31
%142 = getelementptr inbounds double, double* %0, i64 %141
%143 = load double, double* %142
%144 = getelementptr inbounds double, double* %32, i64 %141
store double %143, double* %144
%145 = add nuw nsw i64 %135, 2
%146 = add nsw i64 %145, %31
%147 = getelementptr inbounds double, double* %0, i64 %146
%148 = load double, double* %147
%149 = getelementptr inbounds double, double* %32, i64 %146
store double %148, double* %149
%150 = add nuw nsw i64 %135, 3
%151 = add nsw i64 %150, %31
%152 = getelementptr inbounds double, double* %0, i64 %151
%153 = load double, double* %152
%154 = getelementptr inbounds double, double* %32, i64 %151
store double %153, double* %154
%155 = add nuw nsw i64 %135, 4
%156 = icmp eq i64 %155, %10
br i1 %156, label %131, label %134
}