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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
declare dso_local i64 @mbedtls_mpi_size(ptr noundef) #2

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

17:                                               ; preds = %3
  store i32 -8, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %50

18:                                               ; preds = %3
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memset.p0.i64(ptr align 1 %19, i8 0, i64 %20, i1 false)
  %21 = load i64, ptr %7, align 8, !tbaa !9
  %22 = sub i64 %21, 1
  store i64 %22, ptr %8, align 8, !tbaa !9
  store i64 0, ptr %9, align 8, !tbaa !9
  br label %23

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

26:                                               ; preds = %23
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.mbedtls_mpi, ptr %27, i32 0, i32 2
  %29 = load ptr, ptr %28, align 8, !tbaa !11
  %30 = load i64, ptr %9, align 8, !tbaa !9
  %31 = udiv i64 %30, 8
  %32 = getelementptr inbounds i64, ptr %29, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !9
  %34 = load i64, ptr %9, align 8, !tbaa !9
  %35 = urem i64 %34, 8
  %36 = shl i64 %35, 3
  %37 = lshr i64 %33, %36
  %38 = trunc i64 %37 to i8
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = load i64, ptr %8, align 8, !tbaa !9
  %41 = getelementptr inbounds i8, ptr %39, i64 %40
  store i8 %38, ptr %41, align 1, !tbaa !14
  br label %42

42:                                               ; preds = %26
  %43 = load i64, ptr %8, align 8, !tbaa !9
  %44 = add i64 %43, -1
  store i64 %44, ptr %8, align 8, !tbaa !9
  %45 = load i64, ptr %9, align 8, !tbaa !9
  %46 = add i64 %45, 1
  store i64 %46, ptr %9, align 8, !tbaa !9
  %47 = load i64, ptr %10, align 8, !tbaa !9
  %48 = add i64 %47, -1
  store i64 %48, ptr %10, align 8, !tbaa !9
  br label %23, !llvm.loop !15

49:                                               ; preds = %23
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %50

50:                                               ; preds = %49, %17
  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
  %51 = load i32, ptr %4, align 4
  ret i32 %51
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"", !13, i64 0, !10, i64 8, !6, i64 16}
!13 = !{!"int", !7, i64 0}
!14 = !{!7, !7, i64 0}
!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: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

; Function Attrs: nounwind uwtable
declare dso_local i64 @mbedtls_mpi_size(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -8, 1) i32 @mbedtls_mpi_write_binary(ptr noundef %0, ptr nocapture noundef writeonly %1, i64 noundef %2) local_unnamed_addr #1 {
  %4 = tail call i64 @mbedtls_mpi_size(ptr noundef %0)
  %5 = icmp ugt i64 %4, %2
  br i1 %5, label %.loopexit, label %6

6:                                                ; preds = %3
  tail call void @llvm.memset.p0.i64(ptr align 1 %1, i8 0, i64 %2, i1 false)
  %.not20 = icmp eq i64 %4, 0
  br i1 %.not20, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %6
  %7 = getelementptr inbounds i8, ptr %0, i64 16
  %xtraiter = and i64 %4, 1
  %8 = icmp eq i64 %4, 1
  br i1 %8, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %4, -2
  %invariant.gep = getelementptr i8, ptr %1, i64 -1
  br label %9

9:                                                ; preds = %9, %.lr.ph.new
  %.01522 = phi i64 [ 0, %.lr.ph.new ], [ %28, %9 ]
  %.016.in21 = phi i64 [ %2, %.lr.ph.new ], [ %.016.1, %9 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %9 ]
  %10 = load ptr, ptr %7, align 8, !tbaa !5
  %11 = lshr i64 %.01522, 3
  %12 = getelementptr inbounds i64, ptr %10, i64 %11
  %13 = load i64, ptr %12, align 8, !tbaa !12
  %14 = shl i64 %.01522, 3
  %15 = and i64 %14, 48
  %16 = lshr i64 %13, %15
  %17 = trunc i64 %16 to i8
  %gep = getelementptr i8, ptr %invariant.gep, i64 %.016.in21
  store i8 %17, ptr %gep, align 1, !tbaa !13
  %.016.1 = add i64 %.016.in21, -2
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = lshr i64 %.01522, 3
  %20 = getelementptr inbounds i64, ptr %18, i64 %19
  %21 = load i64, ptr %20, align 8, !tbaa !12
  %22 = shl i64 %.01522, 3
  %23 = and i64 %22, 48
  %24 = or disjoint i64 %23, 8
  %25 = lshr i64 %21, %24
  %26 = trunc i64 %25 to i8
  %27 = getelementptr inbounds i8, ptr %1, i64 %.016.1
  store i8 %26, ptr %27, align 1, !tbaa !13
  %28 = add nuw i64 %.01522, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.loopexit.loopexit.unr-lcssa, label %9, !llvm.loop !14

.loopexit.loopexit.unr-lcssa:                     ; preds = %9, %.lr.ph
  %.01522.unr = phi i64 [ 0, %.lr.ph ], [ %28, %9 ]
  %.016.in21.unr = phi i64 [ %2, %.lr.ph ], [ %.016.1, %9 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.loopexit.loopexit.epilog-lcssa

.loopexit.loopexit.epilog-lcssa:                  ; preds = %.loopexit.loopexit.unr-lcssa
  %29 = load ptr, ptr %7, align 8, !tbaa !5
  %30 = lshr i64 %.01522.unr, 3
  %31 = getelementptr inbounds i64, ptr %29, i64 %30
  %32 = load i64, ptr %31, align 8, !tbaa !12
  %33 = shl i64 %.01522.unr, 3
  %34 = and i64 %33, 56
  %35 = lshr i64 %32, %34
  %36 = trunc i64 %35 to i8
  %37 = getelementptr i8, ptr %1, i64 %.016.in21.unr
  %38 = getelementptr i8, ptr %37, i64 -1
  store i8 %36, ptr %38, align 1, !tbaa !13
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit.epilog-lcssa, %.loopexit.loopexit.unr-lcssa, %6, %3
  %.017 = phi i32 [ -8, %3 ], [ 0, %6 ], [ 0, %.loopexit.loopexit.unr-lcssa ], [ 0, %.loopexit.loopexit.epilog-lcssa ]
  ret i32 %.017
}

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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" }

!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, !11, i64 16}
!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 = !{!10, !10, i64 0}
!13 = !{!8, !8, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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