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

; 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_PAD_ENTRY = type { ptr, ptr, %struct._LIST_ENTRY }
%struct._EFI_IPSEC_PAD_ID = type { i8, %union.anon }
%union.anon = type { %struct._EFI_IP_ADDRESS_INFO, [108 x i8] }
%struct._EFI_IP_ADDRESS_INFO = type { %union.EFI_IP_ADDRESS, i8 }
%union.EFI_IP_ADDRESS = type { [4 x i32] }

@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

; 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 @IpSecLookupPadEntry(i8 noundef zeroext %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %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 i32, align 4
  store i8 %0, ptr %4, align 1, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !8
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  store ptr getelementptr inbounds ([3 x %struct._LIST_ENTRY], ptr @mConfigData, i64 0, i64 2), ptr %6, align 8, !tbaa !8
  %11 = load ptr, ptr %6, align 8, !tbaa !8
  %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 %7, align 8, !tbaa !8
  br label %14

14:                                               ; preds = %35, %2
  %15 = load ptr, ptr %7, align 8, !tbaa !8
  %16 = load ptr, ptr %6, align 8, !tbaa !8
  %17 = icmp ne ptr %15, %16
  br i1 %17, label %18, label %39

18:                                               ; preds = %14
  %19 = load ptr, ptr %7, align 8, !tbaa !8
  %20 = ptrtoint ptr %19 to i64
  %21 = sub i64 %20, ptrtoint (ptr getelementptr inbounds (%struct._IPSEC_PAD_ENTRY, ptr null, i32 0, i32 2) to i64)
  %22 = inttoptr i64 %21 to ptr
  store ptr %22, ptr %9, align 8, !tbaa !8
  %23 = load ptr, ptr %9, align 8, !tbaa !8
  %24 = getelementptr inbounds %struct._IPSEC_PAD_ENTRY, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !12
  %26 = getelementptr inbounds %struct._EFI_IPSEC_PAD_ID, ptr %25, i32 0, i32 1
  store ptr %26, ptr %8, align 8, !tbaa !8
  %27 = load i8, ptr %4, align 1, !tbaa !5
  %28 = load ptr, ptr %5, align 8, !tbaa !8
  %29 = load ptr, ptr %8, align 8, !tbaa !8
  %30 = call zeroext i8 @IpSecMatchIpAddress(i8 noundef zeroext %27, ptr noundef %28, ptr noundef %29, i32 noundef 1)
  %31 = icmp ne i8 %30, 0
  br i1 %31, label %32, label %34

32:                                               ; preds = %18
  %33 = load ptr, ptr %9, align 8, !tbaa !8
  store ptr %33, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %40

34:                                               ; preds = %18
  br label %35

35:                                               ; preds = %34
  %36 = load ptr, ptr %7, align 8, !tbaa !8
  %37 = getelementptr inbounds %struct._LIST_ENTRY, ptr %36, i32 0, i32 0
  %38 = load ptr, ptr %37, align 8, !tbaa !10
  store ptr %38, ptr %7, align 8, !tbaa !8
  br label %14, !llvm.loop !14

39:                                               ; preds = %14
  store ptr null, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %40

40:                                               ; preds = %39, %32
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %41 = load ptr, ptr %3, align 8
  ret ptr %41
}

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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !9, i64 0}
!11 = !{!"_LIST_ENTRY", !9, i64 0, !9, i64 8}
!12 = !{!13, !9, i64 0}
!13 = !{!"_IPSEC_PAD_ENTRY", !9, i64 0, !9, i64 8, !11, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"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

; 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 noundef ptr @IpSecLookupPadEntry(i8 noundef zeroext %0, ptr noundef %1) local_unnamed_addr #0 {
  br label %3

3:                                                ; preds = %4, %2
  %.010.in = phi ptr [ getelementptr inbounds (i8, ptr @mConfigData, i64 32), %2 ], [ %.010, %4 ]
  %.010 = load ptr, ptr %.010.in, align 8, !tbaa !5
  %.not = icmp eq ptr %.010, getelementptr inbounds (i8, ptr @mConfigData, i64 32)
  br i1 %.not, label %11, label %4

4:                                                ; preds = %3
  %5 = ptrtoint ptr %.010 to i64
  %6 = add i64 %5, -16
  %7 = inttoptr i64 %6 to ptr
  %8 = load ptr, ptr %7, align 8, !tbaa !10
  %9 = getelementptr inbounds i8, ptr %8, i64 4
  %10 = tail call zeroext i8 @IpSecMatchIpAddress(i8 noundef zeroext %0, ptr noundef %1, ptr noundef nonnull %9, i32 noundef 1)
  %.not11 = icmp eq i8 %10, 0
  br i1 %.not11, label %3, label %11, !llvm.loop !12

11:                                               ; preds = %3, %4
  %.0 = phi ptr [ %7, %4 ], [ null, %3 ]
  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 0}
!11 = !{!"_IPSEC_PAD_ENTRY", !7, i64 0, !7, i64 8, !6, i64 16}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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