; 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: nounwind uwtable
define dso_local i64 @mbedtls_mpi_lsb(ptr noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store i64 0, ptr %6, align 8, !tbaa !9
  store i64 0, ptr %4, align 8, !tbaa !9
  br label %8

8:                                                ; preds = %38, %1
  %9 = load i64, ptr %4, align 8, !tbaa !9
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.mbedtls_mpi, ptr %10, i32 0, i32 1
  %12 = load i64, ptr %11, align 8, !tbaa !11
  %13 = icmp ult i64 %9, %12
  br i1 %13, label %14, label %41

14:                                               ; preds = %8
  store i64 0, ptr %5, align 8, !tbaa !9
  br label %15

15:                                               ; preds = %32, %14
  %16 = load i64, ptr %5, align 8, !tbaa !9
  %17 = icmp ult i64 %16, 64
  br i1 %17, label %18, label %37

18:                                               ; preds = %15
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.mbedtls_mpi, ptr %19, i32 0, i32 2
  %21 = load ptr, ptr %20, align 8, !tbaa !14
  %22 = load i64, ptr %4, align 8, !tbaa !9
  %23 = getelementptr inbounds i64, ptr %21, i64 %22
  %24 = load i64, ptr %23, align 8, !tbaa !9
  %25 = load i64, ptr %5, align 8, !tbaa !9
  %26 = lshr i64 %24, %25
  %27 = and i64 %26, 1
  %28 = icmp ne i64 %27, 0
  br i1 %28, label %29, label %31

29:                                               ; preds = %18
  %30 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %30, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %42

31:                                               ; preds = %18
  br label %32

32:                                               ; preds = %31
  %33 = load i64, ptr %5, align 8, !tbaa !9
  %34 = add i64 %33, 1
  store i64 %34, ptr %5, align 8, !tbaa !9
  %35 = load i64, ptr %6, align 8, !tbaa !9
  %36 = add i64 %35, 1
  store i64 %36, ptr %6, align 8, !tbaa !9
  br label %15, !llvm.loop !15

37:                                               ; preds = %15
  br label %38

38:                                               ; preds = %37
  %39 = load i64, ptr %4, align 8, !tbaa !9
  %40 = add i64 %39, 1
  store i64 %40, ptr %4, align 8, !tbaa !9
  br label %8, !llvm.loop !17

41:                                               ; preds = %8
  store i64 0, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %42

42:                                               ; preds = %41, %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %43 = load i64, ptr %2, align 8
  ret i64 %43
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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, !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"}
!17 = distinct !{!17, !16}


; 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: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local i64 @mbedtls_mpi_lsb(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load i64, ptr %2, align 8, !tbaa !5
  %.not19 = icmp eq i64 %3, 0
  br i1 %.not19, label %.loopexit, label %.preheader.lr.ph

.preheader.lr.ph:                                 ; preds = %1
  %4 = getelementptr inbounds i8, ptr %0, i64 16
  %5 = load ptr, ptr %4, align 8, !tbaa !12
  br label %.preheader

.preheader:                                       ; preds = %.preheader.lr.ph, %24
  %.018 = phi i64 [ 0, %.preheader.lr.ph ], [ %8, %24 ]
  %.01117 = phi i64 [ 0, %.preheader.lr.ph ], [ %25, %24 ]
  %6 = getelementptr inbounds i64, ptr %5, i64 %.01117
  %7 = load i64, ptr %6, align 8, !tbaa !13
  %8 = add i64 %.018, 64
  br label %9

9:                                                ; preds = %21, %.preheader
  %.116 = phi i64 [ %.018, %.preheader ], [ %23, %21 ]
  %.01015 = phi i64 [ 0, %.preheader ], [ %22, %21 ]
  %10 = shl nuw i64 1, %.01015
  %11 = and i64 %7, %10
  %.not = icmp eq i64 %11, 0
  br i1 %.not, label %12, label %.loopexit

12:                                               ; preds = %9
  %13 = shl nuw i64 2, %.01015
  %14 = and i64 %7, %13
  %.not.1 = icmp eq i64 %14, 0
  br i1 %.not.1, label %15, label %.loopexit.loopexit.split.loop.exit31

15:                                               ; preds = %12
  %16 = shl nuw i64 4, %.01015
  %17 = and i64 %7, %16
  %.not.2 = icmp eq i64 %17, 0
  br i1 %.not.2, label %18, label %.loopexit.loopexit.split.loop.exit29

18:                                               ; preds = %15
  %19 = shl nuw i64 8, %.01015
  %20 = and i64 %7, %19
  %.not.3 = icmp eq i64 %20, 0
  br i1 %.not.3, label %21, label %.loopexit.loopexit.split.loop.exit

21:                                               ; preds = %18
  %22 = add nuw nsw i64 %.01015, 4
  %23 = add i64 %.116, 4
  %exitcond.not.3 = icmp eq i64 %22, 64
  br i1 %exitcond.not.3, label %24, label %9, !llvm.loop !14

24:                                               ; preds = %21
  %25 = add nuw i64 %.01117, 1
  %exitcond22.not = icmp eq i64 %25, %3
  br i1 %exitcond22.not, label %.loopexit, label %.preheader, !llvm.loop !16

.loopexit.loopexit.split.loop.exit:               ; preds = %18
  %26 = or disjoint i64 %.116, 3
  br label %.loopexit

.loopexit.loopexit.split.loop.exit29:             ; preds = %15
  %27 = or disjoint i64 %.116, 2
  br label %.loopexit

.loopexit.loopexit.split.loop.exit31:             ; preds = %12
  %28 = or disjoint i64 %.116, 1
  br label %.loopexit

.loopexit:                                        ; preds = %24, %.loopexit.loopexit.split.loop.exit, %.loopexit.loopexit.split.loop.exit29, %.loopexit.loopexit.split.loop.exit31, %9, %1
  %.012 = phi i64 [ 0, %1 ], [ %26, %.loopexit.loopexit.split.loop.exit ], [ %27, %.loopexit.loopexit.split.loop.exit29 ], [ %28, %.loopexit.loopexit.split.loop.exit31 ], [ %.116, %9 ], [ 0, %24 ]
  ret i64 %.012
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) 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, !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"}
!16 = distinct !{!16, !15}


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