; LLVM IR from tleonhardt/practical_cryptography_engineering :: mbedtls (bignum)

; 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.mbedtls_mpi = type { i32, i64, ptr }

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_mpi_init(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_mpi_free(ptr noundef) #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_mpi_copy(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_mpi_cmp_abs(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_mpi_sub_abs(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca %struct.mbedtls_mpi, align 8
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = 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
  call void @llvm.lifetime.start.p0(i64 24, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = load ptr, ptr %7, align 8, !tbaa !5
  %14 = call i32 @mbedtls_mpi_cmp_abs(ptr noundef %12, ptr noundef %13)
  %15 = icmp slt i32 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %3
  store i32 -10, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %77

17:                                               ; preds = %3
  call void @mbedtls_mpi_init(ptr noundef %8)
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = icmp eq ptr %18, %19
  br i1 %20, label %21, label %30

21:                                               ; preds = %17
  br label %22

22:                                               ; preds = %21
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = call i32 @mbedtls_mpi_copy(ptr noundef %8, ptr noundef %23)
  store i32 %24, ptr %9, align 4, !tbaa !9
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %27

26:                                               ; preds = %22
  br label %75

27:                                               ; preds = %22
  br label %28

28:                                               ; preds = %27
  br label %29

29:                                               ; preds = %28
  store ptr %8, ptr %7, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %29, %17
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = icmp ne ptr %31, %32
  br i1 %33, label %34, label %44

34:                                               ; preds = %30
  br label %35

35:                                               ; preds = %34
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = call i32 @mbedtls_mpi_copy(ptr noundef %36, ptr noundef %37)
  store i32 %38, ptr %9, align 4, !tbaa !9
  %39 = icmp ne i32 %38, 0
  br i1 %39, label %40, label %41

40:                                               ; preds = %35
  br label %75

41:                                               ; preds = %35
  br label %42

42:                                               ; preds = %41
  br label %43

43:                                               ; preds = %42
  br label %44

44:                                               ; preds = %43, %30
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.mbedtls_mpi, ptr %45, i32 0, i32 0
  store i32 1, ptr %46, align 8, !tbaa !11
  store i32 0, ptr %9, align 4, !tbaa !9
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.mbedtls_mpi, ptr %47, i32 0, i32 1
  %49 = load i64, ptr %48, align 8, !tbaa !14
  store i64 %49, ptr %10, align 8, !tbaa !15
  br label %50

50:                                               ; preds = %64, %44
  %51 = load i64, ptr %10, align 8, !tbaa !15
  %52 = icmp ugt i64 %51, 0
  br i1 %52, label %53, label %67

53:                                               ; preds = %50
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.mbedtls_mpi, ptr %54, i32 0, i32 2
  %56 = load ptr, ptr %55, align 8, !tbaa !16
  %57 = load i64, ptr %10, align 8, !tbaa !15
  %58 = sub i64 %57, 1
  %59 = getelementptr inbounds i64, ptr %56, i64 %58
  %60 = load i64, ptr %59, align 8, !tbaa !15
  %61 = icmp ne i64 %60, 0
  br i1 %61, label %62, label %63

62:                                               ; preds = %53
  br label %67

63:                                               ; preds = %53
  br label %64

64:                                               ; preds = %63
  %65 = load i64, ptr %10, align 8, !tbaa !15
  %66 = add i64 %65, -1
  store i64 %66, ptr %10, align 8, !tbaa !15
  br label %50, !llvm.loop !17

67:                                               ; preds = %62, %50
  %68 = load i64, ptr %10, align 8, !tbaa !15
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.mbedtls_mpi, ptr %69, i32 0, i32 2
  %71 = load ptr, ptr %70, align 8, !tbaa !16
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.mbedtls_mpi, ptr %72, i32 0, i32 2
  %74 = load ptr, ptr %73, align 8, !tbaa !16
  call void @mpi_sub_hlp(i64 noundef %68, ptr noundef %71, ptr noundef %74)
  br label %75

75:                                               ; preds = %67, %40, %26
  call void @mbedtls_mpi_free(ptr noundef %8)
  %76 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %76, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %77

77:                                               ; preds = %75, %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 24, ptr %8) #2
  %78 = load i32, ptr %4, align 4
  ret i32 %78
}

; Function Attrs: nounwind uwtable
declare hidden void @mpi_sub_hlp(i64 noundef, ptr noundef, ptr noundef) #0

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 = { 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, !10, i64 0}
!12 = !{!"", !10, i64 0, !13, i64 8, !6, i64 16}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !13, i64 8}
!15 = !{!13, !13, i64 0}
!16 = !{!12, !6, i64 16}
!17 = distinct !{!17, !18}
!18 = !{!"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"

%struct.mbedtls_mpi = type { i32, i64, ptr }

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_mpi_init(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_mpi_free(ptr noundef) local_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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_mpi_copy(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_mpi_cmp_abs(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_mpi_sub_abs(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = alloca %struct.mbedtls_mpi, align 8
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %4) #2
  %5 = tail call i32 @mbedtls_mpi_cmp_abs(ptr noundef %1, ptr noundef %2)
  %6 = icmp slt i32 %5, 0
  %.pn.sroa.gep39 = getelementptr inbounds i8, ptr %4, i64 8
  %.pn.sroa.gep42 = getelementptr inbounds i8, ptr %4, i64 16
  br i1 %6, label %23, label %7

7:                                                ; preds = %3
  %.pn.sroa.gep41 = getelementptr inbounds i8, ptr %2, i64 16
  %.pn.sroa.gep = getelementptr inbounds i8, ptr %2, i64 8
  call void @mbedtls_mpi_init(ptr noundef nonnull %4)
  %8 = icmp eq ptr %0, %2
  br i1 %8, label %9, label %11

9:                                                ; preds = %7
  %10 = call i32 @mbedtls_mpi_copy(ptr noundef nonnull %4, ptr noundef %2)
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %11, label %22

11:                                               ; preds = %9, %7
  %.pn.sroa.phi = phi ptr [ %.pn.sroa.gep, %7 ], [ %.pn.sroa.gep39, %9 ]
  %.pn.sroa.phi40 = phi ptr [ %.pn.sroa.gep41, %7 ], [ %.pn.sroa.gep42, %9 ]
  %.not35 = icmp eq ptr %0, %1
  br i1 %.not35, label %14, label %12

12:                                               ; preds = %11
  %13 = call i32 @mbedtls_mpi_copy(ptr noundef %0, ptr noundef %1)
  %.not36 = icmp eq i32 %13, 0
  br i1 %.not36, label %14, label %22

14:                                               ; preds = %12, %11
  store i32 1, ptr %0, align 8, !tbaa !5
  %15 = load i64, ptr %.pn.sroa.phi, align 8, !tbaa !12
  %.not3743 = icmp eq i64 %15, 0
  %.pre = load ptr, ptr %.pn.sroa.phi40, align 8, !tbaa !13
  br i1 %.not3743, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %14
  %invariant.gep = getelementptr i8, ptr %.pre, i64 -8
  br label %16

16:                                               ; preds = %.lr.ph, %18
  %.044 = phi i64 [ %15, %.lr.ph ], [ %19, %18 ]
  %gep = getelementptr i64, ptr %invariant.gep, i64 %.044
  %17 = load i64, ptr %gep, align 8, !tbaa !14
  %.not38 = icmp eq i64 %17, 0
  br i1 %.not38, label %18, label %._crit_edge

18:                                               ; preds = %16
  %19 = add i64 %.044, -1
  %.not37 = icmp eq i64 %19, 0
  br i1 %.not37, label %._crit_edge, label %16, !llvm.loop !15

._crit_edge:                                      ; preds = %18, %16, %14
  %.0.lcssa = phi i64 [ 0, %14 ], [ %.044, %16 ], [ 0, %18 ]
  %20 = getelementptr inbounds i8, ptr %0, i64 16
  %21 = load ptr, ptr %20, align 8, !tbaa !13
  call void @mpi_sub_hlp(i64 noundef %.0.lcssa, ptr noundef %.pre, ptr noundef %21)
  br label %22

22:                                               ; preds = %12, %9, %._crit_edge
  %.019 = phi i32 [ %10, %9 ], [ %13, %12 ], [ 0, %._crit_edge ]
  call void @mbedtls_mpi_free(ptr noundef nonnull %4)
  br label %23

23:                                               ; preds = %3, %22
  %.021 = phi i32 [ %.019, %22 ], [ -10, %3 ]
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %4) #2
  ret i32 %.021
}

; Function Attrs: nounwind uwtable
declare hidden void @mpi_sub_hlp(i64 noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"", !7, i64 0, !10, i64 8, !11, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !10, i64 8}
!13 = !{!6, !11, i64 16}
!14 = !{!10, !10, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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