; LLVM IR from square/subzero :: core/trezor-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_param__ = type { i8, i8, i8, i8, i32, i32, i32, i8, i8, [14 x i8], [16 x i8], [16 x i8] }

; Function Attrs: nounwind uwtable
declare dso_local i32 @blake2b_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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @blake2b_InitPersonal(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.blake2b_param__], align 16
  %11 = alloca i32, align 4
  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 64, ptr %10) #5
  %12 = load i64, ptr %7, align 8, !tbaa !9
  %13 = icmp ne i64 %12, 0
  br i1 %13, label %14, label %17

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

17:                                               ; preds = %14, %4
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %59

18:                                               ; preds = %14
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = icmp ne ptr %19, null
  br i1 %20, label %21, label %24

21:                                               ; preds = %18
  %22 = load i64, ptr %9, align 8, !tbaa !9
  %23 = icmp ne i64 %22, 16
  br i1 %23, label %24, label %25

24:                                               ; preds = %21, %18
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %59

25:                                               ; preds = %21
  %26 = load i64, ptr %7, align 8, !tbaa !9
  %27 = trunc i64 %26 to i8
  %28 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %29 = getelementptr inbounds %struct.blake2b_param__, ptr %28, i32 0, i32 0
  store i8 %27, ptr %29, align 16, !tbaa !11
  %30 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %31 = getelementptr inbounds %struct.blake2b_param__, ptr %30, i32 0, i32 1
  store i8 0, ptr %31, align 1, !tbaa !14
  %32 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %33 = getelementptr inbounds %struct.blake2b_param__, ptr %32, i32 0, i32 2
  store i8 1, ptr %33, align 2, !tbaa !15
  %34 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %35 = getelementptr inbounds %struct.blake2b_param__, ptr %34, i32 0, i32 3
  store i8 1, ptr %35, align 1, !tbaa !16
  %36 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %37 = getelementptr inbounds %struct.blake2b_param__, ptr %36, i32 0, i32 4
  call void @store32(ptr noundef %37, i32 noundef 0)
  %38 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %39 = getelementptr inbounds %struct.blake2b_param__, ptr %38, i32 0, i32 5
  call void @store32(ptr noundef %39, i32 noundef 0)
  %40 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %41 = getelementptr inbounds %struct.blake2b_param__, ptr %40, i32 0, i32 6
  call void @store32(ptr noundef %41, i32 noundef 0)
  %42 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %43 = getelementptr inbounds %struct.blake2b_param__, ptr %42, i32 0, i32 7
  store i8 0, ptr %43, align 16, !tbaa !17
  %44 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %45 = getelementptr inbounds %struct.blake2b_param__, ptr %44, i32 0, i32 8
  store i8 0, ptr %45, align 1, !tbaa !18
  %46 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %47 = getelementptr inbounds %struct.blake2b_param__, ptr %46, i32 0, i32 9
  %48 = getelementptr inbounds [14 x i8], ptr %47, i64 0, i64 0
  call void @memzero(ptr noundef %48, i64 noundef 14)
  %49 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %50 = getelementptr inbounds %struct.blake2b_param__, ptr %49, i32 0, i32 10
  %51 = getelementptr inbounds [16 x i8], ptr %50, i64 0, i64 0
  call void @memzero(ptr noundef %51, i64 noundef 16)
  %52 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %53 = getelementptr inbounds %struct.blake2b_param__, ptr %52, i32 0, i32 11
  %54 = getelementptr inbounds [16 x i8], ptr %53, i64 0, i64 0
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %54, ptr align 1 %55, i64 16, i1 false)
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %58 = call i32 @blake2b_init_param(ptr noundef %56, ptr noundef %57)
  store i32 %58, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %59

59:                                               ; preds = %25, %24, %17
  call void @llvm.lifetime.end.p0(i64 64, ptr %10) #5
  %60 = load i32, ptr %5, align 4
  ret i32 %60
}

; 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

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 = !{!"blake2b_param__", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !13, i64 4, !13, i64 8, !13, i64 12, !7, i64 16, !7, i64 17, !7, i64 18, !7, i64 32, !7, i64 48}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !7, i64 1}
!15 = !{!12, !7, i64 2}
!16 = !{!12, !7, i64 3}
!17 = !{!12, !7, i64 16}
!18 = !{!12, !7, i64 17}


; 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.blake2b_param__ = type { i8, i8, i8, i8, i32, i32, i32, i8, i8, [14 x i8], [16 x i8], [16 x i8] }

; Function Attrs: nounwind uwtable
declare dso_local i32 @blake2b_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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @blake2b_InitPersonal(ptr noundef %0, i64 noundef %1, ptr noundef readonly %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = alloca [1 x %struct.blake2b_param__], align 16
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %5) #5
  %6 = add i64 %1, -65
  %or.cond = icmp ult i64 %6, -64
  br i1 %or.cond, label %24, label %7

7:                                                ; preds = %4
  %8 = icmp eq ptr %2, null
  %9 = icmp ne i64 %3, 16
  %or.cond3 = or i1 %8, %9
  br i1 %or.cond3, label %24, label %10

10:                                               ; preds = %7
  %11 = trunc nuw nsw i64 %1 to i8
  store i8 %11, ptr %5, align 16, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %5, i64 1
  store i8 0, ptr %12, align 1, !tbaa !10
  %13 = getelementptr inbounds i8, ptr %5, i64 2
  store i8 1, ptr %13, align 2, !tbaa !11
  %14 = getelementptr inbounds i8, ptr %5, i64 3
  store i8 1, ptr %14, align 1, !tbaa !12
  %15 = getelementptr inbounds i8, ptr %5, i64 4
  call void @store32(ptr noundef nonnull %15, i32 noundef 0)
  %16 = getelementptr inbounds i8, ptr %5, i64 8
  call void @store32(ptr noundef nonnull %16, i32 noundef 0)
  %17 = getelementptr inbounds i8, ptr %5, i64 12
  call void @store32(ptr noundef nonnull %17, i32 noundef 0)
  %18 = getelementptr inbounds i8, ptr %5, i64 16
  store i8 0, ptr %18, align 16, !tbaa !13
  %19 = getelementptr inbounds i8, ptr %5, i64 17
  store i8 0, ptr %19, align 1, !tbaa !14
  %20 = getelementptr inbounds i8, ptr %5, i64 18
  call void @memzero(ptr noundef nonnull %20, i64 noundef 14) #5
  %21 = getelementptr inbounds i8, ptr %5, i64 32
  call void @memzero(ptr noundef nonnull %21, i64 noundef 16) #5
  %22 = getelementptr inbounds i8, ptr %5, i64 48
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %22, ptr noundef nonnull align 1 dereferenceable(16) %2, i64 16, i1 false)
  %23 = call i32 @blake2b_init_param(ptr noundef %0, ptr noundef nonnull %5)
  br label %24

24:                                               ; preds = %7, %4, %10
  %.0 = phi i32 [ %23, %10 ], [ -1, %4 ], [ -1, %7 ]
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %5) #5
  ret i32 %.0
}

; 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

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 = !{!"blake2b_param__", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !9, i64 4, !9, i64 8, !9, i64 12, !7, i64 16, !7, i64 17, !7, i64 18, !7, i64 32, !7, i64 48}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!6, !7, i64 1}
!11 = !{!6, !7, i64 2}
!12 = !{!6, !7, i64 3}
!13 = !{!6, !7, i64 16}
!14 = !{!6, !7, i64 17}


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