; LLVM IR from zisisvakras/crossw :: src (maps)

; 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.Map = type { ptr, i32, i32 }

; 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: nounwind uwtable
define dso_local i32 @sum_bit(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.Map, ptr %8, i32 0, i32 0
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  store ptr %10, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.Map, ptr %11, i32 0, i32 1
  %13 = load i32, ptr %12, align 8, !tbaa !12
  store i32 %13, ptr %4, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !13
  br label %14

14:                                               ; preds = %41, %39, %1
  %15 = load i32, ptr %4, align 4, !tbaa !13
  %16 = add nsw i32 %15, -1
  store i32 %16, ptr %4, align 4, !tbaa !13
  %17 = icmp ne i32 %15, 0
  br i1 %17, label %18, label %42

18:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = load i32, ptr %4, align 4, !tbaa !13
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i64, ptr %19, i64 %21
  %23 = load i64, ptr %22, align 8, !tbaa !14
  store i64 %23, ptr %6, align 8, !tbaa !14
  %24 = load i64, ptr %6, align 8, !tbaa !14
  %25 = icmp eq i64 %24, 0
  br i1 %25, label %26, label %27

26:                                               ; preds = %18
  store i32 2, ptr %7, align 4
  br label %39, !llvm.loop !16

27:                                               ; preds = %18
  br label %28

28:                                               ; preds = %31, %27
  %29 = load i64, ptr %6, align 8, !tbaa !14
  %30 = icmp ne i64 %29, 0
  br i1 %30, label %31, label %38

31:                                               ; preds = %28
  %32 = load i64, ptr %6, align 8, !tbaa !14
  %33 = sub i64 %32, 1
  %34 = load i64, ptr %6, align 8, !tbaa !14
  %35 = and i64 %34, %33
  store i64 %35, ptr %6, align 8, !tbaa !14
  %36 = load i32, ptr %5, align 4, !tbaa !13
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %5, align 4, !tbaa !13
  br label %28, !llvm.loop !18

38:                                               ; preds = %28
  store i32 0, ptr %7, align 4
  br label %39

39:                                               ; preds = %38, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %40 = load i32, ptr %7, align 4
  switch i32 %40, label %47 [
    i32 0, label %41
    i32 2, label %14
  ]

41:                                               ; preds = %39
  br label %14, !llvm.loop !16

42:                                               ; preds = %14
  %43 = load i32, ptr %5, align 4, !tbaa !13
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.Map, ptr %44, i32 0, i32 2
  store i32 %43, ptr %45, align 4, !tbaa !19
  %46 = load i32, ptr %5, align 4, !tbaa !13
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret i32 %46

47:                                               ; preds = %39
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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" }
attributes #2 = { 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, !6, i64 0}
!10 = !{!"Map", !6, i64 0, !11, i64 8, !11, i64 12}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!11, !11, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"long long", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}
!19 = !{!10, !11, i64 12}


; 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: nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local i32 @sum_bit(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr %0, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load i32, ptr %3, align 8, !tbaa !11
  %.not18 = icmp eq i32 %4, 0
  br i1 %.not18, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %5 = sext i32 %4 to i64
  %xtraiter = and i64 %5, 1
  %6 = icmp eq i32 %4, 1
  br i1 %6, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %5, -2
  %invariant.gep = getelementptr i8, ptr %2, i64 -8
  br label %.lr.ph

.lr.ph:                                           ; preds = %.loopexit.1, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ %5, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.loopexit.1 ]
  %.01219 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %.2.1, %.loopexit.1 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.loopexit.1 ]
  %gep = getelementptr i64, ptr %invariant.gep, i64 %indvars.iv
  %7 = load i64, ptr %gep, align 8, !tbaa !12
  %8 = icmp eq i64 %7, 0
  br i1 %8, label %.loopexit, label %.preheader, !llvm.loop !14

.preheader:                                       ; preds = %.lr.ph, %.preheader
  %.017 = phi i64 [ %10, %.preheader ], [ %7, %.lr.ph ]
  %.116 = phi i32 [ %11, %.preheader ], [ %.01219, %.lr.ph ]
  %9 = add i64 %.017, -1
  %10 = and i64 %9, %.017
  %11 = add nsw i32 %.116, 1
  %.not15 = icmp eq i64 %10, 0
  br i1 %.not15, label %.loopexit, label %.preheader, !llvm.loop !16

.loopexit:                                        ; preds = %.preheader, %.lr.ph
  %.2 = phi i32 [ %.01219, %.lr.ph ], [ %11, %.preheader ]
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, -2
  %12 = getelementptr inbounds i64, ptr %2, i64 %indvars.iv.next.1
  %13 = load i64, ptr %12, align 8, !tbaa !12
  %14 = icmp eq i64 %13, 0
  br i1 %14, label %.loopexit.1, label %.preheader.1, !llvm.loop !14

.preheader.1:                                     ; preds = %.loopexit, %.preheader.1
  %.017.1 = phi i64 [ %16, %.preheader.1 ], [ %13, %.loopexit ]
  %.116.1 = phi i32 [ %17, %.preheader.1 ], [ %.2, %.loopexit ]
  %15 = add i64 %.017.1, -1
  %16 = and i64 %15, %.017.1
  %17 = add nsw i32 %.116.1, 1
  %.not15.1 = icmp eq i64 %16, 0
  br i1 %.not15.1, label %.loopexit.1, label %.preheader.1, !llvm.loop !16

.loopexit.1:                                      ; preds = %.preheader.1, %.loopexit
  %.2.1 = phi i32 [ %.2, %.loopexit ], [ %17, %.preheader.1 ]
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.loopexit.1, %.lr.ph.preheader
  %.2.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %.2.1, %.loopexit.1 ]
  %indvars.iv.unr = phi i64 [ %5, %.lr.ph.preheader ], [ %indvars.iv.next.1, %.loopexit.1 ]
  %.01219.unr = phi i32 [ 0, %.lr.ph.preheader ], [ %.2.1, %.loopexit.1 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %18 = getelementptr i64, ptr %2, i64 %indvars.iv.unr
  %19 = getelementptr i8, ptr %18, i64 -8
  %20 = load i64, ptr %19, align 8, !tbaa !12
  %21 = icmp eq i64 %20, 0
  br i1 %21, label %._crit_edge, label %.preheader.epil, !llvm.loop !14

.preheader.epil:                                  ; preds = %.lr.ph.epil, %.preheader.epil
  %.017.epil = phi i64 [ %23, %.preheader.epil ], [ %20, %.lr.ph.epil ]
  %.116.epil = phi i32 [ %24, %.preheader.epil ], [ %.01219.unr, %.lr.ph.epil ]
  %22 = add i64 %.017.epil, -1
  %23 = and i64 %22, %.017.epil
  %24 = add nsw i32 %.116.epil, 1
  %.not15.epil = icmp eq i64 %23, 0
  br i1 %.not15.epil, label %._crit_edge, label %.preheader.epil, !llvm.loop !16

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.preheader.epil, %.lr.ph.epil, %1
  %.012.lcssa = phi i32 [ 0, %1 ], [ %.2.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.01219.unr, %.lr.ph.epil ], [ %24, %.preheader.epil ]
  %25 = getelementptr inbounds i8, ptr %0, i64 12
  store i32 %.012.lcssa, ptr %25, align 4, !tbaa !17
  ret i32 %.012.lcssa
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) 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, !7, i64 0}
!6 = !{!"Map", !7, i64 0, !10, i64 8, !10, i64 12}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!13, !13, i64 0}
!13 = !{!"long long", !8, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = !{!6, !10, i64 12}


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