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

; 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._SHA512_CTX = type { [8 x i64], [2 x i64], [16 x i64] }

@sha512_initial_hash_value = external constant [8 x i64], align 16

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

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

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

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

declare void @sha512_Init(ptr noundef) #1

declare void @sha512_Update(ptr noundef, ptr noundef, i64 noundef) #1

declare void @sha512_Final(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @hmac_sha512_prepare(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) #3 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca [16 x i64], align 16
  %10 = alloca %struct._SHA512_CTX, align 8
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  %14 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 128, ptr %9) #4
  %15 = getelementptr inbounds [16 x i64], ptr %9, i64 0, i64 0
  call void @memzero(ptr noundef %15, i64 noundef 128)
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = icmp ugt i32 %16, 128
  br i1 %17, label %18, label %23

18:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 208, ptr %10) #4
  call void @sha512_Init(ptr noundef %10)
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = load i32, ptr %6, align 4, !tbaa !9
  %21 = zext i32 %20 to i64
  call void @sha512_Update(ptr noundef %10, ptr noundef %19, i64 noundef %21)
  %22 = getelementptr inbounds [16 x i64], ptr %9, i64 0, i64 0
  call void @sha512_Final(ptr noundef %10, ptr noundef %22)
  call void @llvm.lifetime.end.p0(i64 208, ptr %10) #4
  br label %28

23:                                               ; preds = %4
  %24 = getelementptr inbounds [16 x i64], ptr %9, i64 0, i64 0
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = load i32, ptr %6, align 4, !tbaa !9
  %27 = zext i32 %26 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %24, ptr align 1 %25, i64 %27, i1 false)
  br label %28

28:                                               ; preds = %23, %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %62, %28
  %30 = load i32, ptr %11, align 4, !tbaa !9
  %31 = icmp slt i32 %30, 16
  br i1 %31, label %33, label %32

32:                                               ; preds = %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  br label %65

33:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  store i64 0, ptr %12, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %34 = load i32, ptr %11, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [16 x i64], ptr %9, i64 0, i64 %35
  %37 = load i64, ptr %36, align 8, !tbaa !11
  store i64 %37, ptr %13, align 8, !tbaa !11
  %38 = load i64, ptr %13, align 8, !tbaa !11
  %39 = lshr i64 %38, 32
  %40 = load i64, ptr %13, align 8, !tbaa !11
  %41 = shl i64 %40, 32
  %42 = or i64 %39, %41
  store i64 %42, ptr %13, align 8, !tbaa !11
  %43 = load i64, ptr %13, align 8, !tbaa !11
  %44 = and i64 %43, -71777214294589696
  %45 = lshr i64 %44, 8
  %46 = load i64, ptr %13, align 8, !tbaa !11
  %47 = and i64 %46, 71777214294589695
  %48 = shl i64 %47, 8
  %49 = or i64 %45, %48
  store i64 %49, ptr %13, align 8, !tbaa !11
  %50 = load i64, ptr %13, align 8, !tbaa !11
  %51 = and i64 %50, -281470681808896
  %52 = lshr i64 %51, 16
  %53 = load i64, ptr %13, align 8, !tbaa !11
  %54 = and i64 %53, 281470681808895
  %55 = shl i64 %54, 16
  %56 = or i64 %52, %55
  store i64 %56, ptr %12, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  %57 = load i64, ptr %12, align 8, !tbaa !11
  %58 = xor i64 %57, 6655295901103053916
  %59 = load i32, ptr %11, align 4, !tbaa !9
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [16 x i64], ptr %9, i64 0, i64 %60
  store i64 %58, ptr %61, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  br label %62

62:                                               ; preds = %33
  %63 = load i32, ptr %11, align 4, !tbaa !9
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %11, align 4, !tbaa !9
  br label %29, !llvm.loop !13

65:                                               ; preds = %32
  %66 = getelementptr inbounds [16 x i64], ptr %9, i64 0, i64 0
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  call void @sha512_Transform(ptr noundef @sha512_initial_hash_value, ptr noundef %66, ptr noundef %67)
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  store i32 0, ptr %14, align 4, !tbaa !9
  br label %68

68:                                               ; preds = %82, %65
  %69 = load i32, ptr %14, align 4, !tbaa !9
  %70 = icmp slt i32 %69, 16
  br i1 %70, label %72, label %71

71:                                               ; preds = %68
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  br label %85

72:                                               ; preds = %68
  %73 = load i32, ptr %14, align 4, !tbaa !9
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds [16 x i64], ptr %9, i64 0, i64 %74
  %76 = load i64, ptr %75, align 8, !tbaa !11
  %77 = xor i64 %76, 6655295901103053916
  %78 = xor i64 %77, 3906369333256140342
  %79 = load i32, ptr %14, align 4, !tbaa !9
  %80 = sext i32 %79 to i64
  %81 = getelementptr inbounds [16 x i64], ptr %9, i64 0, i64 %80
  store i64 %78, ptr %81, align 8, !tbaa !11
  br label %82

82:                                               ; preds = %72
  %83 = load i32, ptr %14, align 4, !tbaa !9
  %84 = add nsw i32 %83, 1
  store i32 %84, ptr %14, align 4, !tbaa !9
  br label %68, !llvm.loop !15

85:                                               ; preds = %71
  %86 = getelementptr inbounds [16 x i64], ptr %9, i64 0, i64 0
  %87 = load ptr, ptr %8, align 8, !tbaa !5
  call void @sha512_Transform(ptr noundef @sha512_initial_hash_value, ptr noundef %86, ptr noundef %87)
  %88 = getelementptr inbounds [16 x i64], ptr %9, i64 0, i64 0
  call void @memzero(ptr noundef %88, i64 noundef 128)
  call void @llvm.lifetime.end.p0(i64 128, ptr %9) #4
  ret void
}

declare void @sha512_Transform(ptr 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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._SHA512_CTX = type { [8 x i64], [2 x i64], [16 x i64] }

@sha512_initial_hash_value = external constant [8 x i64], align 16

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

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

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

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

declare void @sha512_Init(ptr noundef) local_unnamed_addr #1

declare void @sha512_Update(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

declare void @sha512_Final(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @hmac_sha512_prepare(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #3 {
  %5 = alloca [16 x i64], align 16
  %6 = alloca %struct._SHA512_CTX, align 8
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %5) #5
  call void @memzero(ptr noundef nonnull %5, i64 noundef 128) #5
  %7 = icmp ugt i32 %1, 128
  br i1 %7, label %8, label %10

8:                                                ; preds = %4
  call void @llvm.lifetime.start.p0(i64 208, ptr nonnull %6) #5
  call void @sha512_Init(ptr noundef nonnull %6) #5
  %9 = zext i32 %1 to i64
  call void @sha512_Update(ptr noundef nonnull %6, ptr noundef %0, i64 noundef %9) #5
  call void @sha512_Final(ptr noundef nonnull %6, ptr noundef nonnull %5) #5
  call void @llvm.lifetime.end.p0(i64 208, ptr nonnull %6) #5
  br label %12

10:                                               ; preds = %4
  %11 = zext nneg i32 %1 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 %5, ptr align 1 %0, i64 %11, i1 false)
  br label %12

12:                                               ; preds = %10, %8
  %13 = load i64, ptr %5, align 16, !tbaa !5
  %14 = xor i64 %13, 6655295901103053916
  %15 = call i64 @llvm.bswap.i64(i64 %14)
  store i64 %15, ptr %5, align 16, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %5, i64 8
  %17 = load i64, ptr %16, align 8, !tbaa !5
  %18 = xor i64 %17, 6655295901103053916
  %19 = call i64 @llvm.bswap.i64(i64 %18)
  store i64 %19, ptr %16, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %5, i64 16
  %21 = load i64, ptr %20, align 16, !tbaa !5
  %22 = xor i64 %21, 6655295901103053916
  %23 = call i64 @llvm.bswap.i64(i64 %22)
  store i64 %23, ptr %20, align 16, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %5, i64 24
  %25 = load i64, ptr %24, align 8, !tbaa !5
  %26 = xor i64 %25, 6655295901103053916
  %27 = call i64 @llvm.bswap.i64(i64 %26)
  store i64 %27, ptr %24, align 8, !tbaa !5
  %28 = getelementptr inbounds i8, ptr %5, i64 32
  %29 = load i64, ptr %28, align 16, !tbaa !5
  %30 = xor i64 %29, 6655295901103053916
  %31 = call i64 @llvm.bswap.i64(i64 %30)
  store i64 %31, ptr %28, align 16, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %5, i64 40
  %33 = load i64, ptr %32, align 8, !tbaa !5
  %34 = xor i64 %33, 6655295901103053916
  %35 = call i64 @llvm.bswap.i64(i64 %34)
  store i64 %35, ptr %32, align 8, !tbaa !5
  %36 = getelementptr inbounds i8, ptr %5, i64 48
  %37 = load i64, ptr %36, align 16, !tbaa !5
  %38 = xor i64 %37, 6655295901103053916
  %39 = call i64 @llvm.bswap.i64(i64 %38)
  store i64 %39, ptr %36, align 16, !tbaa !5
  %40 = getelementptr inbounds i8, ptr %5, i64 56
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = xor i64 %41, 6655295901103053916
  %43 = call i64 @llvm.bswap.i64(i64 %42)
  store i64 %43, ptr %40, align 8, !tbaa !5
  %44 = getelementptr inbounds i8, ptr %5, i64 64
  %45 = load i64, ptr %44, align 16, !tbaa !5
  %46 = xor i64 %45, 6655295901103053916
  %47 = call i64 @llvm.bswap.i64(i64 %46)
  store i64 %47, ptr %44, align 16, !tbaa !5
  %48 = getelementptr inbounds i8, ptr %5, i64 72
  %49 = load i64, ptr %48, align 8, !tbaa !5
  %50 = xor i64 %49, 6655295901103053916
  %51 = call i64 @llvm.bswap.i64(i64 %50)
  store i64 %51, ptr %48, align 8, !tbaa !5
  %52 = getelementptr inbounds i8, ptr %5, i64 80
  %53 = load i64, ptr %52, align 16, !tbaa !5
  %54 = xor i64 %53, 6655295901103053916
  %55 = call i64 @llvm.bswap.i64(i64 %54)
  store i64 %55, ptr %52, align 16, !tbaa !5
  %56 = getelementptr inbounds i8, ptr %5, i64 88
  %57 = load i64, ptr %56, align 8, !tbaa !5
  %58 = xor i64 %57, 6655295901103053916
  %59 = call i64 @llvm.bswap.i64(i64 %58)
  store i64 %59, ptr %56, align 8, !tbaa !5
  %60 = getelementptr inbounds i8, ptr %5, i64 96
  %61 = load i64, ptr %60, align 16, !tbaa !5
  %62 = xor i64 %61, 6655295901103053916
  %63 = call i64 @llvm.bswap.i64(i64 %62)
  store i64 %63, ptr %60, align 16, !tbaa !5
  %64 = getelementptr inbounds i8, ptr %5, i64 104
  %65 = load i64, ptr %64, align 8, !tbaa !5
  %66 = xor i64 %65, 6655295901103053916
  %67 = call i64 @llvm.bswap.i64(i64 %66)
  store i64 %67, ptr %64, align 8, !tbaa !5
  %68 = getelementptr inbounds i8, ptr %5, i64 112
  %69 = load i64, ptr %68, align 16, !tbaa !5
  %70 = xor i64 %69, 6655295901103053916
  %71 = call i64 @llvm.bswap.i64(i64 %70)
  store i64 %71, ptr %68, align 16, !tbaa !5
  %72 = getelementptr inbounds i8, ptr %5, i64 120
  %73 = load i64, ptr %72, align 8, !tbaa !5
  %74 = xor i64 %73, 6655295901103053916
  %75 = call i64 @llvm.bswap.i64(i64 %74)
  store i64 %75, ptr %72, align 8, !tbaa !5
  call void @sha512_Transform(ptr noundef nonnull @sha512_initial_hash_value, ptr noundef nonnull %5, ptr noundef %2) #5
  %76 = load <2 x i64>, ptr %5, align 16, !tbaa !5
  %77 = xor <2 x i64> %76, <i64 7668058320836127338, i64 7668058320836127338>
  store <2 x i64> %77, ptr %5, align 16, !tbaa !5
  %78 = load <2 x i64>, ptr %20, align 16, !tbaa !5
  %79 = xor <2 x i64> %78, <i64 7668058320836127338, i64 7668058320836127338>
  store <2 x i64> %79, ptr %20, align 16, !tbaa !5
  %80 = load <2 x i64>, ptr %28, align 16, !tbaa !5
  %81 = xor <2 x i64> %80, <i64 7668058320836127338, i64 7668058320836127338>
  store <2 x i64> %81, ptr %28, align 16, !tbaa !5
  %82 = load <2 x i64>, ptr %36, align 16, !tbaa !5
  %83 = xor <2 x i64> %82, <i64 7668058320836127338, i64 7668058320836127338>
  store <2 x i64> %83, ptr %36, align 16, !tbaa !5
  %84 = load <2 x i64>, ptr %44, align 16, !tbaa !5
  %85 = xor <2 x i64> %84, <i64 7668058320836127338, i64 7668058320836127338>
  store <2 x i64> %85, ptr %44, align 16, !tbaa !5
  %86 = load <2 x i64>, ptr %52, align 16, !tbaa !5
  %87 = xor <2 x i64> %86, <i64 7668058320836127338, i64 7668058320836127338>
  store <2 x i64> %87, ptr %52, align 16, !tbaa !5
  %88 = load <2 x i64>, ptr %60, align 16, !tbaa !5
  %89 = xor <2 x i64> %88, <i64 7668058320836127338, i64 7668058320836127338>
  store <2 x i64> %89, ptr %60, align 16, !tbaa !5
  %90 = load <2 x i64>, ptr %68, align 16, !tbaa !5
  %91 = xor <2 x i64> %90, <i64 7668058320836127338, i64 7668058320836127338>
  store <2 x i64> %91, ptr %68, align 16, !tbaa !5
  call void @sha512_Transform(ptr noundef nonnull @sha512_initial_hash_value, ptr noundef nonnull %5, ptr noundef %3) #5
  call void @memzero(ptr noundef nonnull %5, i64 noundef 128) #5
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %5) #5
  ret void
}

declare void @sha512_Transform(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.bswap.i64(i64) #4

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass