; LLVM IR from erthink/libmdbx :: src (pnl)

; 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 willreturn memory(read) uwtable
define dso_local i64 @pnl_maxspan(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  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 i32, ptr %8, i64 0
  %10 = load i32, ptr %9, align 4, !tbaa !9
  %11 = zext i32 %10 to i64
  store i64 %11, ptr %3, align 8, !tbaa !11
  %12 = load i64, ptr %3, align 8, !tbaa !11
  %13 = icmp ugt i64 %12, 1
  br i1 %13, label %14, label %50

14:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 1, ptr %4, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %15 = load i64, ptr %3, align 8, !tbaa !11
  %16 = load i64, ptr %4, align 8, !tbaa !11
  %17 = sub i64 %15, %16
  store i64 %17, ptr %5, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = getelementptr inbounds i32, ptr %18, i64 1
  store ptr %19, ptr %6, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %44, %14
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #2
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = load i32, ptr %21, align 4, !tbaa !9
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = load i64, ptr %4, align 8, !tbaa !11
  %25 = getelementptr inbounds i32, ptr %23, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !9
  %27 = sub i32 %22, %26
  %28 = zext i32 %27 to i64
  %29 = load i64, ptr %4, align 8, !tbaa !11
  %30 = icmp eq i64 %28, %29
  %31 = zext i1 %30 to i8
  store i8 %31, ptr %7, align 1, !tbaa !13
  %32 = load i8, ptr %7, align 1, !tbaa !13, !range !15, !noundef !16
  %33 = trunc i8 %32 to i1
  %34 = zext i1 %33 to i64
  %35 = load i64, ptr %4, align 8, !tbaa !11
  %36 = add i64 %35, %34
  store i64 %36, ptr %4, align 8, !tbaa !11
  %37 = load i8, ptr %7, align 1, !tbaa !13, !range !15, !noundef !16
  %38 = trunc i8 %37 to i1
  %39 = zext i1 %38 to i32
  %40 = sub nsw i32 1, %39
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = sext i32 %40 to i64
  %43 = getelementptr inbounds i32, ptr %41, i64 %42
  store ptr %43, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #2
  br label %44

44:                                               ; preds = %20
  %45 = load i64, ptr %5, align 8, !tbaa !11
  %46 = add i64 %45, -1
  store i64 %46, ptr %5, align 8, !tbaa !11
  %47 = icmp ne i64 %46, 0
  br i1 %47, label %20, label %48, !llvm.loop !17

48:                                               ; preds = %44
  %49 = load i64, ptr %4, align 8, !tbaa !11
  store i64 %49, ptr %3, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %50

50:                                               ; preds = %48, %1
  %51 = load i64, ptr %3, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret i64 %51
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind willreturn memory(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" }
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 = !{!14, !14, i64 0}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{i8 0, i8 2}
!16 = !{}
!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: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local i64 @pnl_maxspan(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = load i32, ptr %0, align 4, !tbaa !5
  %3 = zext i32 %2 to i64
  %4 = icmp ugt i32 %2, 1
  br i1 %4, label %5, label %.loopexit

5:                                                ; preds = %1
  %6 = add nsw i64 %3, -1
  %7 = getelementptr inbounds i8, ptr %0, i64 4
  %xtraiter = and i64 %6, 1
  %8 = icmp eq i32 %2, 2
  br i1 %8, label %.loopexit.loopexit.unr-lcssa, label %.new

.new:                                             ; preds = %5
  %unroll_iter = and i64 %6, -2
  br label %9

9:                                                ; preds = %9, %.new
  %.017 = phi i64 [ 1, %.new ], [ %27, %9 ]
  %.015 = phi ptr [ %7, %.new ], [ %29, %9 ]
  %niter = phi i64 [ 0, %.new ], [ %niter.next.1, %9 ]
  %10 = load i32, ptr %.015, align 4, !tbaa !5
  %11 = getelementptr inbounds i32, ptr %.015, i64 %.017
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = sub i32 %10, %12
  %14 = zext i32 %13 to i64
  %15 = icmp eq i64 %.017, %14
  %16 = zext i1 %15 to i64
  %17 = add i64 %.017, %16
  %not. = xor i1 %15, true
  %18 = zext i1 %not. to i64
  %19 = getelementptr inbounds i32, ptr %.015, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = getelementptr inbounds i32, ptr %19, i64 %17
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = sub i32 %20, %22
  %24 = zext i32 %23 to i64
  %25 = icmp eq i64 %17, %24
  %26 = zext i1 %25 to i64
  %27 = add i64 %17, %26
  %not..1 = xor i1 %25, true
  %28 = zext i1 %not..1 to i64
  %29 = getelementptr inbounds i32, ptr %19, i64 %28
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.loopexit.loopexit.unr-lcssa, label %9, !llvm.loop !9

.loopexit.loopexit.unr-lcssa:                     ; preds = %9, %5
  %.lcssa.ph = phi i64 [ poison, %5 ], [ %27, %9 ]
  %.017.unr = phi i64 [ 1, %5 ], [ %27, %9 ]
  %.015.unr = phi ptr [ %7, %5 ], [ %29, %9 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.loopexit.loopexit.epilog-lcssa

.loopexit.loopexit.epilog-lcssa:                  ; preds = %.loopexit.loopexit.unr-lcssa
  %30 = load i32, ptr %.015.unr, align 4, !tbaa !5
  %31 = getelementptr inbounds i32, ptr %.015.unr, i64 %.017.unr
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = sub i32 %30, %32
  %34 = zext i32 %33 to i64
  %35 = icmp eq i64 %.017.unr, %34
  %36 = zext i1 %35 to i64
  %37 = add i64 %.017.unr, %36
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit.epilog-lcssa, %.loopexit.loopexit.unr-lcssa, %1
  %.0 = phi i64 [ %3, %1 ], [ %.lcssa.ph, %.loopexit.loopexit.unr-lcssa ], [ %37, %.loopexit.loopexit.epilog-lcssa ]
  ret i64 %.0
}

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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