; 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: 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 @blake2b_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.blake2b_param__], align 16
  %11 = alloca i32, align 4
  %12 = alloca [128 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 64, 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, 64
  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 %75

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, 64
  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 %75

29:                                               ; preds = %25
  %30 = load i64, ptr %7, align 8, !tbaa !9
  %31 = trunc i64 %30 to i8
  %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 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.blake2b_param__], ptr %10, i64 0, i64 0
  %37 = getelementptr inbounds %struct.blake2b_param__, ptr %36, i32 0, i32 1
  store i8 %35, ptr %37, align 1, !tbaa !14
  %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 2
  store i8 1, ptr %39, align 2, !tbaa !15
  %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 3
  store i8 1, ptr %41, align 1, !tbaa !16
  %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 4
  call void @store32(ptr noundef %43, i32 noundef 0)
  %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 5
  call void @store32(ptr noundef %45, i32 noundef 0)
  %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 6
  call void @store32(ptr noundef %47, i32 noundef 0)
  %48 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %49 = getelementptr inbounds %struct.blake2b_param__, ptr %48, i32 0, i32 7
  store i8 0, ptr %49, align 16, !tbaa !17
  %50 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %51 = getelementptr inbounds %struct.blake2b_param__, ptr %50, i32 0, i32 8
  store i8 0, ptr %51, align 1, !tbaa !18
  %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 9
  %54 = getelementptr inbounds [14 x i8], ptr %53, i64 0, i64 0
  call void @memzero(ptr noundef %54, i64 noundef 14)
  %55 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %56 = getelementptr inbounds %struct.blake2b_param__, ptr %55, i32 0, i32 10
  %57 = getelementptr inbounds [16 x i8], ptr %56, i64 0, i64 0
  call void @memzero(ptr noundef %57, i64 noundef 16)
  %58 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %59 = getelementptr inbounds %struct.blake2b_param__, ptr %58, i32 0, i32 11
  %60 = getelementptr inbounds [16 x i8], ptr %59, i64 0, i64 0
  call void @memzero(ptr noundef %60, i64 noundef 16)
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = getelementptr inbounds [1 x %struct.blake2b_param__], ptr %10, i64 0, i64 0
  %63 = call i32 @blake2b_init_param(ptr noundef %61, ptr noundef %62)
  %64 = icmp slt i32 %63, 0
  br i1 %64, label %65, label %66

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

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

75:                                               ; preds = %66, %65, %28, %18
  call void @llvm.lifetime.end.p0(i64 64, ptr %10) #5
  %76 = load i32, ptr %5, align 4
  ret i32 %76
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @blake2b_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 = !{!"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: 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 @blake2b_InitKey(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
  %6 = alloca [128 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %5) #5
  %7 = add i64 %1, -65
  %or.cond = icmp ult i64 %7, -64
  br i1 %or.cond, label %30, label %8

8:                                                ; preds = %4
  %9 = icmp eq ptr %2, null
  %10 = add i64 %3, -65
  %11 = icmp ult i64 %10, -64
  %or.cond5 = or i1 %9, %11
  br i1 %or.cond5, label %30, 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 !10
  %16 = getelementptr inbounds i8, ptr %5, i64 2
  store i8 1, ptr %16, align 2, !tbaa !11
  %17 = getelementptr inbounds i8, ptr %5, i64 3
  store i8 1, ptr %17, align 1, !tbaa !12
  %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 @store32(ptr noundef nonnull %20, i32 noundef 0)
  %21 = getelementptr inbounds i8, ptr %5, i64 16
  store i8 0, ptr %21, align 16, !tbaa !13
  %22 = getelementptr inbounds i8, ptr %5, i64 17
  store i8 0, ptr %22, align 1, !tbaa !14
  %23 = getelementptr inbounds i8, ptr %5, i64 18
  call void @memzero(ptr noundef nonnull %23, i64 noundef 14) #5
  %24 = getelementptr inbounds i8, ptr %5, i64 32
  call void @memzero(ptr noundef nonnull %24, i64 noundef 16) #5
  %25 = getelementptr inbounds i8, ptr %5, i64 48
  call void @memzero(ptr noundef nonnull %25, i64 noundef 16) #5
  %26 = call i32 @blake2b_init_param(ptr noundef %0, ptr noundef nonnull %5)
  %27 = icmp slt i32 %26, 0
  br i1 %27, label %30, label %28

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @blake2b_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 = !{!"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: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass