; LLVM IR from albertobsd/keyhunt :: hash/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, i64 }

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN7CSHA5125WriteEPKhm(ptr noundef nonnull align 8 dereferenceable(208) %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 i64, align 8
  %8 = alloca i32, align 4
  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
  %10 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 2
  %11 = load i64, ptr %10, align 8, !tbaa !11
  %12 = icmp ugt i64 %11, 0
  br i1 %12, label %13, label %56

13:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %14 = load i64, ptr %6, align 8, !tbaa !9
  %15 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 2
  %16 = load i64, ptr %15, align 8, !tbaa !11
  %17 = sub i64 128, %16
  %18 = icmp ult i64 %14, %17
  br i1 %18, label %19, label %21

19:                                               ; preds = %13
  %20 = load i64, ptr %6, align 8, !tbaa !9
  br label %25

21:                                               ; preds = %13
  %22 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 2
  %23 = load i64, ptr %22, align 8, !tbaa !11
  %24 = sub i64 128, %23
  br label %25

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

44:                                               ; preds = %25
  store i32 1, ptr %8, align 4
  br label %53

45:                                               ; preds = %25
  %46 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 0
  %47 = getelementptr inbounds [8 x i64], ptr %46, i64 0, i64 0
  %48 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 1
  %49 = getelementptr inbounds [128 x i8], ptr %48, i64 0, i64 0
  call void @_ZN7_sha512L9TransformEPmPKh(ptr noundef %47, ptr noundef %49)
  %50 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 3
  %51 = load i64, ptr %50, align 8, !tbaa !13
  %52 = add i64 %51, 1
  store i64 %52, ptr %50, align 8, !tbaa !13
  store i32 0, ptr %8, align 4
  br label %53

53:                                               ; preds = %45, %44
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  %54 = load i32, ptr %8, align 4
  switch i32 %54, label %79 [
    i32 0, label %55
    i32 1, label %78
  ]

55:                                               ; preds = %53
  br label %56

56:                                               ; preds = %55, %3
  br label %57

57:                                               ; preds = %60, %56
  %58 = load i64, ptr %6, align 8, !tbaa !9
  %59 = icmp uge i64 %58, 128
  br i1 %59, label %60, label %71

60:                                               ; preds = %57
  %61 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 0
  %62 = getelementptr inbounds [8 x i64], ptr %61, i64 0, i64 0
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  call void @_ZN7_sha512L9TransformEPmPKh(ptr noundef %62, ptr noundef %63)
  %64 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 3
  %65 = load i64, ptr %64, align 8, !tbaa !13
  %66 = add i64 %65, 1
  store i64 %66, ptr %64, align 8, !tbaa !13
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  %68 = getelementptr inbounds i8, ptr %67, i64 128
  store ptr %68, ptr %5, align 8, !tbaa !5
  %69 = load i64, ptr %6, align 8, !tbaa !9
  %70 = sub i64 %69, 128
  store i64 %70, ptr %6, align 8, !tbaa !9
  br label %57, !llvm.loop !14

71:                                               ; preds = %57
  %72 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 1
  %73 = getelementptr inbounds [128 x i8], ptr %72, i64 0, i64 0
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = load i64, ptr %6, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %73, ptr align 1 %74, i64 %75, i1 false)
  %76 = load i64, ptr %6, align 8, !tbaa !9
  %77 = getelementptr inbounds %class.CSHA512, ptr %9, i32 0, i32 2
  store i64 %76, ptr %77, align 8, !tbaa !11
  br label %78

78:                                               ; preds = %71, %53
  ret void

79:                                               ; preds = %53
  unreachable
}

; 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 @_ZN7_sha512L9TransformEPmPKh(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, !10, i64 200}
!13 = !{!12, !10, i64 200}
!14 = distinct !{!14, !15}
!15 = !{!"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 void @_ZN7CSHA5125WriteEPKhm(ptr noundef nonnull align 8 dereferenceable(208) %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 192
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %.not = icmp eq i64 %5, 0
  br i1 %.not, label %17, label %6

6:                                                ; preds = %3
  %7 = sub i64 128, %5
  %. = tail call i64 @llvm.umin.i64(i64 %7, i64 %2)
  %8 = getelementptr inbounds i8, ptr %0, i64 64
  %9 = getelementptr inbounds i8, ptr %8, i64 %5
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %9, ptr align 1 %1, i64 %., i1 false)
  %10 = load i64, ptr %4, align 8, !tbaa !5
  %11 = add i64 %10, %.
  store i64 %11, ptr %4, align 8, !tbaa !5
  %12 = icmp ult i64 %11, 128
  br i1 %12, label %27, label %.thread

.thread:                                          ; preds = %6
  %13 = sub i64 %2, %.
  tail call void @_ZN7_sha512L9TransformEPmPKh(ptr noundef nonnull %0, ptr noundef nonnull %8)
  %14 = getelementptr inbounds i8, ptr %0, i64 200
  %15 = load i64, ptr %14, align 8, !tbaa !10
  %16 = add i64 %15, 1
  store i64 %16, ptr %14, align 8, !tbaa !10
  br label %17

17:                                               ; preds = %.thread, %3
  %.016 = phi i64 [ %2, %3 ], [ %13, %.thread ]
  %18 = icmp ugt i64 %.016, 127
  br i1 %18, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %17
  %19 = getelementptr inbounds i8, ptr %0, i64 200
  br label %20

20:                                               ; preds = %.lr.ph, %20
  %.01522 = phi ptr [ %1, %.lr.ph ], [ %23, %20 ]
  %.121 = phi i64 [ %.016, %.lr.ph ], [ %24, %20 ]
  tail call void @_ZN7_sha512L9TransformEPmPKh(ptr noundef nonnull %0, ptr noundef %.01522)
  %21 = load i64, ptr %19, align 8, !tbaa !10
  %22 = add i64 %21, 1
  store i64 %22, ptr %19, align 8, !tbaa !10
  %23 = getelementptr inbounds i8, ptr %.01522, i64 128
  %24 = add i64 %.121, -128
  %25 = icmp ugt i64 %24, 127
  br i1 %25, label %20, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %20, %17
  %.1.lcssa = phi i64 [ %.016, %17 ], [ %24, %20 ]
  %.015.lcssa = phi ptr [ %1, %17 ], [ %23, %20 ]
  %26 = getelementptr inbounds i8, ptr %0, i64 64
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 8 %26, ptr align 1 %.015.lcssa, i64 %.1.lcssa, i1 false)
  store i64 %.1.lcssa, ptr %4, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %6, %._crit_edge
  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: mustprogress uwtable
declare hidden void @_ZN7_sha512L9TransformEPmPKh(ptr noundef, ptr noundef) local_unnamed_addr #0

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

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


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