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

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

@mCrcTable = external dso_local global [256 x i32], align 16

; Function Attrs: nounwind uwtable
define dso_local i64 @RuntimeDriverCalculateCrc32(ptr noundef %0, i64 noundef %1, ptr noundef %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = icmp eq ptr %12, null
  br i1 %13, label %20, label %14

14:                                               ; preds = %3
  %15 = load i64, ptr %6, align 8, !tbaa !9
  %16 = icmp eq i64 %15, 0
  br i1 %16, label %20, label %17

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

20:                                               ; preds = %17, %14, %3
  store i64 -9223372036854775806, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %50

21:                                               ; preds = %17
  store i32 -1, ptr %8, align 4, !tbaa !11
  store i64 0, ptr %9, align 8, !tbaa !9
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %22, ptr %10, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %41, %21
  %24 = load i64, ptr %9, align 8, !tbaa !9
  %25 = load i64, ptr %6, align 8, !tbaa !9
  %26 = icmp ult i64 %24, %25
  br i1 %26, label %27, label %46

27:                                               ; preds = %23
  %28 = load i32, ptr %8, align 4, !tbaa !11
  %29 = lshr i32 %28, 8
  %30 = load i32, ptr %8, align 4, !tbaa !11
  %31 = trunc i32 %30 to i8
  %32 = zext i8 %31 to i32
  %33 = load ptr, ptr %10, align 8, !tbaa !5
  %34 = load i8, ptr %33, align 1, !tbaa !13
  %35 = zext i8 %34 to i32
  %36 = xor i32 %32, %35
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [256 x i32], ptr @mCrcTable, i64 0, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !11
  %40 = xor i32 %29, %39
  store i32 %40, ptr %8, align 4, !tbaa !11
  br label %41

41:                                               ; preds = %27
  %42 = load i64, ptr %9, align 8, !tbaa !9
  %43 = add i64 %42, 1
  store i64 %43, ptr %9, align 8, !tbaa !9
  %44 = load ptr, ptr %10, align 8, !tbaa !5
  %45 = getelementptr inbounds i8, ptr %44, i32 1
  store ptr %45, ptr %10, align 8, !tbaa !5
  br label %23, !llvm.loop !14

46:                                               ; preds = %23
  %47 = load i32, ptr %8, align 4, !tbaa !11
  %48 = xor i32 %47, -1
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 %48, ptr %49, align 4, !tbaa !11
  store i64 0, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %50

50:                                               ; preds = %46, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %51 = load i64, ptr %4, align 8
  ret i64 %51
}

; 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

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 = { 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 = !{!"long long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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

@mCrcTable = external dso_local local_unnamed_addr global [256 x i32], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local range(i64 -9223372036854775806, 1) i64 @RuntimeDriverCalculateCrc32(ptr noundef readonly %0, i64 noundef %1, ptr noundef writeonly %2) local_unnamed_addr #0 {
  %4 = icmp eq ptr %0, null
  %5 = icmp eq i64 %1, 0
  %or.cond = or i1 %4, %5
  %6 = icmp eq ptr %2, null
  %or.cond3 = or i1 %or.cond, %6
  br i1 %or.cond3, label %39, label %.preheader.preheader

.preheader.preheader:                             ; preds = %3
  %xtraiter = and i64 %1, 1
  %7 = icmp eq i64 %1, 1
  br i1 %7, label %.unr-lcssa, label %.preheader.preheader.new

.preheader.preheader.new:                         ; preds = %.preheader.preheader
  %unroll_iter = and i64 %1, -2
  br label %.preheader

.preheader:                                       ; preds = %.preheader, %.preheader.preheader.new
  %.024 = phi ptr [ %0, %.preheader.preheader.new ], [ %27, %.preheader ]
  %.01722 = phi i32 [ -1, %.preheader.preheader.new ], [ %26, %.preheader ]
  %niter = phi i64 [ 0, %.preheader.preheader.new ], [ %niter.next.1, %.preheader ]
  %8 = lshr i32 %.01722, 8
  %9 = and i32 %.01722, 255
  %10 = load i8, ptr %.024, align 1, !tbaa !5
  %11 = zext i8 %10 to i32
  %12 = xor i32 %9, %11
  %13 = zext nneg i32 %12 to i64
  %14 = getelementptr inbounds [256 x i32], ptr @mCrcTable, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !8
  %16 = xor i32 %15, %8
  %17 = getelementptr inbounds i8, ptr %.024, i64 1
  %18 = lshr i32 %16, 8
  %19 = and i32 %16, 255
  %20 = load i8, ptr %17, align 1, !tbaa !5
  %21 = zext i8 %20 to i32
  %22 = xor i32 %19, %21
  %23 = zext nneg i32 %22 to i64
  %24 = getelementptr inbounds [256 x i32], ptr @mCrcTable, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !8
  %26 = xor i32 %25, %18
  %27 = getelementptr inbounds i8, ptr %.024, i64 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.unr-lcssa, label %.preheader, !llvm.loop !10

.unr-lcssa:                                       ; preds = %.preheader, %.preheader.preheader
  %.lcssa.ph = phi i32 [ poison, %.preheader.preheader ], [ %26, %.preheader ]
  %.024.unr = phi ptr [ %0, %.preheader.preheader ], [ %27, %.preheader ]
  %.01722.unr = phi i32 [ -1, %.preheader.preheader ], [ %26, %.preheader ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %37, label %.preheader.epil

.preheader.epil:                                  ; preds = %.unr-lcssa
  %28 = lshr i32 %.01722.unr, 8
  %29 = and i32 %.01722.unr, 255
  %30 = load i8, ptr %.024.unr, align 1, !tbaa !5
  %31 = zext i8 %30 to i32
  %32 = xor i32 %29, %31
  %33 = zext nneg i32 %32 to i64
  %34 = getelementptr inbounds [256 x i32], ptr @mCrcTable, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !8
  %36 = xor i32 %35, %28
  br label %37

37:                                               ; preds = %.unr-lcssa, %.preheader.epil
  %.lcssa = phi i32 [ %.lcssa.ph, %.unr-lcssa ], [ %36, %.preheader.epil ]
  %38 = xor i32 %.lcssa, -1
  store i32 %38, ptr %2, align 4, !tbaa !8
  br label %39

39:                                               ; preds = %3, %37
  %.018 = phi i64 [ 0, %37 ], [ -9223372036854775806, %3 ]
  ret i64 %.018
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) 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" }

!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, !9, i64 0}
!9 = !{!"int", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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