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

; 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.__blake2b_state = type { [8 x i64], [2 x i64], [2 x i64], [128 x i8], i64, 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

; 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: nounwind uwtable
define dso_local i32 @tc_blake2b_Update(ptr noundef %0, ptr noundef %1, i64 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %10, ptr %7, align 8, !tbaa !5
  %11 = load i64, ptr %6, align 8, !tbaa !9
  %12 = icmp ugt i64 %11, 0
  br i1 %12, label %13, label %70

13:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.__blake2b_state, ptr %14, i32 0, i32 4
  %16 = load i64, ptr %15, align 8, !tbaa !11
  store i64 %16, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %17 = load i64, ptr %8, align 8, !tbaa !9
  %18 = sub i64 128, %17
  store i64 %18, ptr %9, align 8, !tbaa !9
  %19 = load i64, ptr %6, align 8, !tbaa !9
  %20 = load i64, ptr %9, align 8, !tbaa !9
  %21 = icmp ugt i64 %19, %20
  br i1 %21, label %22, label %55

22:                                               ; preds = %13
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.__blake2b_state, ptr %23, i32 0, i32 4
  store i64 0, ptr %24, align 8, !tbaa !11
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.__blake2b_state, ptr %25, i32 0, i32 3
  %27 = getelementptr inbounds [128 x i8], ptr %26, i64 0, i64 0
  %28 = load i64, ptr %8, align 8, !tbaa !9
  %29 = getelementptr inbounds i8, ptr %27, i64 %28
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = load i64, ptr %9, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %29, ptr align 1 %30, i64 %31, i1 false)
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  call void @blake2b_increment_counter(ptr noundef %32, i64 noundef 128)
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.__blake2b_state, ptr %34, i32 0, i32 3
  %36 = getelementptr inbounds [128 x i8], ptr %35, i64 0, i64 0
  call void @blake2b_compress(ptr noundef %33, ptr noundef %36)
  %37 = load i64, ptr %9, align 8, !tbaa !9
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %38, i64 %37
  store ptr %39, ptr %7, align 8, !tbaa !5
  %40 = load i64, ptr %9, align 8, !tbaa !9
  %41 = load i64, ptr %6, align 8, !tbaa !9
  %42 = sub i64 %41, %40
  store i64 %42, ptr %6, align 8, !tbaa !9
  br label %43

43:                                               ; preds = %46, %22
  %44 = load i64, ptr %6, align 8, !tbaa !9
  %45 = icmp ugt i64 %44, 128
  br i1 %45, label %46, label %54

46:                                               ; preds = %43
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  call void @blake2b_increment_counter(ptr noundef %47, i64 noundef 128)
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  call void @blake2b_compress(ptr noundef %48, ptr noundef %49)
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = getelementptr inbounds i8, ptr %50, i64 128
  store ptr %51, ptr %7, align 8, !tbaa !5
  %52 = load i64, ptr %6, align 8, !tbaa !9
  %53 = sub i64 %52, 128
  store i64 %53, ptr %6, align 8, !tbaa !9
  br label %43, !llvm.loop !13

54:                                               ; preds = %43
  br label %55

55:                                               ; preds = %54, %13
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.__blake2b_state, ptr %56, i32 0, i32 3
  %58 = getelementptr inbounds [128 x i8], ptr %57, i64 0, i64 0
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.__blake2b_state, ptr %59, i32 0, i32 4
  %61 = load i64, ptr %60, align 8, !tbaa !11
  %62 = getelementptr inbounds i8, ptr %58, i64 %61
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = load i64, ptr %6, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %62, ptr align 1 %63, i64 %64, i1 false)
  %65 = load i64, ptr %6, align 8, !tbaa !9
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.__blake2b_state, ptr %66, i32 0, i32 4
  %68 = load i64, ptr %67, align 8, !tbaa !11
  %69 = add i64 %68, %65
  store i64 %69, ptr %67, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %70

70:                                               ; preds = %55, %3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret i32 0
}

; Function Attrs: nounwind uwtable
declare hidden void @blake2b_increment_counter(ptr noundef, i64 noundef) #2

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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, !10, i64 224}
!12 = !{!"__blake2b_state", !7, i64 0, !7, i64 64, !7, i64 80, !7, i64 96, !10, i64 224, !10, i64 232, !7, i64 240}
!13 = distinct !{!13, !14}
!14 = !{!"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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @tc_blake2b_Update(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #1 {
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %23, label %4

4:                                                ; preds = %3
  %5 = getelementptr inbounds i8, ptr %0, i64 224
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %7 = sub i64 128, %6
  %8 = icmp ult i64 %7, %2
  br i1 %8, label %9, label %.loopexit

9:                                                ; preds = %4
  store i64 0, ptr %5, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %0, i64 96
  %11 = getelementptr inbounds i8, ptr %10, i64 %6
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %11, ptr align 1 %1, i64 %7, i1 false)
  tail call void @blake2b_increment_counter(ptr noundef nonnull %0, i64 noundef 128)
  tail call void @blake2b_compress(ptr noundef nonnull %0, ptr noundef nonnull %10)
  %12 = getelementptr inbounds i8, ptr %1, i64 %7
  %13 = sub i64 %2, %7
  %14 = icmp ugt i64 %13, 128
  br i1 %14, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %9, %.lr.ph
  %.034 = phi i64 [ %16, %.lr.ph ], [ %13, %9 ]
  %.02933 = phi ptr [ %15, %.lr.ph ], [ %12, %9 ]
  tail call void @blake2b_increment_counter(ptr noundef %0, i64 noundef 128)
  tail call void @blake2b_compress(ptr noundef %0, ptr noundef %.02933)
  %15 = getelementptr inbounds i8, ptr %.02933, i64 128
  %16 = add i64 %.034, -128
  %17 = icmp ugt i64 %16, 128
  br i1 %17, label %.lr.ph, label %.loopexit, !llvm.loop !10

.loopexit:                                        ; preds = %.lr.ph, %9, %4
  %.130 = phi ptr [ %1, %4 ], [ %12, %9 ], [ %15, %.lr.ph ]
  %.1 = phi i64 [ %2, %4 ], [ %13, %9 ], [ %16, %.lr.ph ]
  %18 = getelementptr inbounds i8, ptr %0, i64 96
  %19 = load i64, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %18, i64 %19
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %20, ptr align 1 %.130, i64 %.1, i1 false)
  %21 = load i64, ptr %5, align 8, !tbaa !5
  %22 = add i64 %21, %.1
  store i64 %22, ptr %5, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %.loopexit, %3
  ret i32 0
}

; Function Attrs: nounwind uwtable
declare hidden void @blake2b_increment_counter(ptr noundef, i64 noundef) local_unnamed_addr #1

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

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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" }

!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, !9, i64 224}
!6 = !{!"__blake2b_state", !7, i64 0, !7, i64 64, !7, i64 80, !7, i64 96, !9, i64 224, !9, i64 232, !7, i64 240}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"long", !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass