; LLVM IR from trustwallet/wallet-core :: trezor-crypto/crypto (blake2s)

; 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.__blake2s_state = type { [8 x i32], [2 x i32], [2 x i32], [64 x i8], i32, i8, 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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @store32(ptr noundef, i32 noundef) #2

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

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

; Function Attrs: nounwind uwtable
declare hidden void @blake2s_increment_counter(ptr noundef, i32 noundef) #5

; Function Attrs: nounwind uwtable
declare hidden void @blake2s_compress(ptr noundef, ptr noundef) #5

; Function Attrs: nounwind uwtable
define dso_local i32 @blake2s_Final(ptr noundef %0, ptr noundef %1, i64 noundef %2) #5 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca [32 x i8], align 16
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 32, ptr %8) #6
  call void @llvm.memset.p0.i64(ptr align 16 %8, i8 0, i64 32, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #6
  store i64 0, ptr %9, align 8, !tbaa !9
  %11 = load ptr, ptr %6, align 8, !tbaa !5
  %12 = icmp eq ptr %11, null
  br i1 %12, label %20, label %13

13:                                               ; preds = %3
  %14 = load i64, ptr %7, align 8, !tbaa !9
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.__blake2s_state, ptr %15, i32 0, i32 5
  %17 = load i8, ptr %16, align 4, !tbaa !11
  %18 = zext i8 %17 to i64
  %19 = icmp ult i64 %14, %18
  br i1 %19, label %20, label %21

20:                                               ; preds = %13, %3
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %70

21:                                               ; preds = %13
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = call i32 @blake2s_is_lastblock(ptr noundef %22)
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %26

25:                                               ; preds = %21
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %70

26:                                               ; preds = %21
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.__blake2s_state, ptr %28, i32 0, i32 4
  %30 = load i32, ptr %29, align 4, !tbaa !14
  call void @blake2s_increment_counter(ptr noundef %27, i32 noundef %30)
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  call void @blake2s_set_lastblock(ptr noundef %31)
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.__blake2s_state, ptr %32, i32 0, i32 3
  %34 = getelementptr inbounds [64 x i8], ptr %33, i64 0, i64 0
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.__blake2s_state, ptr %35, i32 0, i32 4
  %37 = load i32, ptr %36, align 4, !tbaa !14
  %38 = zext i32 %37 to i64
  %39 = getelementptr inbounds i8, ptr %34, i64 %38
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.__blake2s_state, ptr %40, i32 0, i32 4
  %42 = load i32, ptr %41, align 4, !tbaa !14
  %43 = sub i32 64, %42
  %44 = zext i32 %43 to i64
  call void @memzero(ptr noundef %39, i64 noundef %44)
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.__blake2s_state, ptr %46, i32 0, i32 3
  %48 = getelementptr inbounds [64 x i8], ptr %47, i64 0, i64 0
  call void @blake2s_compress(ptr noundef %45, ptr noundef %48)
  store i64 0, ptr %9, align 8, !tbaa !9
  br label %49

49:                                               ; preds = %62, %26
  %50 = load i64, ptr %9, align 8, !tbaa !9
  %51 = icmp ult i64 %50, 8
  br i1 %51, label %52, label %65

52:                                               ; preds = %49
  %53 = getelementptr inbounds [32 x i8], ptr %8, i64 0, i64 0
  %54 = load i64, ptr %9, align 8, !tbaa !9
  %55 = mul i64 4, %54
  %56 = getelementptr inbounds i8, ptr %53, i64 %55
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.__blake2s_state, ptr %57, i32 0, i32 0
  %59 = load i64, ptr %9, align 8, !tbaa !9
  %60 = getelementptr inbounds [8 x i32], ptr %58, i64 0, i64 %59
  %61 = load i32, ptr %60, align 4, !tbaa !15
  call void @store32(ptr noundef %56, i32 noundef %61)
  br label %62

62:                                               ; preds = %52
  %63 = load i64, ptr %9, align 8, !tbaa !9
  %64 = add i64 %63, 1
  store i64 %64, ptr %9, align 8, !tbaa !9
  br label %49, !llvm.loop !16

65:                                               ; preds = %49
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  %67 = getelementptr inbounds [32 x i8], ptr %8, i64 0, i64 0
  %68 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %66, ptr align 16 %67, i64 %68, i1 false)
  %69 = getelementptr inbounds [32 x i8], ptr %8, i64 0, i64 0
  call void @memzero(ptr noundef %69, i64 noundef 32)
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %70

70:                                               ; preds = %65, %25, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 32, ptr %8) #6
  %71 = load i32, ptr %4, align 4
  ret i32 %71
}

; Function Attrs: nounwind uwtable
declare hidden i32 @blake2s_is_lastblock(ptr noundef) #5

; Function Attrs: nounwind uwtable
declare hidden void @blake2s_set_lastblock(ptr noundef) #5

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { inlinehint 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 = { "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 nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 #6 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !7, i64 116}
!12 = !{!"__blake2s_state", !7, i64 0, !7, i64 32, !7, i64 40, !7, i64 48, !13, i64 112, !7, i64 116, !7, i64 117}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !13, i64 112}
!15 = !{!13, !13, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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"

; 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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @store32(ptr noundef, i32 noundef) local_unnamed_addr #2

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

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

; Function Attrs: nounwind uwtable
declare hidden void @blake2s_increment_counter(ptr noundef, i32 noundef) local_unnamed_addr #5

; Function Attrs: nounwind uwtable
declare hidden void @blake2s_compress(ptr noundef, ptr noundef) local_unnamed_addr #5

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @blake2s_Final(ptr noundef %0, ptr noundef writeonly %1, i64 noundef %2) local_unnamed_addr #5 {
  %4 = alloca [32 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %4) #6
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %4, i8 0, i64 32, i1 false)
  %5 = icmp eq ptr %1, null
  br i1 %5, label %44, label %6

6:                                                ; preds = %3
  %7 = getelementptr inbounds i8, ptr %0, i64 116
  %8 = load i8, ptr %7, align 4, !tbaa !5
  %9 = zext i8 %8 to i64
  %10 = icmp ugt i64 %9, %2
  br i1 %10, label %44, label %11

11:                                               ; preds = %6
  %12 = tail call i32 @blake2s_is_lastblock(ptr noundef nonnull %0)
  %.not = icmp eq i32 %12, 0
  br i1 %.not, label %13, label %44

13:                                               ; preds = %11
  %14 = getelementptr inbounds i8, ptr %0, i64 112
  %15 = load i32, ptr %14, align 4, !tbaa !10
  tail call void @blake2s_increment_counter(ptr noundef nonnull %0, i32 noundef %15)
  tail call void @blake2s_set_lastblock(ptr noundef nonnull %0)
  %16 = getelementptr inbounds i8, ptr %0, i64 48
  %17 = load i32, ptr %14, align 4, !tbaa !10
  %18 = zext i32 %17 to i64
  %19 = getelementptr inbounds i8, ptr %16, i64 %18
  %20 = sub i32 64, %17
  %21 = zext i32 %20 to i64
  tail call void @memzero(ptr noundef nonnull %19, i64 noundef %21) #6
  tail call void @blake2s_compress(ptr noundef nonnull %0, ptr noundef nonnull %16)
  %22 = load i32, ptr %0, align 4, !tbaa !11
  call void @store32(ptr noundef nonnull %4, i32 noundef %22)
  %23 = getelementptr inbounds i8, ptr %4, i64 4
  %24 = getelementptr inbounds i8, ptr %0, i64 4
  %25 = load i32, ptr %24, align 4, !tbaa !11
  call void @store32(ptr noundef nonnull %23, i32 noundef %25)
  %26 = getelementptr inbounds i8, ptr %4, i64 8
  %27 = getelementptr inbounds i8, ptr %0, i64 8
  %28 = load i32, ptr %27, align 4, !tbaa !11
  call void @store32(ptr noundef nonnull %26, i32 noundef %28)
  %29 = getelementptr inbounds i8, ptr %4, i64 12
  %30 = getelementptr inbounds i8, ptr %0, i64 12
  %31 = load i32, ptr %30, align 4, !tbaa !11
  call void @store32(ptr noundef nonnull %29, i32 noundef %31)
  %32 = getelementptr inbounds i8, ptr %4, i64 16
  %33 = getelementptr inbounds i8, ptr %0, i64 16
  %34 = load i32, ptr %33, align 4, !tbaa !11
  call void @store32(ptr noundef nonnull %32, i32 noundef %34)
  %35 = getelementptr inbounds i8, ptr %4, i64 20
  %36 = getelementptr inbounds i8, ptr %0, i64 20
  %37 = load i32, ptr %36, align 4, !tbaa !11
  call void @store32(ptr noundef nonnull %35, i32 noundef %37)
  %38 = getelementptr inbounds i8, ptr %4, i64 24
  %39 = getelementptr inbounds i8, ptr %0, i64 24
  %40 = load i32, ptr %39, align 4, !tbaa !11
  call void @store32(ptr noundef nonnull %38, i32 noundef %40)
  %41 = getelementptr inbounds i8, ptr %4, i64 28
  %42 = getelementptr inbounds i8, ptr %0, i64 28
  %43 = load i32, ptr %42, align 4, !tbaa !11
  call void @store32(ptr noundef nonnull %41, i32 noundef %43)
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %1, ptr nonnull align 16 %4, i64 %2, i1 false)
  call void @memzero(ptr noundef nonnull %4, i64 noundef 32) #6
  br label %44

44:                                               ; preds = %11, %3, %6, %13
  %.019 = phi i32 [ 0, %13 ], [ -1, %6 ], [ -1, %3 ], [ -1, %11 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %4) #6
  ret i32 %.019
}

; Function Attrs: nounwind uwtable
declare hidden i32 @blake2s_is_lastblock(ptr noundef) local_unnamed_addr #5

; Function Attrs: nounwind uwtable
declare hidden void @blake2s_set_lastblock(ptr noundef) local_unnamed_addr #5

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { inlinehint 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 = { "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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 #6 = { 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, !7, i64 116}
!6 = !{!"__blake2s_state", !7, i64 0, !7, i64 32, !7, i64 40, !7, i64 48, !9, i64 112, !7, i64 116, !7, i64 117}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!6, !9, i64 112}
!11 = !{!9, !9, i64 0}


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