; LLVM IR from SSBMTonberry/pocketlzma :: ref_spec/lzma1900/C (Sha256)

; 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.CSha256 = type { [8 x i32], i64, [64 x i8] }

; Function Attrs: nounwind uwtable
define dso_local void @Sha256_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 i32, align 4
  %8 = alloca i32, align 4
  %9 = 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
  %10 = load i64, ptr %6, align 8, !tbaa !9
  %11 = icmp eq i64 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %3
  br label %83

13:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.CSha256, ptr %14, i32 0, i32 1
  %16 = load i64, ptr %15, align 8, !tbaa !11
  %17 = trunc i64 %16 to i32
  %18 = and i32 %17, 63
  store i32 %18, ptr %7, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %19 = load i64, ptr %6, align 8, !tbaa !9
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.CSha256, ptr %20, i32 0, i32 1
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = add i64 %22, %19
  store i64 %23, ptr %21, align 8, !tbaa !11
  %24 = load i32, ptr %7, align 4, !tbaa !14
  %25 = sub i32 64, %24
  store i32 %25, ptr %8, align 4, !tbaa !14
  %26 = load i32, ptr %8, align 4, !tbaa !14
  %27 = zext i32 %26 to i64
  %28 = load i64, ptr %6, align 8, !tbaa !9
  %29 = icmp ugt i64 %27, %28
  br i1 %29, label %30, label %39

30:                                               ; preds = %13
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.CSha256, ptr %31, i32 0, i32 2
  %33 = getelementptr inbounds [64 x i8], ptr %32, i64 0, i64 0
  %34 = load i32, ptr %7, align 4, !tbaa !14
  %35 = zext i32 %34 to i64
  %36 = getelementptr inbounds i8, ptr %33, i64 %35
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = load i64, ptr %6, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %36, ptr align 1 %37, i64 %38, i1 false)
  store i32 1, ptr %9, align 4
  br label %57

39:                                               ; preds = %13
  %40 = load i32, ptr %8, align 4, !tbaa !14
  %41 = zext i32 %40 to i64
  %42 = load i64, ptr %6, align 8, !tbaa !9
  %43 = sub i64 %42, %41
  store i64 %43, ptr %6, align 8, !tbaa !9
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.CSha256, ptr %44, i32 0, i32 2
  %46 = getelementptr inbounds [64 x i8], ptr %45, i64 0, i64 0
  %47 = load i32, ptr %7, align 4, !tbaa !14
  %48 = zext i32 %47 to i64
  %49 = getelementptr inbounds i8, ptr %46, i64 %48
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = load i32, ptr %8, align 4, !tbaa !14
  %52 = zext i32 %51 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %49, ptr align 1 %50, i64 %52, i1 false)
  %53 = load i32, ptr %8, align 4, !tbaa !14
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = zext i32 %53 to i64
  %56 = getelementptr inbounds i8, ptr %54, i64 %55
  store ptr %56, ptr %5, align 8, !tbaa !5
  store i32 0, ptr %9, align 4
  br label %57

57:                                               ; preds = %39, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  %58 = load i32, ptr %9, align 4
  switch i32 %58, label %84 [
    i32 0, label %59
    i32 1, label %83
  ]

59:                                               ; preds = %57
  br label %60

60:                                               ; preds = %65, %59
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  call void @Sha256_WriteByteBlock(ptr noundef %61)
  %62 = load i64, ptr %6, align 8, !tbaa !9
  %63 = icmp ult i64 %62, 64
  br i1 %63, label %64, label %65

64:                                               ; preds = %60
  br label %74

65:                                               ; preds = %60
  %66 = load i64, ptr %6, align 8, !tbaa !9
  %67 = sub i64 %66, 64
  store i64 %67, ptr %6, align 8, !tbaa !9
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.CSha256, ptr %68, i32 0, i32 2
  %70 = getelementptr inbounds [64 x i8], ptr %69, i64 0, i64 0
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %70, ptr align 1 %71, i64 64, i1 false)
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  %73 = getelementptr inbounds i8, ptr %72, i64 64
  store ptr %73, ptr %5, align 8, !tbaa !5
  br label %60

74:                                               ; preds = %64
  %75 = load i64, ptr %6, align 8, !tbaa !9
  %76 = icmp ne i64 %75, 0
  br i1 %76, label %77, label %83

77:                                               ; preds = %74
  %78 = load ptr, ptr %4, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.CSha256, ptr %78, i32 0, i32 2
  %80 = getelementptr inbounds [64 x i8], ptr %79, i64 0, i64 0
  %81 = load ptr, ptr %5, align 8, !tbaa !5
  %82 = load i64, ptr %6, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %80, ptr align 1 %81, i64 %82, i1 false)
  br label %83

83:                                               ; preds = %77, %74, %57, %12
  ret void

84:                                               ; preds = %57
  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: 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 @Sha256_WriteByteBlock(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 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 32}
!12 = !{!"", !7, i64 0, !13, i64 32, !7, i64 40}
!13 = !{!"long long", !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}


; 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 @Sha256_Update(ptr noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq i64 %2, 0
  br i1 %4, label %24, label %5

5:                                                ; preds = %3
  %6 = getelementptr inbounds i8, ptr %0, i64 32
  %7 = load i64, ptr %6, align 8, !tbaa !5
  %8 = and i64 %7, 63
  %9 = add i64 %7, %2
  store i64 %9, ptr %6, align 8, !tbaa !5
  %10 = sub nuw nsw i64 64, %8
  %11 = icmp ugt i64 %10, %2
  br i1 %11, label %.thread, label %14

.thread:                                          ; preds = %5
  %12 = getelementptr inbounds i8, ptr %0, i64 40
  %13 = getelementptr inbounds i8, ptr %12, i64 %8
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %13, ptr align 1 %1, i64 %2, i1 false)
  br label %24

14:                                               ; preds = %5
  %15 = sub i64 %2, %10
  %16 = getelementptr inbounds i8, ptr %0, i64 40
  %17 = getelementptr inbounds i8, ptr %16, i64 %8
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(1) %17, ptr noundef nonnull align 1 dereferenceable(1) %1, i64 %10, i1 false)
  %18 = getelementptr inbounds i8, ptr %1, i64 %10
  tail call void @Sha256_WriteByteBlock(ptr noundef nonnull %0)
  %19 = icmp ult i64 %15, 64
  br i1 %19, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %14, %.lr.ph
  %.138 = phi ptr [ %21, %.lr.ph ], [ %18, %14 ]
  %.13137 = phi i64 [ %20, %.lr.ph ], [ %15, %14 ]
  %20 = add i64 %.13137, -64
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(64) %16, ptr noundef nonnull align 1 dereferenceable(64) %.138, i64 64, i1 false)
  %21 = getelementptr inbounds i8, ptr %.138, i64 64
  tail call void @Sha256_WriteByteBlock(ptr noundef nonnull %0)
  %22 = icmp ult i64 %20, 64
  br i1 %22, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %14
  %.131.lcssa = phi i64 [ %15, %14 ], [ %20, %.lr.ph ]
  %.1.lcssa = phi ptr [ %18, %14 ], [ %21, %.lr.ph ]
  %.not = icmp eq i64 %.131.lcssa, 0
  br i1 %.not, label %24, label %23

23:                                               ; preds = %._crit_edge
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 8 %16, ptr nonnull align 1 %.1.lcssa, i64 %.131.lcssa, i1 false)
  br label %24

24:                                               ; preds = %.thread, %3, %23, %._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: nounwind uwtable
declare hidden void @Sha256_WriteByteBlock(ptr noundef) local_unnamed_addr #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 = { 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 32}
!6 = !{!"", !7, i64 0, !9, i64 32, !7, i64 40}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"long long", !7, i64 0}


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