void encode_double_array_common(int numBits, long packStart, long datasize,
                                unsigned char *lGrib, const double *data,
                                double zref, double factor, long *gz) {
  long i, z = *gz;
  unsigned int ival;
  int cbits, jbits;
  unsigned int c;
  static unsigned int mask[] = {0, 1, 3, 7, 15, 31, 63, 127, 255};
  cbits = 8;
  c = 0;
  for (i = packStart; i < datasize; i++) {
    ival = (unsigned int)((data[i] - zref) * factor + 0.5);
    jbits = numBits;
    while (cbits <= jbits) {
      if (cbits == 8) {
        jbits -= 8;
        lGrib[z++] = (ival >> jbits) & 0xFF;
      } else {
        jbits -= cbits;
        lGrib[z++] = (c << cbits) + ((ival >> jbits) & mask[cbits]);
        cbits = 8;
        c = 0;
      }
    }
    if (jbits) {
      c = (c << jbits) + (ival & mask[jbits]);
      cbits -= jbits;
    }
  }
  if (cbits != 8)
    lGrib[z++] = c << cbits;
  *gz = z;
}


x86 assembly (clang, -O0):
<encode_double_array_common>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movsd  %xmm0,-0x30(%rbp)
movsd  %xmm1,-0x38(%rbp)
mov    %r9,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
movl   $0x8,-0x58(%rbp)
movl   $0x0,-0x60(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    1277 <encode_double_array_common+0x177>
mov    -0x28(%rbp),%rax
mov    -0x48(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  -0x30(%rbp),%xmm1
subsd  %xmm1,%xmm0
movsd  -0x38(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xe85(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %eax,-0x54(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x58(%rbp),%eax
cmp    -0x5c(%rbp),%eax
jg     122e <encode_double_array_common+0x12e>
cmpl   $0x8,-0x58(%rbp)
jne    11d6 <encode_double_array_common+0xd6>
mov    -0x5c(%rbp),%eax
sub    $0x8,%eax
mov    %eax,-0x5c(%rbp)
mov    -0x54(%rbp),%eax
mov    -0x5c(%rbp),%ecx
shr    %cl,%eax
and    $0xff,%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
mov    -0x50(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x50(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    1229 <encode_double_array_common+0x129>
mov    -0x58(%rbp),%ecx
mov    -0x5c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x5c(%rbp)
mov    -0x60(%rbp),%eax
mov    -0x58(%rbp),%ecx
shl    %cl,%eax
mov    -0x54(%rbp),%edx
mov    -0x5c(%rbp),%ecx
shr    %cl,%edx
mov    %edx,%ecx
movslq -0x58(%rbp),%rsi
lea    0x2e32(%rip),%rdx
and    (%rdx,%rsi,4),%ecx
add    %ecx,%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
mov    -0x50(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x50(%rbp)
mov    %dl,(%rax,%rcx,1)
movl   $0x8,-0x58(%rbp)
movl   $0x0,-0x60(%rbp)
jmp    118d <encode_double_array_common+0x8d>
cmpl   $0x0,-0x5c(%rbp)
je     1261 <encode_double_array_common+0x161>
mov    -0x60(%rbp),%eax
mov    -0x5c(%rbp),%ecx
shl    %cl,%eax
mov    -0x54(%rbp),%ecx
movslq -0x5c(%rbp),%rsi
lea    0x2de2(%rip),%rdx
and    (%rdx,%rsi,4),%ecx
add    %ecx,%eax
mov    %eax,-0x60(%rbp)
mov    -0x5c(%rbp),%ecx
mov    -0x58(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x58(%rbp)
jmp    1266 <encode_double_array_common+0x166>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    1146 <encode_double_array_common+0x46>
cmpl   $0x8,-0x58(%rbp)
je     12a1 <encode_double_array_common+0x1a1>
mov    -0x60(%rbp),%eax
mov    -0x58(%rbp),%ecx
shl    %cl,%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
mov    -0x50(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x50(%rbp)
mov    %dl,(%rax,%rcx,1)
mov    -0x50(%rbp),%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_double_array_common>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    (%r9),%r15
cmp    %rdx,%rsi
jge    17eb <encode_double_array_common+0x6eb>
mov    %rcx,%r12
mov    %r9,-0x30(%rsp)
lea    -0x8(%rdi),%r9d
lea    0x11(%rcx),%rax
mov    %rax,-0x28(%rsp)
mov    $0x8,%r11d
xor    %r14d,%r14d
movsd  0xec4(%rip),%xmm3
lea    0xf4d(%rip),%r10
movdqa 0xf04(%rip),%xmm15
movaps 0xf0c(%rip),%xmm11
movdqa 0xf13(%rip),%xmm8
movsd  %xmm1,-0x8(%rsp)
movsd  %xmm0,-0x10(%rsp)
mov    %r8,-0x18(%rsp)
mov    %rdx,-0x20(%rsp)
mov    %r9d,-0x34(%rsp)
jmp    118d <encode_double_array_common+0x8d>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rsi
cmp    %rdx,%rsi
je     17d2 <encode_double_array_common+0x6d2>
movsd  (%r8,%rsi,8),%xmm2
subsd  %xmm0,%xmm2
mulsd  %xmm1,%xmm2
addsd  %xmm3,%xmm2
cvttsd2si %xmm2,%rbp
cmp    %edi,%r11d
jle    11b0 <encode_double_array_common+0xb0>
mov    %edi,%ebx
jmp    17b0 <encode_double_array_common+0x6b0>
cmp    $0x8,%r11d
jne    11d0 <encode_double_array_common+0xd0>
mov    %ebp,%eax
mov    %r9d,%ecx
shr    %cl,%eax
mov    %r9d,%ebx
jmp    11ee <encode_double_array_common+0xee>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %edi,%ebx
sub    %r11d,%ebx
mov    %r11d,%ecx
shl    %cl,%r14d
mov    %ebp,%eax
mov    %ebx,%ecx
shr    %cl,%eax
movslq %r11d,%rcx
and    (%r10,%rcx,4),%eax
add    %r14d,%eax
xor    %r14d,%r14d
mov    %al,(%r12,%r15,1)
lea    0x1(%r15),%rcx
mov    $0x8,%r11d
cmp    $0x8,%ebx
jge    1210 <encode_double_array_common+0x110>
mov    %rcx,%r15
jmp    17b0 <encode_double_array_common+0x6b0>
nopl   0x0(%rax)
mov    $0x7,%r13d
sub    %ebx,%r13d
cmp    $0xfffffff9,%r13d
mov    $0xfffffff8,%eax
cmovb  %eax,%r13d
add    %ebx,%r13d
cmp    $0x78,%r13d
jb     1789 <encode_double_array_common+0x689>
shr    $0x3,%r13d
add    $0x1,%r13d
mov    %r13d,%r8d
and    $0xfffffff0,%r8d
movd   %ebx,%xmm2
pshufd $0x0,%xmm2,%xmm14
movdqa %xmm14,%xmm13
paddd  0xdb6(%rip),%xmm13
movdqa %xmm14,%xmm12
paddd  0xdb8(%rip),%xmm12
movdqa %xmm14,%xmm9
paddd  0xdba(%rip),%xmm9
paddd  0xdc1(%rip),%xmm14
movd   %ebp,%xmm2
pshufd $0x0,%xmm2,%xmm10
lea    -0x10(%r8),%rax
mov    %rax,%rdx
shr    $0x4,%rdx
add    $0x1,%rdx
test   %rax,%rax
je     15d1 <encode_double_array_common+0x4d1>
mov    %rdx,%r10
and    $0xfffffffffffffffe,%r10
add    -0x28(%rsp),%r15
xor    %r9d,%r9d
movdqa %xmm14,%xmm7
paddd  %xmm15,%xmm7
movdqa %xmm9,%xmm3
paddd  %xmm15,%xmm3
movdqa %xmm12,%xmm2
movdqa %xmm13,%xmm4
paddd  %xmm15,%xmm4
pshuflw $0xfe,%xmm4,%xmm6
movdqa %xmm10,%xmm1
psrld  %xmm6,%xmm1
paddd  %xmm15,%xmm2
pshuflw $0x54,%xmm4,%xmm0
movdqa %xmm10,%xmm6
psrld  %xmm0,%xmm6
pshufd $0xee,%xmm4,%xmm0
pshuflw $0xfe,%xmm0,%xmm4
movdqa %xmm10,%xmm5
psrld  %xmm4,%xmm5
punpcklqdq %xmm1,%xmm6
pshuflw $0x54,%xmm0,%xmm0
movdqa %xmm10,%xmm1
psrld  %xmm0,%xmm1
punpckhqdq %xmm5,%xmm1
pshuflw $0xfe,%xmm2,%xmm0
movdqa %xmm10,%xmm4
psrld  %xmm0,%xmm4
shufps $0xcc,%xmm1,%xmm6
pshuflw $0x54,%xmm2,%xmm0
movdqa %xmm8,%xmm15
movdqa %xmm10,%xmm8
psrld  %xmm0,%xmm8
pshufd $0xee,%xmm2,%xmm0
pshuflw $0xfe,%xmm0,%xmm1
movdqa %xmm10,%xmm2
psrld  %xmm1,%xmm2
punpcklqdq %xmm4,%xmm8
pshuflw $0x54,%xmm0,%xmm0
movdqa %xmm10,%xmm1
psrld  %xmm0,%xmm1
punpckhqdq %xmm2,%xmm1
pshuflw $0xfe,%xmm3,%xmm0
movdqa %xmm10,%xmm4
psrld  %xmm0,%xmm4
shufps $0xcc,%xmm1,%xmm8
pshuflw $0x54,%xmm3,%xmm0
movdqa %xmm10,%xmm2
psrld  %xmm0,%xmm2
pshufd $0xee,%xmm3,%xmm0
pshuflw $0xfe,%xmm0,%xmm1
movdqa %xmm10,%xmm3
psrld  %xmm1,%xmm3
punpcklqdq %xmm4,%xmm2
pshuflw $0x54,%xmm0,%xmm0
movdqa %xmm10,%xmm1
psrld  %xmm0,%xmm1
punpckhqdq %xmm3,%xmm1
pshuflw $0xfe,%xmm7,%xmm0
movdqa %xmm10,%xmm3
psrld  %xmm0,%xmm3
shufps $0xcc,%xmm1,%xmm2
pshuflw $0x54,%xmm7,%xmm0
movdqa %xmm10,%xmm1
psrld  %xmm0,%xmm1
pshufd $0xee,%xmm7,%xmm0
pshuflw $0xfe,%xmm0,%xmm4
movdqa %xmm10,%xmm5
psrld  %xmm4,%xmm5
punpcklqdq %xmm3,%xmm1
pshuflw $0x54,%xmm0,%xmm0
movdqa %xmm10,%xmm3
psrld  %xmm0,%xmm3
punpckhqdq %xmm5,%xmm3
shufps $0xcc,%xmm3,%xmm1
andps  %xmm11,%xmm1
andps  %xmm11,%xmm2
packuswb %xmm2,%xmm1
andps  %xmm11,%xmm8
andps  %xmm11,%xmm6
packuswb %xmm6,%xmm8
packuswb %xmm8,%xmm1
movdqa %xmm15,%xmm8
movdqa 0xc20(%rip),%xmm15
movdqu %xmm1,-0x10(%r15,%r9,1)
movdqa %xmm14,%xmm7
paddd  %xmm8,%xmm7
movdqa %xmm9,%xmm2
paddd  %xmm8,%xmm2
movdqa %xmm12,%xmm0
movdqa %xmm13,%xmm1
paddd  %xmm8,%xmm1
pshuflw $0xfe,%xmm1,%xmm3
movdqa %xmm10,%xmm4
psrld  %xmm3,%xmm4
paddd  %xmm8,%xmm0
pshuflw $0x54,%xmm1,%xmm3
movdqa %xmm10,%xmm6
psrld  %xmm3,%xmm6
pshufd $0xee,%xmm1,%xmm1
pshuflw $0xfe,%xmm1,%xmm3
movdqa %xmm10,%xmm5
psrld  %xmm3,%xmm5
punpcklqdq %xmm4,%xmm6
pshuflw $0x54,%xmm1,%xmm1
movdqa %xmm10,%xmm3
psrld  %xmm1,%xmm3
punpckhqdq %xmm5,%xmm3
pshuflw $0xfe,%xmm0,%xmm1
movdqa %xmm10,%xmm4
psrld  %xmm1,%xmm4
shufps $0xcc,%xmm3,%xmm6
pshuflw $0x54,%xmm0,%xmm1
movdqa %xmm10,%xmm3
psrld  %xmm1,%xmm3
pshufd $0xee,%xmm0,%xmm0
pshuflw $0xfe,%xmm0,%xmm1
movdqa %xmm10,%xmm5
psrld  %xmm1,%xmm5
punpcklqdq %xmm4,%xmm3
pshuflw $0x54,%xmm0,%xmm0
movdqa %xmm10,%xmm1
psrld  %xmm0,%xmm1
punpckhqdq %xmm5,%xmm1
pshuflw $0xfe,%xmm2,%xmm0
movdqa %xmm10,%xmm5
psrld  %xmm0,%xmm5
shufps $0xcc,%xmm1,%xmm3
pshuflw $0x54,%xmm2,%xmm0
movdqa %xmm10,%xmm4
psrld  %xmm0,%xmm4
pshufd $0xee,%xmm2,%xmm0
pshuflw $0xfe,%xmm0,%xmm1
movdqa %xmm10,%xmm2
psrld  %xmm1,%xmm2
punpcklqdq %xmm5,%xmm4
pshuflw $0x54,%xmm0,%xmm0
movdqa %xmm10,%xmm1
psrld  %xmm0,%xmm1
punpckhqdq %xmm2,%xmm1
pshuflw $0xfe,%xmm7,%xmm0
movdqa %xmm10,%xmm2
psrld  %xmm0,%xmm2
shufps $0xcc,%xmm1,%xmm4
pshuflw $0x54,%xmm7,%xmm0
movdqa %xmm10,%xmm1
psrld  %xmm0,%xmm1
pshufd $0xee,%xmm7,%xmm0
pshuflw $0xfe,%xmm0,%xmm5
movdqa %xmm10,%xmm7
psrld  %xmm5,%xmm7
punpcklqdq %xmm2,%xmm1
pshuflw $0x54,%xmm0,%xmm0
movdqa %xmm10,%xmm2
psrld  %xmm0,%xmm2
punpckhqdq %xmm7,%xmm2
movdqa 0xaf9(%rip),%xmm0
shufps $0xcc,%xmm2,%xmm1
andps  %xmm11,%xmm1
andps  %xmm11,%xmm4
packuswb %xmm4,%xmm1
andps  %xmm11,%xmm3
andps  %xmm11,%xmm6
packuswb %xmm6,%xmm3
packuswb %xmm3,%xmm1
movdqu %xmm1,(%r15,%r9,1)
add    $0x20,%r9
paddd  %xmm0,%xmm14
paddd  %xmm0,%xmm9
paddd  %xmm0,%xmm12
paddd  %xmm0,%xmm13
add    $0xfffffffffffffffe,%r10
jne    12b0 <encode_double_array_common+0x1b0>
jmp    15d4 <encode_double_array_common+0x4d4>
xor    %r9d,%r9d
lea    0x0(,%r8,8),%r10d
test   $0x1,%dl
je     1750 <encode_double_array_common+0x650>
add    %rcx,%r9
paddd  %xmm15,%xmm14
paddd  %xmm15,%xmm9
paddd  %xmm15,%xmm12
paddd  %xmm15,%xmm13
pshuflw $0xfe,%xmm13,%xmm0
movdqa %xmm10,%xmm1
psrld  %xmm0,%xmm1
pshuflw $0x54,%xmm13,%xmm0
movdqa %xmm10,%xmm6
psrld  %xmm0,%xmm6
punpcklqdq %xmm1,%xmm6
pshufd $0xee,%xmm13,%xmm0
pshuflw $0xfe,%xmm0,%xmm1
movdqa %xmm10,%xmm2
psrld  %xmm1,%xmm2
pshuflw $0x54,%xmm0,%xmm0
movdqa %xmm10,%xmm1
psrld  %xmm0,%xmm1
punpckhqdq %xmm2,%xmm1
shufps $0xcc,%xmm1,%xmm6
pshuflw $0xfe,%xmm12,%xmm0
movdqa %xmm10,%xmm1
psrld  %xmm0,%xmm1
pshuflw $0x54,%xmm12,%xmm0
movdqa %xmm10,%xmm3
psrld  %xmm0,%xmm3
punpcklqdq %xmm1,%xmm3
pshufd $0xee,%xmm12,%xmm0
pshuflw $0xfe,%xmm0,%xmm1
movdqa %xmm10,%xmm2
psrld  %xmm1,%xmm2
pshuflw $0x54,%xmm0,%xmm0
movdqa %xmm10,%xmm1
psrld  %xmm0,%xmm1
punpckhqdq %xmm2,%xmm1
shufps $0xcc,%xmm1,%xmm3
pshuflw $0xfe,%xmm9,%xmm0
movdqa %xmm10,%xmm1
psrld  %xmm0,%xmm1
pshuflw $0x54,%xmm9,%xmm0
movdqa %xmm10,%xmm2
psrld  %xmm0,%xmm2
punpcklqdq %xmm1,%xmm2
pshufd $0xee,%xmm9,%xmm0
pshuflw $0xfe,%xmm0,%xmm1
movdqa %xmm10,%xmm4
psrld  %xmm1,%xmm4
pshuflw $0x54,%xmm0,%xmm0
movdqa %xmm10,%xmm1
psrld  %xmm0,%xmm1
punpckhqdq %xmm4,%xmm1
shufps $0xcc,%xmm1,%xmm2
pshuflw $0xfe,%xmm14,%xmm0
movdqa %xmm10,%xmm1
psrld  %xmm0,%xmm1
pshuflw $0x54,%xmm14,%xmm0
movdqa %xmm10,%xmm4
psrld  %xmm0,%xmm4
punpcklqdq %xmm1,%xmm4
pshufd $0xee,%xmm14,%xmm0
pshuflw $0xfe,%xmm0,%xmm1
movdqa %xmm10,%xmm5
psrld  %xmm1,%xmm5
pshuflw $0x54,%xmm0,%xmm0
psrld  %xmm0,%xmm10
punpckhqdq %xmm5,%xmm10
shufps $0xcc,%xmm10,%xmm4
movaps %xmm11,%xmm0
andps  %xmm11,%xmm4
andps  %xmm11,%xmm2
packuswb %xmm2,%xmm4
andps  %xmm11,%xmm3
andps  %xmm11,%xmm6
packuswb %xmm6,%xmm3
packuswb %xmm3,%xmm4
movdqu %xmm4,(%r12,%r9,1)
sub    %r10d,%ebx
add    %r8,%rcx
cmp    %r13,%r8
movsd  -0x8(%rsp),%xmm1
movsd  -0x10(%rsp),%xmm0
mov    -0x18(%rsp),%r8
mov    -0x20(%rsp),%rdx
mov    -0x34(%rsp),%r9d
movsd  0x884(%rip),%xmm3
lea    0x90d(%rip),%r10
je     1201 <encode_double_array_common+0x101>
mov    %rcx,%r15
nopl   0x0(%rax)
add    $0xfffffff8,%ebx
mov    %ebp,%eax
mov    %ebx,%ecx
shr    %cl,%eax
mov    %al,(%r12,%r15,1)
add    $0x1,%r15
cmp    $0x7,%ebx
ja     1790 <encode_double_array_common+0x690>
cs nopw 0x0(%rax,%rax,1)
test   %ebx,%ebx
je     1180 <encode_double_array_common+0x80>
mov    %ebx,%ecx
shl    %cl,%r14d
movslq %ebx,%rax
and    (%r10,%rax,4),%ebp
add    %r14d,%ebp
sub    %ebx,%r11d
mov    %ebp,%r14d
jmp    1180 <encode_double_array_common+0x80>
cmp    $0x8,%r11d
je     17e6 <encode_double_array_common+0x6e6>
mov    %r11d,%ecx
shl    %cl,%r14d
mov    %r14b,(%r12,%r15,1)
add    $0x1,%r15
mov    -0x30(%rsp),%r9
mov    %r15,(%r9)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@encode_double_array_common.mask = internal global [9 x i32] [i32 0, i32 1, i32 3, i32 7, i32 15, i32 31, i32 63, i32 127, i32 255]
define void @encode_double_array_common(i32 %0, i64 %1, i64 %2, i8* %3, double* %4, double %5, double %6, i64* %7) {
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
%13 = alloca double*
%14 = alloca double
%15 = alloca double
%16 = alloca i64*
%17 = alloca i64
%18 = alloca i64
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
%22 = alloca i32
store i32 %0, i32* %9
store i64 %1, i64* %10
store i64 %2, i64* %11
store i8* %3, i8** %12
store double* %4, double** %13
store double %5, double* %14
store double %6, double* %15
store i64* %7, i64** %16
%23 = load i64*, i64** %16
%24 = load i64, i64* %23
store i64 %24, i64* %18
store i32 8, i32* %20
store i32 0, i32* %22
%25 = load i64, i64* %10
store i64 %25, i64* %17
br label %26
26: ; preds = %100, %8
%27 = load i64, i64* %17
%28 = load i64, i64* %11
%29 = icmp slt i64 %27, %28
br i1 %29, label %30, label %103
30: ; preds = %26
%31 = load double*, double** %13
%32 = load i64, i64* %17
%33 = getelementptr inbounds double, double* %31, i64 %32
%34 = load double, double* %33
%35 = load double, double* %14
%36 = fsub double %34, %35
%37 = load double, double* %15
%38 = call double @llvm.fmuladd.f64(double %36, double %37, double 5.000000e-01)
%39 = fptoui double %38 to i32
store i32 %39, i32* %19
%40 = load i32, i32* %9
store i32 %40, i32* %21
br label %41
41: ; preds = %81, %30
%42 = load i32, i32* %20
%43 = load i32, i32* %21
%44 = icmp sle i32 %42, %43
br i1 %44, label %45, label %82
45: ; preds = %41
%46 = load i32, i32* %20
%47 = icmp eq i32 %46, 8
br i1 %47, label %48, label %60
48: ; preds = %45
%49 = load i32, i32* %21
%50 = sub nsw i32 %49, 8
store i32 %50, i32* %21
%51 = load i32, i32* %19
%52 = load i32, i32* %21
%53 = lshr i32 %51, %52
%54 = and i32 %53, 255
%55 = trunc i32 %54 to i8
%56 = load i8*, i8** %12
%57 = load i64, i64* %18
%58 = add nsw i64 %57, 1
store i64 %58, i64* %18
%59 = getelementptr inbounds i8, i8* %56, i64 %57
store i8 %55, i8* %59
br label %81
60: ; preds = %45
%61 = load i32, i32* %20
%62 = load i32, i32* %21
%63 = sub nsw i32 %62, %61
store i32 %63, i32* %21
%64 = load i32, i32* %22
%65 = load i32, i32* %20
%66 = shl i32 %64, %65
%67 = load i32, i32* %19
%68 = load i32, i32* %21
%69 = lshr i32 %67, %68
%70 = load i32, i32* %20
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds [9 x i32], [9 x i32]* @encode_double_array_common.mask, i64 0, i64 %71
%73 = load i32, i32* %72
%74 = and i32 %69, %73
%75 = add i32 %66, %74
%76 = trunc i32 %75 to i8
%77 = load i8*, i8** %12
%78 = load i64, i64* %18
%79 = add nsw i64 %78, 1
store i64 %79, i64* %18
%80 = getelementptr inbounds i8, i8* %77, i64 %78
store i8 %76, i8* %80
store i32 8, i32* %20
store i32 0, i32* %22
br label %81
81: ; preds = %60, %48
br label %41
82: ; preds = %41
%83 = load i32, i32* %21
%84 = icmp ne i32 %83, 0
br i1 %84, label %85, label %99
85: ; preds = %82
%86 = load i32, i32* %22
%87 = load i32, i32* %21
%88 = shl i32 %86, %87
%89 = load i32, i32* %19
%90 = load i32, i32* %21
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds [9 x i32], [9 x i32]* @encode_double_array_common.mask, i64 0, i64 %91
%93 = load i32, i32* %92
%94 = and i32 %89, %93
%95 = add i32 %88, %94
store i32 %95, i32* %22
%96 = load i32, i32* %21
%97 = load i32, i32* %20
%98 = sub nsw i32 %97, %96
store i32 %98, i32* %20
br label %99
99: ; preds = %85, %82
br label %100
100: ; preds = %99
%101 = load i64, i64* %17
%102 = add nsw i64 %101, 1
store i64 %102, i64* %17
br label %26
103: ; preds = %26
%104 = load i32, i32* %20
%105 = icmp ne i32 %104, 8
br i1 %105, label %106, label %115
106: ; preds = %103
%107 = load i32, i32* %22
%108 = load i32, i32* %20
%109 = shl i32 %107, %108
%110 = trunc i32 %109 to i8
%111 = load i8*, i8** %12
%112 = load i64, i64* %18
%113 = add nsw i64 %112, 1
store i64 %113, i64* %18
%114 = getelementptr inbounds i8, i8* %111, i64 %112
store i8 %110, i8* %114
br label %115
115: ; preds = %106, %103
%116 = load i64, i64* %18
%117 = load i64*, i64** %16
store i64 %116, i64* %117
ret void
}

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