#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int _mm_popcnt_u64(unsigned long);
int countbits(unsigned char *hash, unsigned char *target, int difficulty) {
  int c;
  c = _mm_popcnt_u64(~(*(unsigned long *)hash ^ *(unsigned long *)target)) +
      _mm_popcnt_u64(
          ~(*(unsigned long *)(hash + 8) ^ *(unsigned long *)(target + 8)));
  if (c >= difficulty) {
    return (c);
  } else {
    return (0);
  }
}


x86 assembly (clang, -O0):
<countbits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
xor    (%rax),%rdi
xor    $0xffffffffffffffff,%rdi
call   1030 <_mm_popcnt_u64@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rax
xor    0x8(%rax),%rdi
xor    $0xffffffffffffffff,%rdi
call   1030 <_mm_popcnt_u64@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jl     1177 <countbits+0x67>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    117e <countbits+0x6e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countbits>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    (%rdi),%rdi
xor    (%rsi),%rdi
not    %rdi
call   1030 <_mm_popcnt_u64@plt>
mov    %eax,%r15d
mov    0x8(%rbp),%rdi
xor    0x8(%rbx),%rdi
not    %rdi
call   1030 <_mm_popcnt_u64@plt>
add    %r15d,%eax
xor    %ecx,%ecx
cmp    %r14d,%eax
cmovl  %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @countbits(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = bitcast i8* %9 to i64*
%11 = load i64, i64* %10
%12 = load i8*, i8** %6
%13 = bitcast i8* %12 to i64*
%14 = load i64, i64* %13
%15 = xor i64 %11, %14
%16 = xor i64 %15, -1
%17 = call i32 @_mm_popcnt_u64(i64 %16)
%18 = load i8*, i8** %5
%19 = getelementptr inbounds i8, i8* %18, i64 8
%20 = bitcast i8* %19 to i64*
%21 = load i64, i64* %20
%22 = load i8*, i8** %6
%23 = getelementptr inbounds i8, i8* %22, i64 8
%24 = bitcast i8* %23 to i64*
%25 = load i64, i64* %24
%26 = xor i64 %21, %25
%27 = xor i64 %26, -1
%28 = call i32 @_mm_popcnt_u64(i64 %27)
%29 = add nsw i32 %17, %28
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = load i32, i32* %7
%32 = icmp sge i32 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %3
%34 = load i32, i32* %8
store i32 %34, i32* %4
br label %36
35: ; preds = %3
store i32 0, i32* %4
br label %36
36: ; preds = %35, %33
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
define i32 @countbits(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = bitcast i8* %0 to i64*
%5 = load i64, i64* %4
%6 = bitcast i8* %1 to i64*
%7 = load i64, i64* %6
%8 = xor i64 %5, %7
%9 = xor i64 %8, -1
%10 = tail call i32 @_mm_popcnt_u64(i64 %9)
%11 = getelementptr inbounds i8, i8* %0, i64 8
%12 = bitcast i8* %11 to i64*
%13 = load i64, i64* %12
%14 = getelementptr inbounds i8, i8* %1, i64 8
%15 = bitcast i8* %14 to i64*
%16 = load i64, i64* %15
%17 = xor i64 %13, %16
%18 = xor i64 %17, -1
%19 = tail call i32 @_mm_popcnt_u64(i64 %18)
%20 = add nsw i32 %19, %10
%21 = icmp slt i32 %20, %2
%22 = select i1 %21, i32 0, i32 %20
ret i32 %22
}