; LLVM IR from machinefi/psa-crypto-esp32 :: crypto (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.tc_sha256_state_struct = type { [8 x i32], i64, [64 x i8], i64 }

; Function Attrs: nounwind uwtable
define dso_local i32 @tc_sha256_update(ptr noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  %8 = load ptr, ptr %5, align 8, !tbaa !5
  %9 = icmp eq ptr %8, null
  br i1 %9, label %13, label %10

10:                                               ; preds = %3
  %11 = load ptr, ptr %6, align 8, !tbaa !5
  %12 = icmp eq ptr %11, null
  br i1 %12, label %13, label %14

13:                                               ; preds = %10, %3
  store i32 0, ptr %4, align 4
  br label %54

14:                                               ; preds = %10
  %15 = load i64, ptr %7, align 8, !tbaa !9
  %16 = icmp eq i64 %15, 0
  br i1 %16, label %17, label %18

17:                                               ; preds = %14
  store i32 1, ptr %4, align 4
  br label %54

18:                                               ; preds = %14
  br label %19

19:                                               ; preds = %18
  br label %20

20:                                               ; preds = %52, %19
  %21 = load i64, ptr %7, align 8, !tbaa !9
  %22 = add i64 %21, -1
  store i64 %22, ptr %7, align 8, !tbaa !9
  %23 = icmp ugt i64 %21, 0
  br i1 %23, label %24, label %53

24:                                               ; preds = %20
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %25, i32 1
  store ptr %26, ptr %6, align 8, !tbaa !5
  %27 = load i8, ptr %25, align 1, !tbaa !11
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.tc_sha256_state_struct, ptr %28, i32 0, i32 2
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.tc_sha256_state_struct, ptr %30, i32 0, i32 3
  %32 = load i64, ptr %31, align 8, !tbaa !12
  %33 = add i64 %32, 1
  store i64 %33, ptr %31, align 8, !tbaa !12
  %34 = getelementptr inbounds [64 x i8], ptr %29, i64 0, i64 %32
  store i8 %27, ptr %34, align 1, !tbaa !11
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.tc_sha256_state_struct, ptr %35, i32 0, i32 3
  %37 = load i64, ptr %36, align 8, !tbaa !12
  %38 = icmp uge i64 %37, 64
  br i1 %38, label %39, label %52

39:                                               ; preds = %24
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.tc_sha256_state_struct, ptr %40, i32 0, i32 0
  %42 = getelementptr inbounds [8 x i32], ptr %41, i64 0, i64 0
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.tc_sha256_state_struct, ptr %43, i32 0, i32 2
  %45 = getelementptr inbounds [64 x i8], ptr %44, i64 0, i64 0
  call void @compress(ptr noundef %42, ptr noundef %45)
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.tc_sha256_state_struct, ptr %46, i32 0, i32 3
  store i64 0, ptr %47, align 8, !tbaa !12
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.tc_sha256_state_struct, ptr %48, i32 0, i32 1
  %50 = load i64, ptr %49, align 8, !tbaa !14
  %51 = add i64 %50, 512
  store i64 %51, ptr %49, align 8, !tbaa !14
  br label %52

52:                                               ; preds = %39, %24
  br label %20, !llvm.loop !15

53:                                               ; preds = %20
  store i32 1, ptr %4, align 4
  br label %54

54:                                               ; preds = %53, %17, %13
  %55 = load i32, ptr %4, align 4
  ret i32 %55
}

; Function Attrs: nounwind uwtable
declare hidden void @compress(ptr noundef, 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" }

!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 = !{!7, !7, i64 0}
!12 = !{!13, !10, i64 104}
!13 = !{!"tc_sha256_state_struct", !7, i64 0, !10, i64 32, !7, i64 40, !10, i64 104}
!14 = !{!13, !10, i64 32}
!15 = distinct !{!15, !16}
!16 = !{!"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: nounwind uwtable
define dso_local range(i32 0, 2) i32 @tc_sha256_update(ptr noundef %0, ptr noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq ptr %0, null
  %5 = icmp eq ptr %1, null
  %or.cond = or i1 %4, %5
  br i1 %or.cond, label %.loopexit, label %6

6:                                                ; preds = %3
  %7 = icmp eq i64 %2, 0
  br i1 %7, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %6
  %8 = getelementptr inbounds i8, ptr %0, i64 40
  %9 = getelementptr inbounds i8, ptr %0, i64 104
  %10 = getelementptr inbounds i8, ptr %0, i64 32
  %.pre = load i64, ptr %9, align 8, !tbaa !5
  br label %11

11:                                               ; preds = %.preheader, %23
  %12 = phi i64 [ %.pre, %.preheader ], [ %24, %23 ]
  %.in = phi i64 [ %2, %.preheader ], [ %13, %23 ]
  %.01318 = phi ptr [ %1, %.preheader ], [ %14, %23 ]
  %13 = add i64 %.in, -1
  %14 = getelementptr inbounds i8, ptr %.01318, i64 1
  %15 = load i8, ptr %.01318, align 1, !tbaa !10
  %16 = add i64 %12, 1
  store i64 %16, ptr %9, align 8, !tbaa !5
  %17 = getelementptr inbounds [64 x i8], ptr %8, i64 0, i64 %12
  store i8 %15, ptr %17, align 1, !tbaa !10
  %18 = load i64, ptr %9, align 8, !tbaa !5
  %19 = icmp ugt i64 %18, 63
  br i1 %19, label %20, label %23

20:                                               ; preds = %11
  tail call void @compress(ptr noundef nonnull %0, ptr noundef nonnull %8)
  store i64 0, ptr %9, align 8, !tbaa !5
  %21 = load i64, ptr %10, align 8, !tbaa !11
  %22 = add i64 %21, 512
  store i64 %22, ptr %10, align 8, !tbaa !11
  br label %23

23:                                               ; preds = %20, %11
  %24 = phi i64 [ 0, %20 ], [ %18, %11 ]
  %.not = icmp eq i64 %13, 0
  br i1 %.not, label %.loopexit, label %11, !llvm.loop !12

.loopexit:                                        ; preds = %23, %6, %3
  %.014 = phi i32 [ 0, %3 ], [ 1, %6 ], [ 1, %23 ]
  ret i32 %.014
}

; Function Attrs: nounwind uwtable
declare hidden void @compress(ptr noundef, 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" }

!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 104}
!6 = !{!"tc_sha256_state_struct", !7, i64 0, !9, i64 32, !7, i64 40, !9, i64 104}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"long", !7, i64 0}
!10 = !{!7, !7, i64 0}
!11 = !{!6, !9, i64 32}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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