; LLVM IR from gost-engine/engine :: gosthash2012 (gost2012_hash_block)

; 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.gost2012_hash_ctx = type { %union.uint512_u, %union.uint512_u, %union.uint512_u, %union.uint512_u, i64, i32 }
%union.uint512_u = type { [8 x i64] }

; Function Attrs: nounwind uwtable
define dso_local void @gost2012_hash_block(ptr noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.gost2012_hash_ctx, ptr %9, i32 0, i32 4
  %11 = load i64, ptr %10, align 8, !tbaa !11
  store i64 %11, ptr %7, align 8, !tbaa !9
  %12 = load i64, ptr %7, align 8, !tbaa !9
  %13 = icmp eq i64 %12, 0
  br i1 %13, label %14, label %31

14:                                               ; preds = %3
  br label %15

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

18:                                               ; preds = %15
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.gost2012_hash_ctx, ptr %19, i32 0, i32 0
  %21 = getelementptr inbounds [64 x i8], ptr %20, i64 0, i64 0
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %21, ptr align 1 %22, i64 64, i1 false)
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.gost2012_hash_ctx, ptr %24, i32 0, i32 0
  call void @stage2(ptr noundef %23, ptr noundef %25)
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %26, i64 64
  store ptr %27, ptr %5, align 8, !tbaa !5
  %28 = load i64, ptr %6, align 8, !tbaa !9
  %29 = sub i64 %28, 64
  store i64 %29, ptr %6, align 8, !tbaa !9
  br label %15, !llvm.loop !14

30:                                               ; preds = %15
  br label %31

31:                                               ; preds = %30, %3
  br label %32

32:                                               ; preds = %65, %31
  %33 = load i64, ptr %6, align 8, !tbaa !9
  %34 = icmp ne i64 %33, 0
  br i1 %34, label %35, label %66

35:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %36 = load i64, ptr %7, align 8, !tbaa !9
  %37 = sub i64 64, %36
  store i64 %37, ptr %8, align 8, !tbaa !9
  %38 = load i64, ptr %8, align 8, !tbaa !9
  %39 = load i64, ptr %6, align 8, !tbaa !9
  %40 = icmp ugt i64 %38, %39
  br i1 %40, label %41, label %43

41:                                               ; preds = %35
  %42 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %42, ptr %8, align 8, !tbaa !9
  br label %43

43:                                               ; preds = %41, %35
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.gost2012_hash_ctx, ptr %44, i32 0, i32 0
  %46 = load i64, ptr %7, align 8, !tbaa !9
  %47 = getelementptr inbounds [64 x i8], ptr %45, i64 0, i64 %46
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = load i64, ptr %8, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %47, ptr align 1 %48, i64 %49, i1 false)
  %50 = load i64, ptr %8, align 8, !tbaa !9
  %51 = load i64, ptr %7, align 8, !tbaa !9
  %52 = add i64 %51, %50
  store i64 %52, ptr %7, align 8, !tbaa !9
  %53 = load i64, ptr %8, align 8, !tbaa !9
  %54 = load i64, ptr %6, align 8, !tbaa !9
  %55 = sub i64 %54, %53
  store i64 %55, ptr %6, align 8, !tbaa !9
  %56 = load i64, ptr %8, align 8, !tbaa !9
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = getelementptr inbounds i8, ptr %57, i64 %56
  store ptr %58, ptr %5, align 8, !tbaa !5
  %59 = load i64, ptr %7, align 8, !tbaa !9
  %60 = icmp eq i64 %59, 64
  br i1 %60, label %61, label %65

61:                                               ; preds = %43
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.gost2012_hash_ctx, ptr %63, i32 0, i32 0
  call void @stage2(ptr noundef %62, ptr noundef %64)
  store i64 0, ptr %7, align 8, !tbaa !9
  br label %65

65:                                               ; preds = %61, %43
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %32, !llvm.loop !16

66:                                               ; preds = %32
  %67 = load i64, ptr %7, align 8, !tbaa !9
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.gost2012_hash_ctx, ptr %68, i32 0, i32 4
  store i64 %67, ptr %69, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret void
}

; 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 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 @stage2(ptr noundef, ptr noundef) #3

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

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

!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, !10, i64 256}
!12 = !{!"gost2012_hash_ctx", !7, i64 0, !7, i64 64, !7, i64 128, !7, i64 192, !10, i64 256, !13, i64 264}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !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"

; Function Attrs: nounwind uwtable
define dso_local void @gost2012_hash_block(ptr noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 256
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = icmp eq i64 %5, 0
  %7 = icmp ugt i64 %2, 63
  %or.cond = and i1 %7, %6
  br i1 %or.cond, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %3, %.preheader
  %.033 = phi ptr [ %8, %.preheader ], [ %1, %3 ]
  %.031 = phi i64 [ %9, %.preheader ], [ %2, %3 ]
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(64) %0, ptr noundef nonnull align 1 dereferenceable(64) %.033, i64 64, i1 false)
  tail call void @stage2(ptr noundef nonnull %0, ptr noundef nonnull %0)
  %8 = getelementptr inbounds i8, ptr %.033, i64 64
  %9 = add i64 %.031, -64
  %.old1 = icmp ugt i64 %9, 63
  br i1 %.old1, label %.preheader, label %.loopexit

.loopexit:                                        ; preds = %.preheader, %3
  %.134 = phi ptr [ %1, %3 ], [ %8, %.preheader ]
  %.132 = phi i64 [ %2, %3 ], [ %9, %.preheader ]
  %.not41 = icmp eq i64 %.132, 0
  br i1 %.not41, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.loopexit, %17
  %.03044 = phi i64 [ %.1, %17 ], [ %5, %.loopexit ]
  %.243 = phi i64 [ %13, %17 ], [ %.132, %.loopexit ]
  %.23542 = phi ptr [ %14, %17 ], [ %.134, %.loopexit ]
  %10 = sub i64 64, %.03044
  %spec.select = tail call i64 @llvm.umin.i64(i64 %10, i64 %.243)
  %11 = getelementptr inbounds [64 x i8], ptr %0, i64 0, i64 %.03044
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %11, ptr align 1 %.23542, i64 %spec.select, i1 false)
  %12 = add i64 %spec.select, %.03044
  %13 = sub i64 %.243, %spec.select
  %14 = getelementptr inbounds i8, ptr %.23542, i64 %spec.select
  %15 = icmp eq i64 %12, 64
  br i1 %15, label %16, label %17

16:                                               ; preds = %.lr.ph
  tail call void @stage2(ptr noundef %0, ptr noundef %0)
  br label %17

17:                                               ; preds = %16, %.lr.ph
  %.1 = phi i64 [ 0, %16 ], [ %12, %.lr.ph ]
  %.not = icmp eq i64 %13, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !11

._crit_edge:                                      ; preds = %17, %.loopexit
  %.030.lcssa = phi i64 [ %5, %.loopexit ], [ %.1, %17 ]
  store i64 %.030.lcssa, ptr %4, align 8, !tbaa !5
  ret void
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @stage2(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #3

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 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 256}
!6 = !{!"gost2012_hash_ctx", !7, i64 0, !7, i64 64, !7, i64 128, !7, i64 192, !9, i64 256, !10, i64 264}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"long", !7, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass