void grid_copy(int rmin, int rmax, int cmin, int cmax, double **grid,
               double **next_grid) {
#pragma offload_transfer target(mic) in(grid                                   \
                                        : length(nrows + 2 * ncols + 2) ALLOC) \
    in(next_grid                                                               \
       : length(nrows + 2 * ncols + 2) ALLOC)
#pragma offload target(mic) in(grid : REUSE)in(next_grid : REUSE)
  {
    int i, j;
#pragma omp parallel for private(i, j)
    for (i = rmin; i <= rmax; i++) {
      for (j = cmin; j <= cmax; j++) {
        grid[i][j] = next_grid[i][j];
      }
    }
  }
#pragma offload_transfer target(mic)                                           \
    nocopy(grid, next_grid                                                     \
           : length(nrows + 2 * ncols + 2) FREE)
}


x86 assembly (clang, -O0):
<grid_copy>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    %r8,-0x18(%rbp)
mov    %r9,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x8(%rbp),%eax
jg     1187 <grid_copy+0x87>
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     1174 <grid_copy+0x74>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x28(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x28(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1130 <grid_copy+0x30>
jmp    1179 <grid_copy+0x79>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    111e <grid_copy+0x1e>
pop    %rbp
ret

x86 assembly (clang, -O2):
<grid_copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
cmp    %esi,%edi
jle    1119 <grid_copy+0x19>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r9,%r11
mov    %r8,%r13
mov    %esi,%r8d
movslq %edx,%rsi
movslq %edi,%r9
add    $0x1,%r8d
mov    %ecx,%eax
sub    %esi,%eax
lea    (%rsi,%rax,1),%rdi
add    $0x1,%rdi
mov    %rdi,-0x48(%rsp)
mov    %rax,-0x8(%rsp)
add    $0x1,%rax
mov    %rax,-0x10(%rsp)
and    $0xfffffffffffffffc,%rax
lea    -0x4(%rax),%rdi
mov    %rdi,-0x20(%rsp)
shr    $0x2,%rdi
add    $0x1,%rdi
lea    0x1(%rcx),%ebx
mov    %rax,-0x18(%rsp)
add    %rsi,%rax
mov    %rax,-0x28(%rsp)
mov    %edi,%r12d
and    $0x3,%r12d
and    $0xfffffffffffffffc,%rdi
mov    %rdi,-0x38(%rsp)
lea    0xe(%rsi),%rax
mov    %rax,-0x40(%rsp)
mov    %r12,-0x30(%rsp)
shl    $0x5,%r12
jmp    11ad <grid_copy+0xad>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r9
cmp    %r9d,%r8d
je     110e <grid_copy+0xe>
cmp    %edx,%ecx
jl     11a0 <grid_copy+0xa0>
mov    (%r11,%r9,8),%r15
mov    0x0(%r13,%r9,8),%r14
mov    %rsi,%rax
cmpl   $0x3,-0x8(%rsp)
jb     12f0 <grid_copy+0x1f0>
lea    (%r14,%rsi,8),%rax
mov    -0x48(%rsp),%rdi
lea    (%r15,%rdi,8),%rdi
cmp    %rdi,%rax
jae    11f3 <grid_copy+0xf3>
mov    -0x48(%rsp),%rax
lea    (%r14,%rax,8),%rdi
lea    (%r15,%rsi,8),%rbp
mov    %rsi,%rax
cmp    %rdi,%rbp
jb     12f0 <grid_copy+0x1f0>
mov    %r13,%rbp
mov    %r11,%rdi
cmpq   $0xc,-0x20(%rsp)
jae    1208 <grid_copy+0x108>
xor    %eax,%eax
jmp    1289 <grid_copy+0x189>
mov    -0x40(%rsp),%rax
lea    (%r14,%rax,8),%r13
lea    (%r15,%rax,8),%r11
mov    -0x38(%rsp),%r10
xor    %eax,%eax
nopl   0x0(%rax)
movups -0x70(%r11,%rax,8),%xmm0
movups -0x60(%r11,%rax,8),%xmm1
movups %xmm0,-0x70(%r13,%rax,8)
movups %xmm1,-0x60(%r13,%rax,8)
movups -0x50(%r11,%rax,8),%xmm0
movups -0x40(%r11,%rax,8),%xmm1
movups %xmm0,-0x50(%r13,%rax,8)
movups %xmm1,-0x40(%r13,%rax,8)
movups -0x30(%r11,%rax,8),%xmm0
movups -0x20(%r11,%rax,8),%xmm1
movups %xmm0,-0x30(%r13,%rax,8)
movups %xmm1,-0x20(%r13,%rax,8)
movups -0x10(%r11,%rax,8),%xmm0
movups (%r11,%rax,8),%xmm1
movups %xmm0,-0x10(%r13,%rax,8)
movups %xmm1,0x0(%r13,%rax,8)
add    $0x10,%rax
add    $0xfffffffffffffffc,%r10
jne    1220 <grid_copy+0x120>
cmpq   $0x0,-0x30(%rsp)
mov    %rdi,%r11
mov    %rbp,%r13
je     12cc <grid_copy+0x1cc>
add    %rsi,%rax
lea    (%r14,%rax,8),%rbp
add    $0x10,%rbp
lea    (%r15,%rax,8),%rax
add    $0x10,%rax
xor    %edi,%edi
nopl   0x0(%rax)
movups -0x10(%rax,%rdi,1),%xmm0
movups (%rax,%rdi,1),%xmm1
movups %xmm0,-0x10(%rbp,%rdi,1)
movups %xmm1,0x0(%rbp,%rdi,1)
add    $0x20,%rdi
cmp    %rdi,%r12
jne    12b0 <grid_copy+0x1b0>
mov    -0x28(%rsp),%rax
mov    -0x18(%rsp),%rdi
cmp    %rdi,-0x10(%rsp)
je     11a0 <grid_copy+0xa0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%ebp
sub    %eax,%ebp
mov    %ecx,%r10d
sub    %eax,%r10d
and    $0x3,%ebp
je     1315 <grid_copy+0x215>
nop
movsd  (%r15,%rax,8),%xmm0
movsd  %xmm0,(%r14,%rax,8)
add    $0x1,%rax
add    $0xffffffff,%ebp
jne    1300 <grid_copy+0x200>
cmp    $0x3,%r10d
jb     11a0 <grid_copy+0xa0>
nop
movsd  (%r15,%rax,8),%xmm0
movsd  %xmm0,(%r14,%rax,8)
movsd  0x8(%r15,%rax,8),%xmm0
movsd  %xmm0,0x8(%r14,%rax,8)
movsd  0x10(%r15,%rax,8),%xmm0
movsd  %xmm0,0x10(%r14,%rax,8)
movsd  0x18(%r15,%rax,8),%xmm0
movsd  %xmm0,0x18(%r14,%rax,8)
add    $0x4,%rax
cmp    %eax,%ebx
jne    1320 <grid_copy+0x220>
jmp    11a0 <grid_copy+0xa0>

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

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