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

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

31:                                               ; preds = %9
  br label %82

32:                                               ; preds = %9
  %33 = getelementptr inbounds %struct.iotex_psa_cipher_operation_t, ptr %20, i32 0, i32 1
  %34 = load i8, ptr %33, align 4, !tbaa !13
  %35 = zext i8 %34 to i32
  %36 = icmp sgt i32 %35, 0
  br i1 %36, label %37, label %47

37:                                               ; preds = %32
  %38 = load ptr, ptr %14, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.iotex_psa_cipher_operation_t, ptr %20, i32 0, i32 1
  %40 = load i8, ptr %39, align 4, !tbaa !13
  %41 = zext i8 %40 to i64
  %42 = call i32 @iotex_psa_cipher_set_iv(ptr noundef %20, ptr noundef %38, i64 noundef %41)
  store i32 %42, ptr %19, align 4, !tbaa !11
  %43 = load i32, ptr %19, align 4, !tbaa !11
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %46

45:                                               ; preds = %37
  br label %82

46:                                               ; preds = %37
  br label %47

47:                                               ; preds = %46, %32
  %48 = load ptr, ptr %14, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.iotex_psa_cipher_operation_t, ptr %20, i32 0, i32 1
  %50 = load i8, ptr %49, align 4, !tbaa !13
  %51 = zext i8 %50 to i32
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds i8, ptr %48, i64 %52
  %54 = load i64, ptr %15, align 8, !tbaa !9
  %55 = getelementptr inbounds %struct.iotex_psa_cipher_operation_t, ptr %20, i32 0, i32 1
  %56 = load i8, ptr %55, align 4, !tbaa !13
  %57 = zext i8 %56 to i64
  %58 = sub i64 %54, %57
  %59 = load ptr, ptr %16, align 8, !tbaa !5
  %60 = load i64, ptr %17, align 8, !tbaa !9
  %61 = call i32 @iotex_psa_cipher_update(ptr noundef %20, ptr noundef %53, i64 noundef %58, ptr noundef %59, i64 noundef %60, ptr noundef %21)
  store i32 %61, ptr %19, align 4, !tbaa !11
  %62 = load i32, ptr %19, align 4, !tbaa !11
  %63 = icmp ne i32 %62, 0
  br i1 %63, label %64, label %65

64:                                               ; preds = %47
  br label %82

65:                                               ; preds = %47
  %66 = load i64, ptr %21, align 8, !tbaa !9
  store i64 %66, ptr %22, align 8, !tbaa !9
  %67 = load ptr, ptr %16, align 8, !tbaa !5
  %68 = load i64, ptr %22, align 8, !tbaa !9
  %69 = getelementptr inbounds i8, ptr %67, i64 %68
  %70 = load i64, ptr %17, align 8, !tbaa !9
  %71 = load i64, ptr %22, align 8, !tbaa !9
  %72 = sub i64 %70, %71
  %73 = call i32 @iotex_psa_cipher_finish(ptr noundef %20, ptr noundef %69, i64 noundef %72, ptr noundef %21)
  store i32 %73, ptr %19, align 4, !tbaa !11
  %74 = load i32, ptr %19, align 4, !tbaa !11
  %75 = icmp ne i32 %74, 0
  br i1 %75, label %76, label %77

76:                                               ; preds = %65
  br label %82

77:                                               ; preds = %65
  %78 = load i64, ptr %22, align 8, !tbaa !9
  %79 = load i64, ptr %21, align 8, !tbaa !9
  %80 = add i64 %78, %79
  %81 = load ptr, ptr %18, align 8, !tbaa !5
  store i64 %80, ptr %81, align 8, !tbaa !9
  br label %82

82:                                               ; preds = %77, %76, %64, %45, %31
  %83 = load i32, ptr %19, align 4, !tbaa !11
  %84 = icmp eq i32 %83, 0
  br i1 %84, label %85, label %87

85:                                               ; preds = %82
  %86 = call i32 @iotex_psa_cipher_abort(ptr noundef %20)
  store i32 %86, ptr %19, align 4, !tbaa !11
  br label %89

87:                                               ; preds = %82
  %88 = call i32 @iotex_psa_cipher_abort(ptr noundef %20)
  br label %89

89:                                               ; preds = %87, %85
  %90 = load i32, ptr %19, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %22) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %21) #3
  call void @llvm.lifetime.end.p0(i64 104, ptr %20) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %19) #3
  ret i32 %90
}

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}
!13 = !{!14, !7, i64 4}
!14 = !{!"", !12, i64 0, !7, i64 4, !7, i64 5, !7, i64 8}


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

; Function Attrs: nounwind uwtable
define dso_local i32 @iotex_psa_cipher_decrypt(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 nocapture noundef writeonly %8) local_unnamed_addr #1 {
  %10 = alloca %struct.iotex_psa_cipher_operation_t, align 8
  %11 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 104, ptr nonnull %10) #3
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(104) %10, i8 0, i64 104, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %11) #3
  %12 = call i32 @iotex_psa_cipher_decrypt_setup(ptr noundef nonnull %10, ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3)
  %.not = icmp eq i32 %12, 0
  br i1 %.not, label %13, label %34

13:                                               ; preds = %9
  %14 = getelementptr inbounds i8, ptr %10, i64 4
  %15 = load i8, ptr %14, align 4, !tbaa !5
  %.not25 = icmp eq i8 %15, 0
  br i1 %.not25, label %20, label %16

16:                                               ; preds = %13
  %17 = zext i8 %15 to i64
  %18 = call i32 @iotex_psa_cipher_set_iv(ptr noundef nonnull %10, ptr noundef %4, i64 noundef %17)
  %.not26 = icmp eq i32 %18, 0
  br i1 %.not26, label %._crit_edge, label %34

._crit_edge:                                      ; preds = %16
  %.pre = load i8, ptr %14, align 4, !tbaa !5
  %19 = zext i8 %.pre to i64
  br label %20

20:                                               ; preds = %._crit_edge, %13
  %21 = phi i64 [ %19, %._crit_edge ], [ 0, %13 ]
  %22 = getelementptr inbounds i8, ptr %4, i64 %21
  %23 = sub i64 %5, %21
  %24 = call i32 @iotex_psa_cipher_update(ptr noundef nonnull %10, ptr noundef %22, i64 noundef %23, ptr noundef %6, i64 noundef %7, ptr noundef nonnull %11)
  %.not27 = icmp eq i32 %24, 0
  br i1 %.not27, label %25, label %34

25:                                               ; preds = %20
  %26 = load i64, ptr %11, align 8, !tbaa !10
  %27 = getelementptr inbounds i8, ptr %6, i64 %26
  %28 = sub i64 %7, %26
  %29 = call i32 @iotex_psa_cipher_finish(ptr noundef nonnull %10, ptr noundef %27, i64 noundef %28, ptr noundef nonnull %11)
  %.not28 = icmp eq i32 %29, 0
  br i1 %.not28, label %30, label %34

30:                                               ; preds = %25
  %31 = load i64, ptr %11, align 8, !tbaa !10
  %32 = add i64 %31, %26
  store i64 %32, ptr %8, align 8, !tbaa !10
  %33 = call i32 @iotex_psa_cipher_abort(ptr noundef nonnull %10)
  br label %36

34:                                               ; preds = %9, %16, %20, %25
  %.0.ph = phi i32 [ %29, %25 ], [ %24, %20 ], [ %18, %16 ], [ %12, %9 ]
  %35 = call i32 @iotex_psa_cipher_abort(ptr noundef nonnull %10)
  br label %36

36:                                               ; preds = %34, %30
  %.1 = phi i32 [ %33, %30 ], [ %.0.ph, %34 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %11) #3
  call void @llvm.lifetime.end.p0(i64 104, ptr nonnull %10) #3
  ret i32 %.1
}

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, !8, i64 4}
!6 = !{!"", !7, i64 0, !8, i64 4, !8, i64 5, !8, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !8, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass