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

@hmac_sha256_Init.i_key_pad = external hidden global [64 x i8], align 16

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

10:                                               ; preds = %3
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = zext i32 %12 to i64
  call void @sha256_Raw(ptr noundef %11, i64 noundef %13, ptr noundef @hmac_sha256_Init.i_key_pad)
  br label %18

14:                                               ; preds = %3
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = zext i32 %16 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 @hmac_sha256_Init.i_key_pad, ptr align 1 %15, i64 %17, i1 false)
  br label %18

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

19:                                               ; preds = %43, %18
  %20 = load i32, ptr %7, align 4, !tbaa !9
  %21 = icmp slt i32 %20, 64
  br i1 %21, label %23, label %22

22:                                               ; preds = %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %46

23:                                               ; preds = %19
  %24 = load i32, ptr %7, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [64 x i8], ptr @hmac_sha256_Init.i_key_pad, i64 0, i64 %25
  %27 = load i8, ptr %26, align 1, !tbaa !11
  %28 = zext i8 %27 to i32
  %29 = xor i32 %28, 92
  %30 = trunc i32 %29 to i8
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct._HMAC_SHA256_CTX, ptr %31, i32 0, i32 0
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [64 x i8], ptr %32, i64 0, i64 %34
  store i8 %30, ptr %35, align 1, !tbaa !11
  %36 = load i32, ptr %7, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [64 x i8], ptr @hmac_sha256_Init.i_key_pad, i64 0, i64 %37
  %39 = load i8, ptr %38, align 1, !tbaa !11
  %40 = zext i8 %39 to i32
  %41 = xor i32 %40, 54
  %42 = trunc i32 %41 to i8
  store i8 %42, ptr %38, align 1, !tbaa !11
  br label %43

43:                                               ; preds = %23
  %44 = load i32, ptr %7, align 4, !tbaa !9
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %7, align 4, !tbaa !9
  br label %19, !llvm.loop !12

46:                                               ; preds = %22
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct._HMAC_SHA256_CTX, ptr %47, i32 0, i32 1
  call void @sha256_Init(ptr noundef %48)
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct._HMAC_SHA256_CTX, ptr %49, i32 0, i32 1
  call void @sha256_Update(ptr noundef %50, ptr noundef @hmac_sha256_Init.i_key_pad, i64 noundef 64)
  call void @memzero(ptr noundef @hmac_sha256_Init.i_key_pad, i64 noundef 64)
  ret void
}

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

declare void @sha256_Raw(ptr noundef, i64 noundef, ptr 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #3

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

declare void @sha256_Init(ptr noundef) #1

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

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 = { "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 = { nocallback nofree nosync 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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

@hmac_sha256_Init.i_key_pad = external hidden global [64 x i8], align 16

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

6:                                                ; preds = %3
  tail call void @sha256_Raw(ptr noundef %1, i64 noundef %5, ptr noundef nonnull @hmac_sha256_Init.i_key_pad) #3
  br label %vector.memcheck

7:                                                ; preds = %3
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 @hmac_sha256_Init.i_key_pad, ptr align 1 %1, i64 %5, i1 false)
  br label %vector.memcheck

vector.memcheck:                                  ; preds = %6, %7
  %scevgep = getelementptr i8, ptr %0, i64 64
  %bound0 = icmp ult ptr %0, getelementptr inbounds (i8, ptr @hmac_sha256_Init.i_key_pad, i64 64)
  %bound1 = icmp ugt ptr %scevgep, @hmac_sha256_Init.i_key_pad
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %scalar.ph, label %vector.body

vector.body:                                      ; preds = %vector.memcheck
  %wide.load = load <16 x i8>, ptr @hmac_sha256_Init.i_key_pad, align 16, !tbaa !5, !alias.scope !8
  %wide.load15 = load <16 x i8>, ptr getelementptr inbounds (i8, ptr @hmac_sha256_Init.i_key_pad, i64 16), align 16, !tbaa !5, !alias.scope !8
  %8 = xor <16 x i8> %wide.load, <i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92>
  %9 = xor <16 x i8> %wide.load15, <i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92>
  %10 = getelementptr inbounds i8, ptr %0, i64 16
  store <16 x i8> %8, ptr %0, align 1, !tbaa !5, !alias.scope !11, !noalias !8
  store <16 x i8> %9, ptr %10, align 1, !tbaa !5, !alias.scope !11, !noalias !8
  %wide.load16 = load <16 x i8>, ptr @hmac_sha256_Init.i_key_pad, align 16, !tbaa !5, !alias.scope !8
  %wide.load17 = load <16 x i8>, ptr getelementptr inbounds (i8, ptr @hmac_sha256_Init.i_key_pad, i64 16), align 16, !tbaa !5, !alias.scope !8
  %11 = xor <16 x i8> %wide.load16, <i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54>
  %12 = xor <16 x i8> %wide.load17, <i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54>
  store <16 x i8> %11, ptr @hmac_sha256_Init.i_key_pad, align 16, !tbaa !5, !alias.scope !8
  store <16 x i8> %12, ptr getelementptr inbounds (i8, ptr @hmac_sha256_Init.i_key_pad, i64 16), align 16, !tbaa !5, !alias.scope !8
  %wide.load.1 = load <16 x i8>, ptr getelementptr inbounds (i8, ptr @hmac_sha256_Init.i_key_pad, i64 32), align 16, !tbaa !5, !alias.scope !8
  %wide.load15.1 = load <16 x i8>, ptr getelementptr inbounds (i8, ptr @hmac_sha256_Init.i_key_pad, i64 48), align 16, !tbaa !5, !alias.scope !8
  %13 = xor <16 x i8> %wide.load.1, <i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92>
  %14 = xor <16 x i8> %wide.load15.1, <i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92, i8 92>
  %15 = getelementptr inbounds i8, ptr %0, i64 32
  %16 = getelementptr inbounds i8, ptr %0, i64 48
  store <16 x i8> %13, ptr %15, align 1, !tbaa !5, !alias.scope !11, !noalias !8
  store <16 x i8> %14, ptr %16, align 1, !tbaa !5, !alias.scope !11, !noalias !8
  %wide.load16.1 = load <16 x i8>, ptr getelementptr inbounds (i8, ptr @hmac_sha256_Init.i_key_pad, i64 32), align 16, !tbaa !5, !alias.scope !8
  %wide.load17.1 = load <16 x i8>, ptr getelementptr inbounds (i8, ptr @hmac_sha256_Init.i_key_pad, i64 48), align 16, !tbaa !5, !alias.scope !8
  %17 = xor <16 x i8> %wide.load16.1, <i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54>
  %18 = xor <16 x i8> %wide.load17.1, <i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54, i8 54>
  store <16 x i8> %17, ptr getelementptr inbounds (i8, ptr @hmac_sha256_Init.i_key_pad, i64 32), align 16, !tbaa !5, !alias.scope !8
  store <16 x i8> %18, ptr getelementptr inbounds (i8, ptr @hmac_sha256_Init.i_key_pad, i64 48), align 16, !tbaa !5, !alias.scope !8
  br label %middle.block

middle.block:                                     ; preds = %scalar.ph, %vector.body
  %19 = getelementptr inbounds i8, ptr %0, i64 64
  tail call void @sha256_Init(ptr noundef nonnull %19) #3
  tail call void @sha256_Update(ptr noundef nonnull %19, ptr noundef nonnull @hmac_sha256_Init.i_key_pad, i64 noundef 64) #3
  tail call void @memzero(ptr noundef nonnull @hmac_sha256_Init.i_key_pad, i64 noundef 64) #3
  ret void

scalar.ph:                                        ; preds = %vector.memcheck, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %scalar.ph ], [ 0, %vector.memcheck ]
  %20 = getelementptr inbounds [64 x i8], ptr @hmac_sha256_Init.i_key_pad, i64 0, i64 %indvars.iv
  %21 = load i8, ptr %20, align 2, !tbaa !5
  %22 = xor i8 %21, 92
  %23 = getelementptr inbounds [64 x i8], ptr %0, i64 0, i64 %indvars.iv
  store i8 %22, ptr %23, align 1, !tbaa !5
  %24 = load i8, ptr %20, align 2, !tbaa !5
  %25 = xor i8 %24, 54
  store i8 %25, ptr %20, align 2, !tbaa !5
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %26 = getelementptr inbounds [64 x i8], ptr @hmac_sha256_Init.i_key_pad, i64 0, i64 %indvars.iv.next
  %27 = load i8, ptr %26, align 1, !tbaa !5
  %28 = xor i8 %27, 92
  %29 = getelementptr inbounds [64 x i8], ptr %0, i64 0, i64 %indvars.iv.next
  store i8 %28, ptr %29, align 1, !tbaa !5
  %30 = load i8, ptr %26, align 1, !tbaa !5
  %31 = xor i8 %30, 54
  store i8 %31, ptr %26, align 1, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next.1, 64
  br i1 %exitcond.not.1, label %middle.block, label %scalar.ph, !llvm.loop !13
}

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

declare void @sha256_Raw(ptr noundef, i64 noundef, ptr 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

declare void @sha256_Init(ptr noundef) local_unnamed_addr #1

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

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 = { "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 }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9}
!9 = distinct !{!9, !10}
!10 = distinct !{!10, !"LVerDomain"}
!11 = !{!12}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !14, !15}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!"llvm.loop.isvectorized", i32 1}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LoopLoadEliminationPass,LoopRotatePass,LoopUnrollPass,LoopVectorizePass,SROAPass,SimplifyCFGPass,TailCallElimPass