; 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 hidden void @mbedtls_mpi_zeroize(ptr noundef, i64 noundef) #0

; Function Attrs: nounwind
declare void @free(ptr noundef) #1

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

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

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #3

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #4

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

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

14:                                               ; preds = %2
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = load i64, ptr %5, align 8, !tbaa !9
  %17 = call i32 @mbedtls_mpi_grow(ptr noundef %15, i64 noundef %16)
  store i32 %17, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %80

18:                                               ; preds = %2
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.mbedtls_mpi, ptr %19, i32 0, i32 1
  %21 = load i64, ptr %20, align 8, !tbaa !11
  %22 = sub i64 %21, 1
  store i64 %22, ptr %7, align 8, !tbaa !9
  br label %23

23:                                               ; preds = %36, %18
  %24 = load i64, ptr %7, align 8, !tbaa !9
  %25 = icmp ugt i64 %24, 0
  br i1 %25, label %26, label %39

26:                                               ; preds = %23
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.mbedtls_mpi, ptr %27, i32 0, i32 2
  %29 = load ptr, ptr %28, align 8, !tbaa !14
  %30 = load i64, ptr %7, align 8, !tbaa !9
  %31 = getelementptr inbounds i64, ptr %29, i64 %30
  %32 = load i64, ptr %31, align 8, !tbaa !9
  %33 = icmp ne i64 %32, 0
  br i1 %33, label %34, label %35

34:                                               ; preds = %26
  br label %39

35:                                               ; preds = %26
  br label %36

36:                                               ; preds = %35
  %37 = load i64, ptr %7, align 8, !tbaa !9
  %38 = add i64 %37, -1
  store i64 %38, ptr %7, align 8, !tbaa !9
  br label %23, !llvm.loop !15

39:                                               ; preds = %34, %23
  %40 = load i64, ptr %7, align 8, !tbaa !9
  %41 = add i64 %40, 1
  store i64 %41, ptr %7, align 8, !tbaa !9
  %42 = load i64, ptr %7, align 8, !tbaa !9
  %43 = load i64, ptr %5, align 8, !tbaa !9
  %44 = icmp ult i64 %42, %43
  br i1 %44, label %45, label %47

45:                                               ; preds = %39
  %46 = load i64, ptr %5, align 8, !tbaa !9
  store i64 %46, ptr %7, align 8, !tbaa !9
  br label %47

47:                                               ; preds = %45, %39
  %48 = load i64, ptr %7, align 8, !tbaa !9
  %49 = call noalias ptr @calloc(i64 noundef %48, i64 noundef 8) #6
  store ptr %49, ptr %6, align 8, !tbaa !5
  %50 = icmp eq ptr %49, null
  br i1 %50, label %51, label %52

51:                                               ; preds = %47
  store i32 -16, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %80

52:                                               ; preds = %47
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.mbedtls_mpi, ptr %53, i32 0, i32 2
  %55 = load ptr, ptr %54, align 8, !tbaa !14
  %56 = icmp ne ptr %55, null
  br i1 %56, label %57, label %73

57:                                               ; preds = %52
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.mbedtls_mpi, ptr %59, i32 0, i32 2
  %61 = load ptr, ptr %60, align 8, !tbaa !14
  %62 = load i64, ptr %7, align 8, !tbaa !9
  %63 = mul i64 %62, 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %58, ptr align 8 %61, i64 %63, i1 false)
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.mbedtls_mpi, ptr %64, i32 0, i32 2
  %66 = load ptr, ptr %65, align 8, !tbaa !14
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.mbedtls_mpi, ptr %67, i32 0, i32 1
  %69 = load i64, ptr %68, align 8, !tbaa !11
  call void @mbedtls_mpi_zeroize(ptr noundef %66, i64 noundef %69)
  %70 = load ptr, ptr %4, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.mbedtls_mpi, ptr %70, i32 0, i32 2
  %72 = load ptr, ptr %71, align 8, !tbaa !14
  call void @free(ptr noundef %72) #5
  br label %73

73:                                               ; preds = %57, %52
  %74 = load i64, ptr %7, align 8, !tbaa !9
  %75 = load ptr, ptr %4, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.mbedtls_mpi, ptr %75, i32 0, i32 1
  store i64 %74, ptr %76, align 8, !tbaa !11
  %77 = load ptr, ptr %6, align 8, !tbaa !5
  %78 = load ptr, ptr %4, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.mbedtls_mpi, ptr %78, i32 0, i32 2
  store ptr %77, ptr %79, align 8, !tbaa !14
  store i32 0, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %80

80:                                               ; preds = %73, %51, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  %81 = load i32, ptr %3, align 4
  ret i32 %81
}

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 = { nounwind "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 willreturn memory(argmem: readwrite) }
attributes #3 = { nounwind allocsize(0,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 #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { nounwind }
attributes #6 = { nounwind allocsize(0,1) }

!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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"", !13, i64 0, !10, i64 8, !6, i64 16}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !6, i64 16}
!15 = distinct !{!15, !16}
!16 = !{!"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
declare hidden void @mbedtls_mpi_zeroize(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #1

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

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @calloc(i64 noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_mpi_shrink(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %.not = icmp ugt i64 %4, %1
  br i1 %.not, label %.preheader, label %6

.preheader:                                       ; preds = %2
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  br label %8

6:                                                ; preds = %2
  %7 = tail call i32 @mbedtls_mpi_grow(ptr noundef nonnull %0, i64 noundef %1)
  br label %22

8:                                                ; preds = %.preheader, %9
  %.0.in = phi i64 [ %.0, %9 ], [ %4, %.preheader ]
  %.0 = add i64 %.0.in, -1
  %.not32 = icmp eq i64 %.0, 0
  br i1 %.not32, label %13, label %9

9:                                                ; preds = %8
  %10 = load ptr, ptr %5, align 8, !tbaa !12
  %11 = getelementptr inbounds i64, ptr %10, i64 %.0
  %12 = load i64, ptr %11, align 8, !tbaa !13
  %.not33 = icmp eq i64 %12, 0
  br i1 %.not33, label %8, label %13, !llvm.loop !14

13:                                               ; preds = %9, %8
  %spec.select = tail call i64 @llvm.umax.i64(i64 %.0.in, i64 %1)
  %14 = tail call noalias ptr @calloc(i64 noundef %spec.select, i64 noundef 8) #5
  %15 = icmp eq ptr %14, null
  br i1 %15, label %22, label %16

16:                                               ; preds = %13
  %17 = load ptr, ptr %5, align 8, !tbaa !12
  %.not34 = icmp eq ptr %17, null
  br i1 %.not34, label %21, label %18

18:                                               ; preds = %16
  %19 = shl i64 %spec.select, 3
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 8 %14, ptr nonnull align 8 %17, i64 %19, i1 false)
  tail call void @mbedtls_mpi_zeroize(ptr noundef nonnull %17, i64 noundef %4)
  %20 = load ptr, ptr %5, align 8, !tbaa !12
  tail call void @free(ptr noundef %20) #6
  br label %21

21:                                               ; preds = %18, %16
  store i64 %spec.select, ptr %3, align 8, !tbaa !5
  store ptr %14, ptr %5, align 8, !tbaa !12
  br label %22

22:                                               ; preds = %13, %21, %6
  %.025 = phi i32 [ %7, %6 ], [ 0, %21 ], [ -16, %13 ]
  ret i32 %.025
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #4

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 nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { nounwind allocsize(0,1) }
attributes #6 = { 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 8}
!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, !11, i64 16}
!13 = !{!10, !10, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass