; LLVM IR from hutusi/rethink-c :: src (skip_list)

; 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 { i32, ptr, ptr, ptr, ptr }
%struct._SkipListNode = type { ptr, ptr, 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 ptr @skip_list_find(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct._SkipList, ptr %11, i32 0, i32 1
  %13 = load ptr, ptr %12, align 8, !tbaa !9
  store ptr %13, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct._SkipList, ptr %14, i32 0, i32 0
  %16 = load i32, ptr %15, align 8, !tbaa !12
  store i32 %16, ptr %7, align 4, !tbaa !13
  br label %17

17:                                               ; preds = %68, %2
  %18 = load i32, ptr %7, align 4, !tbaa !13
  %19 = icmp sge i32 %18, 0
  br i1 %19, label %21, label %20

20:                                               ; preds = %17
  store i32 2, ptr %8, align 4
  br label %71

21:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct._SkipListNode, ptr %22, i32 0, i32 2
  %24 = load ptr, ptr %23, align 8, !tbaa !14
  %25 = load i32, ptr %7, align 4, !tbaa !13
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds ptr, ptr %24, i64 %26
  %28 = load ptr, ptr %27, align 8, !tbaa !5
  store ptr %28, ptr %9, align 8, !tbaa !5
  br label %29

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

32:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct._SkipList, ptr %33, i32 0, i32 2
  %35 = load ptr, ptr %34, align 8, !tbaa !16
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = load ptr, ptr %9, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct._SkipListNode, ptr %37, i32 0, i32 0
  %39 = load ptr, ptr %38, align 8, !tbaa !17
  %40 = call i32 %35(ptr noundef %36, ptr noundef %39)
  store i32 %40, ptr %10, align 4, !tbaa !13
  %41 = load i32, ptr %10, align 4, !tbaa !13
  %42 = icmp sgt i32 %41, 0
  br i1 %42, label %43, label %52

43:                                               ; preds = %32
  %44 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %44, ptr %6, align 8, !tbaa !5
  %45 = load ptr, ptr %9, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct._SkipListNode, ptr %45, i32 0, i32 2
  %47 = load ptr, ptr %46, align 8, !tbaa !14
  %48 = load i32, ptr %7, align 4, !tbaa !13
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds ptr, ptr %47, i64 %49
  %51 = load ptr, ptr %50, align 8, !tbaa !5
  store ptr %51, ptr %9, align 8, !tbaa !5
  br label %60

52:                                               ; preds = %32
  %53 = load i32, ptr %10, align 4, !tbaa !13
  %54 = icmp slt i32 %53, 0
  br i1 %54, label %55, label %56

55:                                               ; preds = %52
  store i32 6, ptr %8, align 4
  br label %61

56:                                               ; preds = %52
  %57 = load ptr, ptr %9, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct._SkipListNode, ptr %57, i32 0, i32 1
  %59 = load ptr, ptr %58, align 8, !tbaa !18
  store ptr %59, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %61

60:                                               ; preds = %43
  store i32 0, ptr %8, align 4
  br label %61

61:                                               ; preds = %60, %56, %55
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  %62 = load i32, ptr %8, align 4
  switch i32 %62, label %65 [
    i32 0, label %63
    i32 6, label %64
  ]

63:                                               ; preds = %61
  br label %29, !llvm.loop !19

64:                                               ; preds = %61, %29
  store i32 0, ptr %8, align 4
  br label %65

65:                                               ; preds = %64, %61
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  %66 = load i32, ptr %8, align 4
  switch i32 %66, label %71 [
    i32 0, label %67
  ]

67:                                               ; preds = %65
  br label %68

68:                                               ; preds = %67
  %69 = load i32, ptr %7, align 4, !tbaa !13
  %70 = add nsw i32 %69, -1
  store i32 %70, ptr %7, align 4, !tbaa !13
  br label %17, !llvm.loop !21

71:                                               ; preds = %65, %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  %72 = load i32, ptr %8, align 4
  switch i32 %72, label %74 [
    i32 2, label %73
  ]

73:                                               ; preds = %71
  store ptr null, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %74

74:                                               ; preds = %73, %71
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %75 = load ptr, ptr %3, align 8
  ret ptr %75
}

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, !6, i64 8}
!10 = !{!"_SkipList", !11, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!15, !6, i64 16}
!15 = !{!"_SkipListNode", !6, i64 0, !6, i64 8, !6, i64 16}
!16 = !{!10, !6, i64 16}
!17 = !{!15, !6, i64 0}
!18 = !{!15, !6, i64 8}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = distinct !{!21, !20}


; 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: nounwind uwtable
define dso_local ptr @skip_list_find(ptr nocapture noundef readonly %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr %0, align 8, !tbaa !5
  %4 = icmp slt i32 %3, 0
  br i1 %4, label %.loopexit50, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load ptr, ptr %5, align 8, !tbaa !11
  %7 = getelementptr inbounds i8, ptr %0, i64 16
  %8 = zext nneg i32 %3 to i64
  br label %9

9:                                                ; preds = %.lr.ph, %.loopexit
  %indvars.iv = phi i64 [ %8, %.lr.ph ], [ %indvars.iv.next, %.loopexit ]
  %.02654 = phi ptr [ %6, %.lr.ph ], [ %.127, %.loopexit ]
  br label %10

10:                                               ; preds = %11, %9
  %.127 = phi ptr [ %.02654, %9 ], [ %.020, %11 ]
  %.pn.in = getelementptr inbounds i8, ptr %.127, i64 16
  %.pn = load ptr, ptr %.pn.in, align 8, !tbaa !12
  %.020.in = getelementptr inbounds ptr, ptr %.pn, i64 %indvars.iv
  %.020 = load ptr, ptr %.020.in, align 8, !tbaa !14
  %.not = icmp eq ptr %.020, null
  br i1 %.not, label %.loopexit, label %11

11:                                               ; preds = %10
  %12 = load ptr, ptr %7, align 8, !tbaa !15
  %13 = load ptr, ptr %.020, align 8, !tbaa !16
  %14 = tail call i32 %12(ptr noundef %1, ptr noundef %13) #1
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %10, label %16

16:                                               ; preds = %11
  %17 = icmp slt i32 %14, 0
  br i1 %17, label %.loopexit, label %18

18:                                               ; preds = %16
  %19 = getelementptr inbounds i8, ptr %.020, i64 8
  %20 = load ptr, ptr %19, align 8, !tbaa !17
  br label %.loopexit50

.loopexit:                                        ; preds = %10, %16
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %21 = icmp slt i64 %indvars.iv, 1
  br i1 %21, label %.loopexit50, label %9, !llvm.loop !18

.loopexit50:                                      ; preds = %.loopexit, %2, %18
  %spec.select = phi ptr [ %20, %18 ], [ null, %2 ], [ null, %.loopexit ]
  ret ptr %spec.select
}

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


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