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

; 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._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 }
%union.EFI_IP_ADDRESS = type { [4 x i32] }
%struct._EFI_IPSEC_SPD_SELECTOR = type { i32, ptr, i32, ptr, i16, i16, i16, i16, i16 }

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

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

14:                                               ; preds = %44, %3
  %15 = load ptr, ptr %8, align 8, !tbaa !5
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = icmp ne ptr %15, %16
  br i1 %17, label %18, label %48

18:                                               ; preds = %14
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = ptrtoint ptr %19 to i64
  %21 = sub i64 %20, ptrtoint (ptr getelementptr inbounds (%struct._IPSEC_SAD_ENTRY, ptr null, i32 0, i32 3) to i64)
  %22 = inttoptr i64 %21 to ptr
  store ptr %22, ptr %9, align 8, !tbaa !5
  %23 = load i8, ptr %7, align 1, !tbaa !9
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = load ptr, ptr %9, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct._IPSEC_SAD_ENTRY, ptr %25, i32 0, i32 1
  %27 = load ptr, ptr %26, align 8, !tbaa !12
  %28 = getelementptr inbounds %struct._IPSEC_SAD_DATA, ptr %27, i32 0, i32 8
  %29 = load ptr, ptr %28, align 8, !tbaa !14
  %30 = getelementptr inbounds %struct._EFI_IPSEC_SPD_SELECTOR, ptr %29, i32 0, i32 3
  %31 = load ptr, ptr %30, align 8, !tbaa !19
  %32 = load ptr, ptr %9, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct._IPSEC_SAD_ENTRY, ptr %32, i32 0, i32 1
  %34 = load ptr, ptr %33, align 8, !tbaa !12
  %35 = getelementptr inbounds %struct._IPSEC_SAD_DATA, ptr %34, i32 0, i32 8
  %36 = load ptr, ptr %35, align 8, !tbaa !14
  %37 = getelementptr inbounds %struct._EFI_IPSEC_SPD_SELECTOR, ptr %36, i32 0, i32 2
  %38 = load i32, ptr %37, align 8, !tbaa !22
  %39 = call zeroext i8 @IpSecMatchIpAddress(i8 noundef zeroext %23, ptr noundef %24, ptr noundef %31, i32 noundef %38)
  %40 = icmp ne i8 %39, 0
  br i1 %40, label %41, label %43

41:                                               ; preds = %18
  %42 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %42, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %49

43:                                               ; preds = %18
  br label %44

44:                                               ; preds = %43
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct._LIST_ENTRY, ptr %45, i32 0, i32 0
  %47 = load ptr, ptr %46, align 8, !tbaa !10
  store ptr %47, ptr %8, align 8, !tbaa !5
  br label %14, !llvm.loop !23

48:                                               ; preds = %14
  store ptr null, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %49

49:                                               ; preds = %48, %41
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  %50 = load ptr, ptr %4, align 8
  ret ptr %50
}

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @IpSecLookupSadBySpd(ptr noundef readonly %0, ptr noundef %1, i8 noundef zeroext %2) local_unnamed_addr #0 {
  br label %4

4:                                                ; preds = %5, %3
  %.010.in = phi ptr [ %0, %3 ], [ %.010, %5 ]
  %.010 = load ptr, ptr %.010.in, align 8, !tbaa !5
  %.not = icmp eq ptr %.010, %0
  br i1 %.not, label %18, label %5

5:                                                ; preds = %4
  %6 = ptrtoint ptr %.010 to i64
  %7 = add i64 %6, -32
  %8 = inttoptr i64 %7 to ptr
  %9 = getelementptr inbounds i8, ptr %8, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !10
  %11 = getelementptr inbounds i8, ptr %10, i64 144
  %12 = load ptr, ptr %11, align 8, !tbaa !12
  %13 = getelementptr inbounds i8, ptr %12, i64 24
  %14 = load ptr, ptr %13, align 8, !tbaa !17
  %15 = getelementptr inbounds i8, ptr %12, i64 16
  %16 = load i32, ptr %15, align 8, !tbaa !20
  %17 = tail call zeroext i8 @IpSecMatchIpAddress(i8 noundef zeroext %2, ptr noundef %1, ptr noundef %14, i32 noundef %16)
  %.not11 = icmp eq i8 %17, 0
  br i1 %.not11, label %4, label %18, !llvm.loop !21

18:                                               ; preds = %4, %5
  %.0 = phi ptr [ %8, %5 ], [ null, %4 ]
  ret ptr %.0
}

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

!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 = !{!"_LIST_ENTRY", !7, i64 0, !7, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !7, i64 8}
!11 = !{!"_IPSEC_SAD_ENTRY", !7, i64 0, !7, i64 8, !6, i64 16, !6, i64 32}
!12 = !{!13, !7, i64 144}
!13 = !{!"_IPSEC_SAD_DATA", !14, i64 0, !15, i64 8, !8, i64 16, !8, i64 24, !8, i64 56, !16, i64 104, !14, i64 128, !7, i64 136, !7, i64 144, !8, i64 152, !8, i64 153, !8, i64 156, !8, i64 172}
!14 = !{!"int", !8, i64 0}
!15 = !{!"long long", !8, i64 0}
!16 = !{!"_EFI_IPSEC_SA_LIFETIME", !15, i64 0, !15, i64 8, !15, i64 16}
!17 = !{!18, !7, i64 24}
!18 = !{!"_EFI_IPSEC_SPD_SELECTOR", !14, i64 0, !7, i64 8, !14, i64 16, !7, i64 24, !19, i64 32, !19, i64 34, !19, i64 36, !19, i64 38, !19, i64 40}
!19 = !{!"short", !8, i64 0}
!20 = !{!18, !14, i64 16}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass