; 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_aead_operation_s = type { i32, i32, i16, i64, i64, i8, %union.psa_driver_aead_context_t }
%union.psa_driver_aead_context_t = type { %struct.iotex_psa_aead_operation_t }
%struct.iotex_psa_aead_operation_t = type { i32, i16, i8, i8, %union.anon.2 }
%union.anon.2 = type { i32 }

; 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_aead_abort(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_aead_finish(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6) #2 {
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i64, align 8
  %14 = alloca ptr, align 8
  %15 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store ptr %1, ptr %9, align 8, !tbaa !5
  store i64 %2, ptr %10, align 8, !tbaa !9
  store ptr %3, ptr %11, align 8, !tbaa !5
  store ptr %4, ptr %12, align 8, !tbaa !5
  store i64 %5, ptr %13, align 8, !tbaa !9
  store ptr %6, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #4
  store i32 -151, ptr %15, align 4, !tbaa !11
  %16 = load ptr, ptr %11, align 8, !tbaa !5
  store i64 0, ptr %16, align 8, !tbaa !9
  %17 = load i64, ptr %13, align 8, !tbaa !9
  %18 = load ptr, ptr %14, align 8, !tbaa !5
  store i64 %17, ptr %18, align 8, !tbaa !9
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = call i32 @psa_aead_final_checks(ptr noundef %19)
  store i32 %20, ptr %15, align 4, !tbaa !11
  %21 = load i32, ptr %15, align 4, !tbaa !11
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %24

23:                                               ; preds = %7
  br label %42

24:                                               ; preds = %7
  %25 = load ptr, ptr %8, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.psa_aead_operation_s, ptr %25, i32 0, i32 5
  %27 = load i8, ptr %26, align 8
  %28 = lshr i8 %27, 4
  %29 = and i8 %28, 1
  %30 = zext i8 %29 to i32
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %33, label %32

32:                                               ; preds = %24
  store i32 -137, ptr %15, align 4, !tbaa !11
  br label %42

33:                                               ; preds = %24
  %34 = load ptr, ptr %8, align 8, !tbaa !5
  %35 = load ptr, ptr %9, align 8, !tbaa !5
  %36 = load i64, ptr %10, align 8, !tbaa !9
  %37 = load ptr, ptr %11, align 8, !tbaa !5
  %38 = load ptr, ptr %12, align 8, !tbaa !5
  %39 = load i64, ptr %13, align 8, !tbaa !9
  %40 = load ptr, ptr %14, align 8, !tbaa !5
  %41 = call i32 @psa_driver_wrapper_aead_finish(ptr noundef %34, ptr noundef %35, i64 noundef %36, ptr noundef %37, ptr noundef %38, i64 noundef %39, ptr noundef %40)
  store i32 %41, ptr %15, align 4, !tbaa !11
  br label %42

42:                                               ; preds = %33, %32, %23
  %43 = load ptr, ptr %12, align 8, !tbaa !5
  %44 = icmp ne ptr %43, null
  br i1 %44, label %45, label %67

45:                                               ; preds = %42
  %46 = load i32, ptr %15, align 4, !tbaa !11
  %47 = icmp ne i32 %46, 0
  br i1 %47, label %48, label %51

48:                                               ; preds = %45
  %49 = load ptr, ptr %12, align 8, !tbaa !5
  %50 = load i64, ptr %13, align 8, !tbaa !9
  call void @llvm.memset.p0.i64(ptr align 1 %49, i8 33, i64 %50, i1 false)
  br label %66

51:                                               ; preds = %45
  %52 = load ptr, ptr %14, align 8, !tbaa !5
  %53 = load i64, ptr %52, align 8, !tbaa !9
  %54 = load i64, ptr %13, align 8, !tbaa !9
  %55 = icmp ult i64 %53, %54
  br i1 %55, label %56, label %65

56:                                               ; preds = %51
  %57 = load ptr, ptr %12, align 8, !tbaa !5
  %58 = load ptr, ptr %14, align 8, !tbaa !5
  %59 = load i64, ptr %58, align 8, !tbaa !9
  %60 = getelementptr inbounds i8, ptr %57, i64 %59
  %61 = load i64, ptr %13, align 8, !tbaa !9
  %62 = load ptr, ptr %14, align 8, !tbaa !5
  %63 = load i64, ptr %62, align 8, !tbaa !9
  %64 = sub i64 %61, %63
  call void @llvm.memset.p0.i64(ptr align 1 %60, i8 33, i64 %64, i1 false)
  br label %65

65:                                               ; preds = %56, %51
  br label %66

66:                                               ; preds = %65, %48
  br label %67

67:                                               ; preds = %66, %42
  %68 = load ptr, ptr %8, align 8, !tbaa !5
  %69 = call i32 @psa_aead_abort(ptr noundef %68)
  %70 = load i32, ptr %15, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  ret i32 %70
}

; Function Attrs: nounwind uwtable
declare hidden i32 @psa_aead_final_checks(ptr noundef) #2

declare i32 @psa_driver_wrapper_aead_finish(ptr noundef, ptr noundef, i64 noundef, 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}


; 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_aead_abort(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_aead_finish(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6) local_unnamed_addr #1 {
  store i64 0, ptr %3, align 8, !tbaa !5
  store i64 %5, ptr %6, align 8, !tbaa !5
  %8 = tail call i32 @psa_aead_final_checks(ptr noundef %0)
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %9, label %15

9:                                                ; preds = %7
  %10 = getelementptr inbounds i8, ptr %0, i64 32
  %11 = load i8, ptr %10, align 8
  %12 = and i8 %11, 16
  %.not28 = icmp eq i8 %12, 0
  br i1 %.not28, label %.thread, label %13

13:                                               ; preds = %9
  %14 = tail call i32 @psa_driver_wrapper_aead_finish(ptr noundef nonnull %0, ptr noundef %1, i64 noundef %2, ptr noundef nonnull %3, ptr noundef %4, i64 noundef %5, ptr noundef nonnull %6) #3
  br label %15

15:                                               ; preds = %7, %13
  %.0 = phi i32 [ %8, %7 ], [ %14, %13 ]
  %.not29 = icmp eq ptr %4, null
  br i1 %.not29, label %23, label %16

.thread:                                          ; preds = %9
  %.not2932 = icmp eq ptr %4, null
  br i1 %.not2932, label %23, label %.thread35

16:                                               ; preds = %15
  %.not30 = icmp eq i32 %.0, 0
  br i1 %.not30, label %17, label %.thread35

.thread35:                                        ; preds = %.thread, %16
  %.03338 = phi i32 [ %.0, %16 ], [ -137, %.thread ]
  tail call void @llvm.memset.p0.i64(ptr nonnull align 1 %4, i8 33, i64 %5, i1 false)
  br label %23

17:                                               ; preds = %16
  %18 = load i64, ptr %6, align 8, !tbaa !5
  %19 = icmp ult i64 %18, %5
  br i1 %19, label %20, label %23

20:                                               ; preds = %17
  %21 = getelementptr inbounds i8, ptr %4, i64 %18
  %22 = sub i64 %5, %18
  tail call void @llvm.memset.p0.i64(ptr nonnull align 1 %21, i8 33, i64 %22, i1 false)
  br label %23

23:                                               ; preds = %.thread, %.thread35, %20, %17, %15
  %.034 = phi i32 [ -137, %.thread ], [ %.03338, %.thread35 ], [ 0, %20 ], [ 0, %17 ], [ %.0, %15 ]
  %24 = tail call i32 @psa_aead_abort(ptr noundef %0)
  ret i32 %.034
}

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

declare i32 @psa_driver_wrapper_aead_finish(ptr noundef, ptr noundef, i64 noundef, 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, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


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