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

; 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.EFI_TCP4_ACCESS_POINT = type { i8, %struct.EFI_IPv4_ADDRESS, %struct.EFI_IPv4_ADDRESS, i16, %struct.EFI_IPv4_ADDRESS, i16, i8 }
%struct.EFI_IPv4_ADDRESS = type { [4 x i8] }

@mTcp4RandomPort = external global i16, align 2
@.str.3 = external hidden unnamed_addr constant [49 x i8], align 1

; 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
define dso_local i64 @Tcp4Bind(ptr noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #3
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.EFI_TCP4_ACCESS_POINT, ptr %6, i32 0, i32 3
  %8 = load i16, ptr %7, align 2, !tbaa !9
  %9 = zext i16 %8 to i32
  %10 = icmp ne i32 0, %9
  br i1 %10, label %11, label %21

11:                                               ; preds = %1
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.EFI_TCP4_ACCESS_POINT, ptr %12, i32 0, i32 1
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.EFI_TCP4_ACCESS_POINT, ptr %14, i32 0, i32 3
  %16 = load i16, ptr %15, align 2, !tbaa !9
  %17 = call zeroext i8 @TcpFindTcbByPeer(ptr noundef %13, i16 noundef zeroext %16)
  %18 = icmp ne i8 %17, 0
  br i1 %18, label %19, label %20

19:                                               ; preds = %11
  store i64 -9223372036854775806, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %66

20:                                               ; preds = %11
  br label %65

21:                                               ; preds = %1
  store i8 0, ptr %4, align 1, !tbaa !13
  %22 = load i16, ptr @mTcp4RandomPort, align 2, !tbaa !14
  %23 = zext i16 %22 to i32
  %24 = icmp eq i32 65535, %23
  br i1 %24, label %25, label %26

25:                                               ; preds = %21
  store i16 1024, ptr @mTcp4RandomPort, align 2, !tbaa !14
  br label %26

26:                                               ; preds = %25, %21
  %27 = load i16, ptr @mTcp4RandomPort, align 2, !tbaa !14
  %28 = add i16 %27, 1
  store i16 %28, ptr @mTcp4RandomPort, align 2, !tbaa !14
  br label %29

29:                                               ; preds = %60, %26
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.EFI_TCP4_ACCESS_POINT, ptr %30, i32 0, i32 1
  %32 = load i16, ptr @mTcp4RandomPort, align 2, !tbaa !14
  %33 = call zeroext i8 @TcpFindTcbByPeer(ptr noundef %31, i16 noundef zeroext %32)
  %34 = icmp ne i8 %33, 0
  br i1 %34, label %35, label %61

35:                                               ; preds = %29
  %36 = load i16, ptr @mTcp4RandomPort, align 2, !tbaa !14
  %37 = add i16 %36, 1
  store i16 %37, ptr @mTcp4RandomPort, align 2, !tbaa !14
  %38 = load i16, ptr @mTcp4RandomPort, align 2, !tbaa !14
  %39 = zext i16 %38 to i32
  %40 = icmp sle i32 %39, 1024
  br i1 %40, label %41, label %60

41:                                               ; preds = %35
  %42 = load i8, ptr %4, align 1, !tbaa !13
  %43 = icmp ne i8 %42, 0
  br i1 %43, label %44, label %59

44:                                               ; preds = %41
  br label %45

45:                                               ; preds = %44
  %46 = call zeroext i8 @DebugPrintEnabled()
  %47 = icmp ne i8 %46, 0
  br i1 %47, label %48, label %56

48:                                               ; preds = %45
  br label %49

49:                                               ; preds = %48
  %50 = call zeroext i8 @DebugPrintLevelEnabled(i64 noundef 2147483648)
  %51 = icmp ne i8 %50, 0
  br i1 %51, label %52, label %53

52:                                               ; preds = %49
  call void (i64, ptr, ...) @DebugPrint(i64 noundef 2147483648, ptr noundef @.str.3)
  br label %53

53:                                               ; preds = %52, %49
  br label %54

54:                                               ; preds = %53
  br label %55

55:                                               ; preds = %54
  br label %56

56:                                               ; preds = %55, %45
  br label %57

57:                                               ; preds = %56
  br label %58

58:                                               ; preds = %57
  store i64 -9223372036854775799, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %66

59:                                               ; preds = %41
  store i16 1025, ptr @mTcp4RandomPort, align 2, !tbaa !14
  store i8 1, ptr %4, align 1, !tbaa !13
  br label %60

60:                                               ; preds = %59, %35
  br label %29, !llvm.loop !15

61:                                               ; preds = %29
  %62 = load i16, ptr @mTcp4RandomPort, align 2, !tbaa !14
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.EFI_TCP4_ACCESS_POINT, ptr %63, i32 0, i32 3
  store i16 %62, ptr %64, align 2, !tbaa !9
  br label %65

65:                                               ; preds = %61, %20
  store i64 0, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %66

66:                                               ; preds = %65, %58, %19
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #3
  %67 = load i64, ptr %2, align 8
  ret i64 %67
}

