; LLVM IR from seppo0010/rlite :: src (type_zset)

; 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.rl_zrangespec = type { double, i32, double, i32 }
%struct.rl_skiplist = type { i64, i64, i64, i64 }

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
declare hidden i32 @rl_zset_get_objects(ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

declare i32 @rl_skiplist_first_node(ptr noundef, ptr noundef, double noundef, i32 noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @rl_zcount(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, ptr noundef %4) #1 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  %14 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  store ptr %4, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %15 = load ptr, ptr %9, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.rl_zrangespec, ptr %15, i32 0, i32 2
  %17 = load double, ptr %16, align 8, !tbaa !11
  %18 = load ptr, ptr %9, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.rl_zrangespec, ptr %18, i32 0, i32 0
  %20 = load double, ptr %19, align 8, !tbaa !15
  %21 = fcmp olt double %17, %20
  br i1 %21, label %22, label %24

22:                                               ; preds = %5
  %23 = load ptr, ptr %10, align 8, !tbaa !5
  store i64 0, ptr %23, align 8, !tbaa !9
  store i32 0, ptr %14, align 4, !tbaa !16
  br label %84

24:                                               ; preds = %5
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  %27 = load i64, ptr %8, align 8, !tbaa !9
  %28 = call i32 @rl_zset_get_objects(ptr noundef %25, ptr noundef %26, i64 noundef %27, ptr noundef null, ptr noundef null, ptr noundef null, ptr noundef %11, ptr noundef null, i32 noundef 0, i32 noundef 0)
  store i32 %28, ptr %14, align 4, !tbaa !16
  %29 = load i32, ptr %14, align 4, !tbaa !16
  %30 = icmp ne i32 0, %29
  br i1 %30, label %31, label %32

31:                                               ; preds = %24
  br label %84

32:                                               ; preds = %24
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = load ptr, ptr %11, align 8, !tbaa !5
  %35 = load ptr, ptr %9, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.rl_zrangespec, ptr %35, i32 0, i32 2
  %37 = load double, ptr %36, align 8, !tbaa !11
  %38 = load ptr, ptr %9, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.rl_zrangespec, ptr %38, i32 0, i32 3
  %40 = load i32, ptr %39, align 8, !tbaa !17
  %41 = icmp ne i32 %40, 0
  %42 = zext i1 %41 to i64
  %43 = select i1 %41, i32 1, i32 2
  %44 = call i32 @rl_skiplist_first_node(ptr noundef %33, ptr noundef %34, double noundef %37, i32 noundef %43, ptr noundef null, i64 noundef 0, ptr noundef null, ptr noundef %12)
  store i32 %44, ptr %14, align 4, !tbaa !16
  %45 = load i32, ptr %14, align 4, !tbaa !16
  %46 = icmp eq i32 %45, 3
  br i1 %46, label %47, label %52

47:                                               ; preds = %32
  %48 = load ptr, ptr %11, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.rl_skiplist, ptr %48, i32 0, i32 2
  %50 = load i64, ptr %49, align 8, !tbaa !18
  %51 = sub nsw i64 %50, 1
  store i64 %51, ptr %12, align 8, !tbaa !9
  br label %57

52:                                               ; preds = %32
  %53 = load i32, ptr %14, align 4, !tbaa !16
  %54 = icmp ne i32 %53, 2
  br i1 %54, label %55, label %56

55:                                               ; preds = %52
  br label %84

56:                                               ; preds = %52
  br label %57

57:                                               ; preds = %56, %47
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = load ptr, ptr %11, align 8, !tbaa !5
  %60 = load ptr, ptr %9, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.rl_zrangespec, ptr %60, i32 0, i32 0
  %62 = load double, ptr %61, align 8, !tbaa !15
  %63 = load ptr, ptr %9, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.rl_zrangespec, ptr %63, i32 0, i32 1
  %65 = load i32, ptr %64, align 8, !tbaa !20
  %66 = icmp ne i32 %65, 0
  %67 = zext i1 %66 to i64
  %68 = select i1 %66, i32 4, i32 3
  %69 = call i32 @rl_skiplist_first_node(ptr noundef %58, ptr noundef %59, double noundef %62, i32 noundef %68, ptr noundef null, i64 noundef 0, ptr noundef null, ptr noundef %13)
  store i32 %69, ptr %14, align 4, !tbaa !16
  %70 = load i32, ptr %14, align 4, !tbaa !16
  %71 = icmp ne i32 %70, 2
  br i1 %71, label %72, label %73

72:                                               ; preds = %57
  br label %84

73:                                               ; preds = %57
  %74 = load i64, ptr %13, align 8, !tbaa !9
  %75 = icmp slt i64 %74, 0
  br i1 %75, label %76, label %77

76:                                               ; preds = %73
  store i64 0, ptr %13, align 8, !tbaa !9
  br label %77

77:                                               ; preds = %76, %73
  br label %78

78:                                               ; preds = %77
  %79 = load i64, ptr %12, align 8, !tbaa !9
  %80 = load i64, ptr %13, align 8, !tbaa !9
  %81 = sub nsw i64 %79, %80
  %82 = add nsw i64 %81, 1
  %83 = load ptr, ptr %10, align 8, !tbaa !5
  store i64 %82, ptr %83, align 8, !tbaa !9
  store i32 0, ptr %14, align 4, !tbaa !16
  br label %84

84:                                               ; preds = %78, %72, %55, %31, %22
  %85 = load i32, ptr %14, align 4, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  ret i32 %85
}

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 = { "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 #3 = { 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 = !{!12, !13, i64 16}
!12 = !{!"", !13, i64 0, !14, i64 8, !13, i64 16, !14, i64 24}
!13 = !{!"double", !7, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !13, i64 0}
!16 = !{!14, !14, i64 0}
!17 = !{!12, !14, i64 24}
!18 = !{!19, !10, i64 16}
!19 = !{!"", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!20 = !{!12, !14, i64 8}


; 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
declare hidden i32 @rl_zset_get_objects(ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

declare i32 @rl_skiplist_first_node(ptr noundef, ptr noundef, double noundef, i32 noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local i32 @rl_zcount(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr nocapture noundef readonly %3, ptr nocapture noundef writeonly %4) local_unnamed_addr #1 {
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %7) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %8) #4
  %9 = getelementptr inbounds i8, ptr %3, i64 16
  %10 = load double, ptr %9, align 8, !tbaa !5
  %11 = load double, ptr %3, align 8, !tbaa !11
  %12 = fcmp olt double %10, %11
  br i1 %12, label %.sink.split, label %13

13:                                               ; preds = %5
  %14 = call i32 @rl_zset_get_objects(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef null, ptr noundef null, ptr noundef null, ptr noundef nonnull %6, ptr noundef null, i32 noundef 0, i32 noundef 0)
  %.not = icmp eq i32 %14, 0
  br i1 %.not, label %15, label %39

15:                                               ; preds = %13
  %16 = load ptr, ptr %6, align 8, !tbaa !12
  %17 = load double, ptr %9, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %3, i64 24
  %19 = load i32, ptr %18, align 8, !tbaa !14
  %.not20 = icmp eq i32 %19, 0
  %20 = select i1 %.not20, i32 2, i32 1
  %21 = call i32 @rl_skiplist_first_node(ptr noundef %0, ptr noundef %16, double noundef %17, i32 noundef %20, ptr noundef null, i64 noundef 0, ptr noundef null, ptr noundef nonnull %7) #4
  switch i32 %21, label %39 [
    i32 3, label %22
    i32 2, label %._crit_edge
  ]

._crit_edge:                                      ; preds = %15
  %.pre = load ptr, ptr %6, align 8, !tbaa !12
  br label %27

22:                                               ; preds = %15
  %23 = load ptr, ptr %6, align 8, !tbaa !12
  %24 = getelementptr inbounds i8, ptr %23, i64 16
  %25 = load i64, ptr %24, align 8, !tbaa !15
  %26 = add nsw i64 %25, -1
  store i64 %26, ptr %7, align 8, !tbaa !18
  br label %27

27:                                               ; preds = %._crit_edge, %22
  %28 = phi ptr [ %.pre, %._crit_edge ], [ %23, %22 ]
  %29 = load double, ptr %3, align 8, !tbaa !11
  %30 = getelementptr inbounds i8, ptr %3, i64 8
  %31 = load i32, ptr %30, align 8, !tbaa !19
  %.not22 = icmp eq i32 %31, 0
  %32 = select i1 %.not22, i32 3, i32 4
  %33 = call i32 @rl_skiplist_first_node(ptr noundef %0, ptr noundef %28, double noundef %29, i32 noundef %32, ptr noundef null, i64 noundef 0, ptr noundef null, ptr noundef nonnull %8) #4
  %.not23 = icmp eq i32 %33, 2
  br i1 %.not23, label %34, label %39

34:                                               ; preds = %27
  %35 = load i64, ptr %8, align 8, !tbaa !18
  %spec.select = call i64 @llvm.smax.i64(i64 %35, i64 0)
  %36 = load i64, ptr %7, align 8, !tbaa !18
  %37 = add i64 %36, 1
  %38 = sub i64 %37, %spec.select
  br label %.sink.split

.sink.split:                                      ; preds = %5, %34
  %.sink = phi i64 [ %38, %34 ], [ 0, %5 ]
  store i64 %.sink, ptr %4, align 8, !tbaa !18
  br label %39

39:                                               ; preds = %.sink.split, %15, %27, %13
  %.0 = phi i32 [ %14, %13 ], [ %33, %27 ], [ %21, %15 ], [ 0, %.sink.split ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #4
  ret i32 %.0
}

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

attributes #0 = { mustprogress 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 = { "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 #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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, !7, i64 16}
!6 = !{!"", !7, i64 0, !10, i64 8, !7, i64 16, !10, i64 24}
!7 = !{!"double", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"any pointer", !8, i64 0}
!14 = !{!6, !10, i64 24}
!15 = !{!16, !17, i64 16}
!16 = !{!"", !17, i64 0, !17, i64 8, !17, i64 16, !17, i64 24}
!17 = !{!"long", !8, i64 0}
!18 = !{!17, !17, i64 0}
!19 = !{!6, !10, i64 8}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass