; 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._SHA256_CTX = type { [8 x i32], i64, [16 x i32] }
%struct._PBKDF2_HMAC_SHA256_CTX = type { [8 x i32], [8 x i32], [8 x i32], [16 x i32], i8 }

; Function Attrs: nounwind uwtable
define dso_local void @pbkdf2_hmac_sha256_Init(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3, i32 noundef %4, i32 noundef %5) #0 {
  %7 = alloca ptr, align 8
  %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 %struct._SHA256_CTX, align 8
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i32 %2, ptr %9, align 4, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !5
  store i32 %4, ptr %11, align 4, !tbaa !9
  store i32 %5, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 104, ptr %13) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  %17 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %17, ptr %14, align 4, !tbaa !9
  %18 = load i32, ptr %14, align 4, !tbaa !9
  %19 = lshr i32 %18, 16
  %20 = load i32, ptr %14, align 4, !tbaa !9
  %21 = shl i32 %20, 16
  %22 = or i32 %19, %21
  store i32 %22, ptr %14, align 4, !tbaa !9
  %23 = load i32, ptr %14, align 4, !tbaa !9
  %24 = zext i32 %23 to i64
  %25 = and i64 %24, 4278255360
  %26 = lshr i64 %25, 8
  %27 = load i32, ptr %14, align 4, !tbaa !9
  %28 = zext i32 %27 to i64
  %29 = and i64 %28, 16711935
  %30 = shl i64 %29, 8
  %31 = or i64 %26, %30
  %32 = trunc i64 %31 to i32
  store i32 %32, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = load i32, ptr %9, align 4, !tbaa !9
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %35, i32 0, i32 0
  %37 = getelementptr inbounds [8 x i32], ptr %36, i64 0, i64 0
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %38, i32 0, i32 1
  %40 = getelementptr inbounds [8 x i32], ptr %39, i64 0, i64 0
  call void @hmac_sha256_prepare(ptr noundef %33, i32 noundef %34, ptr noundef %37, ptr noundef %40)
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %41, i32 0, i32 3
  %43 = getelementptr inbounds [16 x i32], ptr %42, i64 0, i64 0
  call void @memzero(ptr noundef %43, i64 noundef 64)
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %44, i32 0, i32 3
  %46 = getelementptr inbounds [16 x i32], ptr %45, i64 0, i64 8
  store i32 -2147483648, ptr %46, align 4, !tbaa !9
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %47, i32 0, i32 3
  %49 = getelementptr inbounds [16 x i32], ptr %48, i64 0, i64 15
  store i32 768, ptr %49, align 4, !tbaa !9
  %50 = getelementptr inbounds %struct._SHA256_CTX, ptr %13, i32 0, i32 0
  %51 = getelementptr inbounds [8 x i32], ptr %50, i64 0, i64 0
  %52 = load ptr, ptr %7, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %52, i32 0, i32 1
  %54 = getelementptr inbounds [8 x i32], ptr %53, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %51, ptr align 4 %54, i64 32, i1 false)
  %55 = getelementptr inbounds %struct._SHA256_CTX, ptr %13, i32 0, i32 1
  store i64 512, ptr %55, align 8, !tbaa !11
  %56 = load ptr, ptr %10, align 8, !tbaa !5
  %57 = load i32, ptr %11, align 4, !tbaa !9
  %58 = sext i32 %57 to i64
  call void @sha256_Update(ptr noundef %13, ptr noundef %56, i64 noundef %58)
  call void @sha256_Update(ptr noundef %13, ptr noundef %12, i64 noundef 4)
  %59 = load ptr, ptr %7, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %59, i32 0, i32 3
  %61 = getelementptr inbounds [16 x i32], ptr %60, i64 0, i64 0
  call void @sha256_Final(ptr noundef %13, ptr noundef %61)
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #4
  store i32 0, ptr %15, align 4, !tbaa !9
  br label %62

62:                                               ; preds = %94, %6
  %63 = load i32, ptr %15, align 4, !tbaa !9
  %64 = zext i32 %63 to i64
  %65 = icmp ult i64 %64, 8
  br i1 %65, label %67, label %66

66:                                               ; preds = %62
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  br label %97

67:                                               ; preds = %62
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #4
  %68 = load ptr, ptr %7, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %68, i32 0, i32 3
  %70 = load i32, ptr %15, align 4, !tbaa !9
  %71 = zext i32 %70 to i64
  %72 = getelementptr inbounds [16 x i32], ptr %69, i64 0, i64 %71
  %73 = load i32, ptr %72, align 4, !tbaa !9
  store i32 %73, ptr %16, align 4, !tbaa !9
  %74 = load i32, ptr %16, align 4, !tbaa !9
  %75 = lshr i32 %74, 16
  %76 = load i32, ptr %16, align 4, !tbaa !9
  %77 = shl i32 %76, 16
  %78 = or i32 %75, %77
  store i32 %78, ptr %16, align 4, !tbaa !9
  %79 = load i32, ptr %16, align 4, !tbaa !9
  %80 = zext i32 %79 to i64
  %81 = and i64 %80, 4278255360
  %82 = lshr i64 %81, 8
  %83 = load i32, ptr %16, align 4, !tbaa !9
  %84 = zext i32 %83 to i64
  %85 = and i64 %84, 16711935
  %86 = shl i64 %85, 8
  %87 = or i64 %82, %86
  %88 = trunc i64 %87 to i32
  %89 = load ptr, ptr %7, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %89, i32 0, i32 3
  %91 = load i32, ptr %15, align 4, !tbaa !9
  %92 = zext i32 %91 to i64
  %93 = getelementptr inbounds [16 x i32], ptr %90, i64 0, i64 %92
  store i32 %88, ptr %93, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #4
  br label %94

94:                                               ; preds = %67
  %95 = load i32, ptr %15, align 4, !tbaa !9
  %96 = add i32 %95, 1
  store i32 %96, ptr %15, align 4, !tbaa !9
  br label %62, !llvm.loop !14

97:                                               ; preds = %66
  %98 = load ptr, ptr %7, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %98, i32 0, i32 0
  %100 = getelementptr inbounds [8 x i32], ptr %99, i64 0, i64 0
  %101 = load ptr, ptr %7, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %101, i32 0, i32 3
  %103 = getelementptr inbounds [16 x i32], ptr %102, i64 0, i64 0
  %104 = load ptr, ptr %7, align 8, !tbaa !5
  %105 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %104, i32 0, i32 3
  %106 = getelementptr inbounds [16 x i32], ptr %105, i64 0, i64 0
  call void @sha256_Transform(ptr noundef %100, ptr noundef %103, ptr noundef %106)
  %107 = load ptr, ptr %7, align 8, !tbaa !5
  %108 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %107, i32 0, i32 2
  %109 = getelementptr inbounds [8 x i32], ptr %108, i64 0, i64 0
  %110 = load ptr, ptr %7, align 8, !tbaa !5
  %111 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %110, i32 0, i32 3
  %112 = getelementptr inbounds [16 x i32], ptr %111, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %109, ptr align 4 %112, i64 32, i1 false)
  %113 = load ptr, ptr %7, align 8, !tbaa !5
  %114 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA256_CTX, ptr %113, i32 0, i32 4
  store i8 1, ptr %114, align 4, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 104, ptr %13) #4
  ret void
}

; 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 @hmac_sha256_prepare(ptr noundef, i32 noundef, ptr noundef, ptr noundef) #2

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

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

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

declare void @sha256_Final(ptr noundef, ptr noundef) #2

declare void @sha256_Transform(ptr noundef, ptr noundef, ptr noundef) #2

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 = { "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 nounwind willreturn memory(argmem: readwrite) }
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, !13, i64 32}
!12 = !{!"_SHA256_CTX", !7, i64 0, !13, i64 32, !7, i64 40}
!13 = !{!"long", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !7, i64 160}
!17 = !{!"_PBKDF2_HMAC_SHA256_CTX", !7, i64 0, !7, i64 32, !7, i64 64, !7, i64 96, !7, i64 160}


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

; Function Attrs: nounwind uwtable
define dso_local void @pbkdf2_hmac_sha256_Init(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3, i32 noundef %4, i32 noundef %5) local_unnamed_addr #0 {
  %7 = alloca i32, align 4
  %8 = alloca %struct._SHA256_CTX, align 8
  call void @llvm.lifetime.start.p0(i64 104, ptr nonnull %8) #5
  %rev = tail call i32 @llvm.bswap.i32(i32 %5)
  store i32 %rev, ptr %7, align 4, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %0, i64 32
  tail call void @hmac_sha256_prepare(ptr noundef %1, i32 noundef %2, ptr noundef %0, ptr noundef nonnull %9) #5
  %10 = getelementptr inbounds i8, ptr %0, i64 96
  tail call void @memzero(ptr noundef nonnull %10, i64 noundef 64) #5
  %11 = getelementptr inbounds i8, ptr %0, i64 128
  store i32 -2147483648, ptr %11, align 4, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %0, i64 156
  store i32 768, ptr %12, align 4, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %8, ptr noundef nonnull align 4 dereferenceable(32) %9, i64 32, i1 false)
  %13 = getelementptr inbounds i8, ptr %8, i64 32
  store i64 512, ptr %13, align 8, !tbaa !9
  %14 = sext i32 %4 to i64
  call void @sha256_Update(ptr noundef nonnull %8, ptr noundef %3, i64 noundef %14) #5
  call void @sha256_Update(ptr noundef nonnull %8, ptr noundef nonnull %7, i64 noundef 4) #5
  call void @sha256_Final(ptr noundef nonnull %8, ptr noundef nonnull %10) #5
  %15 = load <4 x i32>, ptr %10, align 4, !tbaa !5
  %16 = call <4 x i32> @llvm.bswap.v4i32(<4 x i32> %15)
  store <4 x i32> %16, ptr %10, align 4, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %0, i64 112
  %18 = load <4 x i32>, ptr %17, align 4, !tbaa !5
  %19 = call <4 x i32> @llvm.bswap.v4i32(<4 x i32> %18)
  store <4 x i32> %19, ptr %17, align 4, !tbaa !5
  call void @sha256_Transform(ptr noundef nonnull %0, ptr noundef nonnull %10, ptr noundef nonnull %10) #5
  %20 = getelementptr inbounds i8, ptr %0, i64 64
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(32) %20, ptr noundef nonnull align 4 dereferenceable(32) %10, i64 32, i1 false)
  %21 = getelementptr inbounds i8, ptr %0, i64 160
  store i8 1, ptr %21, align 4, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 104, ptr nonnull %8) #5
  ret void
}

; 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 @hmac_sha256_prepare(ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

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

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

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

declare void @sha256_Final(ptr noundef, ptr noundef) local_unnamed_addr #2

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

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <4 x i32> @llvm.bswap.v4i32(<4 x i32>) #4

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 = { "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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 32}
!10 = !{!"_SHA256_CTX", !7, i64 0, !11, i64 32, !7, i64 40}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !7, i64 160}
!13 = !{!"_PBKDF2_HMAC_SHA256_CTX", !7, i64 0, !7, i64 32, !7, i64 64, !7, i64 96, !7, i64 160}


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