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

; 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.IP6_ICMP_HEAD = type { i8, i8, i16 }
%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] }
%struct._LIST_ENTRY = type { ptr, ptr }
%union.NET_IP_HEAD = type { ptr }
%struct.NET_BLOCK_OP = type { ptr, ptr, ptr, ptr, i32 }
%struct._EFI_IP6_HEADER = type { i16, i16, i16, i8, i8, %struct.EFI_IPv6_ADDRESS, %struct.EFI_IPv6_ADDRESS }
%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

declare void @NetbufFree(ptr noundef) #1

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

declare i32 @NetbufCopy(ptr noundef, i32 noundef, i32 noundef, ptr noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local i64 @Ip6IcmpHandle(ptr noundef %0, ptr noundef %1, ptr noundef %2) #2 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca %struct.IP6_ICMP_HEAD, align 1
  %9 = alloca i16, align 2
  %10 = alloca i16, align 2
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #3
  %12 = load ptr, ptr %7, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.NET_BUF, ptr %12, i32 0, i32 9
  %14 = load i32, ptr %13, align 4, !tbaa !9
  %15 = zext i32 %14 to i64
  %16 = icmp ult i64 %15, 4
  br i1 %16, label %17, label %18

17:                                               ; preds = %3
  br label %55

18:                                               ; preds = %3
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = call i32 @NetbufCopy(ptr noundef %19, i32 noundef 0, i32 noundef 4, ptr noundef %8)
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct._EFI_IP6_HEADER, ptr %21, i32 0, i32 5
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct._EFI_IP6_HEADER, ptr %23, i32 0, i32 6
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.NET_BUF, ptr %25, i32 0, i32 9
  %27 = load i32, ptr %26, align 4, !tbaa !9
  %28 = call zeroext i16 @NetIp6PseudoHeadChecksum(ptr noundef %22, ptr noundef %24, i8 noundef zeroext 58, i32 noundef %27)
  store i16 %28, ptr %9, align 2, !tbaa !14
  %29 = load i16, ptr %9, align 2, !tbaa !14
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = call zeroext i16 @NetbufChecksum(ptr noundef %30)
  %32 = call zeroext i16 @NetAddChecksum(i16 noundef zeroext %29, i16 noundef zeroext %31)
  %33 = zext i16 %32 to i32
  %34 = xor i32 %33, -1
  %35 = trunc i32 %34 to i16
  store i16 %35, ptr %10, align 2, !tbaa !14
  %36 = load i16, ptr %10, align 2, !tbaa !14
  %37 = zext i16 %36 to i32
  %38 = icmp ne i32 %37, 0
  br i1 %38, label %39, label %40

39:                                               ; preds = %18
  br label %55

40:                                               ; preds = %18
  %41 = getelementptr inbounds %struct.IP6_ICMP_HEAD, ptr %8, i32 0, i32 0
  %42 = load i8, ptr %41, align 1, !tbaa !16
  %43 = zext i8 %42 to i32
  %44 = icmp sle i32 %43, 127
  br i1 %44, label %45, label %50

45:                                               ; preds = %40
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = call i64 @Ip6ProcessIcmpError(ptr noundef %46, ptr noundef %47, ptr noundef %48)
  store i64 %49, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %57

50:                                               ; preds = %40
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  %54 = call i64 @Ip6ProcessIcmpInformation(ptr noundef %51, ptr noundef %52, ptr noundef %53)
  store i64 %54, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %57

55:                                               ; preds = %39, %17
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  call void @NetbufFree(ptr noundef %56)
  store i64 -9223372036854775806, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %57

57:                                               ; preds = %55, %50, %45
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %58 = load i64, ptr %4, align 8
  ret i64 %58
}

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

declare zeroext i16 @NetAddChecksum(i16 noundef zeroext, i16 noundef zeroext) #1

