; 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_key_derivation_s = type { i32, i8, i64, %union.anon.3 }
%union.anon.3 = type { %struct.psa_hkdf_key_derivation_t }
%struct.psa_hkdf_key_derivation_t = type { ptr, i64, i8, i8, i8, [32 x i8], [32 x i8], %struct.psa_mac_operation_s }
%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_key_derivation_abort(ptr noundef) #2

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

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

18:                                               ; preds = %3
  store i32 -137, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %96

19:                                               ; preds = %3
  %20 = load i64, ptr %7, align 8, !tbaa !9
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.psa_key_derivation_s, ptr %21, i32 0, i32 2
  %23 = load i64, ptr %22, align 8, !tbaa !15
  %24 = icmp ugt i64 %20, %23
  br i1 %24, label %25, label %28

25:                                               ; preds = %19
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.psa_key_derivation_s, ptr %26, i32 0, i32 2
  store i64 0, ptr %27, align 8, !tbaa !15
  store i32 -143, ptr %8, align 4, !tbaa !11
  br label %80

28:                                               ; preds = %19
  %29 = load i64, ptr %7, align 8, !tbaa !9
  %30 = icmp eq i64 %29, 0
  br i1 %30, label %31, label %37

31:                                               ; preds = %28
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.psa_key_derivation_s, ptr %32, i32 0, i32 2
  %34 = load i64, ptr %33, align 8, !tbaa !15
  %35 = icmp eq i64 %34, 0
  br i1 %35, label %36, label %37

36:                                               ; preds = %31
  store i32 -143, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %96

37:                                               ; preds = %31, %28
  %38 = load i64, ptr %7, align 8, !tbaa !9
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.psa_key_derivation_s, ptr %39, i32 0, i32 2
  %41 = load i64, ptr %40, align 8, !tbaa !15
  %42 = sub i64 %41, %38
  store i64 %42, ptr %40, align 8, !tbaa !15
  %43 = load i32, ptr %9, align 4, !tbaa !11
  %44 = and i32 %43, -256
  %45 = icmp eq i32 %44, 134217984
  br i1 %45, label %54, label %46

46:                                               ; preds = %37
  %47 = load i32, ptr %9, align 4, !tbaa !11
  %48 = and i32 %47, -256
  %49 = icmp eq i32 %48, 134218752
  br i1 %49, label %54, label %50

50:                                               ; preds = %46
  %51 = load i32, ptr %9, align 4, !tbaa !11
  %52 = and i32 %51, -256
  %53 = icmp eq i32 %52, 134219008
  br i1 %53, label %54, label %61

54:                                               ; preds = %50, %46, %37
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.psa_key_derivation_s, ptr %55, i32 0, i32 3
  %57 = load i32, ptr %9, align 4, !tbaa !11
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = load i64, ptr %7, align 8, !tbaa !9
  %60 = call i32 @psa_key_derivation_hkdf_read(ptr noundef %56, i32 noundef %57, ptr noundef %58, i64 noundef %59)
  store i32 %60, ptr %8, align 4, !tbaa !11
  br label %79

61:                                               ; preds = %50
  %62 = load i32, ptr %9, align 4, !tbaa !11
  %63 = and i32 %62, -256
  %64 = icmp eq i32 %63, 134218240
  br i1 %64, label %69, label %65

65:                                               ; preds = %61
  %66 = load i32, ptr %9, align 4, !tbaa !11
  %67 = and i32 %66, -256
  %68 = icmp eq i32 %67, 134218496
  br i1 %68, label %69, label %76

69:                                               ; preds = %65, %61
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.psa_key_derivation_s, ptr %70, i32 0, i32 3
  %72 = load i32, ptr %9, align 4, !tbaa !11
  %73 = load ptr, ptr %6, align 8, !tbaa !5
  %74 = load i64, ptr %7, align 8, !tbaa !9
  %75 = call i32 @psa_key_derivation_tls12_prf_read(ptr noundef %71, i32 noundef %72, ptr noundef %73, i64 noundef %74)
  store i32 %75, ptr %8, align 4, !tbaa !11
  br label %78

76:                                               ; preds = %65
  %77 = load i32, ptr %9, align 4, !tbaa !11
  store i32 -137, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %96

78:                                               ; preds = %69
  br label %79

79:                                               ; preds = %78, %54
  br label %80

80:                                               ; preds = %79, %25
  %81 = load i32, ptr %8, align 4, !tbaa !11
  %82 = icmp ne i32 %81, 0
  br i1 %82, label %83, label %94

83:                                               ; preds = %80
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.psa_key_derivation_s, ptr %84, i32 0, i32 0
  %86 = load i32, ptr %85, align 8, !tbaa !13
  store i32 %86, ptr %11, align 4, !tbaa !11
  %87 = load ptr, ptr %5, align 8, !tbaa !5
  %88 = call i32 @psa_key_derivation_abort(ptr noundef %87)
  %89 = load i32, ptr %11, align 4, !tbaa !11
  %90 = load ptr, ptr %5, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.psa_key_derivation_s, ptr %90, i32 0, i32 0
  store i32 %89, ptr %91, align 8, !tbaa !13
  %92 = load ptr, ptr %6, align 8, !tbaa !5
  %93 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memset.p0.i64(ptr align 1 %92, i8 33, i64 %93, i1 false)
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %94

94:                                               ; preds = %83, %80
  %95 = load i32, ptr %8, align 4, !tbaa !11
  store i32 %95, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %96

96:                                               ; preds = %94, %76, %36, %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %97 = load i32, ptr %4, align 4
  ret i32 %97
}

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

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

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 = { 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_key_derivation_s", !12, i64 0, !12, i64 4, !10, i64 8, !7, i64 16}
!15 = !{!14, !10, 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"

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_key_derivation_output_bytes(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #1 {
  %4 = tail call i32 @psa_key_derivation_get_kdf_alg(ptr noundef %0)
  %5 = load i32, ptr %0, align 8, !tbaa !5
  %6 = icmp eq i32 %5, 0
  br i1 %6, label %27, label %7

7:                                                ; preds = %3
  %8 = getelementptr inbounds i8, ptr %0, i64 8
  %9 = load i64, ptr %8, align 8, !tbaa !11
  %10 = icmp ult i64 %9, %2
  br i1 %10, label %.thread, label %11

.thread:                                          ; preds = %7
  store i64 0, ptr %8, align 8, !tbaa !11
  br label %24

11:                                               ; preds = %7
  %12 = or i64 %9, %2
  %or.cond = icmp eq i64 %12, 0
  br i1 %or.cond, label %27, label %13

13:                                               ; preds = %11
  %14 = sub i64 %9, %2
  store i64 %14, ptr %8, align 8, !tbaa !11
  %15 = add i32 %4, -134217984
  %16 = lshr i32 %15, 8
  switch i32 %16, label %27 [
    i32 0, label %17
    i32 3, label %17
    i32 4, label %17
    i32 1, label %20
    i32 2, label %20
  ]

17:                                               ; preds = %13, %13, %13
  %18 = getelementptr inbounds i8, ptr %0, i64 16
  %19 = tail call i32 @psa_key_derivation_hkdf_read(ptr noundef nonnull %18, i32 noundef %4, ptr noundef %1, i64 noundef %2)
  br label %23

20:                                               ; preds = %13, %13
  %21 = getelementptr inbounds i8, ptr %0, i64 16
  %22 = tail call i32 @psa_key_derivation_tls12_prf_read(ptr noundef nonnull %21, i32 noundef %4, ptr noundef %1, i64 noundef %2)
  br label %23

23:                                               ; preds = %17, %20
  %.031 = phi i32 [ %19, %17 ], [ %22, %20 ]
  %.not = icmp eq i32 %.031, 0
  br i1 %.not, label %27, label %._crit_edge

._crit_edge:                                      ; preds = %23
  %.pre = load i32, ptr %0, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %._crit_edge, %.thread
  %25 = phi i32 [ %5, %.thread ], [ %.pre, %._crit_edge ]
  %.03137 = phi i32 [ -143, %.thread ], [ %.031, %._crit_edge ]
  %26 = tail call i32 @psa_key_derivation_abort(ptr noundef nonnull %0)
  store i32 %25, ptr %0, align 8, !tbaa !5
  tail call void @llvm.memset.p0.i64(ptr align 1 %1, i8 33, i64 %2, i1 false)
  br label %27

27:                                               ; preds = %23, %24, %13, %11, %3
  %.0 = phi i32 [ -137, %3 ], [ -143, %11 ], [ -137, %13 ], [ %.03137, %24 ], [ 0, %23 ]
  ret i32 %.0
}

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

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

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" }

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


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