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

; 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_TOKEN = type { i64, ptr, ptr, ptr, ptr, i32, ptr, i64, ptr, ptr, ptr, ptr, ptr }
%struct._MTFTP6_GETINFO_CONTEXT = type { ptr, ptr, i64 }

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

declare ptr @AllocateZeroPool(i64 noundef) #1

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i64 @Mtftp6CheckPacket(ptr noundef %0, ptr noundef %1, i16 noundef zeroext %2, ptr noundef %3) #2 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i16, align 2
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i16, align 2
  %12 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i16 %2, ptr %8, align 2, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %11) #3
  %13 = load ptr, ptr %7, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct._EFI_MTFTP6_TOKEN, ptr %13, i32 0, i32 9
  %15 = load ptr, ptr %14, align 8, !tbaa !11
  store ptr %15, ptr %10, align 8, !tbaa !5
  %16 = load ptr, ptr %9, align 8, !tbaa !5
  %17 = load i16, ptr %16, align 1, !tbaa !15
  %18 = call zeroext i16 @SwapBytes16(i16 noundef zeroext %17)
  store i16 %18, ptr %11, align 2, !tbaa !9
  %19 = load i16, ptr %11, align 2, !tbaa !9
  %20 = zext i16 %19 to i32
  switch i32 %20, label %27 [
    i32 5, label %21
    i32 6, label %24
  ]

21:                                               ; preds = %4
  %22 = load ptr, ptr %10, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct._MTFTP6_GETINFO_CONTEXT, ptr %22, i32 0, i32 2
  store i64 -9223372036854775785, ptr %23, align 8, !tbaa !16
  br label %30

24:                                               ; preds = %4
  %25 = load ptr, ptr %10, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct._MTFTP6_GETINFO_CONTEXT, ptr %25, i32 0, i32 2
  store i64 0, ptr %26, align 8, !tbaa !16
  br label %30

27:                                               ; preds = %4
  %28 = load ptr, ptr %10, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct._MTFTP6_GETINFO_CONTEXT, ptr %28, i32 0, i32 2
  store i64 -9223372036854775784, ptr %29, align 8, !tbaa !16
  br label %30

30:                                               ; preds = %27, %24, %21
  %31 = load i16, ptr %8, align 2, !tbaa !9
  %32 = zext i16 %31 to i64
  %33 = call ptr @AllocateZeroPool(i64 noundef %32)
  %34 = load ptr, ptr %10, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct._MTFTP6_GETINFO_CONTEXT, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !18
  store ptr %33, ptr %36, align 8, !tbaa !5
  %37 = load ptr, ptr %10, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct._MTFTP6_GETINFO_CONTEXT, ptr %37, i32 0, i32 0
  %39 = load ptr, ptr %38, align 8, !tbaa !18
  %40 = load ptr, ptr %39, align 8, !tbaa !5
  %41 = icmp eq ptr %40, null
  br i1 %41, label %42, label %45

42:                                               ; preds = %30
  %43 = load ptr, ptr %10, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct._MTFTP6_GETINFO_CONTEXT, ptr %43, i32 0, i32 2
  store i64 -9223372036854775799, ptr %44, align 8, !tbaa !16
  store i64 -9223372036854775787, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %59

45:                                               ; preds = %30
  %46 = load i16, ptr %8, align 2, !tbaa !9
  %47 = zext i16 %46 to i32
  %48 = load ptr, ptr %10, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct._MTFTP6_GETINFO_CONTEXT, ptr %48, i32 0, i32 1
  %50 = load ptr, ptr %49, align 8, !tbaa !19
  store i32 %47, ptr %50, align 4, !tbaa !20
  %51 = load ptr, ptr %10, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct._MTFTP6_GETINFO_CONTEXT, ptr %51, i32 0, i32 0
  %53 = load ptr, ptr %52, align 8, !tbaa !18
  %54 = load ptr, ptr %53, align 8, !tbaa !5
  %55 = load ptr, ptr %9, align 8, !tbaa !5
  %56 = load i16, ptr %8, align 2, !tbaa !9
  %57 = zext i16 %56 to i64
  %58 = call ptr @CopyMem(ptr noundef %54, ptr noundef %55, i64 noundef %57)
  store i64 -9223372036854775787, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %59

59:                                               ; preds = %45, %42
  call void @llvm.lifetime.end.p0(i64 2, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %60 = load i64, ptr %5, align 8
  ret i64 %60
}

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 = !{!"short", !7, i64 0}
!11 = !{!12, !6, i64 72}
!12 = !{!"_EFI_MTFTP6_TOKEN", !13, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !14, i64 40, !6, i64 48, !13, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96}
!13 = !{!"long long", !7, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!7, !7, i64 0}
!16 = !{!17, !13, i64 16}
!17 = !{!"_MTFTP6_GETINFO_CONTEXT", !6, i64 0, !6, i64 8, !13, i64 16}
!18 = !{!17, !6, i64 0}
!19 = !{!17, !6, i64 8}
!20 = !{!14, !14, i64 0}


; 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 ptr @AllocateZeroPool(i64 noundef) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
define dso_local noundef i64 @Mtftp6CheckPacket(ptr nocapture noundef readnone %0, ptr nocapture noundef readonly %1, i16 noundef zeroext %2, ptr noundef %3) local_unnamed_addr #1 {
  %5 = getelementptr inbounds i8, ptr %1, i64 72
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = load i16, ptr %3, align 1, !tbaa !12
  %8 = tail call zeroext i16 @SwapBytes16(i16 noundef zeroext %7) #2
  %9 = getelementptr inbounds i8, ptr %6, i64 16
  %switch.selectcmp = icmp eq i16 %8, 6
  %switch.select = select i1 %switch.selectcmp, i64 0, i64 -9223372036854775784
  %switch.selectcmp15 = icmp eq i16 %8, 5
  %switch.select16 = select i1 %switch.selectcmp15, i64 -9223372036854775785, i64 %switch.select
  store i64 %switch.select16, ptr %9, align 8, !tbaa !13
  %10 = zext i16 %2 to i64
  %11 = tail call ptr @AllocateZeroPool(i64 noundef %10) #2
  %12 = load ptr, ptr %6, align 8, !tbaa !15
  store ptr %11, ptr %12, align 8, !tbaa !16
  %13 = load ptr, ptr %6, align 8, !tbaa !15
  %14 = load ptr, ptr %13, align 8, !tbaa !16
  %15 = icmp eq ptr %14, null
  br i1 %15, label %16, label %18

16:                                               ; preds = %4
  %17 = getelementptr inbounds i8, ptr %6, i64 16
  store i64 -9223372036854775799, ptr %17, align 8, !tbaa !13
  br label %23

18:                                               ; preds = %4
  %19 = zext i16 %2 to i32
  %20 = getelementptr inbounds i8, ptr %6, i64 8
  %21 = load ptr, ptr %20, align 8, !tbaa !17
  store i32 %19, ptr %21, align 4, !tbaa !18
  %22 = tail call ptr @CopyMem(ptr noundef nonnull %14, ptr noundef nonnull %3, i64 noundef %10) #2
  br label %23

23:                                               ; preds = %18, %16
  ret i64 -9223372036854775787
}

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, !10, i64 72}
!6 = !{!"_EFI_MTFTP6_TOKEN", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !11, i64 40, !10, i64 48, !7, i64 56, !10, i64 64, !10, i64 72, !10, i64 80, !10, i64 88, !10, i64 96}
!7 = !{!"long long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!8, !8, i64 0}
!13 = !{!14, !7, i64 16}
!14 = !{!"_MTFTP6_GETINFO_CONTEXT", !10, i64 0, !10, i64 8, !7, i64 16}
!15 = !{!14, !10, i64 0}
!16 = !{!10, !10, i64 0}
!17 = !{!14, !10, i64 8}
!18 = !{!11, !11, i64 0}


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