; LLVM IR from specinfo-ua/UAPKI :: library/uapkic/src (math-ecp-internal)

; 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.ECPoint_st = type { ptr, ptr, ptr }
%struct.EcGfpCtx_st = type { ptr, ptr, ptr, i8, i64 }
%struct.GfpCtx_st = type { ptr, ptr, ptr, ptr }

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

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

declare i64 @int_bit_len(ptr noundef) #1

declare i32 @wa_copy(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @ecp_add_point(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i32 noundef, ptr noundef) #2

declare void @wa_zero(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @ecp_double_point(ptr noundef, ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @ecp_point_to_affine(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @ecp_mul(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) #2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %11 = load ptr, ptr %8, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.ECPoint_st, ptr %11, i32 0, i32 0
  %13 = load ptr, ptr %12, align 8, !tbaa !9
  call void @wa_zero(ptr noundef %13)
  %14 = load ptr, ptr %8, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.ECPoint_st, ptr %14, i32 0, i32 1
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  call void @wa_zero(ptr noundef %16)
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.EcGfpCtx_st, ptr %17, i32 0, i32 0
  %19 = load ptr, ptr %18, align 8, !tbaa !12
  %20 = getelementptr inbounds %struct.GfpCtx_st, ptr %19, i32 0, i32 1
  %21 = load ptr, ptr %20, align 8, !tbaa !16
  %22 = load ptr, ptr %8, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.ECPoint_st, ptr %22, i32 0, i32 2
  %24 = load ptr, ptr %23, align 8, !tbaa !18
  %25 = call i32 @wa_copy(ptr noundef %21, ptr noundef %24)
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  %27 = call i64 @int_bit_len(ptr noundef %26)
  %28 = trunc i64 %27 to i32
  store i32 %28, ptr %9, align 4, !tbaa !19
  %29 = load i32, ptr %9, align 4, !tbaa !19
  %30 = sub nsw i32 %29, 1
  store i32 %30, ptr %10, align 4, !tbaa !19
  br label %31

31:                                               ; preds = %54, %4
  %32 = load i32, ptr %10, align 4, !tbaa !19
  %33 = icmp sge i32 %32, 0
  br i1 %33, label %34, label %57

34:                                               ; preds = %31
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = load ptr, ptr %8, align 8, !tbaa !5
  call void @ecp_double_point(ptr noundef %35, ptr noundef %36, ptr noundef %37)
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = load i32, ptr %10, align 4, !tbaa !19
  %40 = sext i32 %39 to i64
  %41 = call i32 @int_get_bit(ptr noundef %38, i64 noundef %40)
  %42 = icmp ne i32 %41, 0
  br i1 %42, label %43, label %53

43:                                               ; preds = %34
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.ECPoint_st, ptr %46, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !9
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.ECPoint_st, ptr %49, i32 0, i32 1
  %51 = load ptr, ptr %50, align 8, !tbaa !11
  %52 = load ptr, ptr %8, align 8, !tbaa !5
  call void @ecp_add_point(ptr noundef %44, ptr noundef %45, ptr noundef %48, ptr noundef %51, i32 noundef 1, ptr noundef %52)
  br label %53

53:                                               ; preds = %43, %34
  br label %54

54:                                               ; preds = %53
  %55 = load i32, ptr %10, align 4, !tbaa !19
  %56 = add nsw i32 %55, -1
  store i32 %56, ptr %10, align 4, !tbaa !19
  br label %31, !llvm.loop !21

57:                                               ; preds = %31
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = load ptr, ptr %8, align 8, !tbaa !5
  call void @ecp_point_to_affine(ptr noundef %58, ptr noundef %59)
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  ret void
}

declare i32 @int_get_bit(ptr noundef, i64 noundef) #1

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, !6, i64 0}
!10 = !{!"ECPoint_st", !6, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!10, !6, i64 8}
!12 = !{!13, !6, i64 0}
!13 = !{!"EcGfpCtx_st", !6, i64 0, !6, i64 8, !6, i64 16, !14, i64 24, !15, i64 32}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{!"long", !7, i64 0}
!16 = !{!17, !6, i64 8}
!17 = !{!"GfpCtx_st", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!18 = !{!10, !6, i64 16}
!19 = !{!20, !20, i64 0}
!20 = !{!"int", !7, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"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"

declare i64 @int_bit_len(ptr noundef) local_unnamed_addr #0

declare i32 @wa_copy(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @ecp_add_point(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

declare void @wa_zero(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @ecp_double_point(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @ecp_point_to_affine(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @ecp_mul(ptr noundef %0, ptr nocapture noundef readonly %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #1 {
  %5 = load ptr, ptr %3, align 8, !tbaa !5
  tail call void @wa_zero(ptr noundef %5) #2
  %6 = getelementptr inbounds i8, ptr %3, i64 8
  %7 = load ptr, ptr %6, align 8, !tbaa !10
  tail call void @wa_zero(ptr noundef %7) #2
  %8 = load ptr, ptr %0, align 8, !tbaa !11
  %9 = getelementptr inbounds i8, ptr %8, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !15
  %11 = getelementptr inbounds i8, ptr %3, i64 16
  %12 = load ptr, ptr %11, align 8, !tbaa !17
  %13 = tail call i32 @wa_copy(ptr noundef %10, ptr noundef %12) #2
  %14 = tail call i64 @int_bit_len(ptr noundef %2) #2
  %15 = trunc i64 %14 to i32
  %16 = icmp sgt i32 %15, 0
  br i1 %16, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %4
  %17 = getelementptr inbounds i8, ptr %1, i64 8
  %18 = and i64 %14, 2147483647
  br label %19

19:                                               ; preds = %.lr.ph, %24
  %indvars.iv = phi i64 [ %18, %.lr.ph ], [ %indvars.iv.next, %24 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  tail call void @ecp_double_point(ptr noundef nonnull %0, ptr noundef nonnull %3, ptr noundef nonnull %3)
  %20 = tail call i32 @int_get_bit(ptr noundef %2, i64 noundef %indvars.iv.next) #2
  %.not = icmp eq i32 %20, 0
  br i1 %.not, label %24, label %21

21:                                               ; preds = %19
  %22 = load ptr, ptr %1, align 8, !tbaa !5
  %23 = load ptr, ptr %17, align 8, !tbaa !10
  tail call void @ecp_add_point(ptr noundef nonnull %0, ptr noundef nonnull %3, ptr noundef %22, ptr noundef %23, i32 noundef 1, ptr noundef nonnull %3)
  br label %24

24:                                               ; preds = %19, %21
  %25 = icmp ugt i64 %indvars.iv, 1
  br i1 %25, label %19, label %._crit_edge, !llvm.loop !18

._crit_edge:                                      ; preds = %24, %4
  tail call void @ecp_point_to_affine(ptr noundef nonnull %0, ptr noundef nonnull %3)
  ret void
}

declare i32 @int_get_bit(ptr noundef, i64 noundef) local_unnamed_addr #0

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 = { 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, !7, i64 0}
!6 = !{!"ECPoint_st", !7, i64 0, !7, i64 8, !7, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = !{!12, !7, i64 0}
!12 = !{!"EcGfpCtx_st", !7, i64 0, !7, i64 8, !7, i64 16, !13, i64 24, !14, i64 32}
!13 = !{!"_Bool", !8, i64 0}
!14 = !{!"long", !8, i64 0}
!15 = !{!16, !7, i64 8}
!16 = !{!"GfpCtx_st", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24}
!17 = !{!6, !7, i64 16}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass