; 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: nounwind uwtable
define dso_local i32 @blake2s_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.blake2s_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 32, 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, 32
  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 %56

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, 8
  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 %56

25:                                               ; preds = %21
  %26 = load i64, ptr %7, align 8, !tbaa !9
  %27 = trunc i64 %26 to i8
  %28 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %10, i64 0, i64 0
  %29 = getelementptr inbounds %struct.blake2s_param__, ptr %28, i32 0, i32 0
  store i8 %27, ptr %29, align 16, !tbaa !11
  %30 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %10, i64 0, i64 0
  %31 = getelementptr inbounds %struct.blake2s_param__, ptr %30, i32 0, i32 1
  store i8 0, ptr %31, align 1, !tbaa !15
  %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 2
  store i8 1, ptr %33, align 2, !tbaa !16
  %34 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %10, i64 0, i64 0
  %35 = getelementptr inbounds %struct.blake2s_param__, ptr %34, i32 0, i32 3
  store i8 1, ptr %35, align 1, !tbaa !17
  %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 4
  call void @store32(ptr noundef %37, i32 noundef 0)
  %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 5
  call void @store32(ptr noundef %39, i32 noundef 0)
  %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 6
  call void @store16(ptr noundef %41, i16 noundef zeroext 0)
  %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 7
  store i8 0, ptr %43, align 2, !tbaa !18
  %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 8
  store i8 0, ptr %45, align 1, !tbaa !19
  %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 9
  %48 = getelementptr inbounds [8 x i8], ptr %47, i64 0, i64 0
  call void @memzero(ptr noundef %48, i64 noundef 8)
  %49 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %10, i64 0, i64 0
  %50 = getelementptr inbounds %struct.blake2s_param__, ptr %49, i32 0, i32 10
  %51 = getelementptr inbounds [8 x i8], ptr %50, i64 0, i64 0
  %52 = load ptr, ptr %8, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %51, ptr align 1 %52, i64 8, i1 false)
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = getelementptr inbounds [1 x %struct.blake2s_param__], ptr %10, i64 0, i64 0
  %55 = call i32 @blake2s_init_param(ptr noundef %53, ptr noundef %54)
  store i32 %55, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %56

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

; 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 = !{!"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: nounwind uwtable
define dso_local i32 @blake2s_InitPersonal(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
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %5) #4
  %6 = add i64 %1, -33
  %or.cond = icmp ult i64 %6, -32
  br i1 %or.cond, label %24, label %7

7:                                                ; preds = %4
  %8 = icmp eq ptr %2, null
  %9 = icmp ne i64 %3, 8
  %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 !11
  %13 = getelementptr inbounds i8, ptr %5, i64 2
  store i8 1, ptr %13, align 2, !tbaa !12
  %14 = getelementptr inbounds i8, ptr %5, i64 3
  store i8 1, ptr %14, align 1, !tbaa !13
  %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 @store16(ptr noundef nonnull %17, i16 noundef zeroext 0)
  %18 = getelementptr inbounds i8, ptr %5, i64 14
  store i8 0, ptr %18, align 2, !tbaa !14
  %19 = getelementptr inbounds i8, ptr %5, i64 15
  store i8 0, ptr %19, align 1, !tbaa !15
  %20 = getelementptr inbounds i8, ptr %5, i64 16
  call void @memzero(ptr noundef nonnull %20, i64 noundef 8) #4
  %21 = getelementptr inbounds i8, ptr %5, i64 24
  %22 = load i64, ptr %2, align 1
  store i64 %22, ptr %21, align 8
  %23 = call i32 @blake2s_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 32, ptr nonnull %5) #4
  ret i32 %.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 = { 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: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass