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

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

; 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

declare ptr @uECC_secp256r1() #1

declare ptr @uECC_secp256k1() #1

; Function Attrs: inlinehint nounwind uwtable
define dso_local i32 @iotex_ecdsa_sign(i16 noundef zeroext %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4, ptr noundef %5, ptr noundef %6) #2 {
  %8 = alloca i32, align 4
  %9 = alloca i16, align 2
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i64, align 8
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  %16 = alloca ptr, align 8
  %17 = alloca i32, align 4
  %18 = alloca i32, align 4
  store i16 %0, ptr %9, align 2, !tbaa !5
  store ptr %1, ptr %10, align 8, !tbaa !9
  store i64 %2, ptr %11, align 8, !tbaa !11
  store ptr %3, ptr %12, align 8, !tbaa !9
  store i64 %4, ptr %13, align 8, !tbaa !11
  store ptr %5, ptr %14, align 8, !tbaa !9
  store ptr %6, ptr %15, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #3
  store i32 0, ptr %17, align 4, !tbaa !13
  call void @uECC_set_rng(ptr noundef @default_CSPRNG)
  %19 = load i16, ptr %9, align 2, !tbaa !5
  %20 = zext i16 %19 to i32
  switch i32 %20, label %25 [
    i32 18, label %21
    i32 23, label %23
  ]

21:                                               ; preds = %7
  %22 = call ptr @uECC_secp256r1()
  store ptr %22, ptr %16, align 8, !tbaa !9
  br label %26

23:                                               ; preds = %7
  %24 = call ptr @uECC_secp256k1()
  store ptr %24, ptr %16, align 8, !tbaa !9
  br label %26

25:                                               ; preds = %7
  store i32 1, ptr %8, align 4
  store i32 1, ptr %18, align 4
  br label %40

26:                                               ; preds = %23, %21
  %27 = load ptr, ptr %10, align 8, !tbaa !9
  %28 = load ptr, ptr %12, align 8, !tbaa !9
  %29 = load i64, ptr %13, align 8, !tbaa !11
  %30 = trunc i64 %29 to i32
  %31 = load ptr, ptr %14, align 8, !tbaa !9
  %32 = load ptr, ptr %16, align 8, !tbaa !9
  %33 = call i32 @uECC_sign(ptr noundef %27, ptr noundef %28, i32 noundef %30, ptr noundef %31, ptr noundef %32)
  store i32 %33, ptr %17, align 4, !tbaa !13
  %34 = load i32, ptr %17, align 4, !tbaa !13
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %36, label %38

36:                                               ; preds = %26
  %37 = load ptr, ptr %15, align 8, !tbaa !9
  store i64 64, ptr %37, align 8, !tbaa !11
  br label %39

38:                                               ; preds = %26
  store i32 -132, ptr %8, align 4
  store i32 1, ptr %18, align 4
  br label %40

39:                                               ; preds = %36
  store i32 0, ptr %8, align 4
  store i32 1, ptr %18, align 4
  br label %40

40:                                               ; preds = %39, %38, %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #3
  %41 = load i32, ptr %8, align 4
  ret i32 %41
}

declare void @uECC_set_rng(ptr noundef) #1

declare i32 @default_CSPRNG(ptr noundef, i32 noundef) #1

declare i32 @uECC_sign(ptr noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #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 #2 = { inlinehint 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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}


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

declare ptr @uECC_secp256r1() local_unnamed_addr #0

declare ptr @uECC_secp256k1() local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
define dso_local range(i32 -132, 2) i32 @iotex_ecdsa_sign(i16 noundef zeroext %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4, ptr noundef %5, ptr nocapture noundef writeonly %6) local_unnamed_addr #1 {
  tail call void @uECC_set_rng(ptr noundef nonnull @default_CSPRNG) #2
  switch i16 %0, label %16 [
    i16 18, label %8
    i16 23, label %10
  ]

8:                                                ; preds = %7
  %9 = tail call ptr @uECC_secp256r1() #2
  br label %12

10:                                               ; preds = %7
  %11 = tail call ptr @uECC_secp256k1() #2
  br label %12

12:                                               ; preds = %10, %8
  %.0 = phi ptr [ %11, %10 ], [ %9, %8 ]
  %13 = trunc i64 %4 to i32
  %14 = tail call i32 @uECC_sign(ptr noundef %1, ptr noundef %3, i32 noundef %13, ptr noundef %5, ptr noundef %.0) #2
  %.not = icmp eq i32 %14, 0
  br i1 %.not, label %16, label %15

15:                                               ; preds = %12
  store i64 64, ptr %6, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %12, %7, %15
  %.08 = phi i32 [ 0, %15 ], [ 1, %7 ], [ -132, %12 ]
  ret i32 %.08
}

declare void @uECC_set_rng(ptr noundef) local_unnamed_addr #0

declare i32 @default_CSPRNG(ptr noundef, i32 noundef) #0

declare i32 @uECC_sign(ptr noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

attributes #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 = { inlinehint 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


; Effective O3 passes: GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass