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

; 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.iotex_psa_cipher_operation_t = type { i32, i8, i8, %union.anon }
%union.anon = type { %struct.iotex_cipher_context_t }
%struct.iotex_cipher_context_t = type { ptr, i32, i32, ptr, ptr, [16 x i8], i64, [16 x i8], i64, ptr, 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: nounwind uwtable
declare dso_local i32 @iotex_psa_cipher_encrypt_setup(ptr noundef, ptr noundef, ptr noundef, i64 noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @iotex_psa_cipher_set_iv(ptr noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @iotex_psa_cipher_update(ptr noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @iotex_psa_cipher_finish(ptr noundef, ptr noundef, i64 noundef, ptr noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @iotex_psa_cipher_encrypt(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6, i64 noundef %7, ptr noundef %8, i64 noundef %9, ptr noundef %10) #1 {
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i64, align 8
  %15 = alloca i32, align 4
  %16 = alloca ptr, align 8
  %17 = alloca i64, align 8
  %18 = alloca ptr, align 8
  %19 = alloca i64, align 8
  %20 = alloca ptr, align 8
  %21 = alloca i64, align 8
  %22 = alloca ptr, align 8
  %23 = alloca i32, align 4
  %24 = alloca %struct.iotex_psa_cipher_operation_t, align 8
  %25 = alloca i64, align 8
  %26 = alloca i64, align 8
  store ptr %0, ptr %12, align 8, !tbaa !5
  store ptr %1, ptr %13, align 8, !tbaa !5
  store i64 %2, ptr %14, align 8, !tbaa !9
  store i32 %3, ptr %15, align 4, !tbaa !11
  store ptr %4, ptr %16, align 8, !tbaa !5
  store i64 %5, ptr %17, align 8, !tbaa !9
  store ptr %6, ptr %18, align 8, !tbaa !5
  store i64 %7, ptr %19, align 8, !tbaa !9
  store ptr %8, ptr %20, align 8, !tbaa !5
  store i64 %9, ptr %21, align 8, !tbaa !9
  store ptr %10, ptr %22, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %23) #3
  store i32 -151, ptr %23, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 104, ptr %24) #3
  call void @llvm.memset.p0.i64(ptr align 8 %24, i8 0, i64 104, i1 false)
  %27 = getelementptr inbounds { i32, i8, i8, { i32, [92 x i8] } }, ptr %24, i32 0, i32 3
  call void @llvm.lifetime.start.p0(i64 8, ptr %25) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %26) #3
  %28 = load ptr, ptr %12, align 8, !tbaa !5
  %29 = load ptr, ptr %13, align 8, !tbaa !5
  %30 = load i64, ptr %14, align 8, !tbaa !9
  %31 = load i32, ptr %15, align 4, !tbaa !11
  %32 = call i32 @iotex_psa_cipher_encrypt_setup(ptr noundef %24, ptr noundef %28, ptr noundef %29, i64 noundef %30, i32 noundef %31)
  store i32 %32, ptr %23, align 4, !tbaa !11
  %33 = load i32, ptr %23, align 4, !tbaa !11
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %35, label %36

35:                                               ; preds = %11
  br label %72

36:                                               ; preds = %11
  %37 = load i64, ptr %17, align 8, !tbaa !9
  %38 = icmp ugt i64 %37, 0
  br i1 %38, label %39, label %47

39:                                               ; preds = %36
  %40 = load ptr, ptr %16, align 8, !tbaa !5
  %41 = load i64, ptr %17, align 8, !tbaa !9
  %42 = call i32 @iotex_psa_cipher_set_iv(ptr noundef %24, ptr noundef %40, i64 noundef %41)
  store i32 %42, ptr %23, align 4, !tbaa !11
  %43 = load i32, ptr %23, align 4, !tbaa !11
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %46

45:                                               ; preds = %39
  br label %72

46:                                               ; preds = %39
  br label %47

47:                                               ; preds = %46, %36
  %48 = load ptr, ptr %18, align 8, !tbaa !5
  %49 = load i64, ptr %19, align 8, !tbaa !9
  %50 = load ptr, ptr %20, align 8, !tbaa !5
  %51 = load i64, ptr %21, align 8, !tbaa !9
  %52 = call i32 @iotex_psa_cipher_update(ptr noundef %24, ptr noundef %48, i64 noundef %49, ptr noundef %50, i64 noundef %51, ptr noundef %25)
  store i32 %52, ptr %23, align 4, !tbaa !11
  %53 = load i32, ptr %23, align 4, !tbaa !11
  %54 = icmp ne i32 %53, 0
  br i1 %54, label %55, label %56

55:                                               ; preds = %47
  br label %72

56:                                               ; preds = %47
  %57 = load ptr, ptr %20, align 8, !tbaa !5
  %58 = load i64, ptr %25, align 8, !tbaa !9
  %59 = getelementptr inbounds i8, ptr %57, i64 %58
  %60 = load i64, ptr %21, align 8, !tbaa !9
  %61 = load i64, ptr %25, align 8, !tbaa !9
  %62 = sub i64 %60, %61
  %63 = call i32 @iotex_psa_cipher_finish(ptr noundef %24, ptr noundef %59, i64 noundef %62, ptr noundef %26)
  store i32 %63, ptr %23, align 4, !tbaa !11
  %64 = load i32, ptr %23, align 4, !tbaa !11
  %65 = icmp ne i32 %64, 0
  br i1 %65, label %66, label %67

66:                                               ; preds = %56
  br label %72

67:                                               ; preds = %56
  %68 = load i64, ptr %25, align 8, !tbaa !9
  %69 = load i64, ptr %26, align 8, !tbaa !9
  %70 = add i64 %68, %69
  %71 = load ptr, ptr %22, align 8, !tbaa !5
  store i64 %70, ptr %71, align 8, !tbaa !9
  br label %72

72:                                               ; preds = %67, %66, %55, %45, %35
  %73 = load i32, ptr %23, align 4, !tbaa !11
  %74 = icmp eq i32 %73, 0
  br i1 %74, label %75, label %77

75:                                               ; preds = %72
  %76 = call i32 @iotex_psa_cipher_abort(ptr noundef %24)
  store i32 %76, ptr %23, align 4, !tbaa !11
  br label %79

77:                                               ; preds = %72
  %78 = call i32 @iotex_psa_cipher_abort(ptr noundef %24)
  br label %79

79:                                               ; preds = %77, %75
  %80 = load i32, ptr %23, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %26) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %25) #3
  call void @llvm.lifetime.end.p0(i64 104, ptr %24) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %23) #3
  ret i32 %80
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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, !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"

%struct.iotex_psa_cipher_operation_t = type { i32, i8, i8, %union.anon }
%union.anon = type { %struct.iotex_cipher_context_t }
%struct.iotex_cipher_context_t = type { ptr, i32, i32, ptr, ptr, [16 x i8], i64, [16 x i8], i64, ptr, ptr }

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

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

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

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @iotex_psa_cipher_encrypt(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6, i64 noundef %7, ptr noundef %8, i64 noundef %9, ptr nocapture noundef writeonly %10) local_unnamed_addr #1 {
  %12 = alloca %struct.iotex_psa_cipher_operation_t, align 8
  %13 = alloca i64, align 8
  %14 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 104, ptr nonnull %12) #3
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(104) %12, i8 0, i64 104, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %13) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %14) #3
  %15 = call i32 @iotex_psa_cipher_encrypt_setup(ptr noundef nonnull %12, ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3)
  %.not = icmp eq i32 %15, 0
  br i1 %.not, label %16, label %31

16:                                               ; preds = %11
  %.not25 = icmp eq i64 %5, 0
  br i1 %.not25, label %19, label %17

17:                                               ; preds = %16
  %18 = call i32 @iotex_psa_cipher_set_iv(ptr noundef nonnull %12, ptr noundef %4, i64 noundef %5)
  %.not26 = icmp eq i32 %18, 0
  br i1 %.not26, label %19, label %31

19:                                               ; preds = %17, %16
  %20 = call i32 @iotex_psa_cipher_update(ptr noundef nonnull %12, ptr noundef %6, i64 noundef %7, ptr noundef %8, i64 noundef %9, ptr noundef nonnull %13)
  %.not27 = icmp eq i32 %20, 0
  br i1 %.not27, label %21, label %31

21:                                               ; preds = %19
  %22 = load i64, ptr %13, align 8, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %8, i64 %22
  %24 = sub i64 %9, %22
  %25 = call i32 @iotex_psa_cipher_finish(ptr noundef nonnull %12, ptr noundef %23, i64 noundef %24, ptr noundef nonnull %14)
  %.not28 = icmp eq i32 %25, 0
  br i1 %.not28, label %26, label %31

26:                                               ; preds = %21
  %27 = load i64, ptr %13, align 8, !tbaa !5
  %28 = load i64, ptr %14, align 8, !tbaa !5
  %29 = add i64 %28, %27
  store i64 %29, ptr %10, align 8, !tbaa !5
  %30 = call i32 @iotex_psa_cipher_abort(ptr noundef nonnull %12)
  br label %33

31:                                               ; preds = %11, %17, %19, %21
  %.0.ph = phi i32 [ %25, %21 ], [ %20, %19 ], [ %18, %17 ], [ %15, %11 ]
  %32 = call i32 @iotex_psa_cipher_abort(ptr noundef nonnull %12)
  br label %33

33:                                               ; preds = %31, %26
  %.1 = phi i32 [ %30, %26 ], [ %.0.ph, %31 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %13) #3
  call void @llvm.lifetime.end.p0(i64 104, ptr nonnull %12) #3
  ret i32 %.1
}

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass