; LLVM IR from machinefi/psa-crypto-esp32 :: psa_layer (psa_crypto)

; 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.psa_mac_operation_s = type { i32, i8, i8, %union.psa_driver_mac_context_t }
%union.psa_driver_mac_context_t = type { %struct.iotex_psa_mac_operation_t }
%struct.iotex_psa_mac_operation_t = type { i32, %union.anon.0 }
%union.anon.0 = type { %struct.iotex_psa_hmac_operation_t }
%struct.iotex_psa_hmac_operation_t = type { i32, %struct.psa_hash_operation_s, [64 x i8] }
%struct.psa_hash_operation_s = type { i32, %union.psa_driver_hash_context_t }
%union.psa_driver_hash_context_t = type { %struct.iotex_psa_hash_operation_t }
%struct.iotex_psa_hash_operation_t = type { i32, %union.anon }
%union.anon = type { %struct.iotex_sha256_context }
%struct.iotex_sha256_context = type { ptr }

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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @psa_mac_abort(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_mac_sign_finish(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) #2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  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
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 -151, ptr %9, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  store i32 -151, ptr %10, align 4, !tbaa !11
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.psa_mac_operation_s, ptr %11, i32 0, i32 0
  %13 = load i32, ptr %12, align 8, !tbaa !13
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %15, label %16

15:                                               ; preds = %4
  store i32 -137, ptr %9, align 4, !tbaa !11
  br label %48

16:                                               ; preds = %4
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.psa_mac_operation_s, ptr %17, i32 0, i32 2
  %19 = load i8, ptr %18, align 1
  %20 = and i8 %19, 1
  %21 = zext i8 %20 to i32
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %24, label %23

23:                                               ; preds = %16
  store i32 -137, ptr %9, align 4, !tbaa !11
  br label %48

24:                                               ; preds = %16
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.psa_mac_operation_s, ptr %25, i32 0, i32 1
  %27 = load i8, ptr %26, align 4, !tbaa !15
  %28 = zext i8 %27 to i32
  %29 = icmp eq i32 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %24
  store i32 -137, ptr %9, align 4, !tbaa !11
  br label %48

31:                                               ; preds = %24
  %32 = load i64, ptr %7, align 8, !tbaa !9
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.psa_mac_operation_s, ptr %33, i32 0, i32 1
  %35 = load i8, ptr %34, align 4, !tbaa !15
  %36 = zext i8 %35 to i64
  %37 = icmp ult i64 %32, %36
  br i1 %37, label %38, label %39

38:                                               ; preds = %31
  store i32 -138, ptr %9, align 4, !tbaa !11
  br label %48

39:                                               ; preds = %31
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.psa_mac_operation_s, ptr %42, i32 0, i32 1
  %44 = load i8, ptr %43, align 4, !tbaa !15
  %45 = zext i8 %44 to i64
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  %47 = call i32 @psa_driver_wrapper_mac_sign_finish(ptr noundef %40, ptr noundef %41, i64 noundef %45, ptr noundef %46)
  store i32 %47, ptr %9, align 4, !tbaa !11
  br label %48

48:                                               ; preds = %39, %38, %30, %23, %15
  %49 = load i32, ptr %9, align 4, !tbaa !11
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %51, label %56

51:                                               ; preds = %48
  %52 = load i64, ptr %7, align 8, !tbaa !9
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  store i64 %52, ptr %53, align 8, !tbaa !9
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.psa_mac_operation_s, ptr %54, i32 0, i32 1
  store i8 0, ptr %55, align 4, !tbaa !15
  br label %56

56:                                               ; preds = %51, %48
  %57 = load i64, ptr %7, align 8, !tbaa !9
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.psa_mac_operation_s, ptr %58, i32 0, i32 1
  %60 = load i8, ptr %59, align 4, !tbaa !15
  %61 = zext i8 %60 to i64
  %62 = icmp ugt i64 %57, %61
  br i1 %62, label %63, label %76

63:                                               ; preds = %56
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.psa_mac_operation_s, ptr %65, i32 0, i32 1
  %67 = load i8, ptr %66, align 4, !tbaa !15
  %68 = zext i8 %67 to i64
  %69 = getelementptr inbounds i8, ptr %64, i64 %68
  %70 = load i64, ptr %7, align 8, !tbaa !9
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.psa_mac_operation_s, ptr %71, i32 0, i32 1
  %73 = load i8, ptr %72, align 4, !tbaa !15
  %74 = zext i8 %73 to i64
  %75 = sub i64 %70, %74
  call void @llvm.memset.p0.i64(ptr align 1 %69, i8 33, i64 %75, i1 false)
  br label %76

76:                                               ; preds = %63, %56
  %77 = load ptr, ptr %5, align 8, !tbaa !5
  %78 = call i32 @psa_mac_abort(ptr noundef %77)
  store i32 %78, ptr %10, align 4, !tbaa !11
  %79 = load i32, ptr %9, align 4, !tbaa !11
  %80 = icmp eq i32 %79, 0
  br i1 %80, label %81, label %83

81:                                               ; preds = %76
  %82 = load i32, ptr %10, align 4, !tbaa !11
  br label %85

83:                                               ; preds = %76
  %84 = load i32, ptr %9, align 4, !tbaa !11
  br label %85

85:                                               ; preds = %83, %81
  %86 = phi i32 [ %82, %81 ], [ %84, %83 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  ret i32 %86
}

declare i32 @psa_driver_wrapper_mac_sign_finish(ptr noundef, ptr noundef, i64 noundef, ptr noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 #3 = { "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 #4 = { 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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"psa_mac_operation_s", !12, i64 0, !7, i64 4, !12, i64 5, !7, i64 8}
!15 = !{!14, !7, i64 4}


; 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 nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @psa_mac_abort(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_mac_sign_finish(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) local_unnamed_addr #1 {
  %5 = load i32, ptr %0, align 8, !tbaa !5
  %6 = icmp eq i32 %5, 0
  br i1 %6, label %.thread, label %7

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %0, i64 5
  %9 = load i8, ptr %8, align 1
  %10 = and i8 %9, 1
  %.not = icmp eq i8 %10, 0
  br i1 %.not, label %.thread, label %11

11:                                               ; preds = %7
  %12 = getelementptr inbounds i8, ptr %0, i64 4
  %13 = load i8, ptr %12, align 4, !tbaa !10
  %14 = icmp eq i8 %13, 0
  br i1 %14, label %.thread, label %15

15:                                               ; preds = %11
  %16 = zext i8 %13 to i64
  %17 = icmp ugt i64 %16, %2
  br i1 %17, label %.thread, label %18

18:                                               ; preds = %15
  %19 = tail call i32 @psa_driver_wrapper_mac_sign_finish(ptr noundef nonnull %0, ptr noundef %1, i64 noundef %16, ptr noundef %3) #3
  %.not25 = icmp eq i32 %19, 0
  br i1 %.not25, label %._crit_edge, label %.thread

._crit_edge:                                      ; preds = %18
  %.pre = load i8, ptr %12, align 4, !tbaa !10
  %20 = zext i8 %.pre to i64
  br label %22

.thread:                                          ; preds = %15, %11, %7, %4, %18
  %.028 = phi i32 [ %19, %18 ], [ -138, %15 ], [ -137, %11 ], [ -137, %7 ], [ -137, %4 ]
  store i64 %2, ptr %3, align 8, !tbaa !11
  %21 = getelementptr inbounds i8, ptr %0, i64 4
  store i8 0, ptr %21, align 4, !tbaa !10
  br label %22

22:                                               ; preds = %._crit_edge, %.thread
  %23 = phi i64 [ 0, %.thread ], [ %20, %._crit_edge ]
  %.not2531 = phi i1 [ false, %.thread ], [ true, %._crit_edge ]
  %.029 = phi i32 [ %.028, %.thread ], [ 0, %._crit_edge ]
  %24 = icmp ult i64 %23, %2
  br i1 %24, label %25, label %28

25:                                               ; preds = %22
  %26 = getelementptr inbounds i8, ptr %1, i64 %23
  %27 = sub i64 %2, %23
  tail call void @llvm.memset.p0.i64(ptr align 1 %26, i8 33, i64 %27, i1 false)
  br label %28

28:                                               ; preds = %25, %22
  %29 = tail call i32 @psa_mac_abort(ptr noundef nonnull %0)
  %30 = select i1 %.not2531, i32 %29, i32 %.029
  ret i32 %30
}

declare i32 @psa_driver_wrapper_mac_sign_finish(ptr noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #1 = { 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 #2 = { "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 #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, !7, i64 0}
!6 = !{!"psa_mac_operation_s", !7, i64 0, !8, i64 4, !7, i64 5, !8, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !8, i64 4}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !8, i64 0}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass