; LLVM IR from LoupVaillant/Monocypher-Utils :: src (sha512)

; 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.crypto_sha512_ctx = type { [80 x i64], [8 x i64], [16 x i64], [2 x i64], i64 }

; Function Attrs: nounwind uwtable
define dso_local void @crypto_sha512_update(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
  %9 = alloca i32, align 4
  %10 = 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) #2
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.crypto_sha512_ctx, ptr %11, i32 0, i32 4
  %13 = load i64, ptr %12, align 8, !tbaa !11
  %14 = xor i64 %13, -1
  %15 = add i64 %14, 1
  %16 = and i64 %15, 127
  %17 = load i64, ptr %6, align 8, !tbaa !9
  %18 = icmp ule i64 %16, %17
  br i1 %18, label %19, label %26

19:                                               ; preds = %3
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.crypto_sha512_ctx, ptr %20, i32 0, i32 4
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = xor i64 %22, -1
  %24 = add i64 %23, 1
  %25 = and i64 %24, 127
  br label %28

26:                                               ; preds = %3
  %27 = load i64, ptr %6, align 8, !tbaa !9
  br label %28

28:                                               ; preds = %26, %19
  %29 = phi i64 [ %25, %19 ], [ %27, %26 ]
  store i64 %29, ptr %7, align 8, !tbaa !9
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = load i64, ptr %7, align 8, !tbaa !9
  call void @sha512_update(ptr noundef %30, ptr noundef %31, i64 noundef %32)
  %33 = load i64, ptr %7, align 8, !tbaa !9
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %34, i64 %33
  store ptr %35, ptr %5, align 8, !tbaa !5
  %36 = load i64, ptr %7, align 8, !tbaa !9
  %37 = load i64, ptr %6, align 8, !tbaa !9
  %38 = sub i64 %37, %36
  store i64 %38, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  store i64 0, ptr %8, align 8, !tbaa !9
  br label %39

39:                                               ; preds = %71, %28
  %40 = load i64, ptr %8, align 8, !tbaa !9
  %41 = load i64, ptr %6, align 8, !tbaa !9
  %42 = udiv i64 %41, 128
  %43 = icmp ult i64 %40, %42
  br i1 %43, label %45, label %44

44:                                               ; preds = %39
  store i32 2, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %74

45:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %46

46:                                               ; preds = %60, %45
  %47 = load i64, ptr %10, align 8, !tbaa !9
  %48 = icmp ult i64 %47, 16
  br i1 %48, label %50, label %49

49:                                               ; preds = %46
  store i32 5, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  br label %63

50:                                               ; preds = %46
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = load i64, ptr %10, align 8, !tbaa !9
  %53 = mul i64 %52, 8
  %54 = getelementptr inbounds i8, ptr %51, i64 %53
  %55 = call i64 @load64_be(ptr noundef %54)
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.crypto_sha512_ctx, ptr %56, i32 0, i32 2
  %58 = load i64, ptr %10, align 8, !tbaa !9
  %59 = getelementptr inbounds [16 x i64], ptr %57, i64 0, i64 %58
  store i64 %55, ptr %59, align 8, !tbaa !9
  br label %60

60:                                               ; preds = %50
  %61 = load i64, ptr %10, align 8, !tbaa !9
  %62 = add i64 %61, 1
  store i64 %62, ptr %10, align 8, !tbaa !9
  br label %46, !llvm.loop !13

63:                                               ; preds = %49
  %64 = load ptr, ptr %5, align 8, !tbaa !5
  %65 = getelementptr inbounds i8, ptr %64, i64 128
  store ptr %65, ptr %5, align 8, !tbaa !5
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.crypto_sha512_ctx, ptr %66, i32 0, i32 4
  %68 = load i64, ptr %67, align 8, !tbaa !11
  %69 = add i64 %68, 128
  store i64 %69, ptr %67, align 8, !tbaa !11
  %70 = load ptr, ptr %4, align 8, !tbaa !5
  call void @sha512_end_block(ptr noundef %70)
  br label %71

