; LLVM IR from trustwallet/wallet-core :: trezor-crypto/crypto (zilliqa)

; 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.bignum256 = type { [9 x i32] }
%struct._HMAC_DRBG_CTX = type { [8 x i32], [8 x i32], [16 x i32] }
%struct.schnorr_sign_pair = type { [32 x i8], [32 x i8] }
%struct.ecdsa_curve = type { %struct.bignum256, %struct.curve_point, %struct.bignum256, %struct.bignum256, i32, %struct.bignum256, [64 x [8 x %struct.curve_point]] }
%struct.curve_point = type { %struct.bignum256, %struct.bignum256 }

; Function Attrs: nounwind uwtable
define dso_local i32 @zil_schnorr_sign(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, ptr noundef %4) #0 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca %struct.bignum256, align 4
  %14 = alloca [32 x i8], align 16
  %15 = alloca %struct._HMAC_DRBG_CTX, align 4
  %16 = alloca %struct.schnorr_sign_pair, align 1
  %17 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store ptr %2, ptr %9, align 8, !tbaa !5
  store i32 %3, ptr %10, align 4, !tbaa !9
  store ptr %4, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.start.p0(i64 36, ptr %13) #4
  call void @llvm.lifetime.start.p0(i64 32, ptr %14) #4
  %18 = load ptr, ptr %9, align 8, !tbaa !5
  %19 = load i32, ptr %10, align 4, !tbaa !9
  %20 = zext i32 %19 to i64
  %21 = getelementptr inbounds [32 x i8], ptr %14, i64 0, i64 0
  call void @sha256_Raw(ptr noundef %18, i64 noundef %20, ptr noundef %21)
  call void @llvm.lifetime.start.p0(i64 128, ptr %15) #4
  %22 = load ptr, ptr %8, align 8, !tbaa !5
  %23 = getelementptr inbounds [32 x i8], ptr %14, i64 0, i64 0
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  call void @init_rfc6979(ptr noundef %22, ptr noundef %23, ptr noundef %24, ptr noundef %15)
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %55, %5
  %26 = load i32, ptr %12, align 4, !tbaa !9
  %27 = icmp slt i32 %26, 10000
  br i1 %27, label %28, label %58

28:                                               ; preds = %25
  call void @generate_k_rfc6979(ptr noundef %13, ptr noundef %15)
  %29 = call i32 @bn_is_zero(ptr noundef %13)
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %36, label %31

31:                                               ; preds = %28
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.ecdsa_curve, ptr %32, i32 0, i32 2
  %34 = call i32 @bn_is_less(ptr noundef %13, ptr noundef %33)
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %37, label %36

36:                                               ; preds = %31, %28
  br label %55

37:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 64, ptr %16) #4
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  %40 = load ptr, ptr %9, align 8, !tbaa !5
  %41 = load i32, ptr %10, align 4, !tbaa !9
  %42 = call i32 @zil_schnorr_sign_k(ptr noundef %38, ptr noundef %39, ptr noundef %13, ptr noundef %40, i32 noundef %41, ptr noundef %16)
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %45

44:                                               ; preds = %37
  store i32 4, ptr %17, align 4
  br label %53

45:                                               ; preds = %37
  %46 = load ptr, ptr %11, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.schnorr_sign_pair, ptr %16, i32 0, i32 0
  %48 = getelementptr inbounds [32 x i8], ptr %47, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %46, ptr align 1 %48, i64 32, i1 false)
  %49 = load ptr, ptr %11, align 8, !tbaa !5
  %50 = getelementptr inbounds i8, ptr %49, i64 32
  %51 = getelementptr inbounds %struct.schnorr_sign_pair, ptr %16, i32 0, i32 1
  %52 = getelementptr inbounds [32 x i8], ptr %51, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %50, ptr align 1 %52, i64 32, i1 false)
  call void @memzero(ptr noundef %13, i64 noundef 36)
  call void @memzero(ptr noundef %15, i64 noundef 128)
  call void @memzero(ptr noundef %16, i64 noundef 64)
  store i32 0, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %53

53:                                               ; preds = %45, %44
  call void @llvm.lifetime.end.p0(i64 64, ptr %16) #4
  %54 = load i32, ptr %17, align 4
  switch i32 %54, label %59 [
    i32 4, label %55
  ]

55:                                               ; preds = %53, %36
  %56 = load i32, ptr %12, align 4, !tbaa !9
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %12, align 4, !tbaa !9
  br label %25, !llvm.loop !11

58:                                               ; preds = %25
  call void @memzero(ptr noundef %13, i64 noundef 36)
  call void @memzero(ptr noundef %15, i64 noundef 128)
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %59

59:                                               ; preds = %58, %53
  call void @llvm.lifetime.end.p0(i64 128, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  %60 = load i32, ptr %6, align 4
  ret i32 %60
}

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

declare void @sha256_Raw(ptr noundef, i64 noundef, ptr noundef) #2

declare void @init_rfc6979(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #2

declare void @generate_k_rfc6979(ptr noundef, ptr noundef) #2

declare i32 @bn_is_zero(ptr noundef) #2

declare i32 @bn_is_less(ptr noundef, ptr noundef) #2

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

; 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) #3

declare void @memzero(ptr noundef, i64 noundef) #2

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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"}


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

%struct.bignum256 = type { [9 x i32] }
%struct._HMAC_DRBG_CTX = type { [8 x i32], [8 x i32], [16 x i32] }
%struct.schnorr_sign_pair = type { [32 x i8], [32 x i8] }

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @zil_schnorr_sign(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, ptr nocapture noundef writeonly %4) local_unnamed_addr #0 {
  %6 = alloca %struct.bignum256, align 4
  %7 = alloca [32 x i8], align 16
  %8 = alloca %struct._HMAC_DRBG_CTX, align 4
  %9 = alloca %struct.schnorr_sign_pair, align 1
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %6) #4
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %7) #4
  %10 = zext i32 %3 to i64
  call void @sha256_Raw(ptr noundef %2, i64 noundef %10, ptr noundef nonnull %7) #4
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %8) #4
  call void @init_rfc6979(ptr noundef %1, ptr noundef nonnull %7, ptr noundef %0, ptr noundef nonnull %8) #4
  %11 = getelementptr inbounds i8, ptr %0, i64 108
  br label %12

12:                                               ; preds = %5, %21
  %.01418 = phi i32 [ 0, %5 ], [ %22, %21 ]
  call void @generate_k_rfc6979(ptr noundef nonnull %6, ptr noundef nonnull %8) #4
  %13 = call i32 @bn_is_zero(ptr noundef nonnull %6) #4
  %.not = icmp eq i32 %13, 0
  br i1 %.not, label %14, label %21

14:                                               ; preds = %12
  %15 = call i32 @bn_is_less(ptr noundef nonnull %6, ptr noundef nonnull %11) #4
  %.not16 = icmp eq i32 %15, 0
  br i1 %.not16, label %21, label %16

16:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %9) #4
  %17 = call i32 @zil_schnorr_sign_k(ptr noundef %0, ptr noundef %1, ptr noundef nonnull %6, ptr noundef %2, i32 noundef %3, ptr noundef nonnull %9)
  %.not17.not = icmp eq i32 %17, 0
  br i1 %.not17.not, label %18, label %.critedge

18:                                               ; preds = %16
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(32) %4, ptr noundef nonnull align 1 dereferenceable(32) %9, i64 32, i1 false)
  %19 = getelementptr inbounds i8, ptr %4, i64 32
  %20 = getelementptr inbounds i8, ptr %9, i64 32
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(32) %19, ptr noundef nonnull align 1 dereferenceable(32) %20, i64 32, i1 false)
  call void @memzero(ptr noundef nonnull %6, i64 noundef 36) #4
  call void @memzero(ptr noundef nonnull %8, i64 noundef 128) #4
  call void @memzero(ptr noundef nonnull %9, i64 noundef 64) #4
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %9) #4
  br label %24

.critedge:                                        ; preds = %16
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %9) #4
  br label %21

21:                                               ; preds = %.critedge, %12, %14
  %22 = add nuw nsw i32 %.01418, 1
  %exitcond.not = icmp eq i32 %22, 10000
  br i1 %exitcond.not, label %23, label %12, !llvm.loop !5

23:                                               ; preds = %21
  call void @memzero(ptr noundef nonnull %6, i64 noundef 36) #4
  call void @memzero(ptr noundef nonnull %8, i64 noundef 128) #4
  br label %24

24:                                               ; preds = %18, %23
  %.3 = phi i32 [ 0, %18 ], [ -1, %23 ]
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %8) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %6) #4
  ret i32 %.3
}

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

declare void @sha256_Raw(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

declare void @init_rfc6979(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

declare void @generate_k_rfc6979(ptr noundef, ptr noundef) local_unnamed_addr #2

declare i32 @bn_is_zero(ptr noundef) local_unnamed_addr #2

declare i32 @bn_is_less(ptr noundef, ptr noundef) local_unnamed_addr #2

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

; 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

declare void @memzero(ptr noundef, i64 noundef) local_unnamed_addr #2

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

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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass