; LLVM IR from specinfo-ua/UAPKI :: library/uapkic/src (math-gfp-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.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 void @wa_free(ptr noundef) #1

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

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

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

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

declare i64 @int_bit_len(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @gfp_mod_dual_pow(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, ptr noundef %5) #2 {
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  %17 = 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 ptr %3, ptr %10, align 8, !tbaa !5
  store ptr %4, ptr %11, align 8, !tbaa !5
  store ptr %5, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  store ptr null, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #3
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.GfpCtx_st, ptr %18, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !9
  %21 = load ptr, ptr %12, align 8, !tbaa !5
  %22 = call i32 @wa_copy(ptr noundef %20, ptr noundef %21)
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.GfpCtx_st, ptr %23, i32 0, i32 1
  %25 = load ptr, ptr %24, align 8, !tbaa !9
  %26 = load ptr, ptr %13, align 8, !tbaa !5
  %27 = call i32 @wa_copy(ptr noundef %25, ptr noundef %26)
  %28 = load ptr, ptr %9, align 8, !tbaa !5
  %29 = call i64 @int_bit_len(ptr noundef %28)
  %30 = trunc i64 %29 to i32
  store i32 %30, ptr %14, align 4, !tbaa !11
  %31 = load ptr, ptr %11, align 8, !tbaa !5
  %32 = call i64 @int_bit_len(ptr noundef %31)
  %33 = trunc i64 %32 to i32
  store i32 %33, ptr %15, align 4, !tbaa !11
  %34 = load i32, ptr %14, align 4, !tbaa !11
  %35 = load i32, ptr %15, align 4, !tbaa !11
  %36 = icmp sgt i32 %34, %35
  br i1 %36, label %37, label %39

37:                                               ; preds = %6
  %38 = load i32, ptr %14, align 4, !tbaa !11
  br label %41

39:                                               ; preds = %6
  %40 = load i32, ptr %15, align 4, !tbaa !11
  br label %41

41:                                               ; preds = %39, %37
  %42 = phi i32 [ %38, %37 ], [ %40, %39 ]
  store i32 %42, ptr %16, align 4, !tbaa !11
  %43 = load i32, ptr %16, align 4, !tbaa !11
  %44 = sub nsw i32 %43, 1
  store i32 %44, ptr %17, align 4, !tbaa !11
  br label %45

45:                                               ; preds = %84, %41
  %46 = load i32, ptr %17, align 4, !tbaa !11
  %47 = icmp sge i32 %46, 0
  br i1 %47, label %48, label %87

48:                                               ; preds = %45
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = load ptr, ptr %12, align 8, !tbaa !5
  %51 = load ptr, ptr %12, align 8, !tbaa !5
  call void @gfp_mod_sqr(ptr noundef %49, ptr noundef %50, ptr noundef %51)
  %52 = load ptr, ptr %9, align 8, !tbaa !5
  %53 = load i32, ptr %17, align 4, !tbaa !11
  %54 = sext i32 %53 to i64
  %55 = call i32 @int_get_bit(ptr noundef %52, i64 noundef %54)
  %56 = icmp ne i32 %55, 0
  br i1 %56, label %57, label %62

57:                                               ; preds = %48
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = load ptr, ptr %8, align 8, !tbaa !5
  %60 = load ptr, ptr %12, align 8, !tbaa !5
  %61 = load ptr, ptr %12, align 8, !tbaa !5
  call void @gfp_mod_mul(ptr noundef %58, ptr noundef %59, ptr noundef %60, ptr noundef %61)
  br label %67

62:                                               ; preds = %48
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = load ptr, ptr %8, align 8, !tbaa !5
  %65 = load ptr, ptr %12, align 8, !tbaa !5
  %66 = load ptr, ptr %13, align 8, !tbaa !5
  call void @gfp_mod_mul(ptr noundef %63, ptr noundef %64, ptr noundef %65, ptr noundef %66)
  br label %67

67:                                               ; preds = %62, %57
  %68 = load ptr, ptr %11, align 8, !tbaa !5
  %69 = load i32, ptr %17, align 4, !tbaa !11
  %70 = sext i32 %69 to i64
  %71 = call i32 @int_get_bit(ptr noundef %68, i64 noundef %70)
  %72 = icmp ne i32 %71, 0
  br i1 %72, label %73, label %78

73:                                               ; preds = %67
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  %75 = load ptr, ptr %10, align 8, !tbaa !5
  %76 = load ptr, ptr %12, align 8, !tbaa !5
  %77 = load ptr, ptr %12, align 8, !tbaa !5
  call void @gfp_mod_mul(ptr noundef %74, ptr noundef %75, ptr noundef %76, ptr noundef %77)
  br label %83

78:                                               ; preds = %67
  %79 = load ptr, ptr %7, align 8, !tbaa !5
  %80 = load ptr, ptr %10, align 8, !tbaa !5
  %81 = load ptr, ptr %12, align 8, !tbaa !5
  %82 = load ptr, ptr %13, align 8, !tbaa !5
  call void @gfp_mod_mul(ptr noundef %79, ptr noundef %80, ptr noundef %81, ptr noundef %82)
  br label %83

83:                                               ; preds = %78, %73
  br label %84

84:                                               ; preds = %83
  %85 = load i32, ptr %17, align 4, !tbaa !11
  %86 = add nsw i32 %85, -1
  store i32 %86, ptr %17, align 4, !tbaa !11
  br label %45, !llvm.loop !13

87:                                               ; preds = %45
  %88 = load ptr, ptr %13, align 8, !tbaa !5
  call void @wa_free(ptr noundef %88)
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #3
  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 8, ptr %13) #3
  ret void
}

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 8}
!10 = !{!"GfpCtx_st", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !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"

declare void @wa_free(ptr noundef) local_unnamed_addr #0

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

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

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

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

declare i64 @int_bit_len(ptr noundef) local_unnamed_addr #0

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

.lr.ph.preheader:                                 ; preds = %6
  %18 = zext nneg i32 %16 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %18, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  tail call void @gfp_mod_sqr(ptr noundef %0, ptr noundef %5, ptr noundef %5)
  %19 = tail call i32 @int_get_bit(ptr noundef %2, i64 noundef %indvars.iv.next) #3
  %.not = icmp eq i32 %19, 0
  %. = select i1 %.not, ptr null, ptr %5
  tail call void @gfp_mod_mul(ptr noundef %0, ptr noundef %1, ptr noundef %5, ptr noundef %.)
  %20 = tail call i32 @int_get_bit(ptr noundef %4, i64 noundef %indvars.iv.next) #3
  %.not36 = icmp eq i32 %20, 0
  %.sink40 = select i1 %.not36, ptr null, ptr %5
  tail call void @gfp_mod_mul(ptr noundef %0, ptr noundef %3, ptr noundef %5, ptr noundef %.sink40)
  %21 = icmp ugt i64 %indvars.iv, 1
  br i1 %21, label %.lr.ph, label %._crit_edge, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph, %6
  tail call void @wa_free(ptr noundef null) #3
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #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, !7, i64 8}
!6 = !{!"GfpCtx_st", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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