; LLVM IR from louk78/crypto_project :: rsa (modular_inverse)

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

; Function Attrs: nounwind uwtable
define dso_local void @modular_inverse(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %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
  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
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %15 = call ptr (...) @bignum_alloc()
  store ptr %15, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %16 = call ptr (...) @bignum_alloc()
  store ptr %16, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %17 = call ptr (...) @bignum_alloc()
  store ptr %17, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %18 = call ptr (...) @bignum_alloc()
  store ptr %18, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %19 = call ptr (...) @bignum_alloc()
  store ptr %19, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %20 = call ptr (...) @bignum_alloc()
  store ptr %20, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %21 = call ptr (...) @bignum_alloc()
  store ptr %21, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  store i32 1, ptr %14, align 4, !tbaa !9
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  call void @bignum_fromint(ptr noundef %22, i32 noundef 1)
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  call void @bignum_copy(ptr noundef %23, ptr noundef %24)
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = load ptr, ptr %10, align 8, !tbaa !5
  call void @bignum_copy(ptr noundef %25, ptr noundef %26)
  br label %27

27:                                               ; preds = %32, %3
  %28 = load ptr, ptr %10, align 8, !tbaa !5
  %29 = call i32 @bignum_iszero(ptr noundef %28)
  %30 = icmp ne i32 %29, 0
  %31 = xor i1 %30, true
  br i1 %31, label %32, label %52

32:                                               ; preds = %27
  %33 = load ptr, ptr %13, align 8, !tbaa !5
  %34 = load ptr, ptr %12, align 8, !tbaa !5
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  %36 = load ptr, ptr %10, align 8, !tbaa !5
  call void @bignum_divide(ptr noundef %33, ptr noundef %34, ptr noundef %35, ptr noundef %36)
  %37 = load ptr, ptr %13, align 8, !tbaa !5
  %38 = load ptr, ptr %9, align 8, !tbaa !5
  call void @bignum_imultiply(ptr noundef %37, ptr noundef %38)
  %39 = load ptr, ptr %11, align 8, !tbaa !5
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = load ptr, ptr %13, align 8, !tbaa !5
  call void @bignum_add(ptr noundef %39, ptr noundef %40, ptr noundef %41)
  %42 = load ptr, ptr %9, align 8, !tbaa !5
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  call void @bignum_copy(ptr noundef %42, ptr noundef %43)
  %44 = load ptr, ptr %11, align 8, !tbaa !5
  %45 = load ptr, ptr %9, align 8, !tbaa !5
  call void @bignum_copy(ptr noundef %44, ptr noundef %45)
  %46 = load ptr, ptr %10, align 8, !tbaa !5
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  call void @bignum_copy(ptr noundef %46, ptr noundef %47)
  %48 = load ptr, ptr %12, align 8, !tbaa !5
  %49 = load ptr, ptr %10, align 8, !tbaa !5
  call void @bignum_copy(ptr noundef %48, ptr noundef %49)
  %50 = load i32, ptr %14, align 4, !tbaa !9
  %51 = sub nsw i32 0, %50
  store i32 %51, ptr %14, align 4, !tbaa !9
  br label %27, !llvm.loop !11

52:                                               ; preds = %27
  %53 = load i32, ptr %14, align 4, !tbaa !9
  %54 = icmp slt i32 %53, 0
  br i1 %54, label %55, label %59

55:                                               ; preds = %52
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  call void @bignum_subtract(ptr noundef %56, ptr noundef %57, ptr noundef %58)
  br label %62

59:                                               ; preds = %52
  %60 = load ptr, ptr %7, align 8, !tbaa !5
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  call void @bignum_copy(ptr noundef %60, ptr noundef %61)
  br label %62

62:                                               ; preds = %59, %55
  %63 = load ptr, ptr %11, align 8, !tbaa !5
  call void @bignum_free(ptr noundef %63)
  %64 = load ptr, ptr %7, align 8, !tbaa !5
  call void @bignum_free(ptr noundef %64)
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  call void @bignum_free(ptr noundef %65)
  %66 = load ptr, ptr %10, align 8, !tbaa !5
  call void @bignum_free(ptr noundef %66)
  %67 = load ptr, ptr %12, align 8, !tbaa !5
  call void @bignum_free(ptr noundef %67)
  %68 = load ptr, ptr %8, align 8, !tbaa !5
  call void @bignum_free(ptr noundef %68)
  %69 = load ptr, ptr %13, align 8, !tbaa !5
  call void @bignum_free(ptr noundef %69)
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret void
}

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

declare ptr @bignum_alloc(...) #2

declare void @bignum_fromint(ptr noundef, i32 noundef) #2

declare void @bignum_copy(ptr noundef, ptr noundef) #2

declare i32 @bignum_iszero(ptr noundef) #2

declare void @bignum_divide(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #2

declare void @bignum_imultiply(ptr noundef, ptr noundef) #2

declare void @bignum_add(ptr noundef, ptr noundef, ptr noundef) #2

declare void @bignum_subtract(ptr noundef, ptr noundef, ptr noundef) #2

declare void @bignum_free(ptr noundef) #2

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

; Function Attrs: nounwind uwtable
define dso_local void @modular_inverse(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = tail call ptr (...) @bignum_alloc() #2
  %5 = tail call ptr (...) @bignum_alloc() #2
  %6 = tail call ptr (...) @bignum_alloc() #2
  %7 = tail call ptr (...) @bignum_alloc() #2
  %8 = tail call ptr (...) @bignum_alloc() #2
  %9 = tail call ptr (...) @bignum_alloc() #2
  %10 = tail call ptr (...) @bignum_alloc() #2
  tail call void @bignum_fromint(ptr noundef %4, i32 noundef 1) #2
  tail call void @bignum_copy(ptr noundef %0, ptr noundef %5) #2
  tail call void @bignum_copy(ptr noundef %1, ptr noundef %7) #2
  %11 = tail call i32 @bignum_iszero(ptr noundef %7) #2
  %.not36 = icmp eq i32 %11, 0
  br i1 %.not36, label %.lr.ph, label %._crit_edge.thread

.lr.ph:                                           ; preds = %3, %.lr.ph
  %.037.neg = phi i32 [ %.037, %.lr.ph ], [ -1, %3 ]
  %.037 = phi i32 [ %.037.neg, %.lr.ph ], [ 1, %3 ]
  tail call void @bignum_divide(ptr noundef %10, ptr noundef %9, ptr noundef %5, ptr noundef %7) #2
  tail call void @bignum_imultiply(ptr noundef %10, ptr noundef %6) #2
  tail call void @bignum_add(ptr noundef %8, ptr noundef %4, ptr noundef %10) #2
  tail call void @bignum_copy(ptr noundef %6, ptr noundef %4) #2
  tail call void @bignum_copy(ptr noundef %8, ptr noundef %6) #2
  tail call void @bignum_copy(ptr noundef %7, ptr noundef %5) #2
  tail call void @bignum_copy(ptr noundef %9, ptr noundef %7) #2
  %12 = tail call i32 @bignum_iszero(ptr noundef %7) #2
  %.not = icmp eq i32 %12, 0
  br i1 %.not, label %.lr.ph, label %._crit_edge, !llvm.loop !5

._crit_edge:                                      ; preds = %.lr.ph
  %13 = icmp slt i32 %.037.neg, 0
  br i1 %13, label %14, label %._crit_edge.thread

14:                                               ; preds = %._crit_edge
  tail call void @bignum_subtract(ptr noundef %2, ptr noundef %1, ptr noundef %4) #2
  br label %15

._crit_edge.thread:                               ; preds = %3, %._crit_edge
  tail call void @bignum_copy(ptr noundef %4, ptr noundef %2) #2
  br label %15

15:                                               ; preds = %._crit_edge.thread, %14
  tail call void @bignum_free(ptr noundef %8) #2
  tail call void @bignum_free(ptr noundef %4) #2
  tail call void @bignum_free(ptr noundef %6) #2
  tail call void @bignum_free(ptr noundef %7) #2
  tail call void @bignum_free(ptr noundef %9) #2
  tail call void @bignum_free(ptr noundef %5) #2
  tail call void @bignum_free(ptr noundef %10) #2
  ret void
}

declare ptr @bignum_alloc(...) local_unnamed_addr #1

declare void @bignum_fromint(ptr noundef, i32 noundef) local_unnamed_addr #1

declare void @bignum_copy(ptr noundef, ptr noundef) local_unnamed_addr #1

declare i32 @bignum_iszero(ptr noundef) local_unnamed_addr #1

declare void @bignum_divide(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare void @bignum_imultiply(ptr noundef, ptr noundef) local_unnamed_addr #1

declare void @bignum_add(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare void @bignum_subtract(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare void @bignum_free(ptr noundef) local_unnamed_addr #1

attributes #0 = { 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 #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 }

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass