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

; 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.NET_BUF = type { i32, i64, %struct._LIST_ENTRY, %union.NET_IP_HEAD, ptr, ptr, [64 x i8], ptr, i32, i32, [1 x %struct.NET_BLOCK_OP] }
%union.NET_IP_HEAD = type { ptr }
%struct.NET_BLOCK_OP = type { ptr, ptr, ptr, ptr, i32 }
%struct._ISCSI_TCB = type { %struct._LIST_ENTRY, i8, i32, i8, i8, i32, i32, ptr, i32, i32, i32, %struct._ISCSI_XFER_CONTEXT, ptr }
%struct._ISCSI_XFER_CONTEXT = type { i32, i32, i32, i32 }
%struct._ISCSI_CONNECTION = type { i32, %struct._LIST_ENTRY, ptr, ptr, i8, i8, i8, i8, i8, i8, i8, i16, i32, %struct.NET_BUF_QUEUE, %struct._TCP4_IO, i32, i32, i32 }
%struct.NET_BUF_QUEUE = type { i32, i64, %struct._LIST_ENTRY, %struct._LIST_ENTRY, i32, i32 }
%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 }

; 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

declare i64 @Tcp4IoTransmit(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @IScsiFreeNbufList(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local ptr @IScsiGenerateDataOutPduSequence(ptr noundef, ptr noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i64 @IScsiSendDataOutPduSequence(ptr noundef %0, i64 noundef %1, ptr noundef %2) #2 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  %15 = load i64, ptr %6, align 8, !tbaa !9
  %16 = call ptr @IScsiGenerateDataOutPduSequence(ptr noundef %13, ptr noundef %14, i64 noundef %15)
  store ptr %16, ptr %8, align 8, !tbaa !5
  %17 = load ptr, ptr %8, align 8, !tbaa !5
  %18 = icmp eq ptr %17, null
  br i1 %18, label %19, label %20

19:                                               ; preds = %3
  store i64 -9223372036854775799, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %50

20:                                               ; preds = %3
  store i64 0, ptr %11, align 8, !tbaa !9
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct._LIST_ENTRY, ptr %21, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8, !tbaa !11
  store ptr %23, ptr %9, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %43, %20
  %25 = load ptr, ptr %9, align 8, !tbaa !5
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  %27 = icmp ne ptr %25, %26
  br i1 %27, label %28, label %47

28:                                               ; preds = %24
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = ptrtoint ptr %29 to i64
  %31 = sub i64 %30, ptrtoint (ptr getelementptr inbounds (%struct.NET_BUF, ptr null, i32 0, i32 2) to i64)
  %32 = inttoptr i64 %31 to ptr
  store ptr %32, ptr %10, align 8, !tbaa !5
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct._ISCSI_TCB, ptr %33, i32 0, i32 12
  %35 = load ptr, ptr %34, align 8, !tbaa !13
  %36 = getelementptr inbounds %struct._ISCSI_CONNECTION, ptr %35, i32 0, i32 14
  %37 = load ptr, ptr %10, align 8, !tbaa !5
  %38 = call i64 @Tcp4IoTransmit(ptr noundef %36, ptr noundef %37)
  store i64 %38, ptr %11, align 8, !tbaa !9
  %39 = load i64, ptr %11, align 8, !tbaa !9
  %40 = icmp slt i64 %39, 0
  br i1 %40, label %41, label %42

41:                                               ; preds = %28
  br label %47

42:                                               ; preds = %28
  br label %43

43:                                               ; preds = %42
  %44 = load ptr, ptr %9, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct._LIST_ENTRY, ptr %44, i32 0, i32 0
  %46 = load ptr, ptr %45, align 8, !tbaa !11
  store ptr %46, ptr %9, align 8, !tbaa !5
  br label %24, !llvm.loop !17

47:                                               ; preds = %41, %24
  %48 = load ptr, ptr %8, align 8, !tbaa !5
  call void @IScsiFreeNbufList(ptr noundef %48)
  %49 = load i64, ptr %11, align 8, !tbaa !9
  store i64 %49, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %50

50:                                               ; preds = %47, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %51 = load i64, ptr %4, align 8
  ret i64 %51
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!13 = !{!14, !6, i64 80}
!14 = !{!"_ISCSI_TCB", !12, i64 0, !7, i64 16, !15, i64 20, !7, i64 24, !7, i64 25, !15, i64 28, !15, i64 32, !6, i64 40, !15, i64 48, !15, i64 52, !15, i64 56, !16, i64 60, !6, i64 80}
!15 = !{!"int", !7, i64 0}
!16 = !{!"_ISCSI_XFER_CONTEXT", !15, i64 0, !15, i64 4, !15, i64 8, !15, i64 12}
!17 = distinct !{!17, !18}
!18 = !{!"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"

declare i64 @Tcp4IoTransmit(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @IScsiFreeNbufList(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @IScsiGenerateDataOutPduSequence(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i64 @IScsiSendDataOutPduSequence(ptr noundef %0, i64 noundef %1, ptr noundef %2) local_unnamed_addr #1 {
  %4 = tail call ptr @IScsiGenerateDataOutPduSequence(ptr noundef %0, ptr noundef %2, i64 noundef %1)
  %5 = icmp eq ptr %4, null
  br i1 %5, label %16, label %.preheader

.preheader:                                       ; preds = %3
  %.01519 = load ptr, ptr %4, align 8, !tbaa !5
  %.not20 = icmp eq ptr %.01519, %4
  br i1 %.not20, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %6 = getelementptr inbounds i8, ptr %2, i64 80
  br label %8

7:                                                ; preds = %8
  %.015 = load ptr, ptr %.01521, align 8, !tbaa !5
  %.not = icmp eq ptr %.015, %4
  br i1 %.not, label %._crit_edge, label %8, !llvm.loop !10

8:                                                ; preds = %.lr.ph, %7
  %.01521 = phi ptr [ %.01519, %.lr.ph ], [ %.015, %7 ]
  %9 = ptrtoint ptr %.01521 to i64
  %10 = add i64 %9, -16
  %11 = inttoptr i64 %10 to ptr
  %12 = load ptr, ptr %6, align 8, !tbaa !12
  %13 = getelementptr inbounds i8, ptr %12, i64 112
  %14 = tail call i64 @Tcp4IoTransmit(ptr noundef nonnull %13, ptr noundef %11) #2
  %15 = icmp slt i64 %14, 0
  br i1 %15, label %._crit_edge, label %7, !llvm.loop !10

._crit_edge:                                      ; preds = %7, %8, %.preheader
  %.1 = phi i64 [ 0, %.preheader ], [ %14, %8 ], [ %14, %7 ]
  tail call void @IScsiFreeNbufList(ptr noundef nonnull %4)
  br label %16

16:                                               ; preds = %3, %._crit_edge
  %.014 = phi i64 [ %.1, %._crit_edge ], [ -9223372036854775799, %3 ]
  ret i64 %.014
}

attributes #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 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, !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 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!13, !7, i64 80}
!13 = !{!"_ISCSI_TCB", !6, i64 0, !8, i64 16, !14, i64 20, !8, i64 24, !8, i64 25, !14, i64 28, !14, i64 32, !7, i64 40, !14, i64 48, !14, i64 52, !14, i64 56, !15, i64 60, !7, i64 80}
!14 = !{!"int", !8, i64 0}
!15 = !{!"_ISCSI_XFER_CONTEXT", !14, i64 0, !14, i64 4, !14, i64 8, !14, i64 12}


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