int popcount5(unsigned *array, int len) {
  int result = 0, i, val;
  int SSE_mask[] = {0x0f0f0f0f, 0x0f0f0f0f, 0x0f0f0f0f, 0x0f0f0f0f};
  int SSE_LUTb[] = {0x02010100, 0x03020201, 0x03020201, 0x04030302};
  if (len & 0x3)
    printf("leyendo 128b pero len no múltiplo de 4?\n");
  for (i = 0; i < len; i += 4) {
    asm("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] "=r"(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,-0x10(%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,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f5 <popcount5+0xe5>
mov    -0x8(%rbp),%rax
movslq -0x14(%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,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x14(%rbp)
jmp    1176 <popcount5+0x66>
mov    -0x10(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<popcount5>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
movaps 0xede(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xee2(%rip),%xmm0
movaps %xmm0,(%rsp)
test   $0x3,%bpl
je     1144 <popcount5+0x34>
lea    0xee1(%rip),%rdi
call   1030 <puts@plt>
test   %ebp,%ebp
jle    11a4 <popcount5+0x94>
xor    %ecx,%ecx
xor    %eax,%eax
nopl   0x0(%rax)
movdqu (%rbx),%xmm0
movdqa %xmm0,%xmm1
movdqu 0x10(%rsp),%xmm6
psrlw  $0x4,%xmm1
pand   %xmm6,%xmm0
pand   %xmm6,%xmm1
movdqu (%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,%edx
add    %edx,%eax
add    $0x10,%rbx
add    $0x4,%ecx
cmp    %ebp,%ecx
jl     1150 <popcount5+0x40>
jmp    11a6 <popcount5+0x96>
xor    %eax,%eax
add    $0x28,%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* %5
%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* %6
br label %18
18: ; preds = %33, %17
%19 = load i32, i32* %6
%20 = load i32, i32* %4
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %36
22: ; preds = %18
%23 = load i32*, i32** %3
%24 = load i32, i32* %6
%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 = call i32 asm "movdqu $1, %xmm0 \0A\09movdqa %xmm0, %xmm1 \0A\09movdqu $2, %xmm6 \0A\09psrlw $$4 , %xmm1 \0A\09pand %xmm6, %xmm0 \0A\09pand %xmm6, %xmm1 \0A\09movdqu $3, %xmm2 \0A\09movdqa %xmm2, %xmm3 \0A\09pshufb %xmm0, %xmm2 \0A\09pshufb %xmm1, %xmm3 \0A\09paddb %xmm2, %xmm3 \0A\09pxor %xmm0, %xmm0 \0A\09psadbw %xmm0, %xmm3 \0A\09movhlps %xmm3, %xmm0 \0A\09paddd %xmm3, %xmm0 \0A\09movd %xmm0, $0 \0A\09", "=r,*m,*m,*m,~{dirflag},~{fpsr},~{flags}"(i32* elementtype(i32) %26, i32* elementtype(i32) %27, i32* elementtype(i32) %28)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = load i32, i32* %5
%32 = add nsw i32 %31, %30
store i32 %32, i32* %5
br label %33
33: ; preds = %22
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 4
store i32 %35, i32* %6
br label %18
36: ; preds = %18
%37 = load i32, i32* %5
ret i32 %37
}

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 [4 x i32]
%4 = alloca [4 x i32]
%5 = bitcast [4 x i32]* %3 to i8*
%6 = bitcast [4 x i32]* %4 to i8*
%7 = and i32 %1, 3
%8 = icmp eq i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = icmp sgt i32 %1, 0
br i1 %12, label %13, label %25
13: ; preds = %11
%14 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%15 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 0
br label %16
16: ; preds = %13, %16
%17 = phi i64 [ 0, %13 ], [ %22, %16 ]
%18 = phi i32 [ 0, %13 ], [ %21, %16 ]
%19 = getelementptr inbounds i32, i32* %0, i64 %17
%20 = call i32 asm "movdqu $1, %xmm0 \0A\09movdqa %xmm0, %xmm1 \0A\09movdqu $2, %xmm6 \0A\09psrlw $$4 , %xmm1 \0A\09pand %xmm6, %xmm0 \0A\09pand %xmm6, %xmm1 \0A\09movdqu $3, %xmm2 \0A\09movdqa %xmm2, %xmm3 \0A\09pshufb %xmm0, %xmm2 \0A\09pshufb %xmm1, %xmm3 \0A\09paddb %xmm2, %xmm3 \0A\09pxor %xmm0, %xmm0 \0A\09psadbw %xmm0, %xmm3 \0A\09movhlps %xmm3, %xmm0 \0A\09paddd %xmm3, %xmm0 \0A\09movd %xmm0, $0 \0A\09", "=r,*m,*m,*m,~{dirflag},~{fpsr},~{flags}"(i32* elementtype(i32) %19, i32* elementtype(i32) %14, i32* elementtype(i32) %15)
%21 = add nsw i32 %20, %18
%22 = add nuw i64 %17, 4
%23 = trunc i64 %22 to i32
%24 = icmp slt i32 %23, %1
br i1 %24, label %16, label %25
25: ; preds = %16, %11
%26 = phi i32 [ 0, %11 ], [ %21, %16 ]
ret i32 %26
}