; LLVM IR from vergecurrency/verge :: src/crypto/sha512.ll (ZN7CSHA5125WriteEPKhm)

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

%class.CSHA512 = type { [8 x i64], [128 x i8], i64 }

; Function Attrs: mustprogress uwtable
define dso_local noundef nonnull align 8 dereferenceable(200) ptr @_ZN7CSHA5125WriteEPKhm(ptr noundef nonnull align 8 dereferenceable(200) %0, ptr noundef %1, i64 noundef %2) #0 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, 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
  %9 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = load i64, ptr %6, align 8, !tbaa !9
  %12 = getelementptr inbounds i8, ptr %10, i64 %11
  store ptr %12, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %13 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 2
  %14 = load i64, ptr %13, align 8, !tbaa !11
  %15 = urem i64 %14, 128
  store i64 %15, ptr %8, align 8, !tbaa !9
  %16 = load i64, ptr %8, align 8, !tbaa !9
  %17 = icmp ne i64 %16, 0
  br i1 %17, label %18, label %44

18:                                               ; preds = %3
  %19 = load i64, ptr %8, align 8, !tbaa !9
  %20 = load i64, ptr %6, align 8, !tbaa !9
  %21 = add i64 %19, %20
  %22 = icmp uge i64 %21, 128
  br i1 %22, label %23, label %44

23:                                               ; preds = %18
  %24 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 1
  %25 = getelementptr inbounds [128 x i8], ptr %24, i64 0, i64 0
  %26 = load i64, ptr %8, align 8, !tbaa !9
  %27 = getelementptr inbounds i8, ptr %25, i64 %26
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = load i64, ptr %8, align 8, !tbaa !9
  %30 = sub i64 128, %29
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %27, ptr align 1 %28, i64 %30, i1 false)
  %31 = load i64, ptr %8, align 8, !tbaa !9
  %32 = sub i64 128, %31
  %33 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 2
  %34 = load i64, ptr %33, align 8, !tbaa !11
  %35 = add i64 %34, %32
  store i64 %35, ptr %33, align 8, !tbaa !11
  %36 = load i64, ptr %8, align 8, !tbaa !9
  %37 = sub i64 128, %36
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %38, i64 %37
  store ptr %39, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 0
  %41 = getelementptr inbounds [8 x i64], ptr %40, i64 0, i64 0
  %42 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 1
  %43 = getelementptr inbounds [128 x i8], ptr %42, i64 0, i64 0
  call void @_ZN12_GLOBAL__N_16sha5129TransformEPmPKh(ptr noundef %41, ptr noundef %43)
  store i64 0, ptr %8, align 8, !tbaa !9
  br label %44

44:                                               ; preds = %23, %18, %3
  br label %45

45:                                               ; preds = %50, %44
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds i8, ptr %47, i64 128
  %49 = icmp uge ptr %46, %48
  br i1 %49, label %50, label %59

50:                                               ; preds = %45
  %51 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 0
  %52 = getelementptr inbounds [8 x i64], ptr %51, i64 0, i64 0
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  call void @_ZN12_GLOBAL__N_16sha5129TransformEPmPKh(ptr noundef %52, ptr noundef %53)
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds i8, ptr %54, i64 128
  store ptr %55, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 2
  %57 = load i64, ptr %56, align 8, !tbaa !11
  %58 = add i64 %57, 128
  store i64 %58, ptr %56, align 8, !tbaa !11
  br label %45, !llvm.loop !13

59:                                               ; preds = %45
  %60 = load ptr, ptr %7, align 8, !tbaa !5
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = icmp ugt ptr %60, %61
  br i1 %62, label %63, label %82

63:                                               ; preds = %59
  %64 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 1
  %65 = getelementptr inbounds [128 x i8], ptr %64, i64 0, i64 0
  %66 = load i64, ptr %8, align 8, !tbaa !9
  %67 = getelementptr inbounds i8, ptr %65, i64 %66
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = ptrtoint ptr %69 to i64
  %72 = ptrtoint ptr %70 to i64
  %73 = sub i64 %71, %72
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %67, ptr align 1 %68, i64 %73, i1 false)
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  %75 = load ptr, ptr %5, align 8, !tbaa !5
  %76 = ptrtoint ptr %74 to i64
  %77 = ptrtoint ptr %75 to i64
  %78 = sub i64 %76, %77
  %79 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 2
  %80 = load i64, ptr %79, align 8, !tbaa !11
  %81 = add i64 %80, %78
  store i64 %81, ptr %79, align 8, !tbaa !11
  br label %82

82:                                               ; preds = %63, %59
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret ptr %9
}

; 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: mustprogress uwtable
declare hidden void @_ZN12_GLOBAL__N_16sha5129TransformEPmPKh(ptr noundef, ptr noundef) #0

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

attributes #0 = { mustprogress 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 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 192}
!12 = !{!"_ZTS7CSHA512", !7, i64 0, !7, i64 64, !10, i64 192}
!13 = distinct !{!13, !14}
!14 = !{!"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 uwtable
define dso_local noundef nonnull align 8 dereferenceable(200) ptr @_ZN7CSHA5125WriteEPKhm(ptr noundef nonnull returned align 8 dereferenceable(200) %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 align 2 {
  %4 = getelementptr inbounds i8, ptr %1, i64 %2
  %5 = getelementptr inbounds i8, ptr %0, i64 192
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %7 = and i64 %6, 127
  %.not = icmp eq i64 %7, 0
  br i1 %.not, label %18, label %8

8:                                                ; preds = %3
  %9 = add i64 %7, %2
  %10 = icmp ugt i64 %9, 127
  br i1 %10, label %11, label %18

11:                                               ; preds = %8
  %12 = getelementptr inbounds i8, ptr %0, i64 64
  %13 = getelementptr inbounds i8, ptr %12, i64 %7
  %14 = sub nuw nsw i64 128, %7
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(1) %13, ptr noundef nonnull align 1 dereferenceable(1) %1, i64 %14, i1 false)
  %15 = load i64, ptr %5, align 8, !tbaa !5
  %16 = add i64 %15, %14
  store i64 %16, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %1, i64 %14
  tail call void @_ZN12_GLOBAL__N_16sha5129TransformEPmPKh(ptr noundef nonnull %0, ptr noundef nonnull %12)
  br label %18

18:                                               ; preds = %11, %8, %3
  %.023 = phi ptr [ %17, %11 ], [ %1, %8 ], [ %1, %3 ]
  %.0 = phi i64 [ 0, %11 ], [ %7, %8 ], [ 0, %3 ]
  %19 = getelementptr inbounds i8, ptr %.023, i64 128
  %.not2829 = icmp ult ptr %4, %19
  br i1 %.not2829, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %18, %.lr.ph
  %20 = phi ptr [ %23, %.lr.ph ], [ %19, %18 ]
  %.130 = phi ptr [ %20, %.lr.ph ], [ %.023, %18 ]
  tail call void @_ZN12_GLOBAL__N_16sha5129TransformEPmPKh(ptr noundef nonnull %0, ptr noundef %.130)
  %21 = load i64, ptr %5, align 8, !tbaa !5
  %22 = add i64 %21, 128
  store i64 %22, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %20, i64 128
  %.not28 = icmp ult ptr %4, %23
  br i1 %.not28, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph, %18
  %.1.lcssa = phi ptr [ %.023, %18 ], [ %20, %.lr.ph ]
  %24 = icmp ugt ptr %4, %.1.lcssa
  br i1 %24, label %25, label %33

25:                                               ; preds = %._crit_edge
  %26 = getelementptr inbounds i8, ptr %0, i64 64
  %27 = getelementptr inbounds i8, ptr %26, i64 %.0
  %28 = ptrtoint ptr %4 to i64
  %29 = ptrtoint ptr %.1.lcssa to i64
  %30 = sub i64 %28, %29
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %27, ptr align 1 %.1.lcssa, i64 %30, i1 false)
  %31 = load i64, ptr %5, align 8, !tbaa !5
  %32 = add i64 %31, %30
  store i64 %32, ptr %5, align 8, !tbaa !5
  br label %33

33:                                               ; preds = %25, %._crit_edge
  ret ptr %0
}

; 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: mustprogress uwtable
declare hidden void @_ZN12_GLOBAL__N_16sha5129TransformEPmPKh(ptr noundef, ptr noundef) local_unnamed_addr #0

attributes #0 = { mustprogress 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) }

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


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