; LLVM IR from intel/cryptography-primitives :: sources/ippcp (pcpmont_multiexp_fast)

; 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._gsModEngine = type { ptr, i32, i32, i32, i32, i32, ptr, ptr, ptr, i64, ptr, ptr, ptr, ptr, i32, i32, ptr }

; Function Attrs: nounwind uwtable
define dso_local void @cpFastMontMultiExp(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %4, ptr noundef %5) #0 {
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store ptr %2, ptr %9, align 8, !tbaa !5
  store i32 %3, ptr %10, align 4, !tbaa !9
  store i32 %4, ptr %11, align 4, !tbaa !9
  store ptr %5, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %17 = load ptr, ptr %12, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct._gsModEngine, ptr %17, i32 0, i32 3
  %19 = load i32, ptr %18, align 8, !tbaa !11
  store i32 %19, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  %20 = load i32, ptr %10, align 4, !tbaa !9
  %21 = sub nsw i32 %20, 1
  store i32 %21, ptr %14, align 4, !tbaa !9
  store i32 0, ptr %15, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %35, %6
  %23 = load i32, ptr %15, align 4, !tbaa !9
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %28, label %25

25:                                               ; preds = %22
  %26 = load i32, ptr %14, align 4, !tbaa !9
  %27 = icmp sge i32 %26, 0
  br label %28

28:                                               ; preds = %25, %22
  %29 = phi i1 [ false, %22 ], [ %27, %25 ]
  br i1 %29, label %30, label %38

30:                                               ; preds = %28
  %31 = load ptr, ptr %9, align 8, !tbaa !5
  %32 = load i32, ptr %11, align 4, !tbaa !9
  %33 = load i32, ptr %14, align 4, !tbaa !9
  %34 = call i32 @GetIndex(ptr noundef %31, i32 noundef %32, i32 noundef %33)
  store i32 %34, ptr %15, align 4, !tbaa !9
  br label %35

35:                                               ; preds = %30
  %36 = load i32, ptr %14, align 4, !tbaa !9
  %37 = add nsw i32 %36, -1
  store i32 %37, ptr %14, align 4, !tbaa !9
  br label %22, !llvm.loop !14

38:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #3
  store i32 0, ptr %16, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %58, %38
  %40 = load i32, ptr %16, align 4, !tbaa !9
  %41 = load i32, ptr %13, align 4, !tbaa !9
  %42 = icmp slt i32 %40, %41
  br i1 %42, label %43, label %61

43:                                               ; preds = %39
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  %45 = load i32, ptr %15, align 4, !tbaa !9
  %46 = load i32, ptr %13, align 4, !tbaa !9
  %47 = mul nsw i32 %45, %46
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds i64, ptr %44, i64 %48
  %50 = load i32, ptr %16, align 4, !tbaa !9
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds i64, ptr %49, i64 %51
  %53 = load i64, ptr %52, align 8, !tbaa !16
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  %55 = load i32, ptr %16, align 4, !tbaa !9
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds i64, ptr %54, i64 %56
  store i64 %53, ptr %57, align 8, !tbaa !16
  br label %58

58:                                               ; preds = %43
  %59 = load i32, ptr %16, align 4, !tbaa !9
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %16, align 4, !tbaa !9
  br label %39, !llvm.loop !17

61:                                               ; preds = %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #3
  br label %62

62:                                               ; preds = %87, %61
  %63 = load i32, ptr %14, align 4, !tbaa !9
  %64 = icmp sge i32 %63, 0
  br i1 %64, label %65, label %90

65:                                               ; preds = %62
  %66 = load ptr, ptr %7, align 8, !tbaa !5
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = load ptr, ptr %7, align 8, !tbaa !5
  %69 = load ptr, ptr %12, align 8, !tbaa !5
  call void @cpMontMul_BNU(ptr noundef %66, ptr noundef %67, ptr noundef %68, ptr noundef %69)
  %70 = load ptr, ptr %9, align 8, !tbaa !5
  %71 = load i32, ptr %11, align 4, !tbaa !9
  %72 = load i32, ptr %14, align 4, !tbaa !9
  %73 = call i32 @GetIndex(ptr noundef %70, i32 noundef %71, i32 noundef %72)
  store i32 %73, ptr %15, align 4, !tbaa !9
  %74 = load i32, ptr %15, align 4, !tbaa !9
  %75 = icmp ne i32 %74, 0
  br i1 %75, label %76, label %86

76:                                               ; preds = %65
  %77 = load ptr, ptr %7, align 8, !tbaa !5
  %78 = load ptr, ptr %7, align 8, !tbaa !5
  %79 = load ptr, ptr %8, align 8, !tbaa !5
  %80 = load i32, ptr %15, align 4, !tbaa !9
  %81 = load i32, ptr %13, align 4, !tbaa !9
  %82 = mul nsw i32 %80, %81
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds i64, ptr %79, i64 %83
  %85 = load ptr, ptr %12, align 8, !tbaa !5
  call void @cpMontMul_BNU(ptr noundef %77, ptr noundef %78, ptr noundef %84, ptr noundef %85)
  br label %86

86:                                               ; preds = %76, %65
  br label %87

87:                                               ; preds = %86
  %88 = load i32, ptr %14, align 4, !tbaa !9
  %89 = add nsw i32 %88, -1
  store i32 %89, ptr %14, align 4, !tbaa !9
  br label %62, !llvm.loop !18

90:                                               ; preds = %62
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  ret void
}

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

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @cpMontMul_BNU(ptr noundef, ptr noundef, 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 = { inlinehint 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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 16}
!12 = !{!"_gsModEngine", !6, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !13, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !10, i64 96, !10, i64 100, !6, i64 104}
!13 = !{!"long long", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!13, !13, i64 0}
!17 = distinct !{!17, !15}
!18 = distinct !{!18, !15}


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

; Function Attrs: nounwind uwtable
define dso_local void @cpFastMontMultiExp(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %4, ptr noundef %5) local_unnamed_addr #0 {
  %7 = ptrtoint ptr %1 to i64
  %8 = ptrtoint ptr %0 to i64
  %9 = getelementptr inbounds i8, ptr %5, i64 16
  %10 = load i32, ptr %9, align 8, !tbaa !5
  %.03339 = add nsw i32 %3, -1
  %11 = icmp sgt i32 %3, 0
  br i1 %11, label %.lr.ph, label %.preheader38

.preheader38:                                     ; preds = %.lr.ph, %6
  %.032.lcssa = phi i32 [ 0, %6 ], [ %29, %.lr.ph ]
  %.033.lcssa = phi i32 [ %.03339, %6 ], [ %.033, %.lr.ph ]
  %12 = icmp sgt i32 %10, 0
  br i1 %12, label %.lr.ph43, label %.preheader

.lr.ph43:                                         ; preds = %.preheader38
  %13 = mul nsw i32 %.032.lcssa, %10
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds i64, ptr %1, i64 %14
  %wide.trip.count = zext nneg i32 %10 to i64
  %min.iters.check = icmp ult i32 %10, 10
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.memcheck

scalar.ph.preheader:                              ; preds = %middle.block, %vector.memcheck, %.lr.ph43
  %indvars.iv.ph = phi i64 [ 0, %vector.memcheck ], [ 0, %.lr.ph43 ], [ %n.vec, %middle.block ]
  %xtraiter = and i64 %wide.trip.count, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol

scalar.ph.prol:                                   ; preds = %scalar.ph.preheader, %scalar.ph.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %scalar.ph.prol ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %scalar.ph.prol ], [ 0, %scalar.ph.preheader ]
  %16 = getelementptr inbounds i64, ptr %15, i64 %indvars.iv.prol
  %17 = load i64, ptr %16, align 8, !tbaa !12
  %18 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv.prol
  store i64 %17, ptr %18, align 8, !tbaa !12
  %indvars.iv.next.prol = add nuw nsw i64 %indvars.iv.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol, !llvm.loop !13

scalar.ph.prol.loopexit:                          ; preds = %scalar.ph.prol, %scalar.ph.preheader
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %scalar.ph.preheader ], [ %indvars.iv.next.prol, %scalar.ph.prol ]
  %19 = sub nsw i64 %indvars.iv.ph, %wide.trip.count
  %20 = icmp ugt i64 %19, -4
  br i1 %20, label %.preheader, label %scalar.ph

vector.memcheck:                                  ; preds = %.lr.ph43
  %21 = shl nsw i64 %14, 3
  %22 = add i64 %21, %7
  %23 = sub i64 %8, %22
  %diff.check = icmp ult i64 %23, 32
  br i1 %diff.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %n.vec = and i64 %wide.trip.count, 2147483644
  br label %vector.body

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

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %.preheader, label %scalar.ph.preheader

.lr.ph:                                           ; preds = %6, %.lr.ph
  %.03340 = phi i32 [ %.033, %.lr.ph ], [ %.03339, %6 ]
  %29 = tail call i32 @GetIndex(ptr noundef %2, i32 noundef %4, i32 noundef %.03340)
  %.033 = add nsw i32 %.03340, -1
  %.not = icmp eq i32 %29, 0
  %30 = icmp ne i32 %.03340, 0
  %31 = and i1 %.not, %30
  br i1 %31, label %.lr.ph, label %.preheader38, !llvm.loop !19

.preheader:                                       ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %middle.block, %.preheader38
  %32 = icmp sgt i32 %.033.lcssa, -1
  br i1 %32, label %.lr.ph45, label %._crit_edge

scalar.ph:                                        ; preds = %scalar.ph.prol.loopexit, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %scalar.ph ], [ %indvars.iv.unr, %scalar.ph.prol.loopexit ]
  %33 = getelementptr inbounds i64, ptr %15, i64 %indvars.iv
  %34 = load i64, ptr %33, align 8, !tbaa !12
  %35 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv
  store i64 %34, ptr %35, align 8, !tbaa !12
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %36 = getelementptr inbounds i64, ptr %15, i64 %indvars.iv.next
  %37 = load i64, ptr %36, align 8, !tbaa !12
  %38 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv.next
  store i64 %37, ptr %38, align 8, !tbaa !12
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %39 = getelementptr inbounds i64, ptr %15, i64 %indvars.iv.next.1
  %40 = load i64, ptr %39, align 8, !tbaa !12
  %41 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv.next.1
  store i64 %40, ptr %41, align 8, !tbaa !12
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %42 = getelementptr inbounds i64, ptr %15, i64 %indvars.iv.next.2
  %43 = load i64, ptr %42, align 8, !tbaa !12
  %44 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv.next.2
  store i64 %43, ptr %44, align 8, !tbaa !12
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, %wide.trip.count
  br i1 %exitcond.not.3, label %.preheader, label %scalar.ph, !llvm.loop !20

.lr.ph45:                                         ; preds = %.preheader, %50
  %.144 = phi i32 [ %51, %50 ], [ %.033.lcssa, %.preheader ]
  tail call void @cpMontMul_BNU(ptr noundef %0, ptr noundef %0, ptr noundef %0, ptr noundef %5)
  %45 = tail call i32 @GetIndex(ptr noundef %2, i32 noundef %4, i32 noundef %.144)
  %.not37 = icmp eq i32 %45, 0
  br i1 %.not37, label %50, label %46

46:                                               ; preds = %.lr.ph45
  %47 = mul nsw i32 %45, %10
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds i64, ptr %1, i64 %48
  tail call void @cpMontMul_BNU(ptr noundef %0, ptr noundef %0, ptr noundef %49, ptr noundef %5)
  br label %50

50:                                               ; preds = %.lr.ph45, %46
  %51 = add nsw i32 %.144, -1
  %52 = icmp sgt i32 %.144, 0
  br i1 %52, label %.lr.ph45, label %._crit_edge, !llvm.loop !21

._crit_edge:                                      ; preds = %50, %.preheader
  ret void
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @cpMontMul_BNU(ptr noundef, ptr noundef, 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 = { inlinehint 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" }

!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 16}
!6 = !{!"_gsModEngine", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !11, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !7, i64 88, !10, i64 96, !10, i64 100, !7, i64 104}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long long", !8, i64 0}
!12 = !{!11, !11, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.unroll.disable"}
!15 = distinct !{!15, !16, !17, !18}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!"llvm.loop.isvectorized", i32 1}
!18 = !{!"llvm.loop.unroll.runtime.disable"}
!19 = distinct !{!19, !16}
!20 = distinct !{!20, !16, !17}
!21 = distinct !{!21, !16}


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