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

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

39:                                               ; preds = %70, %3
  %40 = load i32, ptr %11, align 4, !tbaa !12
  %41 = load i32, ptr @_ZL5Msize, align 4, !tbaa !12
  %42 = icmp slt i32 %40, %41
  br i1 %42, label %44, label %43

43:                                               ; preds = %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #6
  br label %73

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

70:                                               ; preds = %44
  %71 = load i32, ptr %11, align 4, !tbaa !12
  %72 = add nsw i32 %71, 1
  store i32 %72, ptr %11, align 4, !tbaa !12
  br label %39, !llvm.loop !14

73:                                               ; preds = %43
  call void @_ZN3Int3SubEPS_S0_(ptr noundef nonnull align 8 dereferenceable(40) %8, ptr noundef %12, ptr noundef @_ZL2_P)
  %74 = call noundef zeroext i1 @_ZN3Int10IsPositiveEv(ptr noundef nonnull align 8 dereferenceable(40) %8)
  br i1 %74, label %75, label %76

75:                                               ; preds = %73
  call void @_ZN3Int3SetEPS_(ptr noundef nonnull align 8 dereferenceable(40) %12, ptr noundef %8)
  br label %76

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

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

; 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 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 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 #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 uwtable
define dso_local void @_ZN3Int14MontgomeryMultEPS_S0_(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #2 align 2 {
  %4 = alloca %class.Int, align 8
  %5 = alloca %class.Int, align 8
  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 %2, 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) %0, ptr noundef nonnull align 8 dereferenceable(32) %11, i64 32, i1 false)
  %12 = getelementptr inbounds i8, ptr %0, 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, %3
  call void @_ZN3Int3SubEPS_S0_(ptr noundef nonnull align 8 dereferenceable(40) %5, ptr noundef nonnull %0, ptr noundef nonnull @_ZL2_P)
  %15 = call noundef zeroext i1 @_ZN3Int10IsPositiveEv(ptr noundef nonnull align 8 dereferenceable(40) %5)
  br i1 %15, label %27, label %28

.lr.ph:                                           ; preds = %3, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 1, %3 ]
  %16 = getelementptr inbounds [5 x i64], ptr %2, 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 %0, 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) %0, ptr noundef nonnull %0, 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

27:                                               ; preds = %._crit_edge
  call void @_ZN3Int3SetEPS_(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef nonnull %5)
  br label %28

28:                                               ; preds = %27, %._crit_edge
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %5) #6
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %4) #6
  ret void
}

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

; 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 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 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 #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,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass