; LLVM IR from seppo0010/rlite :: src (page_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.rl_data_type = type { ptr, ptr, ptr, ptr }
%struct.rl_skiplist_iterator = type { ptr, ptr, i64, i32, i32, i64 }
%struct.rl_skiplist_node = type { i64, double, i64, i64, [0 x %struct.rl_skiplist_node_level] }
%struct.rl_skiplist_node_level = type { i64, i64 }

@rl_data_type_skiplist_node = external global %struct.rl_data_type, align 8

; 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 i32 @rl_skiplist_iterator_next(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.rl_skiplist_iterator, ptr %8, i32 0, i32 2
  %10 = load i64, ptr %9, align 8, !tbaa !9
  %11 = icmp ne i64 %10, 0
  br i1 %11, label %12, label %21

12:                                               ; preds = %2
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.rl_skiplist_iterator, ptr %13, i32 0, i32 4
  %15 = load i32, ptr %14, align 4, !tbaa !13
  %16 = sext i32 %15 to i64
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.rl_skiplist_iterator, ptr %17, i32 0, i32 5
  %19 = load i64, ptr %18, align 8, !tbaa !14
  %20 = icmp eq i64 %16, %19
  br i1 %20, label %21, label %22

21:                                               ; preds = %12, %2
  store i32 9, ptr %5, align 4, !tbaa !15
  br label %67

22:                                               ; preds = %12
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.rl_skiplist_iterator, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !16
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.rl_skiplist_iterator, ptr %26, i32 0, i32 2
  %28 = load i64, ptr %27, align 8, !tbaa !9
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.rl_skiplist_iterator, ptr %29, i32 0, i32 1
  %31 = load ptr, ptr %30, align 8, !tbaa !17
  %32 = call i32 @rl_read(ptr noundef %25, ptr noundef @rl_data_type_skiplist_node, i64 noundef %28, ptr noundef %31, ptr noundef %6, i32 noundef 1)
  store i32 %32, ptr %5, align 4, !tbaa !15
  %33 = load i32, ptr %5, align 4, !tbaa !15
  %34 = icmp ne i32 2, %33
  br i1 %34, label %35, label %36

35:                                               ; preds = %22
  br label %67

36:                                               ; preds = %22
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %37, ptr %7, align 8, !tbaa !5
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.rl_skiplist_iterator, ptr %38, i32 0, i32 3
  %40 = load i32, ptr %39, align 8, !tbaa !18
  %41 = icmp eq i32 %40, 1
  br i1 %41, label %42, label %50

42:                                               ; preds = %36
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.rl_skiplist_node, ptr %43, i32 0, i32 4
  %45 = getelementptr inbounds [0 x %struct.rl_skiplist_node_level], ptr %44, i64 0, i64 0
  %46 = getelementptr inbounds %struct.rl_skiplist_node_level, ptr %45, i32 0, i32 0
  %47 = load i64, ptr %46, align 8, !tbaa !19
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.rl_skiplist_iterator, ptr %48, i32 0, i32 2
  store i64 %47, ptr %49, align 8, !tbaa !9
  br label %56

50:                                               ; preds = %36
  %51 = load ptr, ptr %7, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.rl_skiplist_node, ptr %51, i32 0, i32 2
  %53 = load i64, ptr %52, align 8, !tbaa !21
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.rl_skiplist_iterator, ptr %54, i32 0, i32 2
  store i64 %53, ptr %55, align 8, !tbaa !9
  br label %56

56:                                               ; preds = %50, %42
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = icmp ne ptr %57, null
  br i1 %58, label %59, label %62

59:                                               ; preds = %56
  %60 = load ptr, ptr %7, align 8, !tbaa !5
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %60, ptr %61, align 8, !tbaa !5
  br label %62

62:                                               ; preds = %59, %56
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.rl_skiplist_iterator, ptr %63, i32 0, i32 4
  %65 = load i32, ptr %64, align 4, !tbaa !13
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %64, align 4, !tbaa !13
  store i32 0, ptr %5, align 4, !tbaa !15
  br label %67

67:                                               ; preds = %62, %35, %21
  %68 = load i32, ptr %5, align 4, !tbaa !15
  %69 = icmp ne i32 %68, 0
  br i1 %69, label %70, label %76

70:                                               ; preds = %67
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.rl_skiplist_iterator, ptr %71, i32 0, i32 0
  %73 = load ptr, ptr %72, align 8, !tbaa !16
  %74 = load ptr, ptr %3, align 8, !tbaa !5
  %75 = call i32 @rl_skiplist_iterator_destroy(ptr noundef %73, ptr noundef %74)
  br label %76

76:                                               ; preds = %70, %67
  %77 = load i32, ptr %5, align 4, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret i32 %77
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @rl_skiplist_iterator_destroy(ptr noundef, ptr noundef) #1

declare i32 @rl_read(ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef, i32 noundef) #2

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, !11, i64 16}
!10 = !{!"rl_skiplist_iterator", !6, i64 0, !6, i64 8, !11, i64 16, !12, i64 24, !12, i64 28, !11, i64 32}
!11 = !{!"long", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !12, i64 28}
!14 = !{!10, !11, i64 32}
!15 = !{!12, !12, i64 0}
!16 = !{!10, !6, i64 0}
!17 = !{!10, !6, i64 8}
!18 = !{!10, !12, i64 24}
!19 = !{!20, !11, i64 0}
!20 = !{!"rl_skiplist_node_level", !11, i64 0, !11, i64 8}
!21 = !{!11, !11, i64 0}


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

%struct.rl_data_type = type { ptr, ptr, ptr, ptr }

@rl_data_type_skiplist_node = external global %struct.rl_data_type, align 8

; Function Attrs: nounwind uwtable
define dso_local range(i32 3, 2) i32 @rl_skiplist_iterator_next(ptr noundef %0, ptr noundef writeonly %1) local_unnamed_addr #0 {
  %3 = alloca ptr, align 8
  %4 = getelementptr inbounds i8, ptr %0, i64 16
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %.not = icmp eq i64 %5, 0
  br i1 %.not, label %.thread, label %6

6:                                                ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 28
  %8 = load i32, ptr %7, align 4, !tbaa !12
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds i8, ptr %0, i64 32
  %11 = load i64, ptr %10, align 8, !tbaa !13
  %12 = icmp eq i64 %11, %9
  br i1 %12, label %.thread, label %13

13:                                               ; preds = %6
  %14 = load ptr, ptr %0, align 8, !tbaa !14
  %15 = getelementptr inbounds i8, ptr %0, i64 8
  %16 = load ptr, ptr %15, align 8, !tbaa !15
  %17 = call i32 @rl_read(ptr noundef %14, ptr noundef nonnull @rl_data_type_skiplist_node, i64 noundef %5, ptr noundef %16, ptr noundef nonnull %3, i32 noundef 1) #2
  switch i32 %17, label %.thread [
    i32 2, label %18
    i32 0, label %28
  ]

18:                                               ; preds = %13
  %19 = load ptr, ptr %3, align 8, !tbaa !16
  %20 = getelementptr inbounds i8, ptr %0, i64 24
  %21 = load i32, ptr %20, align 8, !tbaa !17
  %22 = icmp eq i32 %21, 1
  %storemerge.in.v = select i1 %22, i64 32, i64 16
  %storemerge.in = getelementptr inbounds i8, ptr %19, i64 %storemerge.in.v
  %storemerge = load i64, ptr %storemerge.in, align 8, !tbaa !18
  store i64 %storemerge, ptr %4, align 8, !tbaa !5
  %.not23 = icmp eq ptr %1, null
  br i1 %.not23, label %.thread29, label %23

23:                                               ; preds = %18
  store ptr %19, ptr %1, align 8, !tbaa !16
  br label %.thread29

.thread29:                                        ; preds = %18, %23
  %24 = load i32, ptr %7, align 4, !tbaa !12
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %7, align 4, !tbaa !12
  br label %28

.thread:                                          ; preds = %13, %2, %6
  %.027 = phi i32 [ 9, %6 ], [ 9, %2 ], [ %17, %13 ]
  %26 = load ptr, ptr %0, align 8, !tbaa !14
  %27 = call i32 @rl_skiplist_iterator_destroy(ptr noundef %26, ptr noundef nonnull %0)
  br label %28

28:                                               ; preds = %13, %.thread29, %.thread
  %.028 = phi i32 [ %.027, %.thread ], [ 0, %.thread29 ], [ %17, %13 ]
  ret i32 %.028
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @rl_skiplist_iterator_destroy(ptr noundef, ptr noundef) local_unnamed_addr #0

declare i32 @rl_read(ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

attributes #0 = { 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 #1 = { "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, !10, i64 16}
!6 = !{!"rl_skiplist_iterator", !7, i64 0, !7, i64 8, !10, i64 16, !11, i64 24, !11, i64 28, !10, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!6, !11, i64 28}
!13 = !{!6, !10, i64 32}
!14 = !{!6, !7, i64 0}
!15 = !{!6, !7, i64 8}
!16 = !{!7, !7, i64 0}
!17 = !{!6, !11, i64 24}
!18 = !{!10, !10, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass