; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/NetworkPkg/IpSecDxe (IpSecLookupSadBySpi)

; 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._LIST_ENTRY = type { ptr, ptr }
%struct._IPSEC_SAD_ENTRY = type { ptr, ptr, %struct._LIST_ENTRY, %struct._LIST_ENTRY }
%struct._EFI_IPSEC_SA_ID = type { i32, i32, %union.EFI_IP_ADDRESS }
%union.EFI_IP_ADDRESS = type { [4 x i32] }
%struct._IPSEC_SAD_DATA = type { i32, i64, i8, [4 x i64], %union.EFI_IPSEC_ALGO_INFO, %struct._EFI_IPSEC_SA_LIFETIME, i32, ptr, ptr, i8, i8, %union.EFI_IP_ADDRESS, %union.EFI_IP_ADDRESS }
%union.EFI_IPSEC_ALGO_INFO = type { %struct._EFI_IPSEC_ESP_ALGO_INFO }
%struct._EFI_IPSEC_ESP_ALGO_INFO = type { i8, i64, ptr, i8, i64, ptr }
%struct._EFI_IPSEC_SA_LIFETIME = type { i64, i64, i64 }
%struct._EFI_IPSEC_SPD_SELECTOR = type { i32, ptr, i32, ptr, i16, i16, i16, i16, i16 }

@mConfigData = external global [3 x %struct._LIST_ENTRY], align 16

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

declare i64 @CompareMem(ptr noundef, ptr noundef, i64 noundef) #1

; 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
declare dso_local zeroext i8 @IpSecMatchIpAddress(i8 noundef zeroext, ptr noundef, ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @IpSecLookupSadBySpi(i32 noundef %0, ptr noundef %1, i8 noundef zeroext %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store i8 %2, ptr %7, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  store ptr getelementptr inbounds ([3 x %struct._LIST_ENTRY], ptr @mConfigData, i64 0, i64 1), ptr %9, align 8, !tbaa !9
  %12 = load ptr, ptr %9, align 8, !tbaa !9
  %13 = getelementptr inbounds %struct._LIST_ENTRY, ptr %12, i32 0, i32 0
  %14 = load ptr, ptr %13, align 8, !tbaa !12
  store ptr %14, ptr %8, align 8, !tbaa !9
  br label %15

15:                                               ; preds = %80, %3
  %16 = load ptr, ptr %8, align 8, !tbaa !9
  %17 = load ptr, ptr %9, align 8, !tbaa !9
  %18 = icmp ne ptr %16, %17
  br i1 %18, label %19, label %84

19:                                               ; preds = %15
  %20 = load ptr, ptr %8, align 8, !tbaa !9
  %21 = ptrtoint ptr %20 to i64
  %22 = sub i64 %21, ptrtoint (ptr getelementptr inbounds (%struct._IPSEC_SAD_ENTRY, ptr null, i32 0, i32 2) to i64)
  %23 = inttoptr i64 %22 to ptr
  store ptr %23, ptr %10, align 8, !tbaa !9
  %24 = load ptr, ptr %10, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct._IPSEC_SAD_ENTRY, ptr %24, i32 0, i32 0
  %26 = load ptr, ptr %25, align 8, !tbaa !14
  %27 = getelementptr inbounds %struct._EFI_IPSEC_SA_ID, ptr %26, i32 0, i32 0
  %28 = load i32, ptr %27, align 4, !tbaa !16
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = icmp eq i32 %28, %29
  br i1 %30, label %31, label %79

31:                                               ; preds = %19
  %32 = load ptr, ptr %10, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct._IPSEC_SAD_ENTRY, ptr %32, i32 0, i32 1
  %34 = load ptr, ptr %33, align 8, !tbaa !18
  %35 = getelementptr inbounds %struct._IPSEC_SAD_DATA, ptr %34, i32 0, i32 0
  %36 = load i32, ptr %35, align 8, !tbaa !19
  %37 = icmp eq i32 %36, 1
  br i1 %37, label %38, label %48

38:                                               ; preds = %31
  %39 = load ptr, ptr %10, align 8, !tbaa !9
  %40 = getelementptr inbounds %struct._IPSEC_SAD_ENTRY, ptr %39, i32 0, i32 1
  %41 = load ptr, ptr %40, align 8, !tbaa !18
  %42 = getelementptr inbounds %struct._IPSEC_SAD_DATA, ptr %41, i32 0, i32 11
  %43 = call i64 @CompareMem(ptr noundef %6, ptr noundef %42, i64 noundef 16)
  %44 = icmp ne i64 %43, 0
  br i1 %44, label %45, label %47

45:                                               ; preds = %38
  %46 = load ptr, ptr %10, align 8, !tbaa !9
  store ptr %46, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %85

47:                                               ; preds = %38
  br label %78

48:                                               ; preds = %31
  %49 = load ptr, ptr %10, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct._IPSEC_SAD_ENTRY, ptr %49, i32 0, i32 1
  %51 = load ptr, ptr %50, align 8, !tbaa !18
  %52 = getelementptr inbounds %struct._IPSEC_SAD_DATA, ptr %51, i32 0, i32 8
  %53 = load ptr, ptr %52, align 8, !tbaa !23
  %54 = icmp ne ptr %53, null
  br i1 %54, label %55, label %77

55:                                               ; preds = %48
  %56 = load i8, ptr %7, align 1, !tbaa !11
  %57 = load ptr, ptr %6, align 8, !tbaa !9
  %58 = load ptr, ptr %10, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct._IPSEC_SAD_ENTRY, ptr %58, i32 0, i32 1
  %60 = load ptr, ptr %59, align 8, !tbaa !18
  %61 = getelementptr inbounds %struct._IPSEC_SAD_DATA, ptr %60, i32 0, i32 8
  %62 = load ptr, ptr %61, align 8, !tbaa !23
  %63 = getelementptr inbounds %struct._EFI_IPSEC_SPD_SELECTOR, ptr %62, i32 0, i32 3
  %64 = load ptr, ptr %63, align 8, !tbaa !24
  %65 = load ptr, ptr %10, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct._IPSEC_SAD_ENTRY, ptr %65, i32 0, i32 1
  %67 = load ptr, ptr %66, align 8, !tbaa !18
  %68 = getelementptr inbounds %struct._IPSEC_SAD_DATA, ptr %67, i32 0, i32 8
  %69 = load ptr, ptr %68, align 8, !tbaa !23
  %70 = getelementptr inbounds %struct._EFI_IPSEC_SPD_SELECTOR, ptr %69, i32 0, i32 2
  %71 = load i32, ptr %70, align 8, !tbaa !27
  %72 = call zeroext i8 @IpSecMatchIpAddress(i8 noundef zeroext %56, ptr noundef %57, ptr noundef %64, i32 noundef %71)
  %73 = zext i8 %72 to i32
  %74 = icmp ne i32 %73, 0
  br i1 %74, label %75, label %77

75:                                               ; preds = %55
  %76 = load ptr, ptr %10, align 8, !tbaa !9
  store ptr %76, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %85

77:                                               ; preds = %55, %48
  br label %78

78:                                               ; preds = %77, %47
  br label %79

79:                                               ; preds = %78, %19
  br label %80

80:                                               ; preds = %79
  %81 = load ptr, ptr %8, align 8, !tbaa !9
  %82 = getelementptr inbounds %struct._LIST_ENTRY, ptr %81, i32 0, i32 0
  %83 = load ptr, ptr %82, align 8, !tbaa !12
  store ptr %83, ptr %8, align 8, !tbaa !9
  br label %15, !llvm.loop !28

84:                                               ; preds = %15
  store ptr null, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %85

85:                                               ; preds = %84, %75, %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %86 = load ptr, ptr %4, align 8
  ret ptr %86
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !10, i64 0}
!13 = !{!"_LIST_ENTRY", !10, i64 0, !10, i64 8}
!14 = !{!15, !10, i64 0}
!15 = !{!"_IPSEC_SAD_ENTRY", !10, i64 0, !10, i64 8, !13, i64 16, !13, i64 32}
!16 = !{!17, !6, i64 0}
!17 = !{!"_EFI_IPSEC_SA_ID", !6, i64 0, !6, i64 4, !7, i64 8}
!18 = !{!15, !10, i64 8}
!19 = !{!20, !6, i64 0}
!20 = !{!"_IPSEC_SAD_DATA", !6, i64 0, !21, i64 8, !7, i64 16, !7, i64 24, !7, i64 56, !22, i64 104, !6, i64 128, !10, i64 136, !10, i64 144, !7, i64 152, !7, i64 153, !7, i64 156, !7, i64 172}
!21 = !{!"long long", !7, i64 0}
!22 = !{!"_EFI_IPSEC_SA_LIFETIME", !21, i64 0, !21, i64 8, !21, i64 16}
!23 = !{!20, !10, i64 144}
!24 = !{!25, !10, i64 24}
!25 = !{!"_EFI_IPSEC_SPD_SELECTOR", !6, i64 0, !10, i64 8, !6, i64 16, !10, i64 24, !26, i64 32, !26, i64 34, !26, i64 36, !26, i64 38, !26, i64 40}
!26 = !{!"short", !7, i64 0}
!27 = !{!25, !6, i64 16}
!28 = distinct !{!28, !29}
!29 = !{!"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._LIST_ENTRY = type { ptr, ptr }

@mConfigData = external global [3 x %struct._LIST_ENTRY], align 16

declare i64 @CompareMem(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @IpSecMatchIpAddress(i8 noundef zeroext, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @IpSecLookupSadBySpi(i32 noundef %0, ptr noundef %1, i8 noundef zeroext %2) local_unnamed_addr #1 {
  %4 = alloca ptr, align 8
  store ptr %1, ptr %4, align 8, !tbaa !5
  %.01520 = load ptr, ptr getelementptr inbounds (i8, ptr @mConfigData, i64 16), align 16, !tbaa !9
  %.not21 = icmp eq ptr %.01520, getelementptr inbounds (i8, ptr @mConfigData, i64 16)
  br i1 %.not21, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3, %29
  %.01522 = phi ptr [ %.015, %29 ], [ %.01520, %3 ]
  %5 = ptrtoint ptr %.01522 to i64
  %6 = add i64 %5, -16
  %7 = inttoptr i64 %6 to ptr
  %8 = load ptr, ptr %7, align 8, !tbaa !11
  %9 = load i32, ptr %8, align 4, !tbaa !13
  %10 = icmp eq i32 %9, %0
  br i1 %10, label %11, label %29

11:                                               ; preds = %.lr.ph
  %12 = getelementptr inbounds i8, ptr %7, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !16
  %14 = load i32, ptr %13, align 8, !tbaa !17
  %15 = icmp eq i32 %14, 1
  br i1 %15, label %16, label %19

16:                                               ; preds = %11
  %17 = getelementptr inbounds i8, ptr %13, i64 156
  %18 = call i64 @CompareMem(ptr noundef nonnull %4, ptr noundef nonnull %17, i64 noundef 16) #2
  %.not19 = icmp eq i64 %18, 0
  br i1 %.not19, label %29, label %._crit_edge

19:                                               ; preds = %11
  %20 = getelementptr inbounds i8, ptr %13, i64 144
  %21 = load ptr, ptr %20, align 8, !tbaa !21
  %.not17 = icmp eq ptr %21, null
  br i1 %.not17, label %29, label %22

22:                                               ; preds = %19
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %21, i64 24
  %25 = load ptr, ptr %24, align 8, !tbaa !22
  %26 = getelementptr inbounds i8, ptr %21, i64 16
  %27 = load i32, ptr %26, align 8, !tbaa !25
  %28 = call zeroext i8 @IpSecMatchIpAddress(i8 noundef zeroext %2, ptr noundef %23, ptr noundef %25, i32 noundef %27)
  %.not18 = icmp eq i8 %28, 0
  br i1 %.not18, label %29, label %._crit_edge

29:                                               ; preds = %.lr.ph, %19, %22, %16
  %.015 = load ptr, ptr %.01522, align 8, !tbaa !9
  %.not = icmp eq ptr %.015, getelementptr inbounds (i8, ptr @mConfigData, i64 16)
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !26

._crit_edge:                                      ; preds = %16, %22, %29, %3
  %.0 = phi ptr [ null, %3 ], [ null, %29 ], [ %7, %22 ], [ %7, %16 ]
  ret ptr %.0
}

attributes #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 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 0}
!10 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!11 = !{!12, !6, i64 0}
!12 = !{!"_IPSEC_SAD_ENTRY", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 32}
!13 = !{!14, !15, i64 0}
!14 = !{!"_EFI_IPSEC_SA_ID", !15, i64 0, !15, i64 4, !7, i64 8}
!15 = !{!"int", !7, i64 0}
!16 = !{!12, !6, i64 8}
!17 = !{!18, !15, i64 0}
!18 = !{!"_IPSEC_SAD_DATA", !15, i64 0, !19, i64 8, !7, i64 16, !7, i64 24, !7, i64 56, !20, i64 104, !15, i64 128, !6, i64 136, !6, i64 144, !7, i64 152, !7, i64 153, !7, i64 156, !7, i64 172}
!19 = !{!"long long", !7, i64 0}
!20 = !{!"_EFI_IPSEC_SA_LIFETIME", !19, i64 0, !19, i64 8, !19, i64 16}
!21 = !{!18, !6, i64 144}
!22 = !{!23, !6, i64 24}
!23 = !{!"_EFI_IPSEC_SPD_SELECTOR", !15, i64 0, !6, i64 8, !15, i64 16, !6, i64 24, !24, i64 32, !24, i64 34, !24, i64 36, !24, i64 38, !24, i64 40}
!24 = !{!"short", !7, i64 0}
!25 = !{!23, !15, i64 16}
!26 = distinct !{!26, !27}
!27 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass