; LLVM IR from dpt/Containers :: libraries/datastruct/orderedarray/lookup.ll_orderedarray (lookup_internal)

; 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.orderedarray = type { ptr, i32, i32, ptr, ptr, ptr, ptr }
%struct.orderedarray__node = type { %struct.item }
%struct.item = type { ptr, i64, ptr }

; Function Attrs: nounwind uwtable
define dso_local i32 @orderedarray__lookup_internal(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.orderedarray, ptr %13, i32 0, i32 0
  %15 = load ptr, ptr %14, align 8, !tbaa !9
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %76

17:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.orderedarray, ptr %18, i32 0, i32 0
  %20 = load ptr, ptr %19, align 8, !tbaa !9
  store ptr %20, ptr %8, align 8, !tbaa !5
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.orderedarray, ptr %21, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8, !tbaa !9
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.orderedarray, ptr %24, i32 0, i32 1
  %26 = load i32, ptr %25, align 8, !tbaa !12
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds %struct.orderedarray__node, ptr %23, i64 %27
  store ptr %28, ptr %9, align 8, !tbaa !5
  br label %29

29:                                               ; preds = %69, %17
  %30 = load ptr, ptr %8, align 8, !tbaa !5
  %31 = load ptr, ptr %9, align 8, !tbaa !5
  %32 = icmp ult ptr %30, %31
  br i1 %32, label %33, label %70

33:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %34 = load ptr, ptr %8, align 8, !tbaa !5
  %35 = load ptr, ptr %9, align 8, !tbaa !5
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = ptrtoint ptr %35 to i64
  %38 = ptrtoint ptr %36 to i64
  %39 = sub i64 %37, %38
  %40 = sdiv exact i64 %39, 24
  %41 = sdiv i64 %40, 2
  %42 = getelementptr inbounds %struct.orderedarray__node, ptr %34, i64 %41
  store ptr %42, ptr %10, align 8, !tbaa !5
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.orderedarray, ptr %43, i32 0, i32 4
  %45 = load ptr, ptr %44, align 8, !tbaa !13
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = load ptr, ptr %10, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.orderedarray__node, ptr %47, i32 0, i32 0
  %49 = getelementptr inbounds %struct.item, ptr %48, i32 0, i32 0
  %50 = load ptr, ptr %49, align 8, !tbaa !14
  %51 = call i32 %45(ptr noundef %46, ptr noundef %50)
  store i32 %51, ptr %11, align 4, !tbaa !18
  %52 = load i32, ptr %11, align 4, !tbaa !18
  %53 = icmp eq i32 %52, 0
  br i1 %53, label %54, label %57

54:                                               ; preds = %33
  %55 = load ptr, ptr %10, align 8, !tbaa !5
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %55, ptr %56, align 8, !tbaa !5
  store i32 1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %67

57:                                               ; preds = %33
  %58 = load i32, ptr %11, align 4, !tbaa !18
  %59 = icmp slt i32 %58, 0
  br i1 %59, label %60, label %62

60:                                               ; preds = %57
  %61 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %61, ptr %9, align 8, !tbaa !5
  br label %65

62:                                               ; preds = %57
  %63 = load ptr, ptr %10, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.orderedarray__node, ptr %63, i64 1
  store ptr %64, ptr %8, align 8, !tbaa !5
  br label %65

65:                                               ; preds = %62, %60
  br label %66

66:                                               ; preds = %65
  store i32 0, ptr %12, align 4
  br label %67

67:                                               ; preds = %66, %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  %68 = load i32, ptr %12, align 4
  switch i32 %68, label %73 [
    i32 0, label %69
  ]

69:                                               ; preds = %67
  br label %29, !llvm.loop !19

70:                                               ; preds = %29
  %71 = load ptr, ptr %8, align 8, !tbaa !5
  %72 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %71, ptr %72, align 8, !tbaa !5
  store i32 0, ptr %12, align 4
  br label %73

73:                                               ; preds = %70, %67
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  %74 = load i32, ptr %12, align 4
  switch i32 %74, label %81 [
    i32 0, label %75
    i32 1, label %79
  ]

75:                                               ; preds = %73
  br label %78

76:                                               ; preds = %3
  %77 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr null, ptr %77, align 8, !tbaa !5
  br label %78

78:                                               ; preds = %76, %75
  store i32 0, ptr %4, align 4
  br label %79

79:                                               ; preds = %78, %73
  %80 = load i32, ptr %4, align 4
  ret i32 %80

81:                                               ; preds = %73
  unreachable
}

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 0}
!10 = !{!"orderedarray", !6, i64 0, !11, i64 8, !11, i64 12, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!10, !6, i64 24}
!14 = !{!15, !6, i64 0}
!15 = !{!"orderedarray__node", !16, i64 0}
!16 = !{!"item", !6, i64 0, !17, i64 8, !6, i64 16}
!17 = !{!"long", !7, i64 0}
!18 = !{!11, !11, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"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"

%struct.orderedarray__node = type { %struct.item }
%struct.item = type { ptr, i64, ptr }

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @orderedarray__lookup_internal(ptr nocapture noundef readonly %0, ptr noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = load ptr, ptr %0, align 8, !tbaa !5
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %.thread36, label %5

5:                                                ; preds = %3
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = load i32, ptr %6, align 8, !tbaa !11
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %.lr.ph, label %.thread36

.lr.ph:                                           ; preds = %5
  %9 = zext nneg i32 %7 to i64
  %10 = getelementptr inbounds %struct.orderedarray__node, ptr %4, i64 %9
  %11 = getelementptr inbounds i8, ptr %0, i64 24
  br label %12

12:                                               ; preds = %.lr.ph, %21
  %.02540 = phi ptr [ %4, %.lr.ph ], [ %.126, %21 ]
  %.02839 = phi ptr [ %10, %.lr.ph ], [ %.129, %21 ]
  %13 = ptrtoint ptr %.02839 to i64
  %14 = ptrtoint ptr %.02540 to i64
  %15 = sub i64 %13, %14
  %16 = sdiv i64 %15, 48
  %17 = getelementptr inbounds %struct.orderedarray__node, ptr %.02540, i64 %16
  %18 = load ptr, ptr %11, align 8, !tbaa !12
  %19 = load ptr, ptr %17, align 8, !tbaa !13
  %20 = tail call i32 %18(ptr noundef %1, ptr noundef %19) #1
  %.not33 = icmp eq i32 %20, 0
  br i1 %.not33, label %.thread36, label %21

21:                                               ; preds = %12
  %22 = icmp slt i32 %20, 0
  %23 = getelementptr inbounds i8, ptr %17, i64 24
  %.129 = select i1 %22, ptr %17, ptr %.02839
  %.126 = select i1 %22, ptr %.02540, ptr %23
  %24 = icmp ult ptr %.126, %.129
  br i1 %24, label %12, label %.thread36, !llvm.loop !17

.thread36:                                        ; preds = %12, %21, %3, %5
  %.sink = phi ptr [ %4, %5 ], [ null, %3 ], [ %.126, %21 ], [ %17, %12 ]
  %.3 = phi i32 [ 0, %5 ], [ 0, %3 ], [ 0, %21 ], [ 1, %12 ]
  store ptr %.sink, ptr %2, align 8, !tbaa !19
  ret i32 %.3
}

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


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