unsigned int count_bits(char num) {
  unsigned int count = 0;
  for (unsigned int i = 0; (i < sizeof(char) * 8) && (num != 0); ++i) {
    if (num & (0x1 << i)) {
      count++;
    }
  }
  return count;
}


x86 assembly (clang, -O0):
<count_bits>:
push   %rbp
mov    %rsp,%rbp
mov    %dil,%al
mov    %al,-0x1(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x8,%rcx
mov    %al,-0xd(%rbp)
jae    1139 <count_bits+0x39>
movsbl -0x1(%rbp),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    1149 <count_bits+0x49>
jmp    1180 <count_bits+0x80>
movsbl -0x1(%rbp),%eax
mov    -0xc(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     116d <count_bits+0x6d>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1172 <count_bits+0x72>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1118 <count_bits+0x18>
mov    -0x8(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_bits>:
test   %dil,%dil
je     1195 <count_bits+0x95>
movsbl %dil,%eax
movd   %eax,%xmm0
pshufd $0x0,%xmm0,%xmm0
movdqa %xmm0,%xmm1
psrld  $0x3,%xmm1
movdqa %xmm0,%xmm2
psrld  $0x2,%xmm2
punpckhqdq %xmm1,%xmm2
movdqa %xmm0,%xmm1
psrld  $0x1,%xmm1
movdqa %xmm0,%xmm3
punpcklqdq %xmm1,%xmm3
shufps $0xcc,%xmm2,%xmm3
movaps 0xeb8(%rip),%xmm1
andps  %xmm1,%xmm3
movdqa %xmm0,%xmm2
psrld  $0x7,%xmm2
movdqa %xmm0,%xmm4
psrld  $0x6,%xmm4
punpckhqdq %xmm2,%xmm4
movdqa %xmm0,%xmm2
psrld  $0x5,%xmm2
psrld  $0x4,%xmm0
punpcklqdq %xmm2,%xmm0
shufps $0xcc,%xmm4,%xmm0
andps  %xmm1,%xmm0
paddd  %xmm3,%xmm0
pshufd $0xee,%xmm0,%xmm1
paddd  %xmm0,%xmm1
pshufd $0x55,%xmm1,%xmm0
paddd  %xmm1,%xmm0
movd   %xmm0,%eax
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @count_bits(i8 signext %0) {
%2 = alloca i8
%3 = alloca i32
%4 = alloca i32
store i8 %0, i8* %2
store i32 0, i32* %3
store i32 0, i32* %4
br label %5
5: ; preds = %26, %1
%6 = load i32, i32* %4
%7 = zext i32 %6 to i64
%8 = icmp ult i64 %7, 8
br i1 %8, label %9, label %13
9: ; preds = %5
%10 = load i8, i8* %2
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br label %13
13: ; preds = %9, %5
%14 = phi i1 [ false, %5 ], [ %12, %9 ]
br i1 %14, label %15, label %29
15: ; preds = %13
%16 = load i8, i8* %2
%17 = sext i8 %16 to i32
%18 = load i32, i32* %4
%19 = shl i32 1, %18
%20 = and i32 %17, %19
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i32, i32* %3
%24 = add i32 %23, 1
store i32 %24, i32* %3
br label %25
25: ; preds = %22, %15
br label %26
26: ; preds = %25
%27 = load i32, i32* %4
%28 = add i32 %27, 1
store i32 %28, i32* %4
br label %5
29: ; preds = %13
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
define i32 @count_bits(i8 signext %0) {
%2 = icmp eq i8 %0, 0
br i1 %2, label %13, label %3
3: ; preds = %1
%4 = sext i8 %0 to i32
%5 = insertelement <4 x i32> poison, i32 %4, i64 0
%6 = shufflevector <4 x i32> %5, <4 x i32> poison, <4 x i32> zeroinitializer
%7 = lshr <4 x i32> %6, <i32 0, i32 1, i32 2, i32 3>
%8 = and <4 x i32> %7, <i32 1, i32 1, i32 1, i32 1>
%9 = lshr <4 x i32> %6, <i32 4, i32 5, i32 6, i32 7>
%10 = and <4 x i32> %9, <i32 1, i32 1, i32 1, i32 1>
%11 = add nuw nsw <4 x i32> %10, %8
%12 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %11)
br label %13
13: ; preds = %3, %1
%14 = phi i32 [ 0, %1 ], [ %12, %3 ]
ret i32 %14
}