; LLVM IR from square/subzero :: core/trezor-crypto (hmac)

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

@hmac_sha256_prepare.key_pad = external hidden global [16 x i32], align 16
@hmac_sha256_prepare.context = external hidden global %struct._SHA256_CTX, align 8
@sha256_initial_hash_value = external constant [8 x i32], align 16

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

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

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

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

declare void @sha256_Init(ptr noundef) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local void @hmac_sha256_prepare(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) #3 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @memzero(ptr noundef @hmac_sha256_prepare.key_pad, i64 noundef 64)
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = icmp ugt i32 %13, 64
  br i1 %14, label %15, label %19

15:                                               ; preds = %4
  call void @sha256_Init(ptr noundef @hmac_sha256_prepare.context)
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = zext i32 %17 to i64
  call void @sha256_Update(ptr noundef @hmac_sha256_prepare.context, ptr noundef %16, i64 noundef %18)
  call void @sha256_Final(ptr noundef @hmac_sha256_prepare.context, ptr noundef @hmac_sha256_prepare.key_pad)
  br label %23

19:                                               ; preds = %4
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = zext i32 %21 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 @hmac_sha256_prepare.key_pad, ptr align 1 %20, i64 %22, i1 false)
  br label %23

23:                                               ; preds = %19, %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %53, %23
  %25 = load i32, ptr %9, align 4, !tbaa !9
  %26 = icmp slt i32 %25, 16
  br i1 %26, label %28, label %27

27:                                               ; preds = %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %56

28:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [16 x i32], ptr @hmac_sha256_prepare.key_pad, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !9
  store i32 %32, ptr %11, align 4, !tbaa !9
  %33 = load i32, ptr %11, align 4, !tbaa !9
  %34 = lshr i32 %33, 16
  %35 = load i32, ptr %11, align 4, !tbaa !9
  %36 = shl i32 %35, 16
  %37 = or i32 %34, %36
  store i32 %37, ptr %11, align 4, !tbaa !9
  %38 = load i32, ptr %11, align 4, !tbaa !9
  %39 = zext i32 %38 to i64
  %40 = and i64 %39, 4278255360
  %41 = lshr i64 %40, 8
  %42 = load i32, ptr %11, align 4, !tbaa !9
  %43 = zext i32 %42 to i64
  %44 = and i64 %43, 16711935
  %45 = shl i64 %44, 8
  %46 = or i64 %41, %45
  %47 = trunc i64 %46 to i32
  store i32 %47, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  %48 = load i32, ptr %10, align 4, !tbaa !9
  %49 = xor i32 %48, 1549556828
  %50 = load i32, ptr %9, align 4, !tbaa !9
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [16 x i32], ptr @hmac_sha256_prepare.key_pad, i64 0, i64 %51
  store i32 %49, ptr %52, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  br label %53

53:                                               ; preds = %28
  %54 = load i32, ptr %9, align 4, !tbaa !9
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %9, align 4, !tbaa !9
  br label %24, !llvm.loop !11

56:                                               ; preds = %27
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  call void @sha256_Transform(ptr noundef @sha256_initial_hash_value, ptr noundef @hmac_sha256_prepare.key_pad, ptr noundef %57)
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %58

58:                                               ; preds = %72, %56
  %59 = load i32, ptr %12, align 4, !tbaa !9
  %60 = icmp slt i32 %59, 16
  br i1 %60, label %62, label %61

61:                                               ; preds = %58
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  br label %75

62:                                               ; preds = %58
  %63 = load i32, ptr %12, align 4, !tbaa !9
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [16 x i32], ptr @hmac_sha256_prepare.key_pad, i64 0, i64 %64
  %66 = load i32, ptr %65, align 4, !tbaa !9
  %67 = xor i32 %66, 1549556828
  %68 = xor i32 %67, 909522486
  %69 = load i32, ptr %12, align 4, !tbaa !9
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [16 x i32], ptr @hmac_sha256_prepare.key_pad, i64 0, i64 %70
  store i32 %68, ptr %71, align 4, !tbaa !9
  br label %72

72:                                               ; preds = %62
  %73 = load i32, ptr %12, align 4, !tbaa !9
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %12, align 4, !tbaa !9
  br label %58, !llvm.loop !13

75:                                               ; preds = %61
  %76 = load ptr, ptr %8, align 8, !tbaa !5
  call void @sha256_Transform(ptr noundef @sha256_initial_hash_value, ptr noundef @hmac_sha256_prepare.key_pad, ptr noundef %76)
  call void @memzero(ptr noundef @hmac_sha256_prepare.key_pad, i64 noundef 64)
  ret void
}

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

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync 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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

@hmac_sha256_prepare.key_pad = external hidden global [16 x i32], align 16
@hmac_sha256_prepare.context = external hidden global %struct._SHA256_CTX, align 8
@sha256_initial_hash_value = external constant [8 x i32], align 16

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

declare void @sha256_Init(ptr noundef) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
define dso_local void @hmac_sha256_prepare(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #2 {
  tail call void @memzero(ptr noundef nonnull @hmac_sha256_prepare.key_pad, i64 noundef 64) #4
  %5 = icmp ugt i32 %1, 64
  br i1 %5, label %6, label %8

6:                                                ; preds = %4
  tail call void @sha256_Init(ptr noundef nonnull @hmac_sha256_prepare.context) #4
  %7 = zext i32 %1 to i64
  tail call void @sha256_Update(ptr noundef nonnull @hmac_sha256_prepare.context, ptr noundef %0, i64 noundef %7) #4
  tail call void @sha256_Final(ptr noundef nonnull @hmac_sha256_prepare.context, ptr noundef nonnull @hmac_sha256_prepare.key_pad) #4
  br label %10

8:                                                ; preds = %4
  %9 = zext nneg i32 %1 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 @hmac_sha256_prepare.key_pad, ptr align 1 %0, i64 %9, i1 false)
  br label %10

10:                                               ; preds = %8, %6
  %11 = load <4 x i32>, ptr @hmac_sha256_prepare.key_pad, align 16, !tbaa !5
  %12 = xor <4 x i32> %11, <i32 1549556828, i32 1549556828, i32 1549556828, i32 1549556828>
  %13 = tail call <4 x i32> @llvm.bswap.v4i32(<4 x i32> %12)
  store <4 x i32> %13, ptr @hmac_sha256_prepare.key_pad, align 16, !tbaa !5
  %14 = load <4 x i32>, ptr getelementptr inbounds (i8, ptr @hmac_sha256_prepare.key_pad, i64 16), align 16, !tbaa !5
  %15 = xor <4 x i32> %14, <i32 1549556828, i32 1549556828, i32 1549556828, i32 1549556828>
  %16 = tail call <4 x i32> @llvm.bswap.v4i32(<4 x i32> %15)
  store <4 x i32> %16, ptr getelementptr inbounds (i8, ptr @hmac_sha256_prepare.key_pad, i64 16), align 16, !tbaa !5
  %17 = load <4 x i32>, ptr getelementptr inbounds (i8, ptr @hmac_sha256_prepare.key_pad, i64 32), align 16, !tbaa !5
  %18 = xor <4 x i32> %17, <i32 1549556828, i32 1549556828, i32 1549556828, i32 1549556828>
  %19 = tail call <4 x i32> @llvm.bswap.v4i32(<4 x i32> %18)
  store <4 x i32> %19, ptr getelementptr inbounds (i8, ptr @hmac_sha256_prepare.key_pad, i64 32), align 16, !tbaa !5
  %20 = load <4 x i32>, ptr getelementptr inbounds (i8, ptr @hmac_sha256_prepare.key_pad, i64 48), align 16, !tbaa !5
  %21 = xor <4 x i32> %20, <i32 1549556828, i32 1549556828, i32 1549556828, i32 1549556828>
  %22 = tail call <4 x i32> @llvm.bswap.v4i32(<4 x i32> %21)
  store <4 x i32> %22, ptr getelementptr inbounds (i8, ptr @hmac_sha256_prepare.key_pad, i64 48), align 16, !tbaa !5
  tail call void @sha256_Transform(ptr noundef nonnull @sha256_initial_hash_value, ptr noundef nonnull @hmac_sha256_prepare.key_pad, ptr noundef %2) #4
  %23 = load <4 x i32>, ptr @hmac_sha256_prepare.key_pad, align 16, !tbaa !5
  %24 = xor <4 x i32> %23, <i32 1785358954, i32 1785358954, i32 1785358954, i32 1785358954>
  store <4 x i32> %24, ptr @hmac_sha256_prepare.key_pad, align 16, !tbaa !5
  %25 = load <4 x i32>, ptr getelementptr inbounds (i8, ptr @hmac_sha256_prepare.key_pad, i64 16), align 16, !tbaa !5
  %26 = xor <4 x i32> %25, <i32 1785358954, i32 1785358954, i32 1785358954, i32 1785358954>
  store <4 x i32> %26, ptr getelementptr inbounds (i8, ptr @hmac_sha256_prepare.key_pad, i64 16), align 16, !tbaa !5
  %27 = load <4 x i32>, ptr getelementptr inbounds (i8, ptr @hmac_sha256_prepare.key_pad, i64 32), align 16, !tbaa !5
  %28 = xor <4 x i32> %27, <i32 1785358954, i32 1785358954, i32 1785358954, i32 1785358954>
  store <4 x i32> %28, ptr getelementptr inbounds (i8, ptr @hmac_sha256_prepare.key_pad, i64 32), align 16, !tbaa !5
  %29 = load <4 x i32>, ptr getelementptr inbounds (i8, ptr @hmac_sha256_prepare.key_pad, i64 48), align 16, !tbaa !5
  %30 = xor <4 x i32> %29, <i32 1785358954, i32 1785358954, i32 1785358954, i32 1785358954>
  store <4 x i32> %30, ptr getelementptr inbounds (i8, ptr @hmac_sha256_prepare.key_pad, i64 48), align 16, !tbaa !5
  tail call void @sha256_Transform(ptr noundef nonnull @sha256_initial_hash_value, ptr noundef nonnull @hmac_sha256_prepare.key_pad, ptr noundef %3) #4
  tail call void @memzero(ptr noundef nonnull @hmac_sha256_prepare.key_pad, i64 noundef 64) #4
  ret void
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <4 x i32> @llvm.bswap.v4i32(<4 x i32>) #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


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