; 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_SHA256_CTX = type { [8 x i32], [8 x i32], [8 x i32], [16 x i32], 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 @sha256_Transform(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @pbkdf2_hmac_sha256_Update(ptr noundef %0, i32 noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %8, i32 0, i32 4
  %10 = load i8, ptr %9, align 4, !tbaa !11
  %11 = sext i8 %10 to i32
  store i32 %11, ptr %5, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %59, %2
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = load i32, ptr %4, align 4, !tbaa !9
  %15 = icmp ult i32 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  store i32 2, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %62

17:                                               ; preds = %12
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %18, i32 0, i32 1
  %20 = getelementptr inbounds [8 x i32], ptr %19, i64 0, i64 0
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %21, i32 0, i32 3
  %23 = getelementptr inbounds [16 x i32], ptr %22, i64 0, i64 0
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %24, i32 0, i32 3
  %26 = getelementptr inbounds [16 x i32], ptr %25, i64 0, i64 0
  call void @sha256_Transform(ptr noundef %20, ptr noundef %23, ptr noundef %26)
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %27, i32 0, i32 0
  %29 = getelementptr inbounds [8 x i32], ptr %28, i64 0, i64 0
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %30, i32 0, i32 3
  %32 = getelementptr inbounds [16 x i32], ptr %31, i64 0, i64 0
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %33, i32 0, i32 3
  %35 = getelementptr inbounds [16 x i32], ptr %34, i64 0, i64 0
  call void @sha256_Transform(ptr noundef %29, ptr noundef %32, ptr noundef %35)
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %36

36:                                               ; preds = %55, %17
  %37 = load i32, ptr %7, align 4, !tbaa !9
  %38 = zext i32 %37 to i64
  %39 = icmp ult i64 %38, 8
  br i1 %39, label %41, label %40

40:                                               ; preds = %36
  store i32 5, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %58

41:                                               ; preds = %36
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %42, i32 0, i32 3
  %44 = load i32, ptr %7, align 4, !tbaa !9
  %45 = zext i32 %44 to i64
  %46 = getelementptr inbounds [16 x i32], ptr %43, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !9
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %48, i32 0, i32 2
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = zext i32 %50 to i64
  %52 = getelementptr inbounds [8 x i32], ptr %49, i64 0, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !9
  %54 = xor i32 %53, %47
  store i32 %54, ptr %52, align 4, !tbaa !9
  br label %55

55:                                               ; preds = %41
  %56 = load i32, ptr %7, align 4, !tbaa !9
  %57 = add i32 %56, 1
  store i32 %57, ptr %7, align 4, !tbaa !9
  br label %36, !llvm.loop !13

58:                                               ; preds = %40
  br label %59

59:                                               ; preds = %58
  %60 = load i32, ptr %5, align 4, !tbaa !9
  %61 = add i32 %60, 1
  store i32 %61, ptr %5, align 4, !tbaa !9
  br label %12, !llvm.loop !15

62:                                               ; preds = %16
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %63, i32 0, i32 4
  store i8 0, ptr %64, align 4, !tbaa !11
  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 = { 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 = !{!"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, !7, i64 160}
!12 = !{!"_PBKDF2_HMAC_SHA256_CTX", !7, i64 0, !7, i64 32, !7, i64 64, !7, i64 96, !7, i64 160}
!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"

declare void @sha256_Transform(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @pbkdf2_hmac_sha256_Update(ptr noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = getelementptr inbounds i8, ptr %0, i64 160
  %4 = load i8, ptr %3, align 4, !tbaa !5
  %5 = sext i8 %4 to i32
  %6 = icmp ult i32 %5, %1
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 32
  %8 = getelementptr inbounds i8, ptr %0, i64 96
  %9 = getelementptr inbounds i8, ptr %0, i64 64
  %10 = getelementptr inbounds i8, ptr %0, i64 112
  %11 = getelementptr inbounds i8, ptr %0, i64 80
  br label %12

._crit_edge:                                      ; preds = %12, %2
  store i8 0, ptr %3, align 4, !tbaa !5
  ret void

12:                                               ; preds = %.lr.ph, %12
  %.01619 = phi i32 [ %5, %.lr.ph ], [ %19, %12 ]
  tail call void @sha256_Transform(ptr noundef nonnull %7, ptr noundef nonnull %8, ptr noundef nonnull %8) #2
  tail call void @sha256_Transform(ptr noundef nonnull %0, ptr noundef nonnull %8, ptr noundef nonnull %8) #2
  %13 = load <4 x i32>, ptr %8, align 4, !tbaa !9
  %14 = load <4 x i32>, ptr %9, align 4, !tbaa !9
  %15 = xor <4 x i32> %14, %13
  store <4 x i32> %15, ptr %9, align 4, !tbaa !9
  %16 = load <4 x i32>, ptr %10, align 4, !tbaa !9
  %17 = load <4 x i32>, ptr %11, align 4, !tbaa !9
  %18 = xor <4 x i32> %17, %16
  store <4 x i32> %18, ptr %11, align 4, !tbaa !9
  %19 = add nuw i32 %.01619, 1
  %exitcond.not = icmp eq i32 %19, %1
  br i1 %exitcond.not, label %._crit_edge, label %12, !llvm.loop !11
}

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 = { 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, !7, i64 160}
!6 = !{!"_PBKDF2_HMAC_SHA256_CTX", !7, i64 0, !7, i64 32, !7, i64 64, !7, i64 96, !7, i64 160}
!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"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass