int popcount5(unsigned *array, int len) {
  int i, val, result = 0;
  int SSE_mask[] = {0x0f0f0f0f, 0x00f0f0f0f, 0x0f0f0f0f, 0x0f0f0f0f};
  int SSE_LUTb[] = {0x02010100, 0x003020201, 0x03020201, 0x04030302};
  if (len & 0x3)
    printf("leyendo 128b pero len no múltiplo de 4?\n");
  for (i = 0; i < len; i += 4) {
    asm("\n"
        "      movdqu       %[x], %%xmm0      \n\t"
        "      movdqa     %%xmm0, %%xmm1      \n\t"
        "      movdqu       %[m], %%xmm6      \n\t"
        "      psrlw          $4, %%xmm1      \n\t"
        "      pand       %%xmm6, %%xmm0      \n\t"
        "      pand       %%xmm6, %%xmm1      \n\t"
        "      movdqu       %[l], %%xmm2      \n\t"
        "      movdqa     %%xmm2, %%xmm3      \n\t"
        "      pshufb     %%xmm0, %%xmm2      \n\t"
        "      pshufb     %%xmm1, %%xmm3      \n\t"
        "      paddb      %%xmm2, %%xmm3      \n\t"
        "      pxor       %%xmm0, %%xmm0      \n\t"
        "      psadbw     %%xmm0, %%xmm3      \n\t"
        "      movhlps    %%xmm3, %%xmm0      \n\t"
        "      paddd      %%xmm3, %%xmm0      \n\t"
        "      movd       %%xmm0, %[val]      \n\t"
        : [val] "=m"(val)
        : [x] "m"(array[i]), [m] "m"(SSE_mask[0]), [l] "m"(SSE_LUTb[0]));
    result += val;
  }
  return result;
}


x86 assembly (clang, -O0):
<popcount5>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0xed3(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xed0(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xecd(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xeca(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0xc(%rbp),%eax
and    $0x3,%eax
cmp    $0x0,%eax
je     116f <popcount5+0x5f>
lea    0xeb8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f3 <popcount5+0xe3>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
lea    -0x30(%rbp),%rcx
lea    -0x40(%rbp),%rdx
movdqu (%rax),%xmm0
movdqa %xmm0,%xmm1
movdqu (%rcx),%xmm6
psrlw  $0x4,%xmm1
pand   %xmm6,%xmm0
pand   %xmm6,%xmm1
movdqu (%rdx),%xmm2
movdqa %xmm2,%xmm3
pshufb %xmm0,%xmm2
pshufb %xmm1,%xmm3
paddb  %xmm2,%xmm3
pxor   %xmm0,%xmm0
psadbw %xmm0,%xmm3
movhlps %xmm3,%xmm0
paddd  %xmm3,%xmm0
movd   %xmm0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x10(%rbp)
jmp    1176 <popcount5+0x66>
mov    -0x18(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<popcount5>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
movaps 0xede(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xee2(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
test   $0x3,%bpl
je     1145 <popcount5+0x35>
lea    0xee0(%rip),%rdi
call   1030 <puts@plt>
test   %ebp,%ebp
jle    11a9 <popcount5+0x99>
xor    %ecx,%ecx
xor    %eax,%eax
nopl   (%rax)
movdqu (%rbx),%xmm0
movdqa %xmm0,%xmm1
movdqu 0x20(%rsp),%xmm6
psrlw  $0x4,%xmm1
pand   %xmm6,%xmm0
pand   %xmm6,%xmm1
movdqu 0x10(%rsp),%xmm2
movdqa %xmm2,%xmm3
pshufb %xmm0,%xmm2
pshufb %xmm1,%xmm3
paddb  %xmm2,%xmm3
pxor   %xmm0,%xmm0
psadbw %xmm0,%xmm3
movhlps %xmm3,%xmm0
paddd  %xmm3,%xmm0
movd   %xmm0,0xc(%rsp)
add    0xc(%rsp),%eax
add    $0x10,%rbx
add    $0x4,%ecx
cmp    %ebp,%ecx
jl     1150 <popcount5+0x40>
jmp    11ab <popcount5+0x9b>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.popcount5.SSE_mask = [4 x i32] [i32 252645135, i32 252645135, i32 252645135, i32 252645135]
@__const.popcount5.SSE_LUTb = [4 x i32] [i32 33620224, i32 50463233, i32 50463233, i32 67306242]
@.str = [42 x i8] c"leyendo 128b pero len no m\C3\BAltiplo de 4?\0A\00"
define i32 @popcount5(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4 x i32]
%9 = alloca [4 x i32]
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 0, i32* %7
%10 = bitcast [4 x i32]* %8 to i8*
%11 = bitcast [4 x i32]* %9 to i8*
%12 = load i32, i32* %4
%13 = and i32 %12, 3
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
store i32 0, i32* %5
br label %18
18: ; preds = %32, %17
%19 = load i32, i32* %5
%20 = load i32, i32* %4
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %35
22: ; preds = %18
%23 = load i32*, i32** %3
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
%27 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 0
%28 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 0
%29 = load i32, i32* %6
%30 = load i32, i32* %7
%31 = add nsw i32 %30, %29
store i32 %31, i32* %7
br label %32
32: ; preds = %22
%33 = load i32, i32* %5
%34 = add nsw i32 %33, 4
store i32 %34, i32* %5
br label %18
35: ; preds = %18
%36 = load i32, i32* %7
ret i32 %36
}

LLVM IR (-O2):
@__const.popcount5.SSE_LUTb = [4 x i32] [i32 33620224, i32 50463233, i32 50463233, i32 67306242]
@str = [41 x i8] c"leyendo 128b pero len no m\C3\BAltiplo de 4?\00"
define i32 @popcount5(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca [4 x i32]
%5 = alloca [4 x i32]
%6 = bitcast i32* %3 to i8*
%7 = bitcast [4 x i32]* %4 to i8*
%8 = bitcast [4 x i32]* %5 to i8*
%9 = and i32 %1, 3
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = icmp sgt i32 %1, 0
br i1 %14, label %15, label %27
15: ; preds = %13
%16 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 0
%17 = getelementptr inbounds [4 x i32], [4 x i32]* %5, i64 0, i64 0
br label %18
18: ; preds = %15, %18
%19 = phi i64 [ 0, %15 ], [ %24, %18 ]
%20 = phi i32 [ 0, %15 ], [ %23, %18 ]
%21 = getelementptr inbounds i32, i32* %0, i64 %19
%22 = load i32, i32* %3
%23 = add nsw i32 %22, %20
%24 = add nuw i64 %19, 4
%25 = trunc i64 %24 to i32
%26 = icmp slt i32 %25, %1
br i1 %26, label %18, label %27
27: ; preds = %18, %13
%28 = phi i32 [ 0, %13 ], [ %23, %18 ]
ret i32 %28
}