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

; 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_ROUTE_TABLE = type { i64, i32, [129 x %struct._LIST_ENTRY], %struct.IP6_ROUTE_CACHE }
%struct._LIST_ENTRY = type { ptr, ptr }
%struct.IP6_ROUTE_CACHE = type { [31 x %struct._LIST_ENTRY], [31 x i8] }
%struct.EFI_IP6_ROUTE_TABLE = type { %struct.EFI_IPv6_ADDRESS, %struct.EFI_IPv6_ADDRESS, i8 }
%struct.EFI_IPv6_ADDRESS = type { [16 x i8] }
%struct.IP6_ROUTE_ENTRY = type { %struct._LIST_ENTRY, i64, i32, i8, %struct.EFI_IPv6_ADDRESS, %struct.EFI_IPv6_ADDRESS }

@.str = external hidden unnamed_addr constant [130 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [30 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [30 x i8], align 1

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

declare zeroext i8 @DebugAssertEnabled() #1

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

declare ptr @AllocatePool(i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i64 @Ip6BuildEfiRouteTable(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 ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = 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 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 4, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  br label %14

14:                                               ; preds = %3
  %15 = call zeroext i8 @DebugAssertEnabled()
  %16 = icmp ne i8 %15, 0
  br i1 %16, label %17, label %22

17:                                               ; preds = %14
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = icmp ne ptr %18, null
  br i1 %19, label %21, label %20

20:                                               ; preds = %17
  call void @DebugAssert(ptr noundef @.str, i64 noundef 280, ptr noundef @.str.4)
  br label %21

21:                                               ; preds = %20, %17
  br label %22

22:                                               ; preds = %21, %14
  br label %23

23:                                               ; preds = %22
  br label %24

24:                                               ; preds = %23
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct._IP6_ROUTE_TABLE, ptr %25, i32 0, i32 1
  %27 = load i32, ptr %26, align 8, !tbaa !9
  store i32 %27, ptr %11, align 4, !tbaa !14
  %28 = load i32, ptr %11, align 4, !tbaa !14
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 %28, ptr %29, align 4, !tbaa !14
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = icmp eq ptr %30, null
  br i1 %31, label %35, label %32

32:                                               ; preds = %24
  %33 = load i32, ptr %11, align 4, !tbaa !14
  %34 = icmp eq i32 %33, 0
  br i1 %34, label %35, label %36

35:                                               ; preds = %32, %24
  store i64 0, ptr %4, align 8
  store i32 1, ptr %13, align 4
  br label %129

36:                                               ; preds = %32
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  %38 = load ptr, ptr %37, align 8, !tbaa !5
  %39 = icmp eq ptr %38, null
  br i1 %39, label %40, label %51

40:                                               ; preds = %36
  %41 = load i32, ptr %11, align 4, !tbaa !14
  %42 = zext i32 %41 to i64
  %43 = mul i64 33, %42
  %44 = call ptr @AllocatePool(i64 noundef %43)
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %44, ptr %45, align 8, !tbaa !5
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = load ptr, ptr %46, align 8, !tbaa !5
  %48 = icmp eq ptr %47, null
  br i1 %48, label %49, label %50

49:                                               ; preds = %40
  store i64 -9223372036854775799, ptr %4, align 8
  store i32 1, ptr %13, align 4
  br label %129

50:                                               ; preds = %40
  br label %51

51:                                               ; preds = %50, %36
  %52 = load ptr, ptr %7, align 8, !tbaa !5
  %53 = load ptr, ptr %52, align 8, !tbaa !5
  store ptr %53, ptr %10, align 8, !tbaa !5
  store i32 0, ptr %11, align 4, !tbaa !14
  store i32 128, ptr %12, align 4, !tbaa !14
  br label %54

54:                                               ; preds = %111, %51
  %55 = load i32, ptr %12, align 4, !tbaa !14
  %56 = icmp sge i32 %55, 0
  br i1 %56, label %57, label %114

57:                                               ; preds = %54
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct._IP6_ROUTE_TABLE, ptr %58, i32 0, i32 2
  %60 = load i32, ptr %12, align 4, !tbaa !14
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [129 x %struct._LIST_ENTRY], ptr %59, i64 0, i64 %61
  %63 = getelementptr inbounds %struct._LIST_ENTRY, ptr %62, i32 0, i32 0
  %64 = load ptr, ptr %63, align 8, !tbaa !15
  store ptr %64, ptr %8, align 8, !tbaa !5
  br label %65

65:                                               ; preds = %106, %57
  %66 = load ptr, ptr %8, align 8, !tbaa !5
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct._IP6_ROUTE_TABLE, ptr %67, i32 0, i32 2
  %69 = load i32, ptr %12, align 4, !tbaa !14
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [129 x %struct._LIST_ENTRY], ptr %68, i64 0, i64 %70
  %72 = icmp ne ptr %66, %71
  br i1 %72, label %73, label %110

73:                                               ; preds = %65
  %74 = load ptr, ptr %8, align 8, !tbaa !5
  %75 = ptrtoint ptr %74 to i64
  %76 = sub i64 %75, 0
  %77 = inttoptr i64 %76 to ptr
  store ptr %77, ptr %9, align 8, !tbaa !5
  %78 = load ptr, ptr %10, align 8, !tbaa !5
  %79 = load i32, ptr %11, align 4, !tbaa !14
  %80 = zext i32 %79 to i64
  %81 = getelementptr inbounds %struct.EFI_IP6_ROUTE_TABLE, ptr %78, i64 %80
  %82 = getelementptr inbounds %struct.EFI_IP6_ROUTE_TABLE, ptr %81, i32 0, i32 1
  %83 = load ptr, ptr %9, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.IP6_ROUTE_ENTRY, ptr %83, i32 0, i32 4
  %85 = load ptr, ptr %9, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.IP6_ROUTE_ENTRY, ptr %85, i32 0, i32 3
  %87 = load i8, ptr %86, align 4, !tbaa !17
  call void @Ip6CopyAddressByPrefix(ptr noundef %82, ptr noundef %84, i8 noundef zeroext %87)
  %88 = load ptr, ptr %10, align 8, !tbaa !5
  %89 = load i32, ptr %11, align 4, !tbaa !14
  %90 = zext i32 %89 to i64
  %91 = getelementptr inbounds %struct.EFI_IP6_ROUTE_TABLE, ptr %88, i64 %90
  %92 = getelementptr inbounds %struct.EFI_IP6_ROUTE_TABLE, ptr %91, i32 0, i32 0
  %93 = load ptr, ptr %9, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.IP6_ROUTE_ENTRY, ptr %93, i32 0, i32 5
  %95 = call ptr @CopyMem(ptr noundef %92, ptr noundef %94, i64 noundef 16)
  %96 = load ptr, ptr %9, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.IP6_ROUTE_ENTRY, ptr %96, i32 0, i32 3
  %98 = load i8, ptr %97, align 4, !tbaa !17
  %99 = load ptr, ptr %10, align 8, !tbaa !5
  %100 = load i32, ptr %11, align 4, !tbaa !14
  %101 = zext i32 %100 to i64
  %102 = getelementptr inbounds %struct.EFI_IP6_ROUTE_TABLE, ptr %99, i64 %101
  %103 = getelementptr inbounds %struct.EFI_IP6_ROUTE_TABLE, ptr %102, i32 0, i32 2
  store i8 %98, ptr %103, align 1, !tbaa !20
  %104 = load i32, ptr %11, align 4, !tbaa !14
  %105 = add i32 %104, 1
  store i32 %105, ptr %11, align 4, !tbaa !14
  br label %106

106:                                              ; preds = %73
  %107 = load ptr, ptr %8, align 8, !tbaa !5
  %108 = getelementptr inbounds %struct._LIST_ENTRY, ptr %107, i32 0, i32 0
  %109 = load ptr, ptr %108, align 8, !tbaa !15
  store ptr %109, ptr %8, align 8, !tbaa !5
  br label %65, !llvm.loop !22

110:                                              ; preds = %65
  br label %111

111:                                              ; preds = %110
  %112 = load i32, ptr %12, align 4, !tbaa !14
  %113 = add nsw i32 %112, -1
  store i32 %113, ptr %12, align 4, !tbaa !14
  br label %54, !llvm.loop !24

114:                                              ; preds = %54
  br label %115

115:                                              ; preds = %114
  %116 = call zeroext i8 @DebugAssertEnabled()
  %117 = icmp ne i8 %116, 0
  br i1 %117, label %118, label %126

118:                                              ; preds = %115
  %119 = load i32, ptr %11, align 4, !tbaa !14
  %120 = load ptr, ptr %5, align 8, !tbaa !5
  %121 = getelementptr inbounds %struct._IP6_ROUTE_TABLE, ptr %120, i32 0, i32 1
  %122 = load i32, ptr %121, align 8, !tbaa !9
  %123 = icmp eq i32 %119, %122
  br i1 %123, label %125, label %124

124:                                              ; preds = %118
  call void @DebugAssert(ptr noundef @.str, i64 noundef 321, ptr noundef @.str.5)
  br label %125

125:                                              ; preds = %124, %118
  br label %126

126:                                              ; preds = %125, %115
  br label %127

127:                                              ; preds = %126
  br label %128

128:                                              ; preds = %127
  store i64 0, ptr %4, align 8
  store i32 1, ptr %13, align 4
  br label %129

129:                                              ; preds = %128, %49, %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, 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
  %130 = load i64, ptr %4, align 8
  ret i64 %130
}

declare void @Ip6CopyAddressByPrefix(ptr noundef, ptr noundef, i8 noundef zeroext) #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, !12, i64 8}
!10 = !{!"_IP6_ROUTE_TABLE", !11, i64 0, !12, i64 8, !7, i64 16, !13, i64 2080}
!11 = !{!"long long", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!"", !7, i64 0, !7, i64 496}
!14 = !{!12, !12, i64 0}
!15 = !{!16, !6, i64 0}
!16 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!17 = !{!18, !7, i64 28}
!18 = !{!"", !16, i64 0, !11, i64 16, !12, i64 24, !7, i64 28, !19, i64 29, !19, i64 45}
!19 = !{!"", !7, i64 0}
!20 = !{!21, !7, i64 32}
!21 = !{!"", !19, i64 0, !19, i64 16, !7, i64 32}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}
!24 = distinct !{!24, !23}


; 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._LIST_ENTRY = type { ptr, ptr }
%struct.EFI_IP6_ROUTE_TABLE = type { %struct.EFI_IPv6_ADDRESS, %struct.EFI_IPv6_ADDRESS, i8 }
%struct.EFI_IPv6_ADDRESS = type { [16 x i8] }

@.str = external hidden unnamed_addr constant [130 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [30 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [30 x i8], align 1

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

declare zeroext i8 @DebugAssertEnabled() local_unnamed_addr #0

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

declare ptr @AllocatePool(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i64 -9223372036854775799, 1) i64 @Ip6BuildEfiRouteTable(ptr noundef readonly %0, ptr noundef writeonly %1, ptr noundef %2) local_unnamed_addr #1 {
  %4 = tail call zeroext i8 @DebugAssertEnabled() #2
  %5 = icmp eq i8 %4, 0
  %6 = icmp ne ptr %1, null
  %or.cond3 = or i1 %6, %5
  br i1 %or.cond3, label %8, label %7

7:                                                ; preds = %3
  tail call void @DebugAssert(ptr noundef nonnull @.str, i64 noundef 280, ptr noundef nonnull @.str.4) #2
  br label %8

8:                                                ; preds = %7, %3
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load i32, ptr %9, align 8, !tbaa !5
  store i32 %10, ptr %1, align 4, !tbaa !12
  %11 = icmp eq ptr %2, null
  %12 = icmp eq i32 %10, 0
  %or.cond = select i1 %11, i1 true, i1 %12
  br i1 %or.cond, label %43, label %13

13:                                               ; preds = %8
  %14 = load ptr, ptr %2, align 8, !tbaa !13
  %15 = icmp eq ptr %14, null
  br i1 %15, label %16, label %21

16:                                               ; preds = %13
  %17 = zext i32 %10 to i64
  %18 = mul nuw nsw i64 %17, 33
  %19 = tail call ptr @AllocatePool(i64 noundef %18) #2
  store ptr %19, ptr %2, align 8, !tbaa !13
  %20 = icmp eq ptr %19, null
  br i1 %20, label %43, label %21

21:                                               ; preds = %16, %13
  %22 = phi ptr [ %19, %16 ], [ %14, %13 ]
  %23 = getelementptr inbounds i8, ptr %0, i64 16
  br label %24

24:                                               ; preds = %21, %._crit_edge
  %indvars.iv = phi i64 [ 128, %21 ], [ %indvars.iv.next, %._crit_edge ]
  %.03646 = phi i32 [ 0, %21 ], [ %.1.lcssa, %._crit_edge ]
  %25 = getelementptr inbounds [129 x %struct._LIST_ENTRY], ptr %23, i64 0, i64 %indvars.iv
  %.03842 = load ptr, ptr %25, align 8, !tbaa !15
  %.not4143 = icmp eq ptr %.03842, %25
  br i1 %.not4143, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %24, %.lr.ph
  %.03845 = phi ptr [ %.038, %.lr.ph ], [ %.03842, %24 ]
  %.144 = phi i32 [ %36, %.lr.ph ], [ %.03646, %24 ]
  %26 = zext i32 %.144 to i64
  %27 = getelementptr inbounds %struct.EFI_IP6_ROUTE_TABLE, ptr %22, i64 %26
  %28 = getelementptr inbounds i8, ptr %27, i64 16
  %29 = getelementptr inbounds i8, ptr %.03845, i64 29
  %30 = getelementptr inbounds i8, ptr %.03845, i64 28
  %31 = load i8, ptr %30, align 4, !tbaa !17
  tail call void @Ip6CopyAddressByPrefix(ptr noundef nonnull %28, ptr noundef nonnull %29, i8 noundef zeroext %31) #2
  %32 = getelementptr inbounds i8, ptr %.03845, i64 45
  %33 = tail call ptr @CopyMem(ptr noundef nonnull %27, ptr noundef nonnull %32, i64 noundef 16) #2
  %34 = load i8, ptr %30, align 4, !tbaa !17
  %35 = getelementptr inbounds i8, ptr %27, i64 32
  store i8 %34, ptr %35, align 1, !tbaa !20
  %36 = add i32 %.144, 1
  %.038 = load ptr, ptr %.03845, align 8, !tbaa !15
  %.not41 = icmp eq ptr %.038, %25
  br i1 %.not41, label %._crit_edge, label %.lr.ph, !llvm.loop !22

._crit_edge:                                      ; preds = %.lr.ph, %24
  %.1.lcssa = phi i32 [ %.03646, %24 ], [ %36, %.lr.ph ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %.not49 = icmp eq i64 %indvars.iv, 0
  br i1 %.not49, label %37, label %24, !llvm.loop !24

37:                                               ; preds = %._crit_edge
  %38 = tail call zeroext i8 @DebugAssertEnabled() #2
  %.not = icmp eq i8 %38, 0
  br i1 %.not, label %43, label %39

39:                                               ; preds = %37
  %40 = load i32, ptr %9, align 8, !tbaa !5
  %41 = icmp eq i32 %.1.lcssa, %40
  br i1 %41, label %43, label %42

42:                                               ; preds = %39
  tail call void @DebugAssert(ptr noundef nonnull @.str, i64 noundef 321, ptr noundef nonnull @.str.5) #2
  br label %43

43:                                               ; preds = %37, %42, %39, %16, %8
  %.037 = phi i64 [ 0, %8 ], [ -9223372036854775799, %16 ], [ 0, %39 ], [ 0, %42 ], [ 0, %37 ]
  ret i64 %.037
}

declare void @Ip6CopyAddressByPrefix(ptr noundef, ptr noundef, i8 noundef zeroext) local_unnamed_addr #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, !10, i64 8}
!6 = !{!"_IP6_ROUTE_TABLE", !7, i64 0, !10, i64 8, !8, i64 16, !11, i64 2080}
!7 = !{!"long long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"", !8, i64 0, !8, i64 496}
!12 = !{!10, !10, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"any pointer", !8, i64 0}
!15 = !{!16, !14, i64 0}
!16 = !{!"_LIST_ENTRY", !14, i64 0, !14, i64 8}
!17 = !{!18, !8, i64 28}
!18 = !{!"", !16, i64 0, !7, i64 16, !10, i64 24, !8, i64 28, !19, i64 29, !19, i64 45}
!19 = !{!"", !8, i64 0}
!20 = !{!21, !8, i64 32}
!21 = !{!"", !19, i64 0, !19, i64 16, !8, i64 32}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}
!24 = distinct !{!24, !23}


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