; LLVM IR from jermp/2i_bench :: external/CRoaring/src/containers (bitset)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.bitset_container_s = type { i32, ptr }

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @hamming(i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @bitset_container_rank(ptr noundef %0, i16 noundef zeroext %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca i16, align 2
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i16 %1, ptr %4, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %10 = load i16, ptr %4, align 2, !tbaa !9
  %11 = zext i16 %10 to i32
  store i32 %11, ptr %5, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !11
  br label %12

12:                                               ; preds = %29, %2
  %13 = load i32, ptr %7, align 4, !tbaa !11
  %14 = add i32 %13, 63
  %15 = load i32, ptr %5, align 4, !tbaa !11
  %16 = icmp ule i32 %14, %15
  br i1 %16, label %17, label %32

17:                                               ; preds = %12
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.bitset_container_s, ptr %18, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  %21 = load i32, ptr %7, align 4, !tbaa !11
  %22 = udiv i32 %21, 64
  %23 = zext i32 %22 to i64
  %24 = getelementptr inbounds i64, ptr %20, i64 %23
  %25 = load i64, ptr %24, align 8, !tbaa !15
  %26 = call i32 @hamming(i64 noundef %25)
  %27 = load i32, ptr %6, align 4, !tbaa !11
  %28 = add nsw i32 %27, %26
  store i32 %28, ptr %6, align 4, !tbaa !11
  br label %29

29:                                               ; preds = %17
  %30 = load i32, ptr %7, align 4, !tbaa !11
  %31 = add i32 %30, 64
  store i32 %31, ptr %7, align 4, !tbaa !11
  br label %12, !llvm.loop !17

32:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %33 = load i32, ptr %5, align 4, !tbaa !11
  %34 = load i32, ptr %7, align 4, !tbaa !11
  %35 = sub i32 %33, %34
  %36 = add i32 %35, 1
  store i32 %36, ptr %8, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.bitset_container_s, ptr %37, i32 0, i32 1
  %39 = load ptr, ptr %38, align 8, !tbaa !13
  %40 = load i32, ptr %7, align 4, !tbaa !11
  %41 = udiv i32 %40, 64
  %42 = zext i32 %41 to i64
  %43 = getelementptr inbounds i64, ptr %39, i64 %42
  %44 = load i64, ptr %43, align 8, !tbaa !15
  store i64 %44, ptr %9, align 8, !tbaa !15
  %45 = load i64, ptr %9, align 8, !tbaa !15
  %46 = load i32, ptr %8, align 4, !tbaa !11
  %47 = zext i32 %46 to i64
  %48 = shl i64 1, %47
  %49 = sub i64 %48, 1
  %50 = and i64 %45, %49
  store i64 %50, ptr %9, align 8, !tbaa !15
  %51 = load i64, ptr %9, align 8, !tbaa !15
  %52 = call i32 @hamming(i64 noundef %51)
  %53 = load i32, ptr %6, align 4, !tbaa !11
  %54 = add nsw i32 %53, %52
  store i32 %54, ptr %6, align 4, !tbaa !11
  %55 = load i32, ptr %6, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret i32 %55
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 8}
!14 = !{!"bitset_container_s", !12, i64 0, !6, i64 8}
!15 = !{!16, !16, i64 0}
!16 = !{!"long", !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @hamming(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @bitset_container_rank(ptr nocapture noundef readonly %0, i16 noundef zeroext %1) local_unnamed_addr #1 {
  %.not17 = icmp ult i16 %1, 63
  br i1 %.not17, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = zext i16 %1 to i64
  br label %5

5:                                                ; preds = %.lr.ph, %5
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %5 ]
  %.019 = phi i32 [ 0, %.lr.ph ], [ %11, %5 ]
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = lshr exact i64 %indvars.iv, 3
  %8 = getelementptr inbounds i8, ptr %6, i64 %7
  %9 = load i64, ptr %8, align 8, !tbaa !11
  %10 = tail call i32 @hamming(i64 noundef %9)
  %11 = add nsw i32 %10, %.019
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 64
  %12 = or disjoint i64 %indvars.iv.next, 63
  %.not = icmp ugt i64 %12, %4
  br i1 %.not, label %._crit_edge.loopexit, label %5, !llvm.loop !13

._crit_edge.loopexit:                             ; preds = %5
  %13 = trunc nuw nsw i64 %indvars.iv.next to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %2
  %.015.lcssa = phi i32 [ 0, %2 ], [ %13, %._crit_edge.loopexit ]
  %.0.lcssa = phi i32 [ 0, %2 ], [ %11, %._crit_edge.loopexit ]
  %14 = zext i16 %1 to i32
  %15 = add nuw nsw i32 %14, 1
  %16 = sub i32 %15, %.015.lcssa
  %17 = getelementptr inbounds i8, ptr %0, i64 8
  %18 = load ptr, ptr %17, align 8, !tbaa !5
  %19 = lshr exact i32 %.015.lcssa, 6
  %20 = zext nneg i32 %19 to i64
  %21 = getelementptr inbounds i64, ptr %18, i64 %20
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = zext nneg i32 %16 to i64
  %notmask = shl nsw i64 -1, %23
  %24 = xor i64 %notmask, -1
  %25 = and i64 %22, %24
  %26 = tail call i32 @hamming(i64 noundef %25)
  %27 = add nsw i32 %26, %.0.lcssa
  ret i32 %27
}

attributes #0 = { inlinehint nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !10, i64 8}
!6 = !{!"bitset_container_s", !7, i64 0, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !8, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass