; LLVM IR from marcobambini/gravity :: src/utils (gravity_utils)

; 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"

; 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 i64 @number_from_bin(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = icmp ugt i32 %9, 64
  br i1 %10, label %11, label %12

11:                                               ; preds = %2
  store i64 0, ptr %3, align 8
  br label %36

12:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store i64 0, ptr %6, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %31, %12
  %14 = load i32, ptr %7, align 4, !tbaa !9
  %15 = load i32, ptr %5, align 4, !tbaa !9
  %16 = icmp ult i32 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %34

18:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = load i32, ptr %7, align 4, !tbaa !9
  %21 = zext i32 %20 to i64
  %22 = getelementptr inbounds i8, ptr %19, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !13
  %24 = sext i8 %23 to i32
  store i32 %24, ptr %8, align 4, !tbaa !9
  %25 = load i64, ptr %6, align 8, !tbaa !11
  %26 = shl i64 %25, 1
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = sub nsw i32 %27, 48
  %29 = sext i32 %28 to i64
  %30 = add nsw i64 %26, %29
  store i64 %30, ptr %6, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %31

31:                                               ; preds = %18
  %32 = load i32, ptr %7, align 4, !tbaa !9
  %33 = add i32 %32, 1
  store i32 %33, ptr %7, align 4, !tbaa !9
  br label %13, !llvm.loop !14

34:                                               ; preds = %17
  %35 = load i64, ptr %6, align 8, !tbaa !11
  store i64 %35, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %36

36:                                               ; preds = %34, %11
  %37 = load i64, ptr %3, align 8
  ret i64 %37
}

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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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: nofree norecurse nosync nounwind memory(argmem: read) uwtable
define dso_local i64 @number_from_bin(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = add i32 %1, -1
  %or.cond = icmp ult i32 %3, 64
  br i1 %or.cond, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %2
  %wide.trip.count = zext nneg i32 %1 to i64
  %4 = add nsw i64 %wide.trip.count, -1
  %xtraiter = and i64 %wide.trip.count, 3
  %5 = icmp ult i64 %4, 3
  br i1 %5, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 124
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.3, %.lr.ph ]
  %.01012 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %29, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %6 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv
  %7 = load i8, ptr %6, align 1, !tbaa !5
  %8 = sext i8 %7 to i64
  %9 = shl i64 %.01012, 1
  %10 = add i64 %9, 4611686018427387856
  %11 = add i64 %10, %8
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %12 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next
  %13 = load i8, ptr %12, align 1, !tbaa !5
  %14 = sext i8 %13 to i64
  %15 = shl i64 %11, 1
  %16 = add i64 %15, 4611686018427387856
  %17 = add i64 %16, %14
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %18 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next.1
  %19 = load i8, ptr %18, align 1, !tbaa !5
  %20 = sext i8 %19 to i64
  %21 = shl i64 %17, 1
  %22 = add i64 %21, 9223372036854775760
  %23 = add i64 %22, %20
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %24 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next.2
  %25 = load i8, ptr %24, align 1, !tbaa !5
  %26 = sext i8 %25 to i64
  %27 = shl i64 %23, 1
  %28 = add i64 %27, -48
  %29 = add i64 %28, %26
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !8

.loopexit.loopexit.unr-lcssa:                     ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa.ph = phi i64 [ poison, %.lr.ph.preheader ], [ %29, %.lr.ph ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.3, %.lr.ph ]
  %.01012.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %29, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.loopexit.loopexit.unr-lcssa, %.lr.ph.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph.epil ], [ %indvars.iv.unr, %.loopexit.loopexit.unr-lcssa ]
  %.01012.epil = phi i64 [ %35, %.lr.ph.epil ], [ %.01012.unr, %.loopexit.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %.loopexit.loopexit.unr-lcssa ]
  %30 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.epil
  %31 = load i8, ptr %30, align 1, !tbaa !5
  %32 = sext i8 %31 to i64
  %33 = shl i64 %.01012.epil, 1
  %34 = add i64 %33, -48
  %35 = add i64 %34, %32
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %.loopexit, label %.lr.ph.epil, !llvm.loop !10

.loopexit:                                        ; preds = %.loopexit.loopexit.unr-lcssa, %.lr.ph.epil, %2
  %.0 = phi i64 [ 0, %2 ], [ %.lcssa.ph, %.loopexit.loopexit.unr-lcssa ], [ %35, %.lr.ph.epil ]
  ret i64 %.0
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: read) 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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.unroll.disable"}


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