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

; 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
@.str.3 = external hidden unnamed_addr constant [28 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [20 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

declare zeroext i8 @DebugAssertEnabled() #1

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

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i16 @CalculateSum16(ptr noundef %0, i64 noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i16, align 2
  %6 = alloca i64, align 8
  %7 = 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 2, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  br label %8

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

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

14:                                               ; preds = %11
  call void @DebugAssert(ptr noundef @.str, i64 noundef 122, ptr noundef @.str.1)
  br label %15

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

16:                                               ; preds = %15, %8
  br label %17

17:                                               ; preds = %16
  br label %18

18:                                               ; preds = %17
  br label %19

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

22:                                               ; preds = %19
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = ptrtoint ptr %23 to i64
  %25 = and i64 %24, 1
  %26 = icmp eq i64 %25, 0
  br i1 %26, label %28, label %27

27:                                               ; preds = %22
  call void @DebugAssert(ptr noundef @.str, i64 noundef 123, ptr noundef @.str.3)
  br label %28

28:                                               ; preds = %27, %22
  br label %29

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

30:                                               ; preds = %29
  br label %31

31:                                               ; preds = %30
  br label %32

32:                                               ; preds = %31
  %33 = call zeroext i8 @DebugAssertEnabled()
  %34 = icmp ne i8 %33, 0
  br i1 %34, label %35, label %41

35:                                               ; preds = %32
  %36 = load i64, ptr %4, align 8, !tbaa !9
  %37 = and i64 %36, 1
  %38 = icmp eq i64 %37, 0
  br i1 %38, label %40, label %39

39:                                               ; preds = %35
  call void @DebugAssert(ptr noundef @.str, i64 noundef 124, ptr noundef @.str.4)
  br label %40

40:                                               ; preds = %39, %35
  br label %41

41:                                               ; preds = %40, %32
  br label %42

42:                                               ; preds = %41
  br label %43

43:                                               ; preds = %42
  br label %44

44:                                               ; preds = %43
  %45 = call zeroext i8 @DebugAssertEnabled()
  %46 = icmp ne i8 %45, 0
  br i1 %46, label %47, label %56

47:                                               ; preds = %44
  %48 = load i64, ptr %4, align 8, !tbaa !9
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = ptrtoint ptr %49 to i64
  %51 = sub i64 -1, %50
  %52 = add i64 %51, 1
  %53 = icmp ule i64 %48, %52
  br i1 %53, label %55, label %54

54:                                               ; preds = %47
  call void @DebugAssert(ptr noundef @.str, i64 noundef 125, ptr noundef @.str.2)
  br label %55

55:                                               ; preds = %54, %47
  br label %56

56:                                               ; preds = %55, %44
  br label %57

57:                                               ; preds = %56
  br label %58

58:                                               ; preds = %57
  %59 = load i64, ptr %4, align 8, !tbaa !9
  %60 = udiv i64 %59, 2
  store i64 %60, ptr %7, align 8, !tbaa !9
  store i16 0, ptr %5, align 2, !tbaa !11
  store i64 0, ptr %6, align 8, !tbaa !9
  br label %61

61:                                               ; preds = %75, %58
  %62 = load i64, ptr %6, align 8, !tbaa !9
  %63 = load i64, ptr %7, align 8, !tbaa !9
  %64 = icmp ult i64 %62, %63
  br i1 %64, label %65, label %78

65:                                               ; preds = %61
  %66 = load i16, ptr %5, align 2, !tbaa !11
  %67 = zext i16 %66 to i32
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = load i64, ptr %6, align 8, !tbaa !9
  %70 = getelementptr inbounds i16, ptr %68, i64 %69
  %71 = load i16, ptr %70, align 2, !tbaa !11
  %72 = zext i16 %71 to i32
  %73 = add nsw i32 %67, %72
  %74 = trunc i32 %73 to i16
  store i16 %74, ptr %5, align 2, !tbaa !11
  br label %75

75:                                               ; preds = %65
  %76 = load i64, ptr %6, align 8, !tbaa !9
  %77 = add i64 %76, 1
  store i64 %77, ptr %6, align 8, !tbaa !9
  br label %61, !llvm.loop !13

78:                                               ; preds = %61
  %79 = load i16, ptr %5, align 2, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #3
  ret i16 %79
}

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, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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
@.str.3 = external hidden unnamed_addr constant [28 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [20 x i8], align 1

declare zeroext i8 @DebugAssertEnabled() local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i16 @CalculateSum16(ptr noundef %0, i64 noundef %1) local_unnamed_addr #1 {
  %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 122, ptr noundef nonnull @.str.1) #3
  br label %7

7:                                                ; preds = %2, %6
  %8 = tail call zeroext i8 @DebugAssertEnabled() #3
  %.not = icmp eq i8 %8, 0
  %9 = ptrtoint ptr %0 to i64
  %10 = and i64 %9, 1
  %11 = icmp eq i64 %10, 0
  %or.cond19 = or i1 %11, %.not
  br i1 %or.cond19, label %13, label %12

12:                                               ; preds = %7
  tail call void @DebugAssert(ptr noundef nonnull @.str, i64 noundef 123, ptr noundef nonnull @.str.3) #3
  br label %13

13:                                               ; preds = %7, %12
  %14 = tail call zeroext i8 @DebugAssertEnabled() #3
  %.not14 = icmp eq i8 %14, 0
  %15 = and i64 %1, 1
  %16 = icmp eq i64 %15, 0
  %or.cond21 = or i1 %16, %.not14
  br i1 %or.cond21, label %18, label %17

17:                                               ; preds = %13
  tail call void @DebugAssert(ptr noundef nonnull @.str, i64 noundef 124, ptr noundef nonnull @.str.4) #3
  br label %18

18:                                               ; preds = %13, %17
  %19 = tail call zeroext i8 @DebugAssertEnabled() #3
  %.not15 = icmp ne i8 %19, 0
  %20 = sub i64 0, %9
  %.not16 = icmp ult i64 %20, %1
  %or.cond23 = and i1 %.not16, %.not15
  br i1 %or.cond23, label %21, label %22

21:                                               ; preds = %18
  tail call void @DebugAssert(ptr noundef nonnull @.str, i64 noundef 125, ptr noundef nonnull @.str.2) #3
  br label %22

22:                                               ; preds = %21, %18
  %23 = lshr i64 %1, 1
  %.not26 = icmp ult i64 %1, 2
  br i1 %.not26, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %22
  %min.iters.check = icmp ult i64 %1, 32
  br i1 %min.iters.check, label %.lr.ph.preheader29, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %23, 9223372036854775792
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <8 x i16> [ zeroinitializer, %vector.ph ], [ %26, %vector.body ]
  %vec.phi27 = phi <8 x i16> [ zeroinitializer, %vector.ph ], [ %27, %vector.body ]
  %24 = getelementptr inbounds i16, ptr %0, i64 %index
  %25 = getelementptr inbounds i8, ptr %24, i64 16
  %wide.load = load <8 x i16>, ptr %24, align 2, !tbaa !5
  %wide.load28 = load <8 x i16>, ptr %25, align 2, !tbaa !5
  %26 = add <8 x i16> %wide.load, %vec.phi
  %27 = add <8 x i16> %wide.load28, %vec.phi27
  %index.next = add nuw i64 %index, 16
  %28 = icmp eq i64 %index.next, %n.vec
  br i1 %28, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <8 x i16> %27, %26
  %29 = tail call i16 @llvm.vector.reduce.add.v8i16(<8 x i16> %bin.rdx)
  %cmp.n = icmp eq i64 %23, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader29

.lr.ph.preheader29:                               ; preds = %middle.block, %.lr.ph.preheader
  %.025.ph = phi i16 [ 0, %.lr.ph.preheader ], [ %29, %middle.block ]
  %.01324.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader29, %.lr.ph
  %.025 = phi i16 [ %32, %.lr.ph ], [ %.025.ph, %.lr.ph.preheader29 ]
  %.01324 = phi i64 [ %33, %.lr.ph ], [ %.01324.ph, %.lr.ph.preheader29 ]
  %30 = getelementptr inbounds i16, ptr %0, i64 %.01324
  %31 = load i16, ptr %30, align 2, !tbaa !5
  %32 = add i16 %31, %.025
  %33 = add nuw nsw i64 %.01324, 1
  %exitcond.not = icmp eq i64 %33, %23
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %22
  %.0.lcssa = phi i16 [ 0, %22 ], [ %29, %middle.block ], [ %32, %.lr.ph ]
  ret i16 %.0.lcssa
}

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

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 = { 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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !12, !11}


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