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

; 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_ERROR_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] }

; 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 @AsciiStrLen(ptr noundef) #1

declare ptr @NetbufAlloc(i32 noundef) #1

declare ptr @NetbufAllocSpace(ptr noundef, i32 noundef, i8 noundef zeroext) #1

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

declare ptr @AsciiStrCpy(ptr noundef, ptr noundef) #1

declare void @NetbufFree(ptr noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local i64 @Mtftp6SendError(ptr noundef %0, i16 noundef zeroext %1, ptr noundef %2) #2 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i16, align 2
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i16 %1, ptr %6, align 2, !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 4, ptr %10) #3
  %12 = load ptr, ptr %7, align 8, !tbaa !5
  %13 = call i64 @AsciiStrLen(ptr noundef %12)
  %14 = add i64 %13, 5
  %15 = trunc i64 %14 to i32
  store i32 %15, ptr %10, align 4, !tbaa !11
  %16 = load i32, ptr %10, align 4, !tbaa !11
  %17 = call ptr @NetbufAlloc(i32 noundef %16)
  store ptr %17, ptr %8, align 8, !tbaa !5
  %18 = load ptr, ptr %8, align 8, !tbaa !5
  %19 = icmp eq ptr %18, null
  br i1 %19, label %20, label %21

20:                                               ; preds = %3
  store i64 -9223372036854775799, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %58

21:                                               ; preds = %3
  %22 = load ptr, ptr %8, align 8, !tbaa !5
  %23 = load i32, ptr %10, align 4, !tbaa !11
  %24 = call ptr @NetbufAllocSpace(ptr noundef %22, i32 noundef %23, i8 noundef zeroext 0)
  store ptr %24, ptr %9, align 8, !tbaa !5
  %25 = load ptr, ptr %9, align 8, !tbaa !5
  %26 = icmp eq ptr %25, null
  br i1 %26, label %27, label %29

27:                                               ; preds = %21
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  call void @NetbufFree(ptr noundef %28)
  store i64 -9223372036854775799, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %58

29:                                               ; preds = %21
  %30 = call zeroext i16 @SwapBytes16(i16 noundef zeroext 5)
  %31 = load ptr, ptr %9, align 8, !tbaa !5
  store i16 %30, ptr %31, align 1, !tbaa !13
  %32 = load i16, ptr %6, align 2, !tbaa !9
  %33 = call zeroext i16 @SwapBytes16(i16 noundef zeroext %32)
  %34 = load ptr, ptr %9, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.EFI_MTFTP6_ERROR_HEADER, ptr %34, i32 0, i32 1
  store i16 %33, ptr %35, align 1, !tbaa !13
  %36 = load ptr, ptr %9, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.EFI_MTFTP6_ERROR_HEADER, ptr %36, i32 0, i32 2
  %38 = getelementptr inbounds [1 x i8], ptr %37, i64 0, i64 0
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = call ptr @AsciiStrCpy(ptr noundef %38, ptr noundef %39)
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct._MTFTP6_INSTANCE, ptr %41, i32 0, i32 18
  %43 = load ptr, ptr %42, align 8, !tbaa !14
  %44 = icmp ne ptr %43, null
  br i1 %44, label %45, label %49

45:                                               ; preds = %29
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct._MTFTP6_INSTANCE, ptr %46, i32 0, i32 18
  %48 = load ptr, ptr %47, align 8, !tbaa !14
  call void @NetbufFree(ptr noundef %48)
  br label %49

49:                                               ; preds = %45, %29
  %50 = load ptr, ptr %8, align 8, !tbaa !5
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct._MTFTP6_INSTANCE, ptr %51, i32 0, i32 18
  store ptr %50, ptr %52, align 8, !tbaa !14
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct._MTFTP6_INSTANCE, ptr %53, i32 0, i32 19
  store i32 0, ptr %54, align 8, !tbaa !21
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = load ptr, ptr %8, align 8, !tbaa !5
  %57 = call i64 @Mtftp6TransmitPacket(ptr noundef %55, ptr noundef %56)
  store i64 %57, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %58

58:                                               ; preds = %49, %27, %20
  call void @llvm.lifetime.end.p0(i64 4, 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
  %59 = load i64, ptr %4, align 8
  ret i64 %59
}

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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !6, i64 240}
!15 = !{!"_MTFTP6_INSTANCE", !12, i64 0, !6, i64 8, !16, i64 16, !17, i64 32, !6, i64 96, !6, i64 104, !6, i64 112, !18, i64 120, !10, i64 152, !10, i64 154, !16, i64 160, !19, i64 176, !10, i64 192, !10, i64 194, !6, i64 200, !19, i64 208, !10, i64 224, !6, i64 232, !6, i64 240, !12, i64 248, !12, i64 252, !12, i64 256, !12, i64 260, !20, i64 264, !7, i64 272, !7, i64 273, !7, i64 274}
!16 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!17 = !{!"_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}
!18 = !{!"", !10, i64 0, !7, i64 2, !12, i64 4, !19, i64 8, !10, i64 24, !7, i64 26, !12, i64 28}
!19 = !{!"", !7, i64 0}
!20 = !{!"long long", !7, i64 0}
!21 = !{!15, !12, i64 248}


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

declare ptr @NetbufAlloc(i32 noundef) local_unnamed_addr #0

declare ptr @NetbufAllocSpace(ptr noundef, i32 noundef, i8 noundef zeroext) local_unnamed_addr #0

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

declare ptr @AsciiStrCpy(ptr noundef, ptr noundef) local_unnamed_addr #0

declare void @NetbufFree(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local i64 @Mtftp6SendError(ptr noundef %0, i16 noundef zeroext %1, ptr noundef %2) local_unnamed_addr #1 {
  %4 = tail call i64 @AsciiStrLen(ptr noundef %2) #2
  %5 = trunc i64 %4 to i32
  %6 = add i32 %5, 5
  %7 = tail call ptr @NetbufAlloc(i32 noundef %6) #2
  %8 = icmp eq ptr %7, null
  br i1 %8, label %25, label %9

9:                                                ; preds = %3
  %10 = tail call ptr @NetbufAllocSpace(ptr noundef nonnull %7, i32 noundef %6, i8 noundef zeroext 0) #2
  %11 = icmp eq ptr %10, null
  br i1 %11, label %12, label %13

12:                                               ; preds = %9
  tail call void @NetbufFree(ptr noundef nonnull %7) #2
  br label %25

13:                                               ; preds = %9
  %14 = tail call zeroext i16 @SwapBytes16(i16 noundef zeroext 5) #2
  store i16 %14, ptr %10, align 1, !tbaa !5
  %15 = tail call zeroext i16 @SwapBytes16(i16 noundef zeroext %1) #2
  %16 = getelementptr inbounds i8, ptr %10, i64 2
  store i16 %15, ptr %16, align 1, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %10, i64 4
  %18 = tail call ptr @AsciiStrCpy(ptr noundef nonnull %17, ptr noundef %2) #2
  %19 = getelementptr inbounds i8, ptr %0, i64 240
  %20 = load ptr, ptr %19, align 8, !tbaa !8
  %.not = icmp eq ptr %20, null
  br i1 %.not, label %22, label %21

21:                                               ; preds = %13
  tail call void @NetbufFree(ptr noundef nonnull %20) #2
  br label %22

22:                                               ; preds = %21, %13
  store ptr %7, ptr %19, align 8, !tbaa !8
  %23 = getelementptr inbounds i8, ptr %0, i64 248
  store i32 0, ptr %23, align 8, !tbaa !18
  %24 = tail call i64 @Mtftp6TransmitPacket(ptr noundef nonnull %0, ptr noundef nonnull %7)
  br label %25

25:                                               ; preds = %3, %22, %12
  %.0 = phi i64 [ -9223372036854775799, %12 ], [ %24, %22 ], [ -9223372036854775799, %3 ]
  ret i64 %.0
}

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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !11, i64 240}
!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 = !{!9, !10, i64 248}


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