; LLVM IR from fmela/libdict :: src (skiplist)

; 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.skiplist = type { ptr, i32, i32, ptr, i64 }
%struct.skip_node = type { ptr, ptr, ptr, i32, [0 x 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: nounwind uwtable
define dso_local i64 @skiplist_link_count_histogram(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store i64 0, ptr %7, align 8, !tbaa !9
  br label %10

10:                                               ; preds = %19, %3
  %11 = load i64, ptr %7, align 8, !tbaa !9
  %12 = load i64, ptr %6, align 8, !tbaa !9
  %13 = icmp ult i64 %11, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  br label %22

15:                                               ; preds = %10
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = load i64, ptr %7, align 8, !tbaa !9
  %18 = getelementptr inbounds i64, ptr %16, i64 %17
  store i64 0, ptr %18, align 8, !tbaa !9
  br label %19

19:                                               ; preds = %15
  %20 = load i64, ptr %7, align 8, !tbaa !9
  %21 = add i64 %20, 1
  store i64 %21, ptr %7, align 8, !tbaa !9
  br label %10, !llvm.loop !11

22:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  store i64 0, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.skiplist, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !13
  %26 = getelementptr inbounds %struct.skip_node, ptr %25, i32 0, i32 4
  %27 = getelementptr inbounds [0 x ptr], ptr %26, i64 0, i64 0
  %28 = load ptr, ptr %27, align 8, !tbaa !5
  store ptr %28, ptr %9, align 8, !tbaa !5
  br label %29

29:                                               ; preds = %62, %22
  %30 = load ptr, ptr %9, align 8, !tbaa !5
  %31 = icmp ne ptr %30, null
  br i1 %31, label %33, label %32

32:                                               ; preds = %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  br label %67

33:                                               ; preds = %29
  %34 = load i64, ptr %8, align 8, !tbaa !9
  %35 = load ptr, ptr %9, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.skip_node, ptr %35, i32 0, i32 3
  %37 = load i32, ptr %36, align 8, !tbaa !16
  %38 = zext i32 %37 to i64
  %39 = icmp ult i64 %34, %38
  br i1 %39, label %40, label %45

40:                                               ; preds = %33
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.skip_node, ptr %41, i32 0, i32 3
  %43 = load i32, ptr %42, align 8, !tbaa !16
  %44 = zext i32 %43 to i64
  store i64 %44, ptr %8, align 8, !tbaa !9
  br label %45

45:                                               ; preds = %40, %33
  %46 = load i64, ptr %6, align 8, !tbaa !9
  %47 = load ptr, ptr %9, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.skip_node, ptr %47, i32 0, i32 3
  %49 = load i32, ptr %48, align 8, !tbaa !16
  %50 = zext i32 %49 to i64
  %51 = icmp ugt i64 %46, %50
  br i1 %51, label %52, label %61

52:                                               ; preds = %45
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = load ptr, ptr %9, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.skip_node, ptr %54, i32 0, i32 3
  %56 = load i32, ptr %55, align 8, !tbaa !16
  %57 = zext i32 %56 to i64
  %58 = getelementptr inbounds i64, ptr %53, i64 %57
  %59 = load i64, ptr %58, align 8, !tbaa !9
  %60 = add i64 %59, 1
  store i64 %60, ptr %58, align 8, !tbaa !9
  br label %61

61:                                               ; preds = %52, %45
  br label %62

62:                                               ; preds = %61
  %63 = load ptr, ptr %9, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.skip_node, ptr %63, i32 0, i32 4
  %65 = getelementptr inbounds [0 x ptr], ptr %64, i64 0, i64 0
  %66 = load ptr, ptr %65, align 8, !tbaa !5
  store ptr %66, ptr %9, align 8, !tbaa !5
  br label %29, !llvm.loop !17

67:                                               ; preds = %32
  %68 = load i64, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  ret i64 %68
}

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 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !6, i64 0}
!14 = !{!"skiplist", !6, i64 0, !15, i64 8, !15, i64 12, !6, i64 16, !10, i64 24}
!15 = !{!"int", !7, i64 0}
!16 = !{!15, !15, i64 0}
!17 = distinct !{!17, !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 range(i64 0, 4294967296) i64 @skiplist_link_count_histogram(ptr nocapture noundef readonly %0, ptr nocapture noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %.not29 = icmp eq i64 %2, 0
  br i1 %.not29, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %4 = shl nuw i64 %2, 3
  tail call void @llvm.memset.p0.i64(ptr align 8 %1, i8 0, i64 %4, i1 false), !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.preheader, %3
  %5 = load ptr, ptr %0, align 8, !tbaa !9
  %.0.in21 = getelementptr inbounds i8, ptr %5, i64 32
  %.022 = load ptr, ptr %.0.in21, align 8, !tbaa !13
  %.not23 = icmp eq ptr %.022, null
  br i1 %.not23, label %._crit_edge28, label %.lr.ph27

._crit_edge28:                                    ; preds = %14, %._crit_edge
  %.015.lcssa = phi i64 [ 0, %._crit_edge ], [ %spec.select, %14 ]
  ret i64 %.015.lcssa

.lr.ph27:                                         ; preds = %._crit_edge, %14
  %.025 = phi ptr [ %.0, %14 ], [ %.022, %._crit_edge ]
  %.01524 = phi i64 [ %spec.select, %14 ], [ 0, %._crit_edge ]
  %6 = getelementptr inbounds i8, ptr %.025, i64 24
  %7 = load i32, ptr %6, align 8, !tbaa !14
  %8 = zext i32 %7 to i64
  %spec.select = tail call i64 @llvm.umax.i64(i64 %.01524, i64 %8)
  %9 = icmp ult i64 %8, %2
  br i1 %9, label %10, label %14

10:                                               ; preds = %.lr.ph27
  %11 = getelementptr inbounds i64, ptr %1, i64 %8
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = add i64 %12, 1
  store i64 %13, ptr %11, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %.lr.ph27, %10
  %.0.in = getelementptr inbounds i8, ptr %.025, i64 32
  %.0 = load ptr, ptr %.0.in, align 8, !tbaa !13
  %.not = icmp eq ptr %.0, null
  br i1 %.not, label %._crit_edge28, label %.lr.ph27, !llvm.loop !15
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"skiplist", !11, i64 0, !12, i64 8, !12, i64 12, !11, i64 16, !6, i64 24}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!12, !12, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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