declare zeroext i8 @TcpFindTcbByPeer(ptr noundef, i16 noundef zeroext) #2

declare zeroext i8 @DebugPrintEnabled() #2

declare zeroext i8 @DebugPrintLevelEnabled(i64 noundef) #2

declare void @DebugPrint(i64 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 10}
!10 = !{!"", !7, i64 0, !11, i64 1, !11, i64 5, !12, i64 10, !11, i64 12, !12, i64 16, !7, i64 18}
!11 = !{!"", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!12, !12, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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"

@mTcp4RandomPort = external local_unnamed_addr global i16, align 2
@.str.3 = external hidden unnamed_addr constant [49 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i64 -9223372036854775806, 1) i64 @Tcp4Bind(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 10
  %3 = load i16, ptr %2, align 2, !tbaa !5
  %.not = icmp eq i16 %3, 0
  br i1 %.not, label %7, label %4

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 1
  %6 = tail call zeroext i8 @TcpFindTcbByPeer(ptr noundef nonnull %5, i16 noundef zeroext %3) #2
  %.not11 = icmp eq i8 %6, 0
  br i1 %.not11, label %28, label %29

7:                                                ; preds = %1
  %8 = load i16, ptr @mTcp4RandomPort, align 2, !tbaa !11
  %9 = icmp eq i16 %8, -1
  %10 = add nuw i16 %8, 1
  %11 = select i1 %9, i16 1025, i16 %10
  store i16 %11, ptr @mTcp4RandomPort, align 2, !tbaa !11
  %12 = getelementptr inbounds i8, ptr %0, i64 1
  %13 = tail call zeroext i8 @TcpFindTcbByPeer(ptr noundef nonnull %12, i16 noundef zeroext %11) #2
  %.not712 = icmp eq i8 %13, 0
  br i1 %.not712, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %7, %24
  %.013 = phi i8 [ %.1, %24 ], [ 0, %7 ]
  %14 = load i16, ptr @mTcp4RandomPort, align 2, !tbaa !11
  %15 = add i16 %14, 1
  store i16 %15, ptr @mTcp4RandomPort, align 2, !tbaa !11
  %16 = icmp ult i16 %15, 1025
  br i1 %16, label %17, label %24

17:                                               ; preds = %.lr.ph
  %.not8 = icmp eq i8 %.013, 0
  br i1 %.not8, label %23, label %18

18:                                               ; preds = %17
  %19 = tail call zeroext i8 @DebugPrintEnabled() #2
  %.not9 = icmp eq i8 %19, 0
  br i1 %.not9, label %29, label %20

20:                                               ; preds = %18
  %21 = tail call zeroext i8 @DebugPrintLevelEnabled(i64 noundef 2147483648) #2
  %.not10 = icmp eq i8 %21, 0
  br i1 %.not10, label %29, label %22

22:                                               ; preds = %20
  tail call void (i64, ptr, ...) @DebugPrint(i64 noundef 2147483648, ptr noundef nonnull @.str.3) #2
  br label %29

23:                                               ; preds = %17
  store i16 1025, ptr @mTcp4RandomPort, align 2, !tbaa !11
  br label %24

24:                                               ; preds = %23, %.lr.ph
  %25 = phi i16 [ 1025, %23 ], [ %15, %.lr.ph ]
  %.1 = phi i8 [ 1, %23 ], [ %.013, %.lr.ph ]
  %26 = tail call zeroext i8 @TcpFindTcbByPeer(ptr noundef nonnull %12, i16 noundef zeroext %25) #2
  %.not7 = icmp eq i8 %26, 0
  br i1 %.not7, label %._crit_edge, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %24, %7
  %27 = load i16, ptr @mTcp4RandomPort, align 2, !tbaa !11
  store i16 %27, ptr %2, align 2, !tbaa !5
  br label %28

28:                                               ; preds = %4, %._crit_edge
  br label %29

29:                                               ; preds = %18, %22, %20, %4, %28
  %.06 = phi i64 [ 0, %28 ], [ -9223372036854775806, %4 ], [ -9223372036854775799, %20 ], [ -9223372036854775799, %22 ], [ -9223372036854775799, %18 ]
  ret i64 %.06
}

declare zeroext i8 @TcpFindTcbByPeer(ptr noundef, i16 noundef zeroext) local_unnamed_addr #1

declare zeroext i8 @DebugPrintEnabled() local_unnamed_addr #1

declare zeroext i8 @DebugPrintLevelEnabled(i64 noundef) local_unnamed_addr #1

declare void @DebugPrint(i64 noundef, ptr noundef, ...) local_unnamed_addr #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 = { "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, !10, i64 10}
!6 = !{!"", !7, i64 0, !9, i64 1, !9, i64 5, !10, i64 10, !9, i64 12, !10, i64 16, !7, i64 18}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"", !7, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!10, !10, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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