; LLVM IR from tevador/RandomX :: src/blake2 (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_state = type { [8 x i64], [2 x i64], [2 x i64], [128 x i8], i32, i32, i8 }

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

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

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

; 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) #2

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

; Function Attrs: nounwind uwtable
declare hidden void @blake2b_compress(ptr noundef, ptr noundef) #4

; Function Attrs: nounwind uwtable
define dso_local i32 @randomx_blake2b_final(ptr noundef %0, ptr noundef %1, i64 noundef %2) #4 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca [64 x i8], align 16
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 64, ptr %8) #5
  call void @llvm.memset.p0.i64(ptr align 16 %8, i8 0, i64 64, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = icmp eq ptr %11, null
  br i1 %12, label %23, label %13

13:                                               ; preds = %3
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = icmp eq ptr %14, null
  br i1 %15, label %23, label %16

16:                                               ; preds = %13
  %17 = load i64, ptr %7, align 8, !tbaa !9
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.__blake2b_state, ptr %18, i32 0, i32 5
  %20 = load i32, ptr %19, align 4, !tbaa !11
  %21 = zext i32 %20 to i64
  %22 = icmp ult i64 %17, %21
  br i1 %22, label %23, label %24

23:                                               ; preds = %16, %13, %3
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %79

24:                                               ; preds = %16
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.__blake2b_state, ptr %25, i32 0, i32 2
  %27 = getelementptr inbounds [2 x i64], ptr %26, i64 0, i64 0
  %28 = load i64, ptr %27, align 8, !tbaa !9
  %29 = icmp ne i64 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %24
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %79

31:                                               ; preds = %24
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.__blake2b_state, ptr %33, i32 0, i32 4
  %35 = load i32, ptr %34, align 8, !tbaa !14
  %36 = zext i32 %35 to i64
  call void @blake2b_increment_counter(ptr noundef %32, i64 noundef %36)
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  call void @blake2b_set_lastblock(ptr noundef %37)
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.__blake2b_state, ptr %38, i32 0, i32 3
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.__blake2b_state, ptr %40, i32 0, i32 4
  %42 = load i32, ptr %41, align 8, !tbaa !14
  %43 = zext i32 %42 to i64
  %44 = getelementptr inbounds [128 x i8], ptr %39, i64 0, i64 %43
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.__blake2b_state, ptr %45, i32 0, i32 4
  %47 = load i32, ptr %46, align 8, !tbaa !14
  %48 = sub i32 128, %47
  %49 = zext i32 %48 to i64
  call void @llvm.memset.p0.i64(ptr align 1 %44, i8 0, i64 %49, i1 false)
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.__blake2b_state, ptr %51, i32 0, i32 3
  %53 = getelementptr inbounds [128 x i8], ptr %52, i64 0, i64 0
  call void @blake2b_compress(ptr noundef %50, ptr noundef %53)
  store i32 0, ptr %9, align 4, !tbaa !15
  br label %54

54:                                               ; preds = %69, %31
  %55 = load i32, ptr %9, align 4, !tbaa !15
  %56 = icmp ult i32 %55, 8
  br i1 %56, label %57, label %72

57:                                               ; preds = %54
  %58 = getelementptr inbounds [64 x i8], ptr %8, i64 0, i64 0
  %59 = load i32, ptr %9, align 4, !tbaa !15
  %60 = zext i32 %59 to i64
  %61 = mul i64 8, %60
  %62 = getelementptr inbounds i8, ptr %58, i64 %61
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.__blake2b_state, ptr %63, i32 0, i32 0
  %65 = load i32, ptr %9, align 4, !tbaa !15
  %66 = zext i32 %65 to i64
  %67 = getelementptr inbounds [8 x i64], ptr %64, i64 0, i64 %66
  %68 = load i64, ptr %67, align 8, !tbaa !9
  call void @store64(ptr noundef %62, i64 noundef %68)
  br label %69

69:                                               ; preds = %57
  %70 = load i32, ptr %9, align 4, !tbaa !15
  %71 = add i32 %70, 1
  store i32 %71, ptr %9, align 4, !tbaa !15
  br label %54, !llvm.loop !16

72:                                               ; preds = %54
  %73 = load ptr, ptr %6, align 8, !tbaa !5
  %74 = getelementptr inbounds [64 x i8], ptr %8, i64 0, i64 0
  %75 = load ptr, ptr %5, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.__blake2b_state, ptr %75, i32 0, i32 5
  %77 = load i32, ptr %76, align 4, !tbaa !11
  %78 = zext i32 %77 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %73, ptr align 16 %74, i64 %78, i1 false)
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %79

79:                                               ; preds = %72, %30, %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 64, ptr %8) #5
  %80 = load i32, ptr %4, align 4
  ret i32 %80
}

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

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #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, !13, i64 228}
!12 = !{!"__blake2b_state", !7, i64 0, !7, i64 64, !7, i64 80, !7, i64 96, !13, i64 224, !13, i64 228, !7, i64 232}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !13, i64 224}
!15 = !{!13, !13, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; 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"

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

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

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

; 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) #2

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

; Function Attrs: nounwind uwtable
declare hidden void @blake2b_compress(ptr noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @randomx_blake2b_final(ptr noundef %0, ptr noundef writeonly %1, i64 noundef %2) local_unnamed_addr #4 {
  %4 = alloca [64 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %4) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(64) %4, i8 0, i64 64, i1 false)
  %5 = icmp eq ptr %0, null
  %6 = icmp eq ptr %1, null
  %or.cond = or i1 %5, %6
  br i1 %or.cond, label %49, label %7

7:                                                ; preds = %3
  %8 = getelementptr inbounds i8, ptr %0, i64 228
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = zext i32 %9 to i64
  %11 = icmp ugt i64 %10, %2
  br i1 %11, label %49, label %12

12:                                               ; preds = %7
  %13 = getelementptr inbounds i8, ptr %0, i64 80
  %14 = load i64, ptr %13, align 8, !tbaa !10
  %.not = icmp eq i64 %14, 0
  br i1 %.not, label %15, label %49

15:                                               ; preds = %12
  %16 = getelementptr inbounds i8, ptr %0, i64 224
  %17 = load i32, ptr %16, align 8, !tbaa !12
  %18 = zext i32 %17 to i64
  tail call void @blake2b_increment_counter(ptr noundef nonnull %0, i64 noundef %18)
  tail call void @blake2b_set_lastblock(ptr noundef nonnull %0)
  %19 = getelementptr inbounds i8, ptr %0, i64 96
  %20 = load i32, ptr %16, align 8, !tbaa !12
  %21 = zext i32 %20 to i64
  %22 = getelementptr inbounds [128 x i8], ptr %19, i64 0, i64 %21
  %23 = sub i32 128, %20
  %24 = zext i32 %23 to i64
  tail call void @llvm.memset.p0.i64(ptr nonnull align 1 %22, i8 0, i64 %24, i1 false)
  tail call void @blake2b_compress(ptr noundef nonnull %0, ptr noundef nonnull %19)
  %25 = load i64, ptr %0, align 8, !tbaa !10
  call void @store64(ptr noundef nonnull %4, i64 noundef %25)
  %26 = getelementptr inbounds i8, ptr %4, i64 8
  %27 = getelementptr inbounds i8, ptr %0, i64 8
  %28 = load i64, ptr %27, align 8, !tbaa !10
  call void @store64(ptr noundef nonnull %26, i64 noundef %28)
  %29 = getelementptr inbounds i8, ptr %4, i64 16
  %30 = getelementptr inbounds i8, ptr %0, i64 16
  %31 = load i64, ptr %30, align 8, !tbaa !10
  call void @store64(ptr noundef nonnull %29, i64 noundef %31)
  %32 = getelementptr inbounds i8, ptr %4, i64 24
  %33 = getelementptr inbounds i8, ptr %0, i64 24
  %34 = load i64, ptr %33, align 8, !tbaa !10
  call void @store64(ptr noundef nonnull %32, i64 noundef %34)
  %35 = getelementptr inbounds i8, ptr %4, i64 32
  %36 = getelementptr inbounds i8, ptr %0, i64 32
  %37 = load i64, ptr %36, align 8, !tbaa !10
  call void @store64(ptr noundef nonnull %35, i64 noundef %37)
  %38 = getelementptr inbounds i8, ptr %4, i64 40
  %39 = getelementptr inbounds i8, ptr %0, i64 40
  %40 = load i64, ptr %39, align 8, !tbaa !10
  call void @store64(ptr noundef nonnull %38, i64 noundef %40)
  %41 = getelementptr inbounds i8, ptr %4, i64 48
  %42 = getelementptr inbounds i8, ptr %0, i64 48
  %43 = load i64, ptr %42, align 8, !tbaa !10
  call void @store64(ptr noundef nonnull %41, i64 noundef %43)
  %44 = getelementptr inbounds i8, ptr %4, i64 56
  %45 = getelementptr inbounds i8, ptr %0, i64 56
  %46 = load i64, ptr %45, align 8, !tbaa !10
  call void @store64(ptr noundef nonnull %44, i64 noundef %46)
  %47 = load i32, ptr %8, align 4, !tbaa !5
  %48 = zext i32 %47 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %1, ptr nonnull align 16 %4, i64 %48, i1 false)
  br label %49

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

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

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #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, !9, i64 228}
!6 = !{!"__blake2b_state", !7, i64 0, !7, i64 64, !7, i64 80, !7, i64 96, !9, i64 224, !9, i64 228, !7, i64 232}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !7, i64 0}
!12 = !{!6, !9, i64 224}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass