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

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

; 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 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: nounwind uwtable
define dso_local void @pbkdf2_hmac_sha512_Final(ptr noundef %0, ptr noundef %1) #3 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %7

7:                                                ; preds = %43, %2
  %8 = load i32, ptr %5, align 4, !tbaa !9
  %9 = zext i32 %8 to i64
  %10 = icmp ult i64 %9, 8
  br i1 %10, label %12, label %11

11:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %46

12:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA512_CTX, ptr %13, i32 0, i32 2
  %15 = load i32, ptr %5, align 4, !tbaa !9
  %16 = zext i32 %15 to i64
  %17 = getelementptr inbounds [8 x i64], ptr %14, i64 0, i64 %16
  %18 = load i64, ptr %17, align 8, !tbaa !11
  store i64 %18, ptr %6, align 8, !tbaa !11
  %19 = load i64, ptr %6, align 8, !tbaa !11
  %20 = lshr i64 %19, 32
  %21 = load i64, ptr %6, align 8, !tbaa !11
  %22 = shl i64 %21, 32
  %23 = or i64 %20, %22
  store i64 %23, ptr %6, align 8, !tbaa !11
  %24 = load i64, ptr %6, align 8, !tbaa !11
  %25 = and i64 %24, -71777214294589696
  %26 = lshr i64 %25, 8
  %27 = load i64, ptr %6, align 8, !tbaa !11
  %28 = and i64 %27, 71777214294589695
  %29 = shl i64 %28, 8
  %30 = or i64 %26, %29
  store i64 %30, ptr %6, align 8, !tbaa !11
  %31 = load i64, ptr %6, align 8, !tbaa !11
  %32 = and i64 %31, -281470681808896
  %33 = lshr i64 %32, 16
  %34 = load i64, ptr %6, align 8, !tbaa !11
  %35 = and i64 %34, 281470681808895
  %36 = shl i64 %35, 16
  %37 = or i64 %33, %36
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA512_CTX, ptr %38, i32 0, i32 2
  %40 = load i32, ptr %5, align 4, !tbaa !9
  %41 = zext i32 %40 to i64
  %42 = getelementptr inbounds [8 x i64], ptr %39, i64 0, i64 %41
  store i64 %37, ptr %42, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %43

43:                                               ; preds = %12
  %44 = load i32, ptr %5, align 4, !tbaa !9
  %45 = add i32 %44, 1
  store i32 %45, ptr %5, align 4, !tbaa !9
  br label %7, !llvm.loop !13

46:                                               ; preds = %11
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA512_CTX, ptr %48, i32 0, i32 2
  %50 = getelementptr inbounds [8 x i64], ptr %49, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %47, ptr align 8 %50, i64 64, i1 false)
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  call void @memzero(ptr noundef %51, i64 noundef 328)
  ret void
}

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


; 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 void @memzero(ptr noundef, i64 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) #1

; Function Attrs: nounwind uwtable
define dso_local void @pbkdf2_hmac_sha512_Final(ptr noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 128
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %5 = tail call i64 @llvm.bswap.i64(i64 %4)
  store i64 %5, ptr %3, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 136
  %7 = load i64, ptr %6, align 8, !tbaa !5
  %8 = tail call i64 @llvm.bswap.i64(i64 %7)
  store i64 %8, ptr %6, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %0, i64 144
  %10 = load i64, ptr %9, align 8, !tbaa !5
  %11 = tail call i64 @llvm.bswap.i64(i64 %10)
  store i64 %11, ptr %9, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %0, i64 152
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %14 = tail call i64 @llvm.bswap.i64(i64 %13)
  store i64 %14, ptr %12, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %0, i64 160
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = tail call i64 @llvm.bswap.i64(i64 %16)
  store i64 %17, ptr %15, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %0, i64 168
  %19 = load i64, ptr %18, align 8, !tbaa !5
  %20 = tail call i64 @llvm.bswap.i64(i64 %19)
  store i64 %20, ptr %18, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %0, i64 176
  %22 = load i64, ptr %21, align 8, !tbaa !5
  %23 = tail call i64 @llvm.bswap.i64(i64 %22)
  store i64 %23, ptr %21, align 8, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %0, i64 184
  %25 = load i64, ptr %24, align 8, !tbaa !5
  %26 = tail call i64 @llvm.bswap.i64(i64 %25)
  store i64 %26, ptr %24, align 8, !tbaa !5
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(64) %1, ptr noundef nonnull align 8 dereferenceable(64) %3, i64 64, i1 false)
  tail call void @memzero(ptr noundef %0, i64 noundef 328) #4
  ret void
}

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

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


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