; LLVM IR from openssl/openssl :: crypto/rsa (rsa_mp)

; 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.rsa_st = type { i32, ptr, i32, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, %struct.rsa_pss_params_30_st, ptr, ptr, %struct.crypto_ex_data_st, %struct.CRYPTO_REF_COUNT, i32, ptr, ptr, ptr, ptr, ptr, ptr, i32 }
%struct.rsa_pss_params_30_st = type { i32, %struct.anon, i32, i32 }
%struct.anon = type { i32, i32 }
%struct.crypto_ex_data_st = type { ptr, ptr }
%struct.CRYPTO_REF_COUNT = type { i32 }
%struct.rsa_prime_info_st = type { ptr, 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

declare ptr @BN_secure_new() #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @ossl_rsa_multip_calc_product(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  store ptr null, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  store ptr null, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store ptr null, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.rsa_st, ptr %10, i32 0, i32 15
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %13 = call i32 @sk_RSA_PRIME_INFO_num(ptr noundef %12)
  store i32 %13, ptr %9, align 4, !tbaa !9
  %14 = icmp sle i32 %13, 0
  br i1 %14, label %15, label %16

15:                                               ; preds = %1
  br label %72

16:                                               ; preds = %1
  %17 = call ptr @BN_CTX_new()
  store ptr %17, ptr %6, align 8, !tbaa !5
  %18 = icmp eq ptr %17, null
  br i1 %18, label %19, label %20

19:                                               ; preds = %16
  br label %72

20:                                               ; preds = %16
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.rsa_st, ptr %21, i32 0, i32 8
  %23 = load ptr, ptr %22, align 8, !tbaa !17
  store ptr %23, ptr %4, align 8, !tbaa !5
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.rsa_st, ptr %24, i32 0, i32 9
  %26 = load ptr, ptr %25, align 8, !tbaa !18
  store ptr %26, ptr %5, align 8, !tbaa !5
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %68, %20
  %28 = load i32, ptr %7, align 4, !tbaa !9
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = icmp slt i32 %28, %29
  br i1 %30, label %31, label %71

31:                                               ; preds = %27
  %32 = load ptr, ptr %2, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.rsa_st, ptr %32, i32 0, i32 15
  %34 = load ptr, ptr %33, align 8, !tbaa !11
  %35 = load i32, ptr %7, align 4, !tbaa !9
  %36 = call ptr @sk_RSA_PRIME_INFO_value(ptr noundef %34, i32 noundef %35)
  store ptr %36, ptr %3, align 8, !tbaa !5
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.rsa_prime_info_st, ptr %37, i32 0, i32 3
  %39 = load ptr, ptr %38, align 8, !tbaa !19
  %40 = icmp eq ptr %39, null
  br i1 %40, label %41, label %51

41:                                               ; preds = %31
  %42 = call ptr @BN_secure_new()
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.rsa_prime_info_st, ptr %43, i32 0, i32 3
  store ptr %42, ptr %44, align 8, !tbaa !19
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.rsa_prime_info_st, ptr %45, i32 0, i32 3
  %47 = load ptr, ptr %46, align 8, !tbaa !19
  %48 = icmp eq ptr %47, null
  br i1 %48, label %49, label %50

49:                                               ; preds = %41
  br label %72

50:                                               ; preds = %41
  br label %51

51:                                               ; preds = %50, %31
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.rsa_prime_info_st, ptr %52, i32 0, i32 3
  %54 = load ptr, ptr %53, align 8, !tbaa !19
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = call i32 @BN_mul(ptr noundef %54, ptr noundef %55, ptr noundef %56, ptr noundef %57)
  %59 = icmp ne i32 %58, 0
  br i1 %59, label %61, label %60

60:                                               ; preds = %51
  br label %72

61:                                               ; preds = %51
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.rsa_prime_info_st, ptr %62, i32 0, i32 3
  %64 = load ptr, ptr %63, align 8, !tbaa !19
  store ptr %64, ptr %4, align 8, !tbaa !5
  %65 = load ptr, ptr %3, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.rsa_prime_info_st, ptr %65, i32 0, i32 0
  %67 = load ptr, ptr %66, align 8, !tbaa !21
  store ptr %67, ptr %5, align 8, !tbaa !5
  br label %68

68:                                               ; preds = %61
  %69 = load i32, ptr %7, align 4, !tbaa !9
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %7, align 4, !tbaa !9
  br label %27, !llvm.loop !22

71:                                               ; preds = %27
  store i32 1, ptr %8, align 4, !tbaa !9
  br label %72

72:                                               ; preds = %71, %60, %49, %19, %15
  %73 = load ptr, ptr %6, align 8, !tbaa !5
  call void @BN_CTX_free(ptr noundef %73)
  %74 = load i32, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret i32 %74
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @sk_RSA_PRIME_INFO_num(ptr noundef) #3

declare ptr @BN_CTX_new() #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @sk_RSA_PRIME_INFO_value(ptr noundef, i32 noundef) #3

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

declare void @BN_CTX_free(ptr 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 = { 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 #4 = { 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, !6, i64 136}
!12 = !{!"rsa_st", !10, i64 0, !6, i64 8, !10, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !13, i64 104, !6, i64 128, !6, i64 136, !15, i64 144, !16, i64 160, !10, i64 164, !6, i64 168, !6, i64 176, !6, i64 184, !6, i64 192, !6, i64 200, !6, i64 208, !10, i64 216}
!13 = !{!"rsa_pss_params_30_st", !10, i64 0, !14, i64 4, !10, i64 12, !10, i64 16}
!14 = !{!"", !10, i64 0, !10, i64 4}
!15 = !{!"crypto_ex_data_st", !6, i64 0, !6, i64 8}
!16 = !{!"", !7, i64 0}
!17 = !{!12, !6, i64 64}
!18 = !{!12, !6, i64 72}
!19 = !{!20, !6, i64 24}
!20 = !{!"rsa_prime_info_st", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!21 = !{!20, !6, i64 0}
!22 = distinct !{!22, !23}
!23 = !{!"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 ptr @BN_secure_new() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @ossl_rsa_multip_calc_product(ptr nocapture noundef readonly %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 136
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = tail call i32 @sk_RSA_PRIME_INFO_num(ptr noundef %3)
  %5 = icmp slt i32 %4, 1
  br i1 %5, label %.loopexit, label %6

6:                                                ; preds = %1
  %7 = tail call ptr @BN_CTX_new() #3
  %8 = icmp eq ptr %7, null
  br i1 %8, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %6
  %9 = getelementptr inbounds i8, ptr %0, i64 72
  %10 = getelementptr inbounds i8, ptr %0, i64 64
  br label %.lr.ph

11:                                               ; preds = %21
  %12 = add nuw nsw i32 %.01926, 1
  %exitcond.not = icmp eq i32 %12, %4
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph, !llvm.loop !15

.lr.ph:                                           ; preds = %.lr.ph.preheader, %11
  %.02128.in = phi ptr [ %14, %11 ], [ %9, %.lr.ph.preheader ]
  %.027.in = phi ptr [ %15, %11 ], [ %10, %.lr.ph.preheader ]
  %.01926 = phi i32 [ %12, %11 ], [ 0, %.lr.ph.preheader ]
  %.027 = load ptr, ptr %.027.in, align 8, !tbaa !17
  %.02128 = load ptr, ptr %.02128.in, align 8, !tbaa !17
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = tail call ptr @sk_RSA_PRIME_INFO_value(ptr noundef %13, i32 noundef %.01926)
  %15 = getelementptr inbounds i8, ptr %14, i64 24
  %16 = load ptr, ptr %15, align 8, !tbaa !18
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %21

18:                                               ; preds = %.lr.ph
  %19 = tail call ptr @BN_secure_new() #3
  store ptr %19, ptr %15, align 8, !tbaa !18
  %20 = icmp eq ptr %19, null
  br i1 %20, label %.loopexit, label %21

21:                                               ; preds = %18, %.lr.ph
  %22 = phi ptr [ %19, %18 ], [ %16, %.lr.ph ]
  %23 = tail call i32 @BN_mul(ptr noundef nonnull %22, ptr noundef %.027, ptr noundef %.02128, ptr noundef nonnull %7) #3
  %.not = icmp eq i32 %23, 0
  br i1 %.not, label %.loopexit, label %11

.loopexit:                                        ; preds = %18, %21, %11, %6, %1
  %.020 = phi ptr [ null, %1 ], [ null, %6 ], [ %7, %11 ], [ %7, %21 ], [ %7, %18 ]
  %.018 = phi i32 [ 0, %1 ], [ 0, %6 ], [ 0, %18 ], [ 0, %21 ], [ 1, %11 ]
  tail call void @BN_CTX_free(ptr noundef %.020) #3
  ret i32 %.018
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @sk_RSA_PRIME_INFO_num(ptr noundef) local_unnamed_addr #2

declare ptr @BN_CTX_new() local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @sk_RSA_PRIME_INFO_value(ptr noundef, i32 noundef) local_unnamed_addr #2

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

declare void @BN_CTX_free(ptr 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 = { 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, !10, i64 136}
!6 = !{!"rsa_st", !7, i64 0, !10, i64 8, !7, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !10, i64 72, !10, i64 80, !10, i64 88, !10, i64 96, !11, i64 104, !10, i64 128, !10, i64 136, !13, i64 144, !14, i64 160, !7, i64 164, !10, i64 168, !10, i64 176, !10, i64 184, !10, i64 192, !10, i64 200, !10, i64 208, !7, i64 216}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"rsa_pss_params_30_st", !7, i64 0, !12, i64 4, !7, i64 12, !7, i64 16}
!12 = !{!"", !7, i64 0, !7, i64 4}
!13 = !{!"crypto_ex_data_st", !10, i64 0, !10, i64 8}
!14 = !{!"", !8, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!10, !10, i64 0}
!18 = !{!19, !10, i64 24}
!19 = !{!"rsa_prime_info_st", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}


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