; LLVM IR from lelegard/cryptobench :: src/sys.ll (ZN3sys17large_number_bitsEPKhm)

; 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: mustprogress nounwind uwtable
define dso_local noundef i64 @_ZN3sys17large_number_bitsEPKhm(ptr noundef %0, i64 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i8, align 1
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = icmp eq ptr %7, null
  br i1 %8, label %9, label %10

9:                                                ; preds = %2
  br label %13

10:                                               ; preds = %2
  %11 = load i64, ptr %4, align 8, !tbaa !9
  %12 = mul i64 8, %11
  br label %13

13:                                               ; preds = %10, %9
  %14 = phi i64 [ 0, %9 ], [ %12, %10 ]
  store i64 %14, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #2
  store i8 -128, ptr %6, align 1, !tbaa !11
  br label %15

15:                                               ; preds = %40, %13
  %16 = load i64, ptr %5, align 8, !tbaa !9
  %17 = icmp ugt i64 %16, 0
  br i1 %17, label %18, label %26

18:                                               ; preds = %15
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = load i8, ptr %19, align 1, !tbaa !11
  %21 = zext i8 %20 to i32
  %22 = load i8, ptr %6, align 1, !tbaa !11
  %23 = zext i8 %22 to i32
  %24 = and i32 %21, %23
  %25 = icmp eq i32 %24, 0
  br label %26

26:                                               ; preds = %18, %15
  %27 = phi i1 [ false, %15 ], [ %25, %18 ]
  br i1 %27, label %28, label %41

28:                                               ; preds = %26
  %29 = load i64, ptr %5, align 8, !tbaa !9
  %30 = add i64 %29, -1
  store i64 %30, ptr %5, align 8, !tbaa !9
  %31 = load i8, ptr %6, align 1, !tbaa !11
  %32 = zext i8 %31 to i32
  %33 = ashr i32 %32, 1
  %34 = trunc i32 %33 to i8
  store i8 %34, ptr %6, align 1, !tbaa !11
  %35 = zext i8 %34 to i32
  %36 = icmp eq i32 %35, 0
  br i1 %36, label %37, label %40

37:                                               ; preds = %28
  store i8 -128, ptr %6, align 1, !tbaa !11
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %38, i32 1
  store ptr %39, ptr %3, align 8, !tbaa !5
  br label %40

40:                                               ; preds = %37, %28
  br label %15, !llvm.loop !12

41:                                               ; preds = %26
  %42 = load i64, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret i64 %42
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_ZN3sys17large_number_bitsEPKhm(ptr noundef readonly %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  %4 = shl i64 %1, 3
  %.not1419 = icmp eq i64 %4, 0
  %.not14 = or i1 %3, %.not1419
  br i1 %.not14, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %2, %select.unfold.1
  %.017 = phi i32 [ %spec.select11.1, %select.unfold.1 ], [ 128, %2 ]
  %.0816 = phi i64 [ %16, %select.unfold.1 ], [ %4, %2 ]
  %.0915 = phi ptr [ %spec.select.1, %select.unfold.1 ], [ %0, %2 ]
  %5 = load i8, ptr %.0915, align 1, !tbaa !5
  %6 = zext i8 %5 to i32
  %7 = and i32 %.017, %6
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %select.unfold, label %.critedge

select.unfold:                                    ; preds = %.lr.ph
  %9 = lshr i32 %.017, 1
  %10 = and i32 %9, 127
  %11 = icmp eq i32 %10, 0
  %spec.select.idx = zext i1 %11 to i64
  %spec.select = getelementptr inbounds i8, ptr %.0915, i64 %spec.select.idx
  %spec.select11 = select i1 %11, i32 128, i32 %10
  %12 = load i8, ptr %spec.select, align 1, !tbaa !5
  %13 = zext i8 %12 to i32
  %14 = and i32 %spec.select11, %13
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %select.unfold.1, label %.critedge.loopexit.split.loop.exit20

select.unfold.1:                                  ; preds = %select.unfold
  %16 = add i64 %.0816, -2
  %17 = lshr i32 %spec.select11, 1
  %18 = icmp ult i32 %spec.select11, 2
  %spec.select.idx.1 = zext i1 %18 to i64
  %spec.select.1 = getelementptr inbounds i8, ptr %spec.select, i64 %spec.select.idx.1
  %spec.select11.1 = select i1 %18, i32 128, i32 %17
  %.not.1 = icmp eq i64 %16, 0
  br i1 %.not.1, label %.critedge, label %.lr.ph, !llvm.loop !8

.critedge.loopexit.split.loop.exit20:             ; preds = %select.unfold
  %19 = add i64 %.0816, -1
  br label %.critedge

.critedge:                                        ; preds = %.critedge.loopexit.split.loop.exit20, %select.unfold.1, %.lr.ph, %2
  %.08.lcssa = phi i64 [ 0, %2 ], [ %19, %.critedge.loopexit.split.loop.exit20 ], [ 0, %select.unfold.1 ], [ %.0816, %.lr.ph ]
  ret i64 %.08.lcssa
}

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


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