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

; 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._TCP4_IO = type { ptr, ptr, ptr, ptr, %struct.EFI_TCP4_CONNECTION_TOKEN, %struct.EFI_TCP4_IO_TOKEN, %struct.EFI_TCP4_IO_TOKEN, %struct.EFI_TCP4_CLOSE_TOKEN, i8, i8, i8, i8 }
%struct.EFI_TCP4_CONNECTION_TOKEN = type { %struct.EFI_TCP4_COMPLETION_TOKEN }
%struct.EFI_TCP4_COMPLETION_TOKEN = type { ptr, i64 }
%struct.EFI_TCP4_IO_TOKEN = type { %struct.EFI_TCP4_COMPLETION_TOKEN, %union.anon }
%union.anon = type { ptr }
%struct.EFI_TCP4_CLOSE_TOKEN = type { %struct.EFI_TCP4_COMPLETION_TOKEN, i8 }
%struct._EFI_TCP4_PROTOCOL = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.EFI_BOOT_SERVICES = type { %struct.EFI_TABLE_HEADER, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.EFI_TABLE_HEADER = type { i64, i32, i32, i32, i32 }

@gBS = external global ptr, 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
define dso_local i64 @Tcp4IoConnect(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct._TCP4_IO, ptr %9, i32 0, i32 8
  store i8 0, ptr %10, align 8, !tbaa !9
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct._TCP4_IO, ptr %11, i32 0, i32 3
  %13 = load ptr, ptr %12, align 8, !tbaa !15
  store ptr %13, ptr %6, align 8, !tbaa !5
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct._EFI_TCP4_PROTOCOL, ptr %14, i32 0, i32 3
  %16 = load ptr, ptr %15, align 8, !tbaa !16
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct._TCP4_IO, ptr %18, i32 0, i32 4
  %20 = call i64 %16(ptr noundef %17, ptr noundef %19)
  store i64 %20, ptr %7, align 8, !tbaa !18
  %21 = load i64, ptr %7, align 8, !tbaa !18
  %22 = icmp slt i64 %21, 0
  br i1 %22, label %23, label %25

23:                                               ; preds = %2
  %24 = load i64, ptr %7, align 8, !tbaa !18
  store i64 %24, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %60

25:                                               ; preds = %2
  br label %26

26:                                               ; preds = %40, %25
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct._TCP4_IO, ptr %27, i32 0, i32 8
  %29 = load i8, ptr %28, align 8, !tbaa !9
  %30 = icmp ne i8 %29, 0
  br i1 %30, label %38, label %31

31:                                               ; preds = %26
  %32 = load ptr, ptr @gBS, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %32, i32 0, i32 13
  %34 = load ptr, ptr %33, align 8, !tbaa !19
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = call i64 %34(ptr noundef %35)
  %37 = icmp slt i64 %36, 0
  br label %38

38:                                               ; preds = %31, %26
  %39 = phi i1 [ false, %26 ], [ %37, %31 ]
  br i1 %39, label %40, label %46

40:                                               ; preds = %38
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct._EFI_TCP4_PROTOCOL, ptr %41, i32 0, i32 9
  %43 = load ptr, ptr %42, align 8, !tbaa !23
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = call i64 %43(ptr noundef %44)
  br label %26, !llvm.loop !24

46:                                               ; preds = %38
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct._TCP4_IO, ptr %47, i32 0, i32 8
  %49 = load i8, ptr %48, align 8, !tbaa !9
  %50 = icmp ne i8 %49, 0
  br i1 %50, label %52, label %51

51:                                               ; preds = %46
  store i64 -9223372036854775790, ptr %7, align 8, !tbaa !18
  br label %58

52:                                               ; preds = %46
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct._TCP4_IO, ptr %53, i32 0, i32 4
  %55 = getelementptr inbounds %struct.EFI_TCP4_CONNECTION_TOKEN, ptr %54, i32 0, i32 0
  %56 = getelementptr inbounds %struct.EFI_TCP4_COMPLETION_TOKEN, ptr %55, i32 0, i32 1
  %57 = load i64, ptr %56, align 8, !tbaa !26
  store i64 %57, ptr %7, align 8, !tbaa !18
  br label %58

58:                                               ; preds = %52, %51
  %59 = load i64, ptr %7, align 8, !tbaa !18
  store i64 %59, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %60

60:                                               ; preds = %58, %23
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %61 = load i64, ptr %3, align 8
  ret i64 %61
}

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 = !{!10, !7, i64 120}
!10 = !{!"_TCP4_IO", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !14, i64 48, !14, i64 72, !14, i64 96, !7, i64 120, !7, i64 121, !7, i64 122, !7, i64 123}
!11 = !{!"", !12, i64 0}
!12 = !{!"", !6, i64 0, !13, i64 8}
!13 = !{!"long long", !7, i64 0}
!14 = !{!"", !12, i64 0, !7, i64 16}
!15 = !{!10, !6, i64 24}
!16 = !{!17, !6, i64 24}
!17 = !{!"_EFI_TCP4_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72}
!18 = !{!13, !13, i64 0}
!19 = !{!20, !6, i64 120}
!20 = !{!"", !21, i64 0, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !6, i64 160, !6, i64 168, !6, i64 176, !6, i64 184, !6, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !6, i64 224, !6, i64 232, !6, i64 240, !6, i64 248, !6, i64 256, !6, i64 264, !6, i64 272, !6, i64 280, !6, i64 288, !6, i64 296, !6, i64 304, !6, i64 312, !6, i64 320, !6, i64 328, !6, i64 336, !6, i64 344, !6, i64 352, !6, i64 360, !6, i64 368}
!21 = !{!"", !13, i64 0, !22, i64 8, !22, i64 12, !22, i64 16, !22, i64 20}
!22 = !{!"int", !7, i64 0}
!23 = !{!17, !6, i64 72}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = !{!10, !13, i64 40}


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

@gBS = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local i64 @Tcp4IoConnect(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 120
  store i8 0, ptr %3, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load ptr, ptr %4, align 8, !tbaa !14
  %6 = getelementptr inbounds i8, ptr %5, i64 24
  %7 = load ptr, ptr %6, align 8, !tbaa !15
  %8 = getelementptr inbounds i8, ptr %0, i64 32
  %9 = tail call i64 %7(ptr noundef %5, ptr noundef nonnull %8) #1
  %10 = icmp slt i64 %9, 0
  br i1 %10, label %25, label %.preheader

.preheader:                                       ; preds = %2
  %11 = load i8, ptr %3, align 8, !tbaa !5
  %.not18 = icmp eq i8 %11, 0
  br i1 %.not18, label %.lr.ph, label %.critedge.thread

.lr.ph:                                           ; preds = %.preheader
  %12 = getelementptr inbounds i8, ptr %5, i64 72
  br label %13

13:                                               ; preds = %.lr.ph, %19
  %14 = load ptr, ptr @gBS, align 8, !tbaa !17
  %15 = getelementptr inbounds i8, ptr %14, i64 120
  %16 = load ptr, ptr %15, align 8, !tbaa !18
  %17 = tail call i64 %16(ptr noundef %1) #1
  %18 = icmp slt i64 %17, 0
  br i1 %18, label %19, label %.critedge

19:                                               ; preds = %13
  %20 = load ptr, ptr %12, align 8, !tbaa !22
  %21 = tail call i64 %20(ptr noundef nonnull %5) #1
  %22 = load i8, ptr %3, align 8, !tbaa !5
  %.not = icmp eq i8 %22, 0
  br i1 %.not, label %13, label %.critedge.thread, !llvm.loop !23

.critedge:                                        ; preds = %13
  %.pr = load i8, ptr %3, align 8, !tbaa !5
  %.not16 = icmp eq i8 %.pr, 0
  br i1 %.not16, label %25, label %.critedge.thread

.critedge.thread:                                 ; preds = %19, %.preheader, %.critedge
  %23 = getelementptr inbounds i8, ptr %0, i64 40
  %24 = load i64, ptr %23, align 8, !tbaa !25
  br label %25

25:                                               ; preds = %.critedge.thread, %.critedge, %2
  %.014 = phi i64 [ %9, %2 ], [ %24, %.critedge.thread ], [ -9223372036854775790, %.critedge ]
  ret i64 %.014
}

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 = { 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, !8, i64 120}
!6 = !{!"_TCP4_IO", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !13, i64 48, !13, i64 72, !13, i64 96, !8, i64 120, !8, i64 121, !8, i64 122, !8, i64 123}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"", !11, i64 0}
!11 = !{!"", !7, i64 0, !12, i64 8}
!12 = !{!"long long", !8, i64 0}
!13 = !{!"", !11, i64 0, !8, i64 16}
!14 = !{!6, !7, i64 24}
!15 = !{!16, !7, i64 24}
!16 = !{!"_EFI_TCP4_PROTOCOL", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72}
!17 = !{!7, !7, i64 0}
!18 = !{!19, !7, i64 120}
!19 = !{!"", !20, i64 0, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !7, i64 88, !7, i64 96, !7, i64 104, !7, i64 112, !7, i64 120, !7, i64 128, !7, i64 136, !7, i64 144, !7, i64 152, !7, i64 160, !7, i64 168, !7, i64 176, !7, i64 184, !7, i64 192, !7, i64 200, !7, i64 208, !7, i64 216, !7, i64 224, !7, i64 232, !7, i64 240, !7, i64 248, !7, i64 256, !7, i64 264, !7, i64 272, !7, i64 280, !7, i64 288, !7, i64 296, !7, i64 304, !7, i64 312, !7, i64 320, !7, i64 328, !7, i64 336, !7, i64 344, !7, i64 352, !7, i64 360, !7, i64 368}
!20 = !{!"", !12, i64 0, !21, i64 8, !21, i64 12, !21, i64 16, !21, i64 20}
!21 = !{!"int", !8, i64 0}
!22 = !{!16, !7, i64 72}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}
!25 = !{!6, !12, i64 40}


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