declare zeroext i16 @NetbufChecksum(ptr noundef) #1

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, !11, i64 132}
!10 = !{!"", !11, i64 0, !12, i64 8, !13, i64 16, !7, i64 32, !6, i64 40, !6, i64 48, !7, i64 56, !6, i64 120, !11, i64 128, !11, i64 132, !7, i64 136}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!14 = !{!15, !15, i64 0}
!15 = !{!"short", !7, i64 0}
!16 = !{!17, !7, i64 0}
!17 = !{!"", !7, i64 0, !7, i64 1, !15, i64 2}


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

%struct.IP6_ICMP_HEAD = type { i8, i8, i16 }

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

declare void @NetbufFree(ptr noundef) local_unnamed_addr #1

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

declare i32 @NetbufCopy(ptr noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

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

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

; Function Attrs: nounwind uwtable
define dso_local i64 @Ip6IcmpHandle(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #2 {
  %4 = alloca %struct.IP6_ICMP_HEAD, align 1
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #3
  %5 = getelementptr inbounds i8, ptr %2, i64 132
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = icmp ult i32 %6, 4
  br i1 %7, label %23, label %8

8:                                                ; preds = %3
  %9 = call i32 @NetbufCopy(ptr noundef nonnull %2, i32 noundef 0, i32 noundef 4, ptr noundef nonnull %4) #3
  %10 = getelementptr inbounds i8, ptr %1, i64 8
  %11 = getelementptr inbounds i8, ptr %1, i64 24
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = call zeroext i16 @NetIp6PseudoHeadChecksum(ptr noundef nonnull %10, ptr noundef nonnull %11, i8 noundef zeroext 58, i32 noundef %12) #3
  %14 = call zeroext i16 @NetbufChecksum(ptr noundef nonnull %2) #3
  %15 = call zeroext i16 @NetAddChecksum(i16 noundef zeroext %13, i16 noundef zeroext %14) #3
  %.not = icmp eq i16 %15, -1
  br i1 %.not, label %16, label %23

16:                                               ; preds = %8
  %17 = load i8, ptr %4, align 1, !tbaa !13
  %18 = icmp sgt i8 %17, -1
  br i1 %18, label %19, label %21

19:                                               ; preds = %16
  %20 = call i64 @Ip6ProcessIcmpError(ptr noundef %0, ptr noundef %1, ptr noundef nonnull %2)
  br label %24

21:                                               ; preds = %16
  %22 = call i64 @Ip6ProcessIcmpInformation(ptr noundef %0, ptr noundef %1, ptr noundef nonnull %2)
  br label %24

23:                                               ; preds = %8, %3
  call void @NetbufFree(ptr noundef nonnull %2) #3
  br label %24

24:                                               ; preds = %23, %21, %19
  %.0 = phi i64 [ -9223372036854775806, %23 ], [ %20, %19 ], [ %22, %21 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #3
  ret i64 %.0
}

declare zeroext i16 @NetIp6PseudoHeadChecksum(ptr noundef, ptr noundef, i8 noundef zeroext, i32 noundef) local_unnamed_addr #1

declare zeroext i16 @NetAddChecksum(i16 noundef zeroext, i16 noundef zeroext) local_unnamed_addr #1

declare zeroext i16 @NetbufChecksum(ptr noundef) local_unnamed_addr #1

attributes #0 = { mustprogress 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, !7, i64 132}
!6 = !{!"", !7, i64 0, !10, i64 8, !11, i64 16, !8, i64 32, !12, i64 40, !12, i64 48, !8, i64 56, !12, i64 120, !7, i64 128, !7, i64 132, !8, i64 136}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long long", !8, i64 0}
!11 = !{!"_LIST_ENTRY", !12, i64 0, !12, i64 8}
!12 = !{!"any pointer", !8, i64 0}
!13 = !{!14, !8, i64 0}
!14 = !{!"", !8, i64 0, !8, i64 1, !15, i64 2}
!15 = !{!"short", !8, i64 0}


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