; ModuleID = 'popcount.before.ll'
source_filename = "popcount.before.ll"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define i32 @count_bits(i64 %input) #0 {
entry:
  %0 = call i64 @llvm.ctpop.i64(i64 %input)
  %1 = trunc i64 %0 to i32
  %2 = icmp eq i32 %1, 0
  br i1 %2, label %exit, label %loop.preheader

loop.preheader:                                   ; preds = %entry
  br label %loop

loop:                                             ; preds = %loop.preheader, %loop
  %tcphi = phi i32 [ %1, %loop.preheader ], [ %tcdec, %loop ]
  %x = phi i64 [ %remaining, %loop ], [ %input, %loop.preheader ]
  %count = phi i32 [ %next_count, %loop ], [ 0, %loop.preheader ]
  %minus_one = add i64 %x, -1
  %remaining = and i64 %x, %minus_one
  %next_count = add i32 %count, 1
  %tcdec = sub nsw i32 %tcphi, 1
  %more = icmp ugt i32 %tcdec, 0
  br i1 %more, label %loop, label %exit.loopexit

exit.loopexit:                                    ; preds = %loop
  %next_count.lcssa = phi i32 [ %1, %loop ]
  br label %exit

exit:                                             ; preds = %exit.loopexit, %entry
  %result = phi i32 [ 0, %entry ], [ %next_count.lcssa, %exit.loopexit ]
  ret i32 %result
}

; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.ctpop.i64(i64) #1

attributes #0 = { "target-features"="+popcnt" }
attributes #1 = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
