void _nonreg_x_construct(double val[], double inmat[], int shape[]) {
  int k, j, i, idx0, idx1, gap0, gap1;
  gap0 = shape[0] - 2;
  gap1 = (shape[1] - 2) * shape[0];
  idx0 = 0;
  idx1 = 0;
  for (k = 0; k < 2; k++) {
    for (j = 0; j < 2; j++) {
      for (i = 0; i < 2; i++) {
        val[idx1] = inmat[idx0];
        idx1++;
        idx0++;
      }
      idx0 = idx0 + gap0;
    }
    idx0 = idx0 + gap1;
  }
}


x86 assembly (clang, -O0):
<_nonreg_x_construct>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
sub    $0x2,%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%eax
sub    $0x2,%eax
mov    -0x18(%rbp),%rcx
imul   (%rcx),%eax
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jge    11d9 <_nonreg_x_construct+0xd9>
movl   $0x0,-0x20(%rbp)
cmpl   $0x2,-0x20(%rbp)
jge    11c2 <_nonreg_x_construct+0xc2>
movl   $0x0,-0x24(%rbp)
cmpl   $0x2,-0x24(%rbp)
jge    11ab <_nonreg_x_construct+0xab>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1167 <_nonreg_x_construct+0x67>
mov    -0x28(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1156 <_nonreg_x_construct+0x56>
mov    -0x28(%rbp),%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1145 <_nonreg_x_construct+0x45>
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nonreg_x_construct>:
movslq (%rdx),%rax
mov    0x4(%rdx),%ecx
imul   %eax,%ecx
movslq %ecx,%rcx
movsd  (%rsi),%xmm0
movsd  %xmm0,(%rdi)
movsd  0x8(%rsi),%xmm0
movsd  %xmm0,0x8(%rdi)
movsd  (%rsi,%rax,8),%xmm0
movsd  %xmm0,0x10(%rdi)
movsd  0x8(%rsi,%rax,8),%xmm0
movsd  %xmm0,0x18(%rdi)
movsd  (%rsi,%rcx,8),%xmm0
movsd  %xmm0,0x20(%rdi)
movsd  0x8(%rsi,%rcx,8),%xmm0
movsd  %xmm0,0x28(%rdi)
add    %eax,%ecx
movslq %ecx,%rax
movsd  (%rsi,%rax,8),%xmm0
movsd  %xmm0,0x30(%rdi)
movsd  0x8(%rsi,%rax,8),%xmm0
movsd  %xmm0,0x38(%rdi)
ret

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

LLVM IR (-O2):
define void @_nonreg_x_construct(double* nocapture writeonly %0, double* nocapture %1, i32* nocapture %2) {
%4 = load i32, i32* %2
%5 = getelementptr inbounds i32, i32* %2, i64 1
%6 = load i32, i32* %5
%7 = mul i32 %6, %4
%8 = sext i32 %7 to i64
%9 = load double, double* %1
store double %9, double* %0
%10 = getelementptr inbounds double, double* %1, i64 1
%11 = load double, double* %10
%12 = getelementptr inbounds double, double* %0, i64 1
store double %11, double* %12
%13 = sext i32 %4 to i64
%14 = getelementptr inbounds double, double* %1, i64 %13
%15 = load double, double* %14
%16 = getelementptr inbounds double, double* %0, i64 2
store double %15, double* %16
%17 = add nsw i64 %13, 1
%18 = getelementptr inbounds double, double* %1, i64 %17
%19 = load double, double* %18
%20 = getelementptr inbounds double, double* %0, i64 3
store double %19, double* %20
%21 = getelementptr inbounds double, double* %1, i64 %8
%22 = load double, double* %21
%23 = getelementptr inbounds double, double* %0, i64 4
store double %22, double* %23
%24 = add nsw i64 %8, 1
%25 = getelementptr inbounds double, double* %1, i64 %24
%26 = load double, double* %25
%27 = getelementptr inbounds double, double* %0, i64 5
store double %26, double* %27
%28 = add i32 %4, %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds double, double* %1, i64 %29
%31 = load double, double* %30
%32 = getelementptr inbounds double, double* %0, i64 6
store double %31, double* %32
%33 = add nsw i64 %29, 1
%34 = getelementptr inbounds double, double* %1, i64 %33
%35 = load double, double* %34
%36 = getelementptr inbounds double, double* %0, i64 7
store double %35, double* %36
ret void
}