71:                                               ; preds = %63
  %72 = load i64, ptr %8, align 8, !tbaa !9
  %73 = add i64 %72, 1
  store i64 %73, ptr %8, align 8, !tbaa !9
  br label %39, !llvm.loop !15

74:                                               ; preds = %44
  %75 = load i64, ptr %6, align 8, !tbaa !9
  %76 = and i64 %75, 127
  store i64 %76, ptr %6, align 8, !tbaa !9
  %77 = load ptr, ptr %4, align 8, !tbaa !5
  %78 = load ptr, ptr %5, align 8, !tbaa !5
  %79 = load i64, ptr %6, align 8, !tbaa !9
  call void @sha512_update(ptr noundef %77, ptr noundef %78, i64 noundef %79)
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  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: nounwind uwtable
declare hidden void @sha512_update(ptr noundef, ptr noundef, i64 noundef) #0

; Function Attrs: nounwind uwtable
declare hidden i64 @load64_be(ptr noundef) #0

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

; Function Attrs: nounwind uwtable
declare hidden void @sha512_end_block(ptr 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 = { 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 848}
!12 = !{!"", !7, i64 0, !7, i64 640, !7, i64 704, !7, i64 832, !10, i64 848}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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 @crypto_sha512_update(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 848
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = sub i64 0, %5
  %7 = and i64 %6, 127
  %. = tail call i64 @llvm.umin.i64(i64 %7, i64 %2)
  tail call void @sha512_update(ptr noundef %0, ptr noundef %1, i64 noundef %.)
  %8 = getelementptr inbounds i8, ptr %1, i64 %.
  %9 = sub i64 %2, %.
  %.not = icmp ult i64 %9, 128
  br i1 %.not, label %._crit_edge, label %.preheader.lr.ph

.preheader.lr.ph:                                 ; preds = %3
  %10 = lshr i64 %9, 7
  %11 = getelementptr inbounds i8, ptr %0, i64 704
  %12 = getelementptr inbounds i8, ptr %0, i64 712
  %13 = getelementptr inbounds i8, ptr %0, i64 720
  %14 = getelementptr inbounds i8, ptr %0, i64 728
  %15 = getelementptr inbounds i8, ptr %0, i64 736
  %16 = getelementptr inbounds i8, ptr %0, i64 744
  %17 = getelementptr inbounds i8, ptr %0, i64 752
  %18 = getelementptr inbounds i8, ptr %0, i64 760
  %19 = getelementptr inbounds i8, ptr %0, i64 768
  %20 = getelementptr inbounds i8, ptr %0, i64 776
  %21 = getelementptr inbounds i8, ptr %0, i64 784
  %22 = getelementptr inbounds i8, ptr %0, i64 792
  %23 = getelementptr inbounds i8, ptr %0, i64 800
  %24 = getelementptr inbounds i8, ptr %0, i64 808
  %25 = getelementptr inbounds i8, ptr %0, i64 816
  %26 = getelementptr inbounds i8, ptr %0, i64 824
  br label %.preheader

.preheader:                                       ; preds = %.preheader.lr.ph, %.preheader
  %.02632 = phi i64 [ 0, %.preheader.lr.ph ], [ %61, %.preheader ]
  %.02731 = phi ptr [ %8, %.preheader.lr.ph ], [ %58, %.preheader ]
  %27 = tail call i64 @load64_be(ptr noundef %.02731)
  store i64 %27, ptr %11, align 8, !tbaa !10
  %28 = getelementptr inbounds i8, ptr %.02731, i64 8
  %29 = tail call i64 @load64_be(ptr noundef nonnull %28)
  store i64 %29, ptr %12, align 8, !tbaa !10
  %30 = getelementptr inbounds i8, ptr %.02731, i64 16
  %31 = tail call i64 @load64_be(ptr noundef nonnull %30)
  store i64 %31, ptr %13, align 8, !tbaa !10
  %32 = getelementptr inbounds i8, ptr %.02731, i64 24
  %33 = tail call i64 @load64_be(ptr noundef nonnull %32)
  store i64 %33, ptr %14, align 8, !tbaa !10
  %34 = getelementptr inbounds i8, ptr %.02731, i64 32
  %35 = tail call i64 @load64_be(ptr noundef nonnull %34)
  store i64 %35, ptr %15, align 8, !tbaa !10
  %36 = getelementptr inbounds i8, ptr %.02731, i64 40
  %37 = tail call i64 @load64_be(ptr noundef nonnull %36)
  store i64 %37, ptr %16, align 8, !tbaa !10
  %38 = getelementptr inbounds i8, ptr %.02731, i64 48
  %39 = tail call i64 @load64_be(ptr noundef nonnull %38)
  store i64 %39, ptr %17, align 8, !tbaa !10
  %40 = getelementptr inbounds i8, ptr %.02731, i64 56
  %41 = tail call i64 @load64_be(ptr noundef nonnull %40)
  store i64 %41, ptr %18, align 8, !tbaa !10
  %42 = getelementptr inbounds i8, ptr %.02731, i64 64
  %43 = tail call i64 @load64_be(ptr noundef nonnull %42)
  store i64 %43, ptr %19, align 8, !tbaa !10
  %44 = getelementptr inbounds i8, ptr %.02731, i64 72
  %45 = tail call i64 @load64_be(ptr noundef nonnull %44)
  store i64 %45, ptr %20, align 8, !tbaa !10
  %46 = getelementptr inbounds i8, ptr %.02731, i64 80
  %47 = tail call i64 @load64_be(ptr noundef nonnull %46)
  store i64 %47, ptr %21, align 8, !tbaa !10
  %48 = getelementptr inbounds i8, ptr %.02731, i64 88
  %49 = tail call i64 @load64_be(ptr noundef nonnull %48)
  store i64 %49, ptr %22, align 8, !tbaa !10
  %50 = getelementptr inbounds i8, ptr %.02731, i64 96
  %51 = tail call i64 @load64_be(ptr noundef nonnull %50)
  store i64 %51, ptr %23, align 8, !tbaa !10
  %52 = getelementptr inbounds i8, ptr %.02731, i64 104
  %53 = tail call i64 @load64_be(ptr noundef nonnull %52)
  store i64 %53, ptr %24, align 8, !tbaa !10
  %54 = getelementptr inbounds i8, ptr %.02731, i64 112
  %55 = tail call i64 @load64_be(ptr noundef nonnull %54)
  store i64 %55, ptr %25, align 8, !tbaa !10
  %56 = getelementptr inbounds i8, ptr %.02731, i64 120
  %57 = tail call i64 @load64_be(ptr noundef nonnull %56)
  store i64 %57, ptr %26, align 8, !tbaa !10
  %58 = getelementptr inbounds i8, ptr %.02731, i64 128
  %59 = load i64, ptr %4, align 8, !tbaa !5
  %60 = add i64 %59, 128
  store i64 %60, ptr %4, align 8, !tbaa !5
  tail call void @sha512_end_block(ptr noundef nonnull %0)
  %61 = add nuw nsw i64 %.02632, 1
  %exitcond.not = icmp eq i64 %61, %10
  br i1 %exitcond.not, label %._crit_edge, label %.preheader, !llvm.loop !11

._crit_edge:                                      ; preds = %.preheader, %3
  %.027.lcssa = phi ptr [ %8, %3 ], [ %58, %.preheader ]
  %62 = and i64 %9, 127
  tail call void @sha512_update(ptr noundef nonnull %0, ptr noundef %.027.lcssa, i64 noundef %62)
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @sha512_update(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden i64 @load64_be(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @sha512_end_block(ptr noundef) local_unnamed_addr #0

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #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 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 848}
!6 = !{!"", !7, i64 0, !7, i64 640, !7, i64 704, !7, i64 832, !9, i64 848}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"long", !7, i64 0}
!10 = !{!9, !9, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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