; LLVM IR from square/subzero :: core/trezor-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: nounwind uwtable
declare dso_local void @pbkdf2_hmac_sha256_Init(ptr noundef, ptr noundef, i32 noundef, ptr noundef, i32 noundef, i32 noundef) #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

; 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
declare dso_local void @pbkdf2_hmac_sha256_Update(ptr noundef, i32 noundef) #0

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

; Function Attrs: nounwind uwtable
define dso_local void @pbkdf2_hmac_sha256(ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %4, ptr noundef %5, i32 noundef %6) #0 {
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  %17 = alloca i32, align 4
  %18 = alloca %struct._PBKDF2_HMAC_SHA256_CTX, align 4
  %19 = alloca [32 x i8], align 16
  %20 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store i32 %1, ptr %9, align 4, !tbaa !9
  store ptr %2, ptr %10, align 8, !tbaa !5
  store i32 %3, ptr %11, align 4, !tbaa !9
  store i32 %4, ptr %12, align 4, !tbaa !9
  store ptr %5, ptr %13, align 8, !tbaa !5
  store i32 %6, ptr %14, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  %21 = load i32, ptr %14, align 4, !tbaa !9
  %22 = srem i32 %21, 32
  store i32 %22, ptr %15, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #3
  %23 = load i32, ptr %14, align 4, !tbaa !9
  %24 = sdiv i32 %23, 32
  store i32 %24, ptr %16, align 4, !tbaa !9
  %25 = load i32, ptr %15, align 4, !tbaa !9
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %30

27:                                               ; preds = %7
  %28 = load i32, ptr %16, align 4, !tbaa !9
  %29 = add i32 %28, 1
  store i32 %29, ptr %16, align 4, !tbaa !9
  br label %31

30:                                               ; preds = %7
  store i32 32, ptr %15, align 4, !tbaa !9
  br label %31

31:                                               ; preds = %30, %27
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #3
  store i32 1, ptr %17, align 4, !tbaa !9
  br label %32

32:                                               ; preds = %66, %31
  %33 = load i32, ptr %17, align 4, !tbaa !9
  %34 = load i32, ptr %16, align 4, !tbaa !9
  %35 = icmp ule i32 %33, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #3
  br label %69

37:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 164, ptr %18) #3
  %38 = load ptr, ptr %8, align 8, !tbaa !5
  %39 = load i32, ptr %9, align 4, !tbaa !9
  %40 = load ptr, ptr %10, align 8, !tbaa !5
  %41 = load i32, ptr %11, align 4, !tbaa !9
  %42 = load i32, ptr %17, align 4, !tbaa !9
  call void @pbkdf2_hmac_sha256_Init(ptr noundef %18, ptr noundef %38, i32 noundef %39, ptr noundef %40, i32 noundef %41, i32 noundef %42)
  %43 = load i32, ptr %12, align 4, !tbaa !9
  call void @pbkdf2_hmac_sha256_Update(ptr noundef %18, i32 noundef %43)
  call void @llvm.lifetime.start.p0(i64 32, ptr %19) #3
  %44 = getelementptr inbounds [32 x i8], ptr %19, i64 0, i64 0
  call void @pbkdf2_hmac_sha256_Final(ptr noundef %18, ptr noundef %44)
  call void @llvm.lifetime.start.p0(i64 4, ptr %20) #3
  %45 = load i32, ptr %17, align 4, !tbaa !9
  %46 = sub i32 %45, 1
  %47 = mul i32 %46, 32
  store i32 %47, ptr %20, align 4, !tbaa !9
  %48 = load i32, ptr %17, align 4, !tbaa !9
  %49 = load i32, ptr %16, align 4, !tbaa !9
  %50 = icmp ult i32 %48, %49
  br i1 %50, label %51, label %57

51:                                               ; preds = %37
  %52 = load ptr, ptr %13, align 8, !tbaa !5
  %53 = load i32, ptr %20, align 4, !tbaa !9
  %54 = zext i32 %53 to i64
  %55 = getelementptr inbounds i8, ptr %52, i64 %54
  %56 = getelementptr inbounds [32 x i8], ptr %19, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %55, ptr align 16 %56, i64 32, i1 false)
  br label %65

57:                                               ; preds = %37
  %58 = load ptr, ptr %13, align 8, !tbaa !5
  %59 = load i32, ptr %20, align 4, !tbaa !9
  %60 = zext i32 %59 to i64
  %61 = getelementptr inbounds i8, ptr %58, i64 %60
  %62 = getelementptr inbounds [32 x i8], ptr %19, i64 0, i64 0
  %63 = load i32, ptr %15, align 4, !tbaa !9
  %64 = zext i32 %63 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %61, ptr align 16 %62, i64 %64, i1 false)
  br label %65

65:                                               ; preds = %57, %51
  call void @llvm.lifetime.end.p0(i64 4, ptr %20) #3
  call void @llvm.lifetime.end.p0(i64 32, ptr %19) #3
  call void @llvm.lifetime.end.p0(i64 164, ptr %18) #3
  br label %66

66:                                               ; preds = %65
  %67 = load i32, ptr %17, align 4, !tbaa !9
  %68 = add i32 %67, 1
  store i32 %68, ptr %17, align 4, !tbaa !9
  br label %32, !llvm.loop !11

69:                                               ; preds = %36
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  ret void
}

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = 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._PBKDF2_HMAC_SHA256_CTX = type { [8 x i32], [8 x i32], [8 x i32], [16 x i32], i8 }

; Function Attrs: nounwind uwtable
declare dso_local void @pbkdf2_hmac_sha256_Init(ptr noundef, ptr noundef, i32 noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #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

; 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: nounwind uwtable
declare dso_local void @pbkdf2_hmac_sha256_Update(ptr noundef, i32 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local void @pbkdf2_hmac_sha256(ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %4, ptr nocapture noundef writeonly %5, i32 noundef %6) local_unnamed_addr #0 {
  %8 = alloca %struct._PBKDF2_HMAC_SHA256_CTX, align 4
  %9 = alloca [32 x i8], align 16
  %10 = srem i32 %6, 32
  %11 = sdiv i32 %6, 32
  %.not = icmp ne i32 %10, 0
  %12 = zext i1 %.not to i32
  %.021 = add nsw i32 %11, %12
  %narrow = select i1 %.not, i32 %10, i32 32
  %.0 = zext i32 %narrow to i64
  %.not2324 = icmp eq i32 %.021, 0
  br i1 %.not2324, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %20, %7
  ret void

.lr.ph:                                           ; preds = %7, %20
  %.02025 = phi i32 [ %21, %20 ], [ 1, %7 ]
  call void @llvm.lifetime.start.p0(i64 164, ptr nonnull %8) #3
  call void @pbkdf2_hmac_sha256_Init(ptr noundef nonnull %8, ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %.02025)
  call void @pbkdf2_hmac_sha256_Update(ptr noundef nonnull %8, i32 noundef %4)
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %9) #3
  call void @pbkdf2_hmac_sha256_Final(ptr noundef nonnull %8, ptr noundef nonnull %9)
  %13 = shl i32 %.02025, 5
  %14 = add i32 %13, -32
  %15 = icmp ult i32 %.02025, %.021
  %16 = zext i32 %14 to i64
  %17 = getelementptr inbounds i8, ptr %5, i64 %16
  br i1 %15, label %18, label %19

18:                                               ; preds = %.lr.ph
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(32) %17, ptr noundef nonnull align 16 dereferenceable(32) %9, i64 32, i1 false)
  br label %20

19:                                               ; preds = %.lr.ph
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(1) %17, ptr noundef nonnull align 16 dereferenceable(1) %9, i64 %.0, i1 false)
  br label %20

20:                                               ; preds = %19, %18
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %9) #3
  call void @llvm.lifetime.end.p0(i64 164, ptr nonnull %8) #3
  %21 = add i32 %.02025, 1
  %.not23 = icmp ugt i32 %21, %.021
  br i1 %.not23, label %._crit_edge, label %.lr.ph, !llvm.loop !5
}

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass