; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal (TcpLocateTcb)

; 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._TCP_PEER = type { i32, i16 }
%struct._TCP_CB = type { %struct._LIST_ENTRY, ptr, ptr, %struct._TCP_PEER, %struct._TCP_PEER, %struct._LIST_ENTRY, %struct._LIST_ENTRY, i32, i32, i8, i8, i16, i32, i32, i32, i32, i32, i32, i32, i32, i32, i16, i32, i32, i32, i32, i32, i16, i16, [6 x i32], i32, i32, i32, i8, i8, i8, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i16, i8, i8, i32, i32, i32, i8, i8, i16, i32, i32, i32, i8, i8, i8, %struct.EFI_IPv4_ADDRESS, ptr }
%struct.EFI_IPv4_ADDRESS = type { [4 x i8] }

@mTcpRunQue = external dso_local global %struct._LIST_ENTRY, align 8

; 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 ptr @TcpLocateListenTcb(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @TcpLocateTcb(i16 noundef zeroext %0, i32 noundef %1, i16 noundef zeroext %2, i32 noundef %3, i8 noundef zeroext %4) #1 {
  %6 = alloca ptr, align 8
  %7 = alloca i16, align 2
  %8 = alloca i32, align 4
  %9 = alloca i16, align 2
  %10 = alloca i32, align 4
  %11 = alloca i8, align 1
  %12 = alloca %struct._TCP_PEER, align 4
  %13 = alloca %struct._TCP_PEER, align 4
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  %16 = alloca i32, align 4
  store i16 %0, ptr %7, align 2, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !9
  store i16 %2, ptr %9, align 2, !tbaa !5
  store i32 %3, ptr %10, align 4, !tbaa !9
  store i8 %4, ptr %11, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #3
  %17 = load i16, ptr %7, align 2, !tbaa !5
  %18 = getelementptr inbounds %struct._TCP_PEER, ptr %12, i32 0, i32 1
  store i16 %17, ptr %18, align 4, !tbaa !12
  %19 = load i32, ptr %8, align 4, !tbaa !9
  %20 = getelementptr inbounds %struct._TCP_PEER, ptr %12, i32 0, i32 0
  store i32 %19, ptr %20, align 4, !tbaa !14
  %21 = load i16, ptr %9, align 2, !tbaa !5
  %22 = getelementptr inbounds %struct._TCP_PEER, ptr %13, i32 0, i32 1
  store i16 %21, ptr %22, align 4, !tbaa !12
  %23 = load i32, ptr %10, align 4, !tbaa !9
  %24 = getelementptr inbounds %struct._TCP_PEER, ptr %13, i32 0, i32 0
  store i32 %23, ptr %24, align 4, !tbaa !14
  %25 = load ptr, ptr @mTcpRunQue, align 8, !tbaa !15
  store ptr %25, ptr %14, align 8, !tbaa !18
  br label %26

26:                                               ; preds = %78, %5
  %27 = load ptr, ptr %14, align 8, !tbaa !18
  %28 = icmp ne ptr %27, @mTcpRunQue
  br i1 %28, label %29, label %82

29:                                               ; preds = %26
  %30 = load ptr, ptr %14, align 8, !tbaa !18
  %31 = ptrtoint ptr %30 to i64
  %32 = sub i64 %31, 0
  %33 = inttoptr i64 %32 to ptr
  store ptr %33, ptr %15, align 8, !tbaa !18
  %34 = getelementptr inbounds %struct._TCP_PEER, ptr %13, i32 0, i32 0
  %35 = load i32, ptr %34, align 4, !tbaa !14
  %36 = load ptr, ptr %15, align 8, !tbaa !18
  %37 = getelementptr inbounds %struct._TCP_CB, ptr %36, i32 0, i32 4
  %38 = getelementptr inbounds %struct._TCP_PEER, ptr %37, i32 0, i32 0
  %39 = load i32, ptr %38, align 8, !tbaa !19
  %40 = icmp eq i32 %35, %39
  br i1 %40, label %41, label %77

41:                                               ; preds = %29
  %42 = getelementptr inbounds %struct._TCP_PEER, ptr %13, i32 0, i32 1
  %43 = load i16, ptr %42, align 4, !tbaa !12
  %44 = zext i16 %43 to i32
  %45 = load ptr, ptr %15, align 8, !tbaa !18
  %46 = getelementptr inbounds %struct._TCP_CB, ptr %45, i32 0, i32 4
  %47 = getelementptr inbounds %struct._TCP_PEER, ptr %46, i32 0, i32 1
  %48 = load i16, ptr %47, align 4, !tbaa !22
  %49 = zext i16 %48 to i32
  %50 = icmp eq i32 %44, %49
  br i1 %50, label %51, label %77

51:                                               ; preds = %41
  %52 = getelementptr inbounds %struct._TCP_PEER, ptr %12, i32 0, i32 0
  %53 = load i32, ptr %52, align 4, !tbaa !14
  %54 = load ptr, ptr %15, align 8, !tbaa !18
  %55 = getelementptr inbounds %struct._TCP_CB, ptr %54, i32 0, i32 3
  %56 = getelementptr inbounds %struct._TCP_PEER, ptr %55, i32 0, i32 0
  %57 = load i32, ptr %56, align 8, !tbaa !23
  %58 = icmp eq i32 %53, %57
  br i1 %58, label %59, label %77

59:                                               ; preds = %51
  %60 = getelementptr inbounds %struct._TCP_PEER, ptr %12, i32 0, i32 1
  %61 = load i16, ptr %60, align 4, !tbaa !12
  %62 = zext i16 %61 to i32
  %63 = load ptr, ptr %15, align 8, !tbaa !18
  %64 = getelementptr inbounds %struct._TCP_CB, ptr %63, i32 0, i32 3
  %65 = getelementptr inbounds %struct._TCP_PEER, ptr %64, i32 0, i32 1
  %66 = load i16, ptr %65, align 4, !tbaa !24
  %67 = zext i16 %66 to i32
  %68 = icmp eq i32 %62, %67
  br i1 %68, label %69, label %77

69:                                               ; preds = %59
  %70 = load ptr, ptr %15, align 8, !tbaa !18
  %71 = getelementptr inbounds %struct._TCP_CB, ptr %70, i32 0, i32 0
  %72 = call ptr @RemoveEntryList(ptr noundef %71)
  %73 = load ptr, ptr %15, align 8, !tbaa !18
  %74 = getelementptr inbounds %struct._TCP_CB, ptr %73, i32 0, i32 0
  %75 = call ptr @InsertHeadList(ptr noundef @mTcpRunQue, ptr noundef %74)
  %76 = load ptr, ptr %15, align 8, !tbaa !18
  store ptr %76, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %88

77:                                               ; preds = %59, %51, %41, %29
  br label %78

78:                                               ; preds = %77
  %79 = load ptr, ptr %14, align 8, !tbaa !18
  %80 = getelementptr inbounds %struct._LIST_ENTRY, ptr %79, i32 0, i32 0
  %81 = load ptr, ptr %80, align 8, !tbaa !15
  store ptr %81, ptr %14, align 8, !tbaa !18
  br label %26, !llvm.loop !25

82:                                               ; preds = %26
  %83 = load i8, ptr %11, align 1, !tbaa !11
  %84 = icmp ne i8 %83, 0
  br i1 %84, label %85, label %87

85:                                               ; preds = %82
  %86 = call ptr @TcpLocateListenTcb(ptr noundef %12, ptr noundef %13)
  store ptr %86, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %88

87:                                               ; preds = %82
  store ptr null, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %88

88:                                               ; preds = %87, %85, %69
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  %89 = load ptr, ptr %6, align 8
  ret ptr %89
}

declare ptr @RemoveEntryList(ptr noundef) #2

declare ptr @InsertHeadList(ptr noundef, ptr noundef) #2

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 = { "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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !6, i64 4}
!13 = !{!"_TCP_PEER", !10, i64 0, !6, i64 4}
!14 = !{!13, !10, i64 0}
!15 = !{!16, !17, i64 0}
!16 = !{!"_LIST_ENTRY", !17, i64 0, !17, i64 8}
!17 = !{!"any pointer", !7, i64 0}
!18 = !{!17, !17, i64 0}
!19 = !{!20, !10, i64 40}
!20 = !{!"_TCP_CB", !16, i64 0, !17, i64 16, !17, i64 24, !13, i64 32, !13, i64 40, !16, i64 48, !16, i64 64, !10, i64 80, !10, i64 84, !7, i64 88, !7, i64 89, !6, i64 90, !10, i64 92, !10, i64 96, !10, i64 100, !10, i64 104, !10, i64 108, !10, i64 112, !10, i64 116, !10, i64 120, !10, i64 124, !6, i64 128, !10, i64 132, !10, i64 136, !10, i64 140, !10, i64 144, !10, i64 148, !6, i64 152, !6, i64 154, !7, i64 156, !10, i64 180, !10, i64 184, !10, i64 188, !7, i64 192, !7, i64 193, !7, i64 194, !10, i64 196, !10, i64 200, !10, i64 204, !10, i64 208, !10, i64 212, !10, i64 216, !10, i64 220, !10, i64 224, !10, i64 228, !10, i64 232, !6, i64 236, !7, i64 238, !7, i64 239, !10, i64 240, !10, i64 244, !10, i64 248, !7, i64 252, !7, i64 253, !6, i64 254, !10, i64 256, !10, i64 260, !10, i64 264, !7, i64 268, !7, i64 269, !7, i64 270, !21, i64 271, !17, i64 280}
!21 = !{!"", !7, i64 0}
!22 = !{!20, !6, i64 44}
!23 = !{!20, !10, i64 32}
!24 = !{!20, !6, i64 36}
!25 = distinct !{!25, !26}
!26 = !{!"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 }
%struct._TCP_PEER = type { i32, i16 }

@mTcpRunQue = external dso_local global %struct._LIST_ENTRY, align 8

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

; Function Attrs: mustprogress 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 ptr @TcpLocateListenTcb(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local ptr @TcpLocateTcb(i16 noundef zeroext %0, i32 noundef %1, i16 noundef zeroext %2, i32 noundef %3, i8 noundef zeroext %4) local_unnamed_addr #1 {
  %6 = alloca %struct._TCP_PEER, align 4
  %7 = alloca %struct._TCP_PEER, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %7) #3
  %8 = getelementptr inbounds i8, ptr %6, i64 4
  store i16 %0, ptr %8, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !11
  %9 = getelementptr inbounds i8, ptr %7, i64 4
  store i16 %2, ptr %9, align 4, !tbaa !5
  store i32 %3, ptr %7, align 4, !tbaa !11
  %.01518 = load ptr, ptr @mTcpRunQue, align 8, !tbaa !12
  %.not19 = icmp eq ptr %.01518, @mTcpRunQue
  br i1 %.not19, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %5, %28
  %.01520 = phi ptr [ %.015, %28 ], [ %.01518, %5 ]
  %10 = getelementptr inbounds i8, ptr %.01520, i64 40
  %11 = load i32, ptr %10, align 8, !tbaa !15
  %12 = icmp eq i32 %11, %3
  br i1 %12, label %13, label %28

13:                                               ; preds = %.lr.ph
  %14 = getelementptr inbounds i8, ptr %.01520, i64 44
  %15 = load i16, ptr %14, align 4, !tbaa !18
  %16 = icmp eq i16 %15, %2
  br i1 %16, label %17, label %28

17:                                               ; preds = %13
  %18 = getelementptr inbounds i8, ptr %.01520, i64 32
  %19 = load i32, ptr %18, align 8, !tbaa !19
  %20 = icmp eq i32 %19, %1
  br i1 %20, label %21, label %28

21:                                               ; preds = %17
  %22 = getelementptr inbounds i8, ptr %.01520, i64 36
  %23 = load i16, ptr %22, align 4, !tbaa !20
  %24 = icmp eq i16 %23, %0
  br i1 %24, label %25, label %28

25:                                               ; preds = %21
  %26 = tail call ptr @RemoveEntryList(ptr noundef %.01520) #3
  %27 = tail call ptr @InsertHeadList(ptr noundef nonnull @mTcpRunQue, ptr noundef %.01520) #3
  br label %31

28:                                               ; preds = %.lr.ph, %13, %17, %21
  %.015 = load ptr, ptr %.01520, align 8, !tbaa !12
  %.not = icmp eq ptr %.015, @mTcpRunQue
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !21

._crit_edge:                                      ; preds = %28, %5
  %.not16 = icmp eq i8 %4, 0
  br i1 %.not16, label %31, label %29

29:                                               ; preds = %._crit_edge
  %30 = call ptr @TcpLocateListenTcb(ptr noundef nonnull %6, ptr noundef nonnull %7)
  br label %31

31:                                               ; preds = %._crit_edge, %29, %25
  %.0 = phi ptr [ %.01520, %25 ], [ %30, %29 ], [ null, %._crit_edge ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #3
  ret ptr %.0
}

declare ptr @RemoveEntryList(ptr noundef) local_unnamed_addr #2

declare ptr @InsertHeadList(ptr noundef, ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress 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 = { "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, !10, i64 4}
!6 = !{!"_TCP_PEER", !7, i64 0, !10, i64 4}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!13, !14, i64 0}
!13 = !{!"_LIST_ENTRY", !14, i64 0, !14, i64 8}
!14 = !{!"any pointer", !8, i64 0}
!15 = !{!16, !7, i64 40}
!16 = !{!"_TCP_CB", !13, i64 0, !14, i64 16, !14, i64 24, !6, i64 32, !6, i64 40, !13, i64 48, !13, i64 64, !7, i64 80, !7, i64 84, !8, i64 88, !8, i64 89, !10, i64 90, !7, i64 92, !7, i64 96, !7, i64 100, !7, i64 104, !7, i64 108, !7, i64 112, !7, i64 116, !7, i64 120, !7, i64 124, !10, i64 128, !7, i64 132, !7, i64 136, !7, i64 140, !7, i64 144, !7, i64 148, !10, i64 152, !10, i64 154, !8, i64 156, !7, i64 180, !7, i64 184, !7, i64 188, !8, i64 192, !8, i64 193, !8, i64 194, !7, i64 196, !7, i64 200, !7, i64 204, !7, i64 208, !7, i64 212, !7, i64 216, !7, i64 220, !7, i64 224, !7, i64 228, !7, i64 232, !10, i64 236, !8, i64 238, !8, i64 239, !7, i64 240, !7, i64 244, !7, i64 248, !8, i64 252, !8, i64 253, !10, i64 254, !7, i64 256, !7, i64 260, !7, i64 264, !8, i64 268, !8, i64 269, !8, i64 270, !17, i64 271, !14, i64 280}
!17 = !{!"", !8, i64 0}
!18 = !{!16, !10, i64 44}
!19 = !{!16, !7, i64 32}
!20 = !{!16, !10, i64 36}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


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