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

; 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_MTFTP6_DATA_HEADER = type <{ i16, i16, [1 x i8] }>
%struct._MTFTP6_INSTANCE = type { i32, ptr, %struct._LIST_ENTRY, %struct._EFI_MTFTP6_PROTOCOL, ptr, ptr, ptr, %struct.MTFTP6_EXT_OPTION_INFO, i16, i16, %struct._LIST_ENTRY, %struct.EFI_IPv6_ADDRESS, i16, i16, ptr, %struct.EFI_IPv6_ADDRESS, i16, ptr, ptr, i32, i32, i32, i32, i64, i8, i8, i8 }
%struct._EFI_MTFTP6_PROTOCOL = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.MTFTP6_EXT_OPTION_INFO = type { i16, i8, i32, %struct.EFI_IPv6_ADDRESS, i16, i8, i32 }
%struct._LIST_ENTRY = type { ptr, ptr }
%struct.EFI_IPv6_ADDRESS = type { [16 x i8] }

@.str = external hidden unnamed_addr constant [134 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [14 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i64 @Mtftp6RrqSendAck(ptr noundef, i16 noundef zeroext) #0

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

declare zeroext i8 @DebugAssertEnabled() #2

declare void @DebugAssert(ptr noundef, i64 noundef, ptr noundef) #2

declare zeroext i16 @SwapBytes16(i16 noundef zeroext) #2

declare i64 @Mtftp6TransmitPacket(ptr noundef, ptr noundef) #2

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

; Function Attrs: nounwind uwtable
declare dso_local i64 @Mtftp6RrqSaveBlock(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #0

declare void @NetbufFree(ptr noundef) #2

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

24:                                               ; preds = %5
  %25 = call zeroext i8 @DebugAssertEnabled()
  %26 = icmp ne i8 %25, 0
  br i1 %26, label %27, label %32

27:                                               ; preds = %24
  %28 = load i64, ptr %14, align 8, !tbaa !14
  %29 = icmp sge i64 %28, 0
  br i1 %29, label %31, label %30

30:                                               ; preds = %27
  call void @DebugAssert(ptr noundef @.str, i64 noundef 230, ptr noundef @.str.4)
  br label %31

31:                                               ; preds = %30, %27
  br label %32

32:                                               ; preds = %31, %24
  br label %33

33:                                               ; preds = %32
  br label %34

34:                                               ; preds = %33
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct._MTFTP6_INSTANCE, ptr %35, i32 0, i32 25
  %37 = load i8, ptr %36, align 1, !tbaa !16
  %38 = zext i8 %37 to i32
  %39 = icmp ne i32 %38, 0
  br i1 %39, label %40, label %54

40:                                               ; preds = %34
  %41 = load i64, ptr %14, align 8, !tbaa !14
  %42 = load i16, ptr %13, align 2, !tbaa !12
  %43 = zext i16 %42 to i64
  %44 = icmp ne i64 %41, %43
  br i1 %44, label %45, label %54

45:                                               ; preds = %40
  %46 = load ptr, ptr %10, align 8, !tbaa !5
  %47 = load ptr, ptr %46, align 8, !tbaa !5
  call void @NetbufFree(ptr noundef %47)
  %48 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr null, ptr %48, align 8, !tbaa !5
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct._MTFTP6_INSTANCE, ptr %50, i32 0, i32 18
  %52 = load ptr, ptr %51, align 8, !tbaa !22
  %53 = call i64 @Mtftp6TransmitPacket(ptr noundef %49, ptr noundef %52)
  store i64 0, ptr %6, align 8
  store i32 1, ptr %15, align 4
  br label %108

54:                                               ; preds = %40, %34
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = load ptr, ptr %8, align 8, !tbaa !5
  %57 = load i32, ptr %9, align 4, !tbaa !9
  %58 = load ptr, ptr %10, align 8, !tbaa !5
  %59 = call i64 @Mtftp6RrqSaveBlock(ptr noundef %55, ptr noundef %56, i32 noundef %57, ptr noundef %58)
  store i64 %59, ptr %12, align 8, !tbaa !14
  %60 = load i64, ptr %12, align 8, !tbaa !14
  %61 = icmp slt i64 %60, 0
  br i1 %61, label %62, label %64

62:                                               ; preds = %54
  %63 = load i64, ptr %12, align 8, !tbaa !14
  store i64 %63, ptr %6, align 8
  store i32 1, ptr %15, align 4
  br label %108

64:                                               ; preds = %54
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct._MTFTP6_INSTANCE, ptr %65, i32 0, i32 25
  %67 = load i8, ptr %66, align 1, !tbaa !16
  %68 = icmp ne i8 %67, 0
  br i1 %68, label %76, label %69

69:                                               ; preds = %64
  %70 = load ptr, ptr %7, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct._MTFTP6_INSTANCE, ptr %70, i32 0, i32 22
  %72 = load i32, ptr %71, align 4, !tbaa !23
  %73 = mul i32 %72, 2
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct._MTFTP6_INSTANCE, ptr %74, i32 0, i32 21
  store i32 %73, ptr %75, align 8, !tbaa !24
  br label %76

76:                                               ; preds = %69, %64
  %77 = load ptr, ptr %7, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct._MTFTP6_INSTANCE, ptr %77, i32 0, i32 10
  %79 = call i64 @Mtftp6GetNextBlockNum(ptr noundef %78)
  store i64 %79, ptr %14, align 8, !tbaa !14
  %80 = load ptr, ptr %7, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct._MTFTP6_INSTANCE, ptr %80, i32 0, i32 25
  %82 = load i8, ptr %81, align 1, !tbaa !16
  %83 = zext i8 %82 to i32
  %84 = icmp ne i32 %83, 0
  br i1 %84, label %88, label %85

85:                                               ; preds = %76
  %86 = load i64, ptr %14, align 8, !tbaa !14
  %87 = icmp slt i64 %86, 0
  br i1 %87, label %88, label %107

88:                                               ; preds = %85, %76
  %89 = load i64, ptr %14, align 8, !tbaa !14
  %90 = icmp slt i64 %89, 0
  br i1 %90, label %91, label %96

91:                                               ; preds = %88
  %92 = load ptr, ptr %7, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct._MTFTP6_INSTANCE, ptr %92, i32 0, i32 9
  %94 = load i16, ptr %93, align 2, !tbaa !25
  store i16 %94, ptr %13, align 2, !tbaa !12
  %95 = load ptr, ptr %11, align 8, !tbaa !5
  store i8 1, ptr %95, align 1, !tbaa !11
  br label %100

96:                                               ; preds = %88
  %97 = load i64, ptr %14, align 8, !tbaa !14
  %98 = sub nsw i64 %97, 1
  %99 = trunc i64 %98 to i16
  store i16 %99, ptr %13, align 2, !tbaa !12
  br label %100

100:                                              ; preds = %96, %91
  %101 = load ptr, ptr %10, align 8, !tbaa !5
  %102 = load ptr, ptr %101, align 8, !tbaa !5
  call void @NetbufFree(ptr noundef %102)
  %103 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr null, ptr %103, align 8, !tbaa !5
  %104 = load ptr, ptr %7, align 8, !tbaa !5
  %105 = load i16, ptr %13, align 2, !tbaa !12
  %106 = call i64 @Mtftp6RrqSendAck(ptr noundef %104, i16 noundef zeroext %105)
  br label %107

107:                                              ; preds = %100, %85
  store i64 0, ptr %6, align 8
  store i32 1, ptr %15, align 4
  br label %108

108:                                              ; preds = %107, %62, %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  %109 = load i64, ptr %6, align 8
  ret i64 %109
}

declare i64 @Mtftp6GetNextBlockNum(ptr noundef) #2

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"long long", !7, i64 0}
!16 = !{!17, !7, i64 273}
!17 = !{!"_MTFTP6_INSTANCE", !10, i64 0, !6, i64 8, !18, i64 16, !19, i64 32, !6, i64 96, !6, i64 104, !6, i64 112, !20, i64 120, !13, i64 152, !13, i64 154, !18, i64 160, !21, i64 176, !13, i64 192, !13, i64 194, !6, i64 200, !21, i64 208, !13, i64 224, !6, i64 232, !6, i64 240, !10, i64 248, !10, i64 252, !10, i64 256, !10, i64 260, !15, i64 264, !7, i64 272, !7, i64 273, !7, i64 274}
!18 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!19 = !{!"_EFI_MTFTP6_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}
!20 = !{!"", !13, i64 0, !7, i64 2, !10, i64 4, !21, i64 8, !13, i64 24, !7, i64 26, !10, i64 28}
!21 = !{!"", !7, i64 0}
!22 = !{!17, !6, i64 240}
!23 = !{!17, !10, i64 260}
!24 = !{!17, !10, i64 256}
!25 = !{!17, !13, i64 154}


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

@.str = external hidden unnamed_addr constant [134 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [14 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i64 @Mtftp6RrqSendAck(ptr noundef, i16 noundef zeroext) local_unnamed_addr #0

declare zeroext i8 @DebugAssertEnabled() local_unnamed_addr #1

declare void @DebugAssert(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #1

declare zeroext i16 @SwapBytes16(i16 noundef zeroext) local_unnamed_addr #1

declare i64 @Mtftp6TransmitPacket(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i64 @Mtftp6RrqSaveBlock(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

declare void @NetbufFree(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i64 -9223372036854775808, 1) i64 @Mtftp6RrqHandleData(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3, ptr nocapture noundef writeonly %4) local_unnamed_addr #0 {
  store i8 0, ptr %4, align 1, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %1, i64 2
  %7 = load i16, ptr %6, align 1, !tbaa !5
  %8 = tail call zeroext i16 @SwapBytes16(i16 noundef zeroext %7) #2
  %9 = getelementptr inbounds i8, ptr %0, i64 160
  %10 = tail call i64 @Mtftp6GetNextBlockNum(ptr noundef nonnull %9) #2
  %11 = tail call zeroext i8 @DebugAssertEnabled() #2
  %12 = icmp eq i8 %11, 0
  %13 = icmp sgt i64 %10, -1
  %or.cond3 = select i1 %12, i1 true, i1 %13
  br i1 %or.cond3, label %15, label %14

14:                                               ; preds = %5
  tail call void @DebugAssert(ptr noundef nonnull @.str, i64 noundef 230, ptr noundef nonnull @.str.4) #2
  br label %15

15:                                               ; preds = %14, %5
  %16 = getelementptr inbounds i8, ptr %0, i64 273
  %17 = load i8, ptr %16, align 1, !tbaa !8
  %.not = icmp eq i8 %17, 0
  %18 = zext i16 %8 to i64
  %.not37 = icmp eq i64 %10, %18
  %or.cond39 = select i1 %.not, i1 true, i1 %.not37
  br i1 %or.cond39, label %24, label %19

19:                                               ; preds = %15
  %20 = load ptr, ptr %3, align 8, !tbaa !18
  tail call void @NetbufFree(ptr noundef %20) #2
  store ptr null, ptr %3, align 8, !tbaa !18
  %21 = getelementptr inbounds i8, ptr %0, i64 240
  %22 = load ptr, ptr %21, align 8, !tbaa !19
  %23 = tail call i64 @Mtftp6TransmitPacket(ptr noundef nonnull %0, ptr noundef %22) #2
  br label %49

24:                                               ; preds = %15
  %25 = tail call i64 @Mtftp6RrqSaveBlock(ptr noundef nonnull %0, ptr noundef nonnull %1, i32 noundef %2, ptr noundef %3)
  %26 = icmp slt i64 %25, 0
  br i1 %26, label %49, label %27

27:                                               ; preds = %24
  %28 = load i8, ptr %16, align 1, !tbaa !8
  %.not38 = icmp eq i8 %28, 0
  br i1 %.not38, label %29, label %34

29:                                               ; preds = %27
  %30 = getelementptr inbounds i8, ptr %0, i64 260
  %31 = load i32, ptr %30, align 4, !tbaa !20
  %32 = shl i32 %31, 1
  %33 = getelementptr inbounds i8, ptr %0, i64 256
  store i32 %32, ptr %33, align 8, !tbaa !21
  br label %34

34:                                               ; preds = %29, %27
  %35 = tail call i64 @Mtftp6GetNextBlockNum(ptr noundef nonnull %9) #2
  %36 = load i8, ptr %16, align 1, !tbaa !8
  %37 = icmp ne i8 %36, 0
  %38 = icmp slt i64 %35, 0
  %or.cond = select i1 %37, i1 true, i1 %38
  br i1 %or.cond, label %39, label %49

39:                                               ; preds = %34
  br i1 %38, label %40, label %43

40:                                               ; preds = %39
  %41 = getelementptr inbounds i8, ptr %0, i64 154
  %42 = load i16, ptr %41, align 2, !tbaa !22
  store i8 1, ptr %4, align 1, !tbaa !5
  br label %46

43:                                               ; preds = %39
  %44 = trunc i64 %35 to i16
  %45 = add i16 %44, -1
  br label %46

46:                                               ; preds = %43, %40
  %.0 = phi i16 [ %42, %40 ], [ %45, %43 ]
  %47 = load ptr, ptr %3, align 8, !tbaa !18
  tail call void @NetbufFree(ptr noundef %47) #2
  store ptr null, ptr %3, align 8, !tbaa !18
  %48 = tail call i64 @Mtftp6RrqSendAck(ptr noundef nonnull %0, i16 noundef zeroext %.0)
  br label %49

49:                                               ; preds = %46, %34, %24, %19
  %.034 = phi i64 [ 0, %19 ], [ %25, %24 ], [ 0, %34 ], [ 0, %46 ]
  ret i64 %.034
}

declare i64 @Mtftp6GetNextBlockNum(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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !6, i64 273}
!9 = !{!"_MTFTP6_INSTANCE", !10, i64 0, !11, i64 8, !12, i64 16, !13, i64 32, !11, i64 96, !11, i64 104, !11, i64 112, !14, i64 120, !15, i64 152, !15, i64 154, !12, i64 160, !16, i64 176, !15, i64 192, !15, i64 194, !11, i64 200, !16, i64 208, !15, i64 224, !11, i64 232, !11, i64 240, !10, i64 248, !10, i64 252, !10, i64 256, !10, i64 260, !17, i64 264, !6, i64 272, !6, i64 273, !6, i64 274}
!10 = !{!"int", !6, i64 0}
!11 = !{!"any pointer", !6, i64 0}
!12 = !{!"_LIST_ENTRY", !11, i64 0, !11, i64 8}
!13 = !{!"_EFI_MTFTP6_PROTOCOL", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !11, i64 48, !11, i64 56}
!14 = !{!"", !15, i64 0, !6, i64 2, !10, i64 4, !16, i64 8, !15, i64 24, !6, i64 26, !10, i64 28}
!15 = !{!"short", !6, i64 0}
!16 = !{!"", !6, i64 0}
!17 = !{!"long long", !6, i64 0}
!18 = !{!11, !11, i64 0}
!19 = !{!9, !11, i64 240}
!20 = !{!9, !10, i64 260}
!21 = !{!9, !10, i64 256}
!22 = !{!9, !15, i64 154}


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