; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/MdePkg/Library/BaseLib_ (CalculateSum8)

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

@.str = external hidden unnamed_addr constant [135 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [23 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [54 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @CalculateSum8(ptr noundef %0, i64 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i8, align 1
  %6 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  br label %7

7:                                                ; preds = %2
  %8 = call zeroext i8 @DebugAssertEnabled()
  %9 = icmp ne i8 %8, 0
  br i1 %9, label %10, label %15

10:                                               ; preds = %7
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = icmp ne ptr %11, null
  br i1 %12, label %14, label %13

13:                                               ; preds = %10
  call void @DebugAssert(ptr noundef @.str, i64 noundef 46, ptr noundef @.str.1)
  br label %14

14:                                               ; preds = %13, %10
  br label %15

15:                                               ; preds = %14, %7
  br label %16

16:                                               ; preds = %15
  br label %17

17:                                               ; preds = %16
  br label %18

18:                                               ; preds = %17
  %19 = call zeroext i8 @DebugAssertEnabled()
  %20 = icmp ne i8 %19, 0
  br i1 %20, label %21, label %30

21:                                               ; preds = %18
  %22 = load i64, ptr %4, align 8, !tbaa !9
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = ptrtoint ptr %23 to i64
  %25 = sub i64 -1, %24
  %26 = add i64 %25, 1
  %27 = icmp ule i64 %22, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %21
  call void @DebugAssert(ptr noundef @.str, i64 noundef 47, ptr noundef @.str.2)
  br label %29

29:                                               ; preds = %28, %21
  br label %30

30:                                               ; preds = %29, %18
  br label %31

31:                                               ; preds = %30
  br label %32

32:                                               ; preds = %31
  store i8 0, ptr %5, align 1, !tbaa !11
  store i64 0, ptr %6, align 8, !tbaa !9
  br label %33

33:                                               ; preds = %47, %32
  %34 = load i64, ptr %6, align 8, !tbaa !9
  %35 = load i64, ptr %4, align 8, !tbaa !9
  %36 = icmp ult i64 %34, %35
  br i1 %36, label %37, label %50

37:                                               ; preds = %33
  %38 = load i8, ptr %5, align 1, !tbaa !11
  %39 = zext i8 %38 to i32
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = load i64, ptr %6, align 8, !tbaa !9
  %42 = getelementptr inbounds i8, ptr %40, i64 %41
  %43 = load i8, ptr %42, align 1, !tbaa !11
  %44 = zext i8 %43 to i32
  %45 = add nsw i32 %39, %44
  %46 = trunc i32 %45 to i8
  store i8 %46, ptr %5, align 1, !tbaa !11
  br label %47

47:                                               ; preds = %37
  %48 = load i64, ptr %6, align 8, !tbaa !9
  %49 = add i64 %48, 1
  store i64 %49, ptr %6, align 8, !tbaa !9
  br label %33, !llvm.loop !12

50:                                               ; preds = %33
  %51 = load i8, ptr %5, align 1, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #3
  ret i8 %51
}

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

declare zeroext i8 @DebugAssertEnabled() #2

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

; 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 = { "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 = !{!"long long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

@.str = external hidden unnamed_addr constant [135 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [23 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [54 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @CalculateSum8(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = tail call zeroext i8 @DebugAssertEnabled() #3
  %4 = icmp eq i8 %3, 0
  %5 = icmp ne ptr %0, null
  %or.cond = or i1 %5, %4
  br i1 %or.cond, label %7, label %6

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

7:                                                ; preds = %2, %6
  %8 = tail call zeroext i8 @DebugAssertEnabled() #3
  %.not = icmp ne i8 %8, 0
  %9 = ptrtoint ptr %0 to i64
  %10 = sub i64 0, %9
  %.not11 = icmp ult i64 %10, %1
  %or.cond13 = and i1 %.not11, %.not
  br i1 %or.cond13, label %.thread, label %11

.thread:                                          ; preds = %7
  tail call void @DebugAssert(ptr noundef nonnull @.str, i64 noundef 47, ptr noundef nonnull @.str.2) #3
  br label %iter.check

11:                                               ; preds = %7
  %.not16 = icmp eq i64 %1, 0
  br i1 %.not16, label %._crit_edge, label %iter.check

iter.check:                                       ; preds = %.thread, %11
  %min.iters.check = icmp ult i64 %1, 8
  br i1 %min.iters.check, label %.lr.ph.preheader, label %vector.main.loop.iter.check

vector.main.loop.iter.check:                      ; preds = %iter.check
  %min.iters.check18 = icmp ult i64 %1, 32
  br i1 %min.iters.check18, label %vec.epilog.ph, label %vector.ph

vector.ph:                                        ; preds = %vector.main.loop.iter.check
  %n.vec = and i64 %1, -32
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <16 x i8> [ zeroinitializer, %vector.ph ], [ %14, %vector.body ]
  %vec.phi19 = phi <16 x i8> [ zeroinitializer, %vector.ph ], [ %15, %vector.body ]
  %12 = getelementptr inbounds i8, ptr %0, i64 %index
  %13 = getelementptr inbounds i8, ptr %12, i64 16
  %wide.load = load <16 x i8>, ptr %12, align 1, !tbaa !5
  %wide.load20 = load <16 x i8>, ptr %13, align 1, !tbaa !5
  %14 = add <16 x i8> %wide.load, %vec.phi
  %15 = add <16 x i8> %wide.load20, %vec.phi19
  %index.next = add nuw i64 %index, 32
  %16 = icmp eq i64 %index.next, %n.vec
  br i1 %16, label %middle.block, label %vector.body, !llvm.loop !8

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <16 x i8> %15, %14
  %17 = tail call i8 @llvm.vector.reduce.add.v16i8(<16 x i8> %bin.rdx)
  %cmp.n = icmp eq i64 %n.vec, %1
  br i1 %cmp.n, label %._crit_edge, label %vec.epilog.iter.check

vec.epilog.iter.check:                            ; preds = %middle.block
  %n.vec.remaining = and i64 %1, 24
  %min.epilog.iters.check = icmp eq i64 %n.vec.remaining, 0
  br i1 %min.epilog.iters.check, label %.lr.ph.preheader, label %vec.epilog.ph

vec.epilog.ph:                                    ; preds = %vector.main.loop.iter.check, %vec.epilog.iter.check
  %bc.merge.rdx = phi i8 [ %17, %vec.epilog.iter.check ], [ 0, %vector.main.loop.iter.check ]
  %vec.epilog.resume.val = phi i64 [ %n.vec, %vec.epilog.iter.check ], [ 0, %vector.main.loop.iter.check ]
  %n.vec22 = and i64 %1, -8
  %18 = insertelement <8 x i8> <i8 poison, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0>, i8 %bc.merge.rdx, i64 0
  br label %vec.epilog.vector.body

vec.epilog.vector.body:                           ; preds = %vec.epilog.vector.body, %vec.epilog.ph
  %index23 = phi i64 [ %vec.epilog.resume.val, %vec.epilog.ph ], [ %index.next26, %vec.epilog.vector.body ]
  %vec.phi24 = phi <8 x i8> [ %18, %vec.epilog.ph ], [ %20, %vec.epilog.vector.body ]
  %19 = getelementptr inbounds i8, ptr %0, i64 %index23
  %wide.load25 = load <8 x i8>, ptr %19, align 1, !tbaa !5
  %20 = add <8 x i8> %wide.load25, %vec.phi24
  %index.next26 = add nuw i64 %index23, 8
  %21 = icmp eq i64 %index.next26, %n.vec22
  br i1 %21, label %vec.epilog.middle.block, label %vec.epilog.vector.body, !llvm.loop !12

vec.epilog.middle.block:                          ; preds = %vec.epilog.vector.body
  %22 = tail call i8 @llvm.vector.reduce.add.v8i8(<8 x i8> %20)
  %cmp.n27 = icmp eq i64 %n.vec22, %1
  br i1 %cmp.n27, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %vec.epilog.middle.block, %iter.check, %vec.epilog.iter.check
  %.015.ph = phi i64 [ 0, %iter.check ], [ %n.vec, %vec.epilog.iter.check ], [ %n.vec22, %vec.epilog.middle.block ]
  %.01014.ph = phi i8 [ 0, %iter.check ], [ %17, %vec.epilog.iter.check ], [ %22, %vec.epilog.middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.015 = phi i64 [ %26, %.lr.ph ], [ %.015.ph, %.lr.ph.preheader ]
  %.01014 = phi i8 [ %25, %.lr.ph ], [ %.01014.ph, %.lr.ph.preheader ]
  %23 = getelementptr inbounds i8, ptr %0, i64 %.015
  %24 = load i8, ptr %23, align 1, !tbaa !5
  %25 = add i8 %24, %.01014
  %26 = add nuw i64 %.015, 1
  %exitcond.not = icmp eq i64 %26, %1
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %vec.epilog.middle.block, %11
  %.010.lcssa = phi i8 [ 0, %11 ], [ %17, %middle.block ], [ %22, %vec.epilog.middle.block ], [ %25, %.lr.ph ]
  ret i8 %.010.lcssa
}

declare zeroext i8 @DebugAssertEnabled() local_unnamed_addr #1

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.vector.reduce.add.v16i8(<16 x i8>) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.vector.reduce.add.v8i8(<8 x i8>) #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 = { "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9, !10, !11}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!"llvm.loop.isvectorized", i32 1}
!11 = !{!"llvm.loop.unroll.runtime.disable"}
!12 = distinct !{!12, !9, !10, !11}
!13 = distinct !{!13, !9, !11, !10}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass