; 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_param__ = type { i8, i8, i8, i8, i32, i32, i16, i8, i8, [8 x i8], [8 x i8] }

; Function Attrs: nounwind uwtable
declare dso_local i32 @blake2s_init_param(ptr noundef, ptr noundef) #0

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

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

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

10:                                               ; preds = %2
  %11 = load i64, ptr %5, align 8, !tbaa !9
  %12 = icmp ugt i64 %11, 32
  br i1 %12, label %13, label %14

13:                                               ; preds = %10, %2
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %44

14:                                               ; preds = %10
  %15 = load i64, ptr %5, align 8, !tbaa !9
  %16 = trunc i64 %15 to i8
  %17 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %6, i64 0, i64 0
  %18 = getelementptr inbounds %struct.blake2s_param__, ptr %17, i32 0, i32 0
  store i8 %16, ptr %18, align 16, !tbaa !11
  %19 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %6, i64 0, i64 0
  %20 = getelementptr inbounds %struct.blake2s_param__, ptr %19, i32 0, i32 1
  store i8 0, ptr %20, align 1, !tbaa !15
  %21 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %6, i64 0, i64 0
  %22 = getelementptr inbounds %struct.blake2s_param__, ptr %21, i32 0, i32 2
  store i8 1, ptr %22, align 2, !tbaa !16
  %23 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %6, i64 0, i64 0
  %24 = getelementptr inbounds %struct.blake2s_param__, ptr %23, i32 0, i32 3
  store i8 1, ptr %24, align 1, !tbaa !17
  %25 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %6, i64 0, i64 0
  %26 = getelementptr inbounds %struct.blake2s_param__, ptr %25, i32 0, i32 4
  call void @store32(ptr noundef %26, i32 noundef 0)
  %27 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %6, i64 0, i64 0
  %28 = getelementptr inbounds %struct.blake2s_param__, ptr %27, i32 0, i32 5
  call void @store32(ptr noundef %28, i32 noundef 0)
  %29 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %6, i64 0, i64 0
  %30 = getelementptr inbounds %struct.blake2s_param__, ptr %29, i32 0, i32 6
  call void @store16(ptr noundef %30, i16 noundef zeroext 0)
  %31 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %6, i64 0, i64 0
  %32 = getelementptr inbounds %struct.blake2s_param__, ptr %31, i32 0, i32 7
  store i8 0, ptr %32, align 2, !tbaa !18
  %33 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %6, i64 0, i64 0
  %34 = getelementptr inbounds %struct.blake2s_param__, ptr %33, i32 0, i32 8
  store i8 0, ptr %34, align 1, !tbaa !19
  %35 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %6, i64 0, i64 0
  %36 = getelementptr inbounds %struct.blake2s_param__, ptr %35, i32 0, i32 9
  %37 = getelementptr inbounds [8 x i8], ptr %36, i64 0, i64 0
  call void @memzero(ptr noundef %37, i64 noundef 8)
  %38 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %6, i64 0, i64 0
  %39 = getelementptr inbounds %struct.blake2s_param__, ptr %38, i32 0, i32 10
  %40 = getelementptr inbounds [8 x i8], ptr %39, i64 0, i64 0
  call void @memzero(ptr noundef %40, i64 noundef 8)
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %6, i64 0, i64 0
  %43 = call i32 @blake2s_init_param(ptr noundef %41, ptr noundef %42)
  store i32 %43, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %44

44:                                               ; preds = %14, %13
  call void @llvm.lifetime.end.p0(i64 32, ptr %6) #5
  %45 = load i32, ptr %3, align 4
  ret i32 %45
}

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @store16(ptr noundef, i16 noundef zeroext) #3

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 #4 = { "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 #5 = { 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 0}
!12 = !{!"blake2s_param__", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !13, i64 4, !13, i64 8, !14, i64 12, !7, i64 14, !7, i64 15, !7, i64 16, !7, i64 24}
!13 = !{!"int", !7, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = !{!12, !7, i64 1}
!16 = !{!12, !7, i64 2}
!17 = !{!12, !7, i64 3}
!18 = !{!12, !7, i64 14}
!19 = !{!12, !7, i64 15}


; 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.blake2s_param__ = type { i8, i8, i8, i8, i32, i32, i16, i8, i8, [8 x i8], [8 x i8] }

; Function Attrs: nounwind uwtable
declare dso_local i32 @blake2s_init_param(ptr noundef, ptr noundef) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @blake2s_Init(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = alloca [1 x %struct.blake2s_param__], align 16
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %3) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %3, i8 0, i64 32, i1 false)
  %4 = add i64 %1, -33
  %or.cond = icmp ult i64 %4, -32
  br i1 %or.cond, label %17, label %5

5:                                                ; preds = %2
  %6 = trunc nuw nsw i64 %1 to i8
  store i8 %6, ptr %3, align 16, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %3, i64 2
  store i8 1, ptr %7, align 2, !tbaa !11
  %8 = getelementptr inbounds i8, ptr %3, i64 3
  store i8 1, ptr %8, align 1, !tbaa !12
  %9 = getelementptr inbounds i8, ptr %3, i64 4
  call void @store32(ptr noundef nonnull %9, i32 noundef 0)
  %10 = getelementptr inbounds i8, ptr %3, i64 8
  call void @store32(ptr noundef nonnull %10, i32 noundef 0)
  %11 = getelementptr inbounds i8, ptr %3, i64 12
  call void @store16(ptr noundef nonnull %11, i16 noundef zeroext 0)
  %12 = getelementptr inbounds i8, ptr %3, i64 14
  store i8 0, ptr %12, align 2, !tbaa !13
  %13 = getelementptr inbounds i8, ptr %3, i64 15
  store i8 0, ptr %13, align 1, !tbaa !14
  %14 = getelementptr inbounds i8, ptr %3, i64 16
  call void @memzero(ptr noundef nonnull %14, i64 noundef 8) #5
  %15 = getelementptr inbounds i8, ptr %3, i64 24
  call void @memzero(ptr noundef nonnull %15, i64 noundef 8) #5
  %16 = call i32 @blake2s_init_param(ptr noundef %0, ptr noundef nonnull %3)
  br label %17

17:                                               ; preds = %2, %5
  %.0 = phi i32 [ %16, %5 ], [ -1, %2 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %3) #5
  ret i32 %.0
}

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @store16(ptr noundef, i16 noundef zeroext) local_unnamed_addr #3

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

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 #4 = { "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 #5 = { 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 0}
!6 = !{!"blake2s_param__", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !9, i64 4, !9, i64 8, !10, i64 12, !7, i64 14, !7, i64 15, !7, i64 16, !7, i64 24}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!6, !7, i64 2}
!12 = !{!6, !7, i64 3}
!13 = !{!6, !7, i64 14}
!14 = !{!6, !7, i64 15}


; Effective O3 passes: DSEPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass