; LLVM IR from square/subzero :: core/trezor-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: inlinehint nounwind uwtable
declare hidden void @store32(ptr noundef, i32 noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @store16(ptr noundef, i16 noundef zeroext) #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
define dso_local i32 @blake2s_InitKey(ptr noundef %0, i64 noundef %1, ptr noundef %2, i64 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca [1 x %struct.blake2s_param__], align 16
  %11 = alloca i32, align 4
  %12 = alloca [64 x i8], align 16
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 32, ptr %10) #5
  %13 = load i64, ptr %7, align 8, !tbaa !9
  %14 = icmp ne i64 %13, 0
  br i1 %14, label %15, label %18

15:                                               ; preds = %4
  %16 = load i64, ptr %7, align 8, !tbaa !9
  %17 = icmp ugt i64 %16, 32
  br i1 %17, label %18, label %19

18:                                               ; preds = %15, %4
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %72

19:                                               ; preds = %15
  %20 = load ptr, ptr %8, align 8, !tbaa !5
  %21 = icmp ne ptr %20, null
  br i1 %21, label %22, label %28

22:                                               ; preds = %19
  %23 = load i64, ptr %9, align 8, !tbaa !9
  %24 = icmp ne i64 %23, 0
  br i1 %24, label %25, label %28

25:                                               ; preds = %22
  %26 = load i64, ptr %9, align 8, !tbaa !9
  %27 = icmp ugt i64 %26, 32
  br i1 %27, label %28, label %29

28:                                               ; preds = %25, %22, %19
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %72

29:                                               ; preds = %25
  %30 = load i64, ptr %7, align 8, !tbaa !9
  %31 = trunc i64 %30 to i8
  %32 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %10, i64 0, i64 0
  %33 = getelementptr inbounds %struct.blake2s_param__, ptr %32, i32 0, i32 0
  store i8 %31, ptr %33, align 16, !tbaa !11
  %34 = load i64, ptr %9, align 8, !tbaa !9
  %35 = trunc i64 %34 to i8
  %36 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %10, i64 0, i64 0
  %37 = getelementptr inbounds %struct.blake2s_param__, ptr %36, i32 0, i32 1
  store i8 %35, ptr %37, align 1, !tbaa !15
  %38 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %10, i64 0, i64 0
  %39 = getelementptr inbounds %struct.blake2s_param__, ptr %38, i32 0, i32 2
  store i8 1, ptr %39, align 2, !tbaa !16
  %40 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %10, i64 0, i64 0
  %41 = getelementptr inbounds %struct.blake2s_param__, ptr %40, i32 0, i32 3
  store i8 1, ptr %41, align 1, !tbaa !17
  %42 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %10, i64 0, i64 0
  %43 = getelementptr inbounds %struct.blake2s_param__, ptr %42, i32 0, i32 4
  call void @store32(ptr noundef %43, i32 noundef 0)
  %44 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %10, i64 0, i64 0
  %45 = getelementptr inbounds %struct.blake2s_param__, ptr %44, i32 0, i32 5
  call void @store32(ptr noundef %45, i32 noundef 0)
  %46 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %10, i64 0, i64 0
  %47 = getelementptr inbounds %struct.blake2s_param__, ptr %46, i32 0, i32 6
  call void @store16(ptr noundef %47, i16 noundef zeroext 0)
  %48 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %10, i64 0, i64 0
  %49 = getelementptr inbounds %struct.blake2s_param__, ptr %48, i32 0, i32 7
  store i8 0, ptr %49, align 2, !tbaa !18
  %50 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %10, i64 0, i64 0
  %51 = getelementptr inbounds %struct.blake2s_param__, ptr %50, i32 0, i32 8
  store i8 0, ptr %51, align 1, !tbaa !19
  %52 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %10, i64 0, i64 0
  %53 = getelementptr inbounds %struct.blake2s_param__, ptr %52, i32 0, i32 9
  %54 = getelementptr inbounds [8 x i8], ptr %53, i64 0, i64 0
  call void @memzero(ptr noundef %54, i64 noundef 8)
  %55 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %10, i64 0, i64 0
  %56 = getelementptr inbounds %struct.blake2s_param__, ptr %55, i32 0, i32 10
  %57 = getelementptr inbounds [8 x i8], ptr %56, i64 0, i64 0
  call void @memzero(ptr noundef %57, i64 noundef 8)
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %10, i64 0, i64 0
  %60 = call i32 @blake2s_init_param(ptr noundef %58, ptr noundef %59)
  %61 = icmp slt i32 %60, 0
  br i1 %61, label %62, label %63

62:                                               ; preds = %29
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %72

63:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 64, ptr %12) #5
  %64 = getelementptr inbounds [64 x i8], ptr %12, i64 0, i64 0
  call void @memzero(ptr noundef %64, i64 noundef 64)
  %65 = getelementptr inbounds [64 x i8], ptr %12, i64 0, i64 0
  %66 = load ptr, ptr %8, align 8, !tbaa !5
  %67 = load i64, ptr %9, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %65, ptr align 1 %66, i64 %67, i1 false)
  %68 = load ptr, ptr %6, align 8, !tbaa !5
  %69 = getelementptr inbounds [64 x i8], ptr %12, i64 0, i64 0
  %70 = call i32 @blake2s_Update(ptr noundef %68, ptr noundef %69, i64 noundef 64)
  %71 = getelementptr inbounds [64 x i8], ptr %12, i64 0, i64 0
  call void @memzero(ptr noundef %71, i64 noundef 64)
  call void @llvm.lifetime.end.p0(i64 64, ptr %12) #5
  store i32 0, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %72

72:                                               ; preds = %63, %62, %28, %18
  call void @llvm.lifetime.end.p0(i64 32, ptr %10) #5
  %73 = load i32, ptr %5, align 4
  ret i32 %73
}

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

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

!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: inlinehint nounwind uwtable
declare hidden void @store32(ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @store16(ptr noundef, i16 noundef zeroext) 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
define dso_local range(i32 -1, 1) i32 @blake2s_InitKey(ptr noundef %0, i64 noundef %1, ptr noundef readonly %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = alloca [1 x %struct.blake2s_param__], align 16
  %6 = alloca [64 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %5) #5
  %7 = add i64 %1, -33
  %or.cond = icmp ult i64 %7, -32
  br i1 %or.cond, label %29, label %8

8:                                                ; preds = %4
  %9 = icmp eq ptr %2, null
  %10 = add i64 %3, -33
  %11 = icmp ult i64 %10, -32
  %or.cond5 = or i1 %9, %11
  br i1 %or.cond5, label %29, label %12

12:                                               ; preds = %8
  %13 = trunc nuw nsw i64 %1 to i8
  store i8 %13, ptr %5, align 16, !tbaa !5
  %14 = trunc nuw nsw i64 %3 to i8
  %15 = getelementptr inbounds i8, ptr %5, i64 1
  store i8 %14, ptr %15, align 1, !tbaa !11
  %16 = getelementptr inbounds i8, ptr %5, i64 2
  store i8 1, ptr %16, align 2, !tbaa !12
  %17 = getelementptr inbounds i8, ptr %5, i64 3
  store i8 1, ptr %17, align 1, !tbaa !13
  %18 = getelementptr inbounds i8, ptr %5, i64 4
  call void @store32(ptr noundef nonnull %18, i32 noundef 0)
  %19 = getelementptr inbounds i8, ptr %5, i64 8
  call void @store32(ptr noundef nonnull %19, i32 noundef 0)
  %20 = getelementptr inbounds i8, ptr %5, i64 12
  call void @store16(ptr noundef nonnull %20, i16 noundef zeroext 0)
  %21 = getelementptr inbounds i8, ptr %5, i64 14
  store i8 0, ptr %21, align 2, !tbaa !14
  %22 = getelementptr inbounds i8, ptr %5, i64 15
  store i8 0, ptr %22, align 1, !tbaa !15
  %23 = getelementptr inbounds i8, ptr %5, i64 16
  call void @memzero(ptr noundef nonnull %23, i64 noundef 8) #5
  %24 = getelementptr inbounds i8, ptr %5, i64 24
  call void @memzero(ptr noundef nonnull %24, i64 noundef 8) #5
  %25 = call i32 @blake2s_init_param(ptr noundef %0, ptr noundef nonnull %5)
  %26 = icmp slt i32 %25, 0
  br i1 %26, label %29, label %27

27:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %6) #5
  call void @memzero(ptr noundef nonnull %6, i64 noundef 64) #5
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 %6, ptr nonnull align 1 %2, i64 %3, i1 false)
  %28 = call i32 @blake2s_Update(ptr noundef %0, ptr noundef nonnull %6, i64 noundef 64)
  call void @memzero(ptr noundef nonnull %6, i64 noundef 64) #5
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %6) #5
  br label %29

29:                                               ; preds = %12, %8, %4, %27
  %.0 = phi i32 [ 0, %27 ], [ -1, %4 ], [ -1, %8 ], [ -1, %12 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %5) #5
  ret i32 %.0
}

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

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

!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 1}
!12 = !{!6, !7, i64 2}
!13 = !{!6, !7, i64 3}
!14 = !{!6, !7, i64 14}
!15 = !{!6, !7, i64 15}


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