; LLVM IR from square/subzero :: core/trezor-crypto (ecdsa)

; 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.rfc6979_state = type { [32 x i8], [32 x i8] }

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @init_rfc6979(ptr noundef %0, ptr noundef %1, ptr noundef %2) #3 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca [64 x i8], align 16
  %8 = alloca [97 x i8], align 16
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 64, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 97, ptr %8) #5
  %9 = getelementptr inbounds [64 x i8], ptr %7, i64 0, i64 0
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %9, ptr align 1 %10, i64 32, i1 false)
  %11 = getelementptr inbounds [64 x i8], ptr %7, i64 0, i64 0
  %12 = getelementptr inbounds i8, ptr %11, i64 32
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %12, ptr align 1 %13, i64 32, i1 false)
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.rfc6979_state, ptr %14, i32 0, i32 0
  %16 = getelementptr inbounds [32 x i8], ptr %15, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 1 %16, i8 1, i64 32, i1 false)
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.rfc6979_state, ptr %17, i32 0, i32 1
  %19 = getelementptr inbounds [32 x i8], ptr %18, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 1 %19, i8 0, i64 32, i1 false)
  %20 = getelementptr inbounds [97 x i8], ptr %8, i64 0, i64 0
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.rfc6979_state, ptr %21, i32 0, i32 0
  %23 = getelementptr inbounds [32 x i8], ptr %22, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %20, ptr align 1 %23, i64 32, i1 false)
  %24 = getelementptr inbounds [97 x i8], ptr %8, i64 0, i64 32
  store i8 0, ptr %24, align 16, !tbaa !9
  %25 = getelementptr inbounds [97 x i8], ptr %8, i64 0, i64 0
  %26 = getelementptr inbounds i8, ptr %25, i64 32
  %27 = getelementptr inbounds i8, ptr %26, i64 1
  %28 = getelementptr inbounds [64 x i8], ptr %7, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %27, ptr align 16 %28, i64 64, i1 false)
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.rfc6979_state, ptr %29, i32 0, i32 1
  %31 = getelementptr inbounds [32 x i8], ptr %30, i64 0, i64 0
  %32 = getelementptr inbounds [97 x i8], ptr %8, i64 0, i64 0
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.rfc6979_state, ptr %33, i32 0, i32 1
  %35 = getelementptr inbounds [32 x i8], ptr %34, i64 0, i64 0
  call void @hmac_sha256(ptr noundef %31, i32 noundef 32, ptr noundef %32, i32 noundef 97, ptr noundef %35)
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.rfc6979_state, ptr %36, i32 0, i32 1
  %38 = getelementptr inbounds [32 x i8], ptr %37, i64 0, i64 0
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.rfc6979_state, ptr %39, i32 0, i32 0
  %41 = getelementptr inbounds [32 x i8], ptr %40, i64 0, i64 0
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.rfc6979_state, ptr %42, i32 0, i32 0
  %44 = getelementptr inbounds [32 x i8], ptr %43, i64 0, i64 0
  call void @hmac_sha256(ptr noundef %38, i32 noundef 32, ptr noundef %41, i32 noundef 32, ptr noundef %44)
  %45 = getelementptr inbounds [97 x i8], ptr %8, i64 0, i64 0
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.rfc6979_state, ptr %46, 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 16 %45, ptr align 1 %48, i64 32, i1 false)
  %49 = getelementptr inbounds [97 x i8], ptr %8, i64 0, i64 32
  store i8 1, ptr %49, align 16, !tbaa !9
  %50 = getelementptr inbounds [97 x i8], ptr %8, i64 0, i64 0
  %51 = getelementptr inbounds i8, ptr %50, i64 32
  %52 = getelementptr inbounds i8, ptr %51, i64 1
  %53 = getelementptr inbounds [64 x i8], ptr %7, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %52, ptr align 16 %53, i64 64, i1 false)
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.rfc6979_state, ptr %54, i32 0, i32 1
  %56 = getelementptr inbounds [32 x i8], ptr %55, i64 0, i64 0
  %57 = getelementptr inbounds [97 x i8], ptr %8, i64 0, i64 0
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.rfc6979_state, ptr %58, i32 0, i32 1
  %60 = getelementptr inbounds [32 x i8], ptr %59, i64 0, i64 0
  call void @hmac_sha256(ptr noundef %56, i32 noundef 32, ptr noundef %57, i32 noundef 97, ptr noundef %60)
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.rfc6979_state, ptr %61, i32 0, i32 1
  %63 = getelementptr inbounds [32 x i8], ptr %62, i64 0, i64 0
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.rfc6979_state, ptr %64, i32 0, i32 0
  %66 = getelementptr inbounds [32 x i8], ptr %65, i64 0, i64 0
  %67 = load ptr, ptr %6, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.rfc6979_state, ptr %67, i32 0, i32 0
  %69 = getelementptr inbounds [32 x i8], ptr %68, i64 0, i64 0
  call void @hmac_sha256(ptr noundef %63, i32 noundef 32, ptr noundef %66, i32 noundef 32, ptr noundef %69)
  %70 = getelementptr inbounds [64 x i8], ptr %7, i64 0, i64 0
  call void @memzero(ptr noundef %70, i64 noundef 64)
  %71 = getelementptr inbounds [97 x i8], ptr %8, i64 0, i64 0
  call void @memzero(ptr noundef %71, i64 noundef 97)
  call void @llvm.lifetime.end.p0(i64 97, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 64, ptr %7) #5
  ret void
}

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

