; 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: 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 @blake2s_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 %74

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.__blake2s_state, ptr %14, i32 0, i32 4
  %16 = load i32, ptr %15, align 4, !tbaa !11
  %17 = zext i32 %16 to i64
  store i64 %17, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %18 = load i64, ptr %8, align 8, !tbaa !9
  %19 = sub i64 64, %18
  store i64 %19, ptr %9, align 8, !tbaa !9
  %20 = load i64, ptr %6, align 8, !tbaa !9
  %21 = load i64, ptr %9, align 8, !tbaa !9
  %22 = icmp ugt i64 %20, %21
  br i1 %22, label %23, label %56

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

44:                                               ; preds = %47, %23
  %45 = load i64, ptr %6, align 8, !tbaa !9
  %46 = icmp ugt i64 %45, 64
  br i1 %46, label %47, label %55

47:                                               ; preds = %44
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  call void @blake2s_increment_counter(ptr noundef %48, i32 noundef 64)
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  call void @blake2s_compress(ptr noundef %49, ptr noundef %50)
  %51 = load ptr, ptr %7, align 8, !tbaa !5
  %52 = getelementptr inbounds i8, ptr %51, i64 64
  store ptr %52, ptr %7, align 8, !tbaa !5
  %53 = load i64, ptr %6, align 8, !tbaa !9
  %54 = sub i64 %53, 64
  store i64 %54, ptr %6, align 8, !tbaa !9
  br label %44, !llvm.loop !14

55:                                               ; preds = %44
  br label %56

56:                                               ; preds = %55, %13
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.__blake2s_state, ptr %57, i32 0, i32 3
  %59 = getelementptr inbounds [64 x i8], ptr %58, i64 0, i64 0
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.__blake2s_state, ptr %60, i32 0, i32 4
  %62 = load i32, ptr %61, align 4, !tbaa !11
  %63 = zext i32 %62 to i64
  %64 = getelementptr inbounds i8, ptr %59, i64 %63
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = load i64, ptr %6, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %64, ptr align 1 %65, i64 %66, i1 false)
  %67 = load i64, ptr %6, align 8, !tbaa !9
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.__blake2s_state, ptr %68, i32 0, i32 4
  %70 = load i32, ptr %69, align 4, !tbaa !11
  %71 = zext i32 %70 to i64
  %72 = add i64 %71, %67
  %73 = trunc i64 %72 to i32
  store i32 %73, ptr %69, align 4, !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 %74

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

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

; Function Attrs: nounwind uwtable
declare hidden void @blake2s_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, !13, i64 112}
!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 = distinct !{!14, !15}
!15 = !{!"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 @blake2s_Update(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #1 {
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %26, label %4

4:                                                ; preds = %3
  %5 = getelementptr inbounds i8, ptr %0, i64 112
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = zext i32 %6 to i64
  %8 = sub nsw i64 64, %7
  %9 = icmp ult i64 %8, %2
  br i1 %9, label %10, label %.loopexit

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

.lr.ph:                                           ; preds = %10, %.lr.ph
  %.034 = phi i64 [ %17, %.lr.ph ], [ %14, %10 ]
  %.02933 = phi ptr [ %16, %.lr.ph ], [ %13, %10 ]
  tail call void @blake2s_increment_counter(ptr noundef %0, i32 noundef 64)
  tail call void @blake2s_compress(ptr noundef %0, ptr noundef %.02933)
  %16 = getelementptr inbounds i8, ptr %.02933, i64 64
  %17 = add i64 %.034, -64
  %18 = icmp ugt i64 %17, 64
  br i1 %18, label %.lr.ph, label %.loopexit, !llvm.loop !10

.loopexit:                                        ; preds = %.lr.ph, %10, %4
  %.130 = phi ptr [ %1, %4 ], [ %13, %10 ], [ %16, %.lr.ph ]
  %.1 = phi i64 [ %2, %4 ], [ %14, %10 ], [ %17, %.lr.ph ]
  %19 = getelementptr inbounds i8, ptr %0, i64 48
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = zext i32 %20 to i64
  %22 = getelementptr inbounds i8, ptr %19, i64 %21
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %22, ptr align 1 %.130, i64 %.1, i1 false)
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = trunc i64 %.1 to i32
  %25 = add i32 %23, %24
  store i32 %25, ptr %5, align 4, !tbaa !5
  br label %26

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

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

; Function Attrs: nounwind uwtable
declare hidden void @blake2s_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 112}
!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 = 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