int sortBipartitions(const void *a, const void *b) {
  int ia = *(int *)(a), ib = *(int *)(b), bits_ia = __builtin_popcount(ia),
      bits_ib = __builtin_popcount(ib);
  if (bits_ia == bits_ib)
    return 0;
  if (bits_ib > bits_ia)
    return -1;
  else
    return 1;
}


x86 assembly (clang, -O0):
<sortBipartitions>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%ecx
mov    %ecx,%eax
shr    %eax
and    $0x55555555,%eax
sub    %eax,%ecx
mov    %ecx,%eax
and    $0x33333333,%eax
shr    $0x2,%ecx
and    $0x33333333,%ecx
add    %ecx,%eax
mov    %eax,%ecx
shr    $0x4,%ecx
add    %ecx,%eax
and    $0xf0f0f0f,%eax
imul   $0x1010101,%eax,%eax
shr    $0x18,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%ecx
mov    %ecx,%eax
shr    %eax
and    $0x55555555,%eax
sub    %eax,%ecx
mov    %ecx,%eax
and    $0x33333333,%eax
shr    $0x2,%ecx
and    $0x33333333,%ecx
add    %ecx,%eax
mov    %eax,%ecx
shr    $0x4,%ecx
add    %ecx,%eax
and    $0xf0f0f0f,%eax
imul   $0x1010101,%eax,%eax
shr    $0x18,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jne    11a6 <sortBipartitions+0xa6>
movl   $0x0,-0x4(%rbp)
jmp    11c5 <sortBipartitions+0xc5>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jle    11be <sortBipartitions+0xbe>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <sortBipartitions+0xc5>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<sortBipartitions>:
mov    (%rdi),%ecx
mov    (%rsi),%eax
mov    %ecx,%edx
shr    %edx
and    $0x55555555,%edx
sub    %edx,%ecx
mov    %ecx,%edx
and    $0x33333333,%edx
shr    $0x2,%ecx
and    $0x33333333,%ecx
add    %edx,%ecx
mov    %ecx,%edx
shr    $0x4,%edx
add    %ecx,%edx
and    $0xf0f0f0f,%edx
imul   $0x1010101,%edx,%ecx
shr    $0x18,%ecx
mov    %eax,%edx
shr    %edx
and    $0x55555555,%edx
sub    %edx,%eax
mov    %eax,%edx
and    $0x33333333,%edx
shr    $0x2,%eax
and    $0x33333333,%eax
add    %edx,%eax
mov    %eax,%edx
shr    $0x4,%edx
add    %eax,%edx
and    $0xf0f0f0f,%edx
imul   $0x1010101,%edx,%eax
shr    $0x18,%eax
xor    %edx,%edx
xor    %esi,%esi
cmp    %ecx,%eax
setbe  %sil
lea    -0x1(%rsi,%rsi,1),%eax
cmove  %edx,%eax
ret

LLVM IR (-O0):
define i32 @sortBipartitions(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = bitcast i8* %10 to i32*
%12 = load i32, i32* %11
store i32 %12, i32* %6
%13 = load i8*, i8** %5
%14 = bitcast i8* %13 to i32*
%15 = load i32, i32* %14
store i32 %15, i32* %7
%16 = load i32, i32* %6
%17 = call i32 @llvm.ctpop.i32(i32 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %7
%19 = call i32 @llvm.ctpop.i32(i32 %18)
store i32 %19, i32* %9
%20 = load i32, i32* %8
%21 = load i32, i32* %9
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %2
store i32 0, i32* %3
br label %30
24: ; preds = %2
%25 = load i32, i32* %9
%26 = load i32, i32* %8
%27 = icmp sgt i32 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 -1, i32* %3
br label %30
29: ; preds = %24
store i32 1, i32* %3
br label %30
30: ; preds = %29, %28, %23
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
define i32 @sortBipartitions(i8* nocapture %0, i8* nocapture %1) {
%3 = bitcast i8* %0 to i32*
%4 = load i32, i32* %3
%5 = bitcast i8* %1 to i32*
%6 = load i32, i32* %5
%7 = tail call i32 @llvm.ctpop.i32(i32 %4)
%8 = tail call i32 @llvm.ctpop.i32(i32 %6)
%9 = icmp eq i32 %7, %8
%10 = icmp ugt i32 %8, %7
%11 = select i1 %10, i32 -1, i32 1
%12 = select i1 %9, i32 0, i32 %11
ret i32 %12
}