void gather_vec_double_mul(double *restrict out, const double *restrict in,
                           const unsigned vn,
                           const unsigned int *restrict map) {
  unsigned int i, j;
  while ((i = *map++) != -(unsigned int)1) {
    double *restrict p = &out[i * vn], *pe = p + vn;
    j = *map++;
    do {
      const double *restrict q = &in[j * vn];
      double *restrict pk = p;
      do {
        *pk *= *q;
        ++pk, ++q;
      } while (pk != pe);
    } while ((j = *map++) != -(unsigned int)1);
  }
}


x86 assembly (clang, -O0):
<gather_vec_double_mul>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     11ef <gather_vec_double_mul+0xef>
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
imul   -0x14(%rbp),%ecx
mov    %ecx,%ecx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x14(%rbp),%ecx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
imul   -0x14(%rbp),%ecx
mov    %ecx,%ecx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x48(%rbp),%rax
mulsd  (%rax),%xmm0
movsd  %xmm0,(%rax)
mov    -0x48(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x40(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x38(%rbp),%rax
jne    118e <gather_vec_double_mul+0x8e>
jmp    11cd <gather_vec_double_mul+0xcd>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
cmp    $0xffffffff,%eax
jne    116e <gather_vec_double_mul+0x6e>
jmp    1113 <gather_vec_double_mul+0x13>
pop    %rbp
ret

x86 assembly (clang, -O2):
<gather_vec_double_mul>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,-0x40(%rsp)
mov    (%rcx),%edi
cmp    $0xffffffff,%edi
je     12eb <gather_vec_double_mul+0x1eb>
mov    %edx,%ebp
mov    %rbp,%rax
mov    %rbp,-0x30(%rsp)
lea    -0x1(%rbp),%rax
movabs $0x1fffffffffffffff,%r9
ff ff 1f
and    %rax,%r9
lea    0x1(%r9),%r10
mov    %r10,-0x8(%rsp)
and    $0xfffffffffffffffc,%r10
lea    -0x4(%r10),%r13
mov    %r13,-0x10(%rsp)
shr    $0x2,%r13
add    $0x1,%r13
mov    %r13,%rax
and    $0xfffffffffffffffe,%rax
mov    %rax,-0x18(%rsp)
lea    0x30(%rsi),%rax
mov    %rax,-0x20(%rsp)
mov    -0x40(%rsp),%rax
add    $0x30,%rax
mov    %rax,-0x38(%rsp)
jmp    118b <gather_vec_double_mul+0x8b>
nopl   0x0(%rax,%rax,1)
mov    (%rcx),%edi
cmp    $0xffffffff,%edi
je     12eb <gather_vec_double_mul+0x1eb>
imul   %edx,%edi
mov    -0x40(%rsp),%rax
lea    (%rax,%rdi,8),%r11
mov    -0x30(%rsp),%rax
lea    (%r11,%rax,8),%rbp
mov    0x4(%rcx),%eax
add    $0x8,%rcx
lea    (%r11,%r10,8),%rbx
mov    %rbx,-0x28(%rsp)
mov    -0x38(%rsp),%rbx
lea    (%rbx,%rdi,8),%rdi
jmp    11cb <gather_vec_double_mul+0xcb>
nopl   0x0(%rax,%rax,1)
mov    (%rcx),%eax
add    $0x4,%rcx
cmp    $0xffffffff,%eax
je     1180 <gather_vec_double_mul+0x80>
imul   %edx,%eax
lea    (%rsi,%rax,8),%r12
cmp    $0x3,%r9
jae    11e0 <gather_vec_double_mul+0xe0>
mov    %r11,%rax
jmp    12c0 <gather_vec_double_mul+0x1c0>
cmpq   $0x0,-0x10(%rsp)
je     12e0 <gather_vec_double_mul+0x1e0>
mov    -0x20(%rsp),%rbx
lea    (%rbx,%rax,8),%r14
mov    -0x18(%rsp),%r8
xor    %r15d,%r15d
nopl   (%rax)
movupd -0x30(%r14,%r15,8),%xmm0
movupd -0x20(%r14,%r15,8),%xmm1
movupd -0x30(%rdi,%r15,8),%xmm2
mulpd  %xmm0,%xmm2
movupd -0x20(%rdi,%r15,8),%xmm0
mulpd  %xmm1,%xmm0
movupd -0x10(%rdi,%r15,8),%xmm1
movupd (%rdi,%r15,8),%xmm3
movupd %xmm2,-0x30(%rdi,%r15,8)
movupd %xmm0,-0x20(%rdi,%r15,8)
movupd -0x10(%r14,%r15,8),%xmm0
mulpd  %xmm1,%xmm0
movupd (%r14,%r15,8),%xmm1
mulpd  %xmm3,%xmm1
movupd %xmm0,-0x10(%rdi,%r15,8)
movupd %xmm1,(%rdi,%r15,8)
add    $0x8,%r15
add    $0xfffffffffffffffe,%r8
jne    1200 <gather_vec_double_mul+0x100>
test   $0x1,%r13b
je     12a0 <gather_vec_double_mul+0x1a0>
movupd (%r12,%r15,8),%xmm0
movupd 0x10(%r12,%r15,8),%xmm1
movupd (%r11,%r15,8),%xmm2
mulpd  %xmm0,%xmm2
movupd 0x10(%r11,%r15,8),%xmm0
mulpd  %xmm1,%xmm0
movupd %xmm2,(%r11,%r15,8)
movupd %xmm0,0x10(%r11,%r15,8)
cmp    %r10,-0x8(%rsp)
je     11c0 <gather_vec_double_mul+0xc0>
lea    (%r12,%r10,8),%r12
mov    -0x28(%rsp),%rax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  (%r12),%xmm0
mulsd  (%rax),%xmm0
movsd  %xmm0,(%rax)
add    $0x8,%rax
add    $0x8,%r12
cmp    %rbp,%rax
jne    12c0 <gather_vec_double_mul+0x1c0>
jmp    11c0 <gather_vec_double_mul+0xc0>
xor    %r15d,%r15d
test   $0x1,%r13b
jne    1271 <gather_vec_double_mul+0x171>
jmp    12a0 <gather_vec_double_mul+0x1a0>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @gather_vec_double_mul(double* noalias %0, double* noalias nocapture %1, i32 %2, i32* noalias nocapture %3) {
%5 = load i32, i32* %3
%6 = icmp eq i32 %5, -1
br i1 %6, label %120, label %7
7: ; preds = %4
%8 = zext i32 %2 to i64
%9 = add nuw nsw i64 %8, 2305843009213693951
%10 = and i64 %9, 2305843009213693951
%11 = add nuw nsw i64 %10, 1
%12 = and i64 %11, 4611686018427387900
%13 = add nsw i64 %12, -4
%14 = lshr exact i64 %13, 2
%15 = add nuw nsw i64 %14, 1
%16 = icmp ult i64 %10, 3
%17 = and i64 %11, 4611686018427387900
%18 = and i64 %15, 1
%19 = icmp eq i64 %13, 0
%20 = and i64 %15, 9223372036854775806
%21 = icmp eq i64 %18, 0
%22 = icmp eq i64 %11, %17
br label %26
23: ; preds = %116
%24 = load i32, i32* %117
%25 = icmp eq i32 %24, -1
br i1 %25, label %120, label %26
26: ; preds = %7, %23
%27 = phi i32 [ %5, %7 ], [ %24, %23 ]
%28 = phi i32* [ %3, %7 ], [ %117, %23 ]
%29 = getelementptr inbounds i32, i32* %28, i64 1
%30 = mul i32 %27, %2
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds double, double* %0, i64 %31
%33 = getelementptr inbounds double, double* %32, i64 %8
%34 = getelementptr inbounds i32, i32* %28, i64 2
%35 = load i32, i32* %29
%36 = getelementptr double, double* %32, i64 %17
br label %37
37: ; preds = %116, %26
%38 = phi i32 [ %35, %26 ], [ %118, %116 ]
%39 = phi i32* [ %34, %26 ], [ %117, %116 ]
%40 = mul i32 %38, %2
%41 = zext i32 %40 to i64
%42 = getelementptr inbounds double, double* %1, i64 %41
br i1 %16, label %104, label %43
43: ; preds = %37
%44 = getelementptr double, double* %42, i64 %17
br i1 %19, label %84, label %45
45: ; preds = %43, %45
%46 = phi i64 [ %81, %45 ], [ 0, %43 ]
%47 = phi i64 [ %82, %45 ], [ 0, %43 ]
%48 = getelementptr double, double* %42, i64 %46
%49 = getelementptr double, double* %32, i64 %46
%50 = bitcast double* %48 to <2 x double>*
%51 = load <2 x double>, <2 x double>* %50
%52 = getelementptr double, double* %48, i64 2
%53 = bitcast double* %52 to <2 x double>*
%54 = load <2 x double>, <2 x double>* %53
%55 = bitcast double* %49 to <2 x double>*
%56 = load <2 x double>, <2 x double>* %55
%57 = getelementptr double, double* %49, i64 2
%58 = bitcast double* %57 to <2 x double>*
%59 = load <2 x double>, <2 x double>* %58
%60 = fmul <2 x double> %51, %56
%61 = fmul <2 x double> %54, %59
%62 = bitcast double* %49 to <2 x double>*
store <2 x double> %60, <2 x double>* %62
%63 = bitcast double* %57 to <2 x double>*
store <2 x double> %61, <2 x double>* %63
%64 = or i64 %46, 4
%65 = getelementptr double, double* %42, i64 %64
%66 = getelementptr double, double* %32, i64 %64
%67 = bitcast double* %65 to <2 x double>*
%68 = load <2 x double>, <2 x double>* %67
%69 = getelementptr double, double* %65, i64 2
%70 = bitcast double* %69 to <2 x double>*
%71 = load <2 x double>, <2 x double>* %70
%72 = bitcast double* %66 to <2 x double>*
%73 = load <2 x double>, <2 x double>* %72
%74 = getelementptr double, double* %66, i64 2
%75 = bitcast double* %74 to <2 x double>*
%76 = load <2 x double>, <2 x double>* %75
%77 = fmul <2 x double> %68, %73
%78 = fmul <2 x double> %71, %76
%79 = bitcast double* %66 to <2 x double>*
store <2 x double> %77, <2 x double>* %79
%80 = bitcast double* %74 to <2 x double>*
store <2 x double> %78, <2 x double>* %80
%81 = add nuw i64 %46, 8
%82 = add i64 %47, 2
%83 = icmp eq i64 %82, %20
br i1 %83, label %84, label %45
84: ; preds = %45, %43
%85 = phi i64 [ 0, %43 ], [ %81, %45 ]
br i1 %21, label %103, label %86
86: ; preds = %84
%87 = getelementptr double, double* %42, i64 %85
%88 = getelementptr double, double* %32, i64 %85
%89 = bitcast double* %87 to <2 x double>*
%90 = load <2 x double>, <2 x double>* %89
%91 = getelementptr double, double* %87, i64 2
%92 = bitcast double* %91 to <2 x double>*
%93 = load <2 x double>, <2 x double>* %92
%94 = bitcast double* %88 to <2 x double>*
%95 = load <2 x double>, <2 x double>* %94
%96 = getelementptr double, double* %88, i64 2
%97 = bitcast double* %96 to <2 x double>*
%98 = load <2 x double>, <2 x double>* %97
%99 = fmul <2 x double> %90, %95
%100 = fmul <2 x double> %93, %98
%101 = bitcast double* %88 to <2 x double>*
store <2 x double> %99, <2 x double>* %101
%102 = bitcast double* %96 to <2 x double>*
store <2 x double> %100, <2 x double>* %102
br label %103
103: ; preds = %84, %86
br i1 %22, label %116, label %104
104: ; preds = %37, %103
%105 = phi double* [ %42, %37 ], [ %44, %103 ]
%106 = phi double* [ %32, %37 ], [ %36, %103 ]
br label %107
107: ; preds = %104, %107
%108 = phi double* [ %114, %107 ], [ %105, %104 ]
%109 = phi double* [ %113, %107 ], [ %106, %104 ]
%110 = load double, double* %108
%111 = load double, double* %109
%112 = fmul double %110, %111
store double %112, double* %109
%113 = getelementptr inbounds double, double* %109, i64 1
%114 = getelementptr inbounds double, double* %108, i64 1
%115 = icmp eq double* %113, %33
br i1 %115, label %116, label %107
116: ; preds = %107, %103
%117 = getelementptr inbounds i32, i32* %39, i64 1
%118 = load i32, i32* %39
%119 = icmp eq i32 %118, -1
br i1 %119, label %23, label %37
120: ; preds = %23, %4
ret void
}