; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal (Ip4AddRoute)

; 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._IP4_ROUTE_TABLE = type { i64, i32, [33 x %struct._LIST_ENTRY], ptr, %struct.IP4_ROUTE_CACHE }
%struct._LIST_ENTRY = type { ptr, ptr }
%struct.IP4_ROUTE_CACHE = type { [31 x %struct._LIST_ENTRY] }
%struct.IP4_ROUTE_ENTRY = type { %struct._LIST_ENTRY, i64, i32, i32, i32, i32 }

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

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

; 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
define dso_local i64 @Ip4AddRoute(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct._IP4_ROUTE_TABLE, ptr %14, i32 0, i32 2
  %16 = load i32, ptr %8, align 4, !tbaa !9
  %17 = call i64 @NetGetMaskLength(i32 noundef %16)
  %18 = getelementptr inbounds [33 x %struct._LIST_ENTRY], ptr %15, i64 0, i64 %17
  store ptr %18, ptr %10, align 8, !tbaa !5
  %19 = load ptr, ptr %10, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct._LIST_ENTRY, ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !11
  store ptr %21, ptr %11, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %48, %4
  %23 = load ptr, ptr %11, align 8, !tbaa !5
  %24 = load ptr, ptr %10, align 8, !tbaa !5
  %25 = icmp ne ptr %23, %24
  br i1 %25, label %26, label %52

26:                                               ; preds = %22
  %27 = load ptr, ptr %11, align 8, !tbaa !5
  %28 = ptrtoint ptr %27 to i64
  %29 = sub i64 %28, 0
  %30 = inttoptr i64 %29 to ptr
  store ptr %30, ptr %12, align 8, !tbaa !5
  %31 = load ptr, ptr %12, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.IP4_ROUTE_ENTRY, ptr %31, i32 0, i32 2
  %33 = load i32, ptr %32, align 8, !tbaa !13
  %34 = load i32, ptr %8, align 4, !tbaa !9
  %35 = and i32 %33, %34
  %36 = load i32, ptr %7, align 4, !tbaa !9
  %37 = load i32, ptr %8, align 4, !tbaa !9
  %38 = and i32 %36, %37
  %39 = icmp eq i32 %35, %38
  br i1 %39, label %40, label %47

40:                                               ; preds = %26
  %41 = load ptr, ptr %12, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.IP4_ROUTE_ENTRY, ptr %41, i32 0, i32 4
  %43 = load i32, ptr %42, align 8, !tbaa !16
  %44 = load i32, ptr %9, align 4, !tbaa !9
  %45 = icmp eq i32 %43, %44
  br i1 %45, label %46, label %47

46:                                               ; preds = %40
  store i64 -9223372036854775793, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %75

47:                                               ; preds = %40, %26
  br label %48

48:                                               ; preds = %47
  %49 = load ptr, ptr %11, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct._LIST_ENTRY, ptr %49, i32 0, i32 0
  %51 = load ptr, ptr %50, align 8, !tbaa !11
  store ptr %51, ptr %11, align 8, !tbaa !5
  br label %22, !llvm.loop !17

52:                                               ; preds = %22
  %53 = load i32, ptr %7, align 4, !tbaa !9
  %54 = load i32, ptr %8, align 4, !tbaa !9
  %55 = load i32, ptr %9, align 4, !tbaa !9
  %56 = call ptr @Ip4CreateRouteEntry(i32 noundef %53, i32 noundef %54, i32 noundef %55)
  store ptr %56, ptr %12, align 8, !tbaa !5
  %57 = load ptr, ptr %12, align 8, !tbaa !5
  %58 = icmp eq ptr %57, null
  br i1 %58, label %59, label %60

59:                                               ; preds = %52
  store i64 -9223372036854775799, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %75

60:                                               ; preds = %52
  %61 = load i32, ptr %9, align 4, !tbaa !9
  %62 = icmp eq i32 %61, 0
  br i1 %62, label %63, label %66

63:                                               ; preds = %60
  %64 = load ptr, ptr %12, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.IP4_ROUTE_ENTRY, ptr %64, i32 0, i32 5
  store i32 1, ptr %65, align 4, !tbaa !19
  br label %66

66:                                               ; preds = %63, %60
  %67 = load ptr, ptr %10, align 8, !tbaa !5
  %68 = load ptr, ptr %12, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.IP4_ROUTE_ENTRY, ptr %68, i32 0, i32 0
  %70 = call ptr @InsertHeadList(ptr noundef %67, ptr noundef %69)
  %71 = load ptr, ptr %6, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct._IP4_ROUTE_TABLE, ptr %71, i32 0, i32 1
  %73 = load i32, ptr %72, align 8, !tbaa !20
  %74 = add i32 %73, 1
  store i32 %74, ptr %72, align 8, !tbaa !20
  store i64 0, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %75

75:                                               ; preds = %66, %59, %46
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %76 = load i64, ptr %5, align 8
  ret i64 %76
}

declare i64 @NetGetMaskLength(i32 noundef) #2

declare ptr @InsertHeadList(ptr noundef, 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 = !{!12, !6, i64 0}
!12 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!13 = !{!14, !10, i64 24}
!14 = !{!"", !12, i64 0, !15, i64 16, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36}
!15 = !{!"long long", !7, i64 0}
!16 = !{!14, !10, i64 32}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!14, !10, i64 36}
!20 = !{!21, !10, i64 8}
!21 = !{!"_IP4_ROUTE_TABLE", !15, i64 0, !10, i64 8, !7, i64 16, !6, i64 544, !22, i64 552}
!22 = !{!"", !7, 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"

%struct._LIST_ENTRY = type { ptr, ptr }

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

; Function Attrs: nounwind uwtable
define dso_local range(i64 -9223372036854775799, 1) i64 @Ip4AddRoute(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = tail call i64 @NetGetMaskLength(i32 noundef %2) #2
  %7 = getelementptr inbounds [33 x %struct._LIST_ENTRY], ptr %5, i64 0, i64 %6
  %.025 = load ptr, ptr %7, align 8, !tbaa !5
  %.not26 = icmp eq ptr %.025, %7
  br i1 %.not26, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %17
  %.027 = phi ptr [ %.0, %17 ], [ %.025, %4 ]
  %8 = getelementptr inbounds i8, ptr %.027, i64 24
  %9 = load i32, ptr %8, align 8, !tbaa !10
  %10 = xor i32 %9, %1
  %11 = and i32 %10, %2
  %12 = icmp eq i32 %11, 0
  br i1 %12, label %13, label %17

13:                                               ; preds = %.lr.ph
  %14 = getelementptr inbounds i8, ptr %.027, i64 32
  %15 = load i32, ptr %14, align 8, !tbaa !14
  %16 = icmp eq i32 %15, %3
  br i1 %16, label %.loopexit, label %17

17:                                               ; preds = %.lr.ph, %13
  %.0 = load ptr, ptr %.027, align 8, !tbaa !5
  %.not = icmp eq ptr %.0, %7
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !15

._crit_edge:                                      ; preds = %17, %4
  %18 = tail call ptr @Ip4CreateRouteEntry(i32 noundef %1, i32 noundef %2, i32 noundef %3)
  %19 = icmp eq ptr %18, null
  br i1 %19, label %.loopexit, label %20

20:                                               ; preds = %._crit_edge
  %21 = icmp eq i32 %3, 0
  br i1 %21, label %22, label %24

22:                                               ; preds = %20
  %23 = getelementptr inbounds i8, ptr %18, i64 36
  store i32 1, ptr %23, align 4, !tbaa !17
  br label %24

24:                                               ; preds = %22, %20
  %25 = tail call ptr @InsertHeadList(ptr noundef nonnull %7, ptr noundef nonnull %18) #2
  %26 = getelementptr inbounds i8, ptr %0, i64 8
  %27 = load i32, ptr %26, align 8, !tbaa !18
  %28 = add i32 %27, 1
  store i32 %28, ptr %26, align 8, !tbaa !18
  br label %.loopexit

.loopexit:                                        ; preds = %13, %._crit_edge, %24
  %.022 = phi i64 [ 0, %24 ], [ -9223372036854775799, %._crit_edge ], [ -9223372036854775793, %13 ]
  ret i64 %.022
}

declare i64 @NetGetMaskLength(i32 noundef) local_unnamed_addr #1

declare ptr @InsertHeadList(ptr noundef, 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, !7, i64 0}
!6 = !{!"_LIST_ENTRY", !7, i64 0, !7, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !13, i64 24}
!11 = !{!"", !6, i64 0, !12, i64 16, !13, i64 24, !13, i64 28, !13, i64 32, !13, i64 36}
!12 = !{!"long long", !8, i64 0}
!13 = !{!"int", !8, i64 0}
!14 = !{!11, !13, i64 32}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!11, !13, i64 36}
!18 = !{!19, !13, i64 8}
!19 = !{!"_IP4_ROUTE_TABLE", !12, i64 0, !13, i64 8, !8, i64 16, !7, i64 544, !20, i64 552}
!20 = !{!"", !8, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass