; 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._SHA512_CTX = type { [8 x i64], [2 x i64], [16 x i64] }
%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_Init(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3, i32 noundef %4, i32 noundef %5) #3 {
  %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._SHA512_CTX, align 8
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i64, align 8
  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 208, 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_SHA512_CTX, ptr %35, i32 0, i32 0
  %37 = getelementptr inbounds [8 x i64], ptr %36, i64 0, i64 0
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA512_CTX, ptr %38, i32 0, i32 1
  %40 = getelementptr inbounds [8 x i64], ptr %39, i64 0, i64 0
  call void @hmac_sha512_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_SHA512_CTX, ptr %41, i32 0, i32 3
  %43 = getelementptr inbounds [16 x i64], ptr %42, i64 0, i64 0
  call void @memzero(ptr noundef %43, i64 noundef 128)
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA512_CTX, ptr %44, i32 0, i32 3
  %46 = getelementptr inbounds [16 x i64], ptr %45, i64 0, i64 8
  store i64 -9223372036854775808, ptr %46, align 8, !tbaa !11
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA512_CTX, ptr %47, i32 0, i32 3
  %49 = getelementptr inbounds [16 x i64], ptr %48, i64 0, i64 15
  store i64 1536, ptr %49, align 8, !tbaa !11
  %50 = getelementptr inbounds %struct._SHA512_CTX, ptr %13, i32 0, i32 0
  %51 = getelementptr inbounds [8 x i64], ptr %50, i64 0, i64 0
  %52 = load ptr, ptr %7, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA512_CTX, ptr %52, i32 0, i32 1
  %54 = getelementptr inbounds [8 x i64], ptr %53, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %51, ptr align 8 %54, i64 64, i1 false)
  %55 = getelementptr inbounds %struct._SHA512_CTX, ptr %13, i32 0, i32 1
  %56 = getelementptr inbounds [2 x i64], ptr %55, i64 0, i64 0
  store i64 1024, ptr %56, align 8, !tbaa !11
  %57 = getelementptr inbounds %struct._SHA512_CTX, ptr %13, i32 0, i32 1
  %58 = getelementptr inbounds [2 x i64], ptr %57, i64 0, i64 1
  store i64 0, ptr %58, align 8, !tbaa !11
  %59 = load ptr, ptr %10, align 8, !tbaa !5
  %60 = load i32, ptr %11, align 4, !tbaa !9
  %61 = sext i32 %60 to i64
  call void @sha512_Update(ptr noundef %13, ptr noundef %59, i64 noundef %61)
  call void @sha512_Update(ptr noundef %13, ptr noundef %12, i64 noundef 4)
  %62 = load ptr, ptr %7, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA512_CTX, ptr %62, i32 0, i32 3
  %64 = getelementptr inbounds [16 x i64], ptr %63, i64 0, i64 0
  call void @sha512_Final(ptr noundef %13, ptr noundef %64)
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #4
  store i32 0, ptr %15, align 4, !tbaa !9
  br label %65

65:                                               ; preds = %101, %6
  %66 = load i32, ptr %15, align 4, !tbaa !9
  %67 = zext i32 %66 to i64
  %68 = icmp ult i64 %67, 8
  br i1 %68, label %70, label %69

69:                                               ; preds = %65
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  br label %104

70:                                               ; preds = %65
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  %71 = load ptr, ptr %7, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA512_CTX, ptr %71, i32 0, i32 3
  %73 = load i32, ptr %15, align 4, !tbaa !9
  %74 = zext i32 %73 to i64
  %75 = getelementptr inbounds [16 x i64], ptr %72, i64 0, i64 %74
  %76 = load i64, ptr %75, align 8, !tbaa !11
  store i64 %76, ptr %16, align 8, !tbaa !11
  %77 = load i64, ptr %16, align 8, !tbaa !11
  %78 = lshr i64 %77, 32
  %79 = load i64, ptr %16, align 8, !tbaa !11
  %80 = shl i64 %79, 32
  %81 = or i64 %78, %80
  store i64 %81, ptr %16, align 8, !tbaa !11
  %82 = load i64, ptr %16, align 8, !tbaa !11
  %83 = and i64 %82, -71777214294589696
  %84 = lshr i64 %83, 8
  %85 = load i64, ptr %16, align 8, !tbaa !11
  %86 = and i64 %85, 71777214294589695
  %87 = shl i64 %86, 8
  %88 = or i64 %84, %87
  store i64 %88, ptr %16, align 8, !tbaa !11
  %89 = load i64, ptr %16, align 8, !tbaa !11
  %90 = and i64 %89, -281470681808896
  %91 = lshr i64 %90, 16
  %92 = load i64, ptr %16, align 8, !tbaa !11
  %93 = and i64 %92, 281470681808895
  %94 = shl i64 %93, 16
  %95 = or i64 %91, %94
  %96 = load ptr, ptr %7, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA512_CTX, ptr %96, i32 0, i32 3
  %98 = load i32, ptr %15, align 4, !tbaa !9
  %99 = zext i32 %98 to i64
  %100 = getelementptr inbounds [16 x i64], ptr %97, i64 0, i64 %99
  store i64 %95, ptr %100, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  br label %101

101:                                              ; preds = %70
  %102 = load i32, ptr %15, align 4, !tbaa !9
  %103 = add i32 %102, 1
  store i32 %103, ptr %15, align 4, !tbaa !9
  br label %65, !llvm.loop !13

104:                                              ; preds = %69
  %105 = load ptr, ptr %7, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA512_CTX, ptr %105, i32 0, i32 0
  %107 = getelementptr inbounds [8 x i64], ptr %106, i64 0, i64 0
  %108 = load ptr, ptr %7, align 8, !tbaa !5
  %109 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA512_CTX, ptr %108, i32 0, i32 3
  %110 = getelementptr inbounds [16 x i64], ptr %109, i64 0, i64 0
  %111 = load ptr, ptr %7, align 8, !tbaa !5
  %112 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA512_CTX, ptr %111, i32 0, i32 3
  %113 = getelementptr inbounds [16 x i64], ptr %112, i64 0, i64 0
  call void @sha512_Transform(ptr noundef %107, ptr noundef %110, ptr noundef %113)
  %114 = load ptr, ptr %7, align 8, !tbaa !5
  %115 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA512_CTX, ptr %114, i32 0, i32 2
  %116 = getelementptr inbounds [8 x i64], ptr %115, i64 0, i64 0
  %117 = load ptr, ptr %7, align 8, !tbaa !5
  %118 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA512_CTX, ptr %117, i32 0, i32 3
  %119 = getelementptr inbounds [16 x i64], ptr %118, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %116, ptr align 8 %119, i64 64, i1 false)
  %120 = load ptr, ptr %7, align 8, !tbaa !5
  %121 = getelementptr inbounds %struct._PBKDF2_HMAC_SHA512_CTX, ptr %120, i32 0, i32 4
  store i8 1, ptr %121, align 8, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 208, ptr %13) #4
  ret void
}

declare void @hmac_sha512_prepare(ptr noundef, i32 noundef, ptr noundef, ptr noundef) #1

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

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

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 = !{!16, !7, i64 320}
!16 = !{!"_PBKDF2_HMAC_SHA512_CTX", !7, i64 0, !7, i64 64, !7, i64 128, !7, i64 192, !7, i64 320}


; 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] }

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

; Function Attrs: mustprogress 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) 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: nounwind uwtable
define dso_local void @pbkdf2_hmac_sha512_Init(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3, i32 noundef %4, i32 noundef %5) local_unnamed_addr #3 {
  %7 = alloca i32, align 4
  %8 = alloca %struct._SHA512_CTX, align 8
  call void @llvm.lifetime.start.p0(i64 208, 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 64
  tail call void @hmac_sha512_prepare(ptr noundef %1, i32 noundef %2, ptr noundef %0, ptr noundef nonnull %9) #5
  %10 = getelementptr inbounds i8, ptr %0, i64 192
  tail call void @memzero(ptr noundef nonnull %10, i64 noundef 128) #5
  %11 = getelementptr inbounds i8, ptr %0, i64 256
  store i64 -9223372036854775808, ptr %11, align 8, !tbaa !9
  %12 = getelementptr inbounds i8, ptr %0, i64 312
  store i64 1536, ptr %12, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(64) %8, ptr noundef nonnull align 8 dereferenceable(64) %9, i64 64, i1 false)
  %13 = getelementptr inbounds i8, ptr %8, i64 64
  store i64 1024, ptr %13, align 8, !tbaa !9
  %14 = getelementptr inbounds i8, ptr %8, i64 72
  store i64 0, ptr %14, align 8, !tbaa !9
  %15 = sext i32 %4 to i64
  call void @sha512_Update(ptr noundef nonnull %8, ptr noundef %3, i64 noundef %15) #5
  call void @sha512_Update(ptr noundef nonnull %8, ptr noundef nonnull %7, i64 noundef 4) #5
  call void @sha512_Final(ptr noundef nonnull %8, ptr noundef nonnull %10) #5
  %16 = load i64, ptr %10, align 8, !tbaa !9
  %17 = call i64 @llvm.bswap.i64(i64 %16)
  store i64 %17, ptr %10, align 8, !tbaa !9
  %18 = getelementptr inbounds i8, ptr %0, i64 200
  %19 = load i64, ptr %18, align 8, !tbaa !9
  %20 = call i64 @llvm.bswap.i64(i64 %19)
  store i64 %20, ptr %18, align 8, !tbaa !9
  %21 = getelementptr inbounds i8, ptr %0, i64 208
  %22 = load i64, ptr %21, align 8, !tbaa !9
  %23 = call i64 @llvm.bswap.i64(i64 %22)
  store i64 %23, ptr %21, align 8, !tbaa !9
  %24 = getelementptr inbounds i8, ptr %0, i64 216
  %25 = load i64, ptr %24, align 8, !tbaa !9
  %26 = call i64 @llvm.bswap.i64(i64 %25)
  store i64 %26, ptr %24, align 8, !tbaa !9
  %27 = getelementptr inbounds i8, ptr %0, i64 224
  %28 = load i64, ptr %27, align 8, !tbaa !9
  %29 = call i64 @llvm.bswap.i64(i64 %28)
  store i64 %29, ptr %27, align 8, !tbaa !9
  %30 = getelementptr inbounds i8, ptr %0, i64 232
  %31 = load i64, ptr %30, align 8, !tbaa !9
  %32 = call i64 @llvm.bswap.i64(i64 %31)
  store i64 %32, ptr %30, align 8, !tbaa !9
  %33 = getelementptr inbounds i8, ptr %0, i64 240
  %34 = load i64, ptr %33, align 8, !tbaa !9
  %35 = call i64 @llvm.bswap.i64(i64 %34)
  store i64 %35, ptr %33, align 8, !tbaa !9
  %36 = getelementptr inbounds i8, ptr %0, i64 248
  %37 = load i64, ptr %36, align 8, !tbaa !9
  %38 = call i64 @llvm.bswap.i64(i64 %37)
  store i64 %38, ptr %36, align 8, !tbaa !9
  call void @sha512_Transform(ptr noundef nonnull %0, ptr noundef nonnull %10, ptr noundef nonnull %10) #5
  %39 = getelementptr inbounds i8, ptr %0, i64 128
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(64) %39, ptr noundef nonnull align 8 dereferenceable(64) %10, i64 64, i1 false)
  %40 = getelementptr inbounds i8, ptr %0, i64 320
  store i8 1, ptr %40, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 208, ptr nonnull %8) #5
  ret void
}

declare void @hmac_sha512_prepare(ptr noundef, i32 noundef, ptr noundef, 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

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 i32 @llvm.bswap.i32(i32) #4

; 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !7, i64 320}
!12 = !{!"_PBKDF2_HMAC_SHA512_CTX", !7, i64 0, !7, i64 64, !7, i64 128, !7, i64 192, !7, i64 320}


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