; LLVM IR from machinefi/psa-crypto-esp32 :: crypto (ecc)

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

@g_rng_function = external hidden global ptr, align 8

; 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
declare dso_local i32 @uECC_vli_isZero(ptr noundef, i8 noundef signext) #1

; Function Attrs: nounwind uwtable
declare dso_local signext i16 @uECC_vli_numBits(ptr noundef, i8 noundef signext) #1

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @uECC_vli_cmp(ptr noundef, ptr noundef, i8 noundef signext) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @uECC_generate_random_int(ptr noundef %0, ptr noundef %1, i8 noundef signext %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i16, align 2
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i8 %2, ptr %7, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 -1, ptr %8, align 4, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #2
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = load i8, ptr %7, align 1, !tbaa !9
  %14 = call signext i16 @uECC_vli_numBits(ptr noundef %12, i8 noundef signext %13)
  store i16 %14, ptr %10, align 2, !tbaa !12
  %15 = load ptr, ptr @g_rng_function, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %18, label %17

17:                                               ; preds = %3
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %68

18:                                               ; preds = %3
  store i32 0, ptr %9, align 4, !tbaa !10
  br label %19

19:                                               ; preds = %64, %18
  %20 = load i32, ptr %9, align 4, !tbaa !10
  %21 = icmp ult i32 %20, 64
  br i1 %21, label %22, label %67

22:                                               ; preds = %19
  %23 = load ptr, ptr @g_rng_function, align 8, !tbaa !5
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = load i8, ptr %7, align 1, !tbaa !9
  %26 = sext i8 %25 to i32
  %27 = mul nsw i32 %26, 4
  %28 = call i32 %23(ptr noundef %24, i32 noundef %27)
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %31, label %30

30:                                               ; preds = %22
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %68

31:                                               ; preds = %22
  %32 = load i32, ptr %8, align 4, !tbaa !10
  %33 = load i8, ptr %7, align 1, !tbaa !9
  %34 = sext i8 %33 to i32
  %35 = mul nsw i32 %34, 4
  %36 = mul nsw i32 %35, 8
  %37 = load i16, ptr %10, align 2, !tbaa !12
  %38 = sext i16 %37 to i32
  %39 = sub nsw i32 %36, %38
  %40 = trunc i32 %39 to i16
  %41 = sext i16 %40 to i32
  %42 = lshr i32 %32, %41
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = load i8, ptr %7, align 1, !tbaa !9
  %45 = sext i8 %44 to i32
  %46 = sub nsw i32 %45, 1
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i32, ptr %43, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !10
  %50 = and i32 %49, %42
  store i32 %50, ptr %48, align 4, !tbaa !10
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = load i8, ptr %7, align 1, !tbaa !9
  %53 = call i32 @uECC_vli_isZero(ptr noundef %51, i8 noundef signext %52)
  %54 = icmp ne i32 %53, 0
  br i1 %54, label %63, label %55

55:                                               ; preds = %31
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = load i8, ptr %7, align 1, !tbaa !9
  %59 = call signext i8 @uECC_vli_cmp(ptr noundef %56, ptr noundef %57, i8 noundef signext %58)
  %60 = sext i8 %59 to i32
  %61 = icmp eq i32 %60, 1
  br i1 %61, label %62, label %63

62:                                               ; preds = %55
  store i32 1, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %68

63:                                               ; preds = %55, %31
  br label %64

64:                                               ; preds = %63
  %65 = load i32, ptr %9, align 4, !tbaa !10
  %66 = add i32 %65, 1
  store i32 %66, ptr %9, align 4, !tbaa !10
  br label %19, !llvm.loop !14

67:                                               ; preds = %19
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %68

68:                                               ; preds = %67, %62, %30, %17
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %69 = load i32, ptr %4, align 4
  ret i32 %69
}

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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

@g_rng_function = external hidden local_unnamed_addr global ptr, align 8

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

; Function Attrs: nounwind uwtable
declare dso_local signext i16 @uECC_vli_numBits(ptr noundef, i8 noundef signext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @uECC_vli_cmp(ptr noundef, ptr noundef, i8 noundef signext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @uECC_generate_random_int(ptr noundef %0, ptr noundef %1, i8 noundef signext %2) local_unnamed_addr #0 {
  %4 = tail call signext i16 @uECC_vli_numBits(ptr noundef %1, i8 noundef signext %2)
  %5 = load ptr, ptr @g_rng_function, align 8, !tbaa !5
  %.not = icmp eq ptr %5, null
  br i1 %.not, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %3
  %invariant.gep = getelementptr i8, ptr %0, i64 -4
  %6 = sext i8 %2 to i32
  %7 = shl nsw i32 %6, 2
  %.tr = sext i8 %2 to i16
  %8 = shl nsw i16 %.tr, 5
  %9 = sub i16 %8, %4
  %10 = zext nneg i16 %9 to i32
  %11 = lshr i32 -1, %10
  %12 = sext i8 %2 to i64
  %gep = getelementptr i32, ptr %invariant.gep, i64 %12
  br label %13

13:                                               ; preds = %.preheader, %23
  %.01619 = phi i32 [ 0, %.preheader ], [ %24, %23 ]
  %14 = load ptr, ptr @g_rng_function, align 8, !tbaa !5
  %15 = tail call i32 %14(ptr noundef %0, i32 noundef %7) #1
  %.not17 = icmp eq i32 %15, 0
  br i1 %.not17, label %.loopexit, label %16

16:                                               ; preds = %13
  %17 = load i32, ptr %gep, align 4, !tbaa !9
  %18 = and i32 %17, %11
  store i32 %18, ptr %gep, align 4, !tbaa !9
  %19 = tail call i32 @uECC_vli_isZero(ptr noundef %0, i8 noundef signext %2)
  %.not18 = icmp eq i32 %19, 0
  br i1 %.not18, label %20, label %23

20:                                               ; preds = %16
  %21 = tail call signext i8 @uECC_vli_cmp(ptr noundef %1, ptr noundef nonnull %0, i8 noundef signext %2)
  %22 = icmp eq i8 %21, 1
  br i1 %22, label %.loopexit, label %23

23:                                               ; preds = %16, %20
  %24 = add nuw nsw i32 %.01619, 1
  %exitcond.not = icmp eq i32 %24, 64
  br i1 %exitcond.not, label %.loopexit, label %13, !llvm.loop !11

.loopexit:                                        ; preds = %23, %20, %13, %3
  %.0 = phi i32 [ 0, %3 ], [ 0, %23 ], [ 1, %20 ], [ 0, %13 ]
  ret i32 %.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 = { 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"}


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