; LLVM IR from albertobsd/keyhunt :: secp256k1/IntMod.ll (ZN3Int14MontgomeryMultEPS_)

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

%class.Int = type { %union.anon }
%union.anon = type { [5 x i64] }

@_ZL2_P = external hidden global %class.Int, align 8
@_ZL4MM64 = external hidden global i64, align 8
@_ZL5Msize = external hidden global i32, align 4

declare void @_ZN3IntC1Ev(ptr noundef nonnull align 8 dereferenceable(40)) unnamed_addr #0

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

declare void @_ZN3Int3SubEPS_S0_(ptr noundef nonnull align 8 dereferenceable(40), ptr noundef, ptr noundef) #0

declare noundef zeroext i1 @_ZN3Int10IsPositiveEv(ptr noundef nonnull align 8 dereferenceable(40)) #0

declare void @_ZN3Int3SetEPS_(ptr noundef nonnull align 8 dereferenceable(40), ptr 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: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZN3Int4AddCEPS_(ptr noundef nonnull align 8 dereferenceable(40), ptr noundef) #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN3Int11AddAndShiftEPS_S0_m(ptr noundef nonnull align 8 dereferenceable(40), ptr noundef, ptr noundef, i64 noundef) #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN3Int14MontgomeryMultEPS_(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %1) #3 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca %class.Int, align 8
  %6 = alloca %class.Int, align 8
  %7 = alloca %class.Int, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %11 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 40, ptr %5) #6
  call void @_ZN3IntC1Ev(ptr noundef nonnull align 8 dereferenceable(40) %5)
  call void @llvm.lifetime.start.p0(i64 40, ptr %6) #6
  call void @_ZN3IntC1Ev(ptr noundef nonnull align 8 dereferenceable(40) %6)
  call void @llvm.lifetime.start.p0(i64 40, ptr %7) #6
  call void @_ZN3IntC1Ev(ptr noundef nonnull align 8 dereferenceable(40) %7)
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #6
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds %class.Int, ptr %12, i32 0, i32 0
  %14 = getelementptr inbounds [5 x i64], ptr %13, i64 0, i64 0
  %15 = getelementptr inbounds %class.Int, ptr %11, i32 0, i32 0
  %16 = getelementptr inbounds [5 x i64], ptr %15, i64 0, i64 0
  %17 = load i64, ptr %16, align 8, !tbaa !9
  %18 = getelementptr inbounds %class.Int, ptr %6, i32 0, i32 0
  %19 = getelementptr inbounds [5 x i64], ptr %18, i64 0, i64 0
  call void @_ZL8imm_umulPmmS_(ptr noundef %14, i64 noundef %17, ptr noundef %19)
  %20 = getelementptr inbounds %class.Int, ptr %6, i32 0, i32 0
  %21 = getelementptr inbounds [5 x i64], ptr %20, i64 0, i64 0
  %22 = load i64, ptr %21, align 8, !tbaa !9
  %23 = load i64, ptr @_ZL4MM64, align 8, !tbaa !10
  %24 = mul i64 %22, %23
  store i64 %24, ptr %8, align 8, !tbaa !10
  %25 = load i64, ptr %8, align 8, !tbaa !10
  %26 = getelementptr inbounds %class.Int, ptr %7, i32 0, i32 0
  %27 = getelementptr inbounds [5 x i64], ptr %26, i64 0, i64 0
  call void @_ZL8imm_umulPmmS_(ptr noundef @_ZL2_P, i64 noundef %25, ptr noundef %27)
  %28 = call noundef i64 @_ZN3Int4AddCEPS_(ptr noundef nonnull align 8 dereferenceable(40) %6, ptr noundef %7)
  store i64 %28, ptr %9, align 8, !tbaa !10
  %29 = getelementptr inbounds %class.Int, ptr %5, i32 0, i32 0
  %30 = getelementptr inbounds [5 x i64], ptr %29, i64 0, i64 0
  %31 = getelementptr inbounds %class.Int, ptr %6, i32 0, i32 0
  %32 = getelementptr inbounds [5 x i64], ptr %31, i64 0, i64 0
  %33 = getelementptr inbounds i64, ptr %32, i64 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %30, ptr align 8 %33, i64 32, i1 false)
  %34 = load i64, ptr %9, align 8, !tbaa !10
  %35 = getelementptr inbounds %class.Int, ptr %5, i32 0, i32 0
  %36 = getelementptr inbounds [5 x i64], ptr %35, i64 0, i64 4
  store i64 %34, ptr %36, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #6
  store i32 1, ptr %10, align 4, !tbaa !12
  br label %37

37:                                               ; preds = %67, %2
  %38 = load i32, ptr %10, align 4, !tbaa !12
  %39 = load i32, ptr @_ZL5Msize, align 4, !tbaa !12
  %40 = icmp slt i32 %38, %39
  br i1 %40, label %42, label %41

41:                                               ; preds = %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #6
  br label %70

42:                                               ; preds = %37
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %class.Int, ptr %43, i32 0, i32 0
  %45 = getelementptr inbounds [5 x i64], ptr %44, i64 0, i64 0
  %46 = getelementptr inbounds %class.Int, ptr %11, i32 0, i32 0
  %47 = load i32, ptr %10, align 4, !tbaa !12
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [5 x i64], ptr %46, i64 0, i64 %48
  %50 = load i64, ptr %49, align 8, !tbaa !9
  %51 = getelementptr inbounds %class.Int, ptr %6, i32 0, i32 0
  %52 = getelementptr inbounds [5 x i64], ptr %51, i64 0, i64 0
  call void @_ZL8imm_umulPmmS_(ptr noundef %45, i64 noundef %50, ptr noundef %52)
  %53 = getelementptr inbounds %class.Int, ptr %6, i32 0, i32 0
  %54 = getelementptr inbounds [5 x i64], ptr %53, i64 0, i64 0
  %55 = load i64, ptr %54, align 8, !tbaa !9
  %56 = getelementptr inbounds %class.Int, ptr %5, i32 0, i32 0
  %57 = getelementptr inbounds [5 x i64], ptr %56, i64 0, i64 0
  %58 = load i64, ptr %57, align 8, !tbaa !9
  %59 = add i64 %55, %58
  %60 = load i64, ptr @_ZL4MM64, align 8, !tbaa !10
  %61 = mul i64 %59, %60
  store i64 %61, ptr %8, align 8, !tbaa !10
  %62 = load i64, ptr %8, align 8, !tbaa !10
  %63 = getelementptr inbounds %class.Int, ptr %7, i32 0, i32 0
  %64 = getelementptr inbounds [5 x i64], ptr %63, i64 0, i64 0
  call void @_ZL8imm_umulPmmS_(ptr noundef @_ZL2_P, i64 noundef %62, ptr noundef %64)
  %65 = call noundef i64 @_ZN3Int4AddCEPS_(ptr noundef nonnull align 8 dereferenceable(40) %6, ptr noundef %7)
  store i64 %65, ptr %9, align 8, !tbaa !10
  %66 = load i64, ptr %9, align 8, !tbaa !10
  call void @_ZN3Int11AddAndShiftEPS_S0_m(ptr noundef nonnull align 8 dereferenceable(40) %5, ptr noundef %5, ptr noundef %6, i64 noundef %66)
  br label %67

67:                                               ; preds = %42
  %68 = load i32, ptr %10, align 4, !tbaa !12
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %10, align 4, !tbaa !12
  br label %37, !llvm.loop !14

70:                                               ; preds = %41
  call void @_ZN3Int3SubEPS_S0_(ptr noundef nonnull align 8 dereferenceable(40) %7, ptr noundef %5, ptr noundef @_ZL2_P)
  %71 = call noundef zeroext i1 @_ZN3Int10IsPositiveEv(ptr noundef nonnull align 8 dereferenceable(40) %7)
  br i1 %71, label %72, label %73

72:                                               ; preds = %70
  call void @_ZN3Int3SetEPS_(ptr noundef nonnull align 8 dereferenceable(40) %11, ptr noundef %7)
  br label %74

73:                                               ; preds = %70
  call void @_ZN3Int3SetEPS_(ptr noundef nonnull align 8 dereferenceable(40) %11, ptr noundef %5)
  br label %74

74:                                               ; preds = %73, %72
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 40, ptr %7) #6
  call void @llvm.lifetime.end.p0(i64 40, ptr %6) #6
  call void @llvm.lifetime.end.p0(i64 40, ptr %5) #6
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare hidden void @_ZL8imm_umulPmmS_(ptr noundef, i64 noundef, ptr noundef) #4

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #5

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress 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 = { mustprogress 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 #4 = { inlinehint mustprogress 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 #5 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #6 = { 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++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !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"

%class.Int = type { %union.anon }
%union.anon = type { [5 x i64] }

@_ZL2_P = external hidden global %class.Int, align 8
@_ZL4MM64 = external hidden local_unnamed_addr global i64, align 8
@_ZL5Msize = external hidden local_unnamed_addr global i32, align 4

declare void @_ZN3IntC1Ev(ptr noundef nonnull align 8 dereferenceable(40)) unnamed_addr #0

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

declare void @_ZN3Int3SubEPS_S0_(ptr noundef nonnull align 8 dereferenceable(40), ptr noundef, ptr noundef) local_unnamed_addr #0

declare noundef zeroext i1 @_ZN3Int10IsPositiveEv(ptr noundef nonnull align 8 dereferenceable(40)) local_unnamed_addr #0

declare void @_ZN3Int3SetEPS_(ptr noundef nonnull align 8 dereferenceable(40), ptr noundef) local_unnamed_addr #0

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZN3Int4AddCEPS_(ptr noundef nonnull align 8 dereferenceable(40), ptr noundef) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN3Int11AddAndShiftEPS_S0_m(ptr noundef nonnull align 8 dereferenceable(40), ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN3Int14MontgomeryMultEPS_(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %1) local_unnamed_addr #3 align 2 {
  %3 = alloca %class.Int, align 8
  %4 = alloca %class.Int, align 8
  %5 = alloca %class.Int, align 8
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %3) #6
  call void @_ZN3IntC1Ev(ptr noundef nonnull align 8 dereferenceable(40) %3)
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %4) #6
  call void @_ZN3IntC1Ev(ptr noundef nonnull align 8 dereferenceable(40) %4)
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %5) #6
  call void @_ZN3IntC1Ev(ptr noundef nonnull align 8 dereferenceable(40) %5)
  %6 = load i64, ptr %0, align 8, !tbaa !5
  call void @_ZL8imm_umulPmmS_(ptr noundef %1, i64 noundef %6, ptr noundef nonnull %4)
  %7 = load i64, ptr %4, align 8, !tbaa !5
  %8 = load i64, ptr @_ZL4MM64, align 8, !tbaa !8
  %9 = mul i64 %8, %7
  call void @_ZL8imm_umulPmmS_(ptr noundef nonnull @_ZL2_P, i64 noundef %9, ptr noundef nonnull %5)
  %10 = call noundef i64 @_ZN3Int4AddCEPS_(ptr noundef nonnull align 8 dereferenceable(40) %4, ptr noundef nonnull %5)
  %11 = getelementptr inbounds i8, ptr %4, i64 8
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %3, ptr noundef nonnull align 8 dereferenceable(32) %11, i64 32, i1 false)
  %12 = getelementptr inbounds i8, ptr %3, i64 32
  store i64 %10, ptr %12, align 8, !tbaa !5
  %13 = load i32, ptr @_ZL5Msize, align 4, !tbaa !10
  %14 = icmp sgt i32 %13, 1
  br i1 %14, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %2
  call void @_ZN3Int3SubEPS_S0_(ptr noundef nonnull align 8 dereferenceable(40) %5, ptr noundef nonnull %3, ptr noundef nonnull @_ZL2_P)
  %15 = call noundef zeroext i1 @_ZN3Int10IsPositiveEv(ptr noundef nonnull align 8 dereferenceable(40) %5)
  %. = select i1 %15, ptr %5, ptr %3
  call void @_ZN3Int3SetEPS_(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef nonnull %.)
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %5) #6
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %4) #6
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %3) #6
  ret void

.lr.ph:                                           ; preds = %2, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 1, %2 ]
  %16 = getelementptr inbounds [5 x i64], ptr %0, i64 0, i64 %indvars.iv
  %17 = load i64, ptr %16, align 8, !tbaa !5
  call void @_ZL8imm_umulPmmS_(ptr noundef %1, i64 noundef %17, ptr noundef nonnull %4)
  %18 = load i64, ptr %4, align 8, !tbaa !5
  %19 = load i64, ptr %3, align 8, !tbaa !5
  %20 = add i64 %19, %18
  %21 = load i64, ptr @_ZL4MM64, align 8, !tbaa !8
  %22 = mul i64 %20, %21
  call void @_ZL8imm_umulPmmS_(ptr noundef nonnull @_ZL2_P, i64 noundef %22, ptr noundef nonnull %5)
  %23 = call noundef i64 @_ZN3Int4AddCEPS_(ptr noundef nonnull align 8 dereferenceable(40) %4, ptr noundef nonnull %5)
  call void @_ZN3Int11AddAndShiftEPS_S0_m(ptr noundef nonnull align 8 dereferenceable(40) %3, ptr noundef nonnull %3, ptr noundef nonnull %4, i64 noundef %23)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %24 = load i32, ptr @_ZL5Msize, align 4, !tbaa !10
  %25 = sext i32 %24 to i64
  %26 = icmp slt i64 %indvars.iv.next, %25
  br i1 %26, label %.lr.ph, label %._crit_edge, !llvm.loop !12
}

; Function Attrs: inlinehint mustprogress uwtable
declare hidden void @_ZL8imm_umulPmmS_(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #5

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress 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 = { mustprogress 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 #4 = { inlinehint mustprogress 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 #5 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #6 = { 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++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"long", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !6, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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