declare void @hmac_sha256(ptr noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef) #2

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


; 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: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @init_rfc6979(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, ptr noundef %2) local_unnamed_addr #3 {
  %4 = alloca [64 x i8], align 16
  %5 = alloca [97 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %4) #5
  call void @llvm.lifetime.start.p0(i64 97, ptr nonnull %5) #5
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %4, ptr noundef nonnull align 1 dereferenceable(32) %0, i64 32, i1 false)
  %6 = getelementptr inbounds i8, ptr %4, i64 32
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %6, ptr noundef nonnull align 1 dereferenceable(32) %1, i64 32, i1 false)
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(32) %2, i8 1, i64 32, i1 false)
  %7 = getelementptr inbounds i8, ptr %2, i64 32
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(32) %7, i8 0, i64 32, i1 false)
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %5, i8 1, i64 32, i1 false)
  %8 = getelementptr inbounds i8, ptr %5, i64 32
  store i8 0, ptr %8, align 16, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %5, i64 33
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(64) %9, ptr noundef nonnull align 16 dereferenceable(64) %4, i64 64, i1 false)
  call void @hmac_sha256(ptr noundef nonnull %7, i32 noundef 32, ptr noundef nonnull %5, i32 noundef 97, ptr noundef nonnull %7) #5
  call void @hmac_sha256(ptr noundef nonnull %7, i32 noundef 32, ptr noundef %2, i32 noundef 32, ptr noundef %2) #5
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %5, ptr noundef nonnull align 1 dereferenceable(32) %2, i64 32, i1 false)
  store i8 1, ptr %8, align 16, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(64) %9, ptr noundef nonnull align 16 dereferenceable(64) %4, i64 64, i1 false)
  call void @hmac_sha256(ptr noundef nonnull %7, i32 noundef 32, ptr noundef nonnull %5, i32 noundef 97, ptr noundef nonnull %7) #5
  call void @hmac_sha256(ptr noundef nonnull %7, i32 noundef 32, ptr noundef %2, i32 noundef 32, ptr noundef %2) #5
  call void @memzero(ptr noundef nonnull %4, i64 noundef 64) #5
  call void @memzero(ptr noundef nonnull %5, i64 noundef 97) #5
  call void @llvm.lifetime.end.p0(i64 97, ptr nonnull %5) #5
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %4) #5
  ret void
}

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

declare void @hmac_sha256(ptr noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,MemCpyOptPass,PostOrderFunctionAttrsPass,SROAPass,TailCallElimPass