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

14:                                               ; preds = %2
  store i32 -137, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %69

15:                                               ; preds = %2
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = and i32 %16, 2130706432
  %18 = icmp eq i32 %17, 150994944
  br i1 %18, label %19, label %25

19:                                               ; preds = %15
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = and i32 %20, -33488897
  %22 = or i32 %21, 134217728
  %23 = icmp eq i32 %22, 134217728
  br i1 %23, label %24, label %25

24:                                               ; preds = %19
  store i32 -135, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %69

25:                                               ; preds = %19, %15
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = and i32 %26, 2130706432
  %28 = icmp eq i32 %27, 150994944
  br i1 %28, label %29, label %49

29:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %30 = load i32, ptr %5, align 4, !tbaa !9
  %31 = and i32 %30, -33488897
  %32 = or i32 %31, 134217728
  store i32 %32, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %33 = load i32, ptr %5, align 4, !tbaa !9
  %34 = and i32 %33, -65536
  %35 = or i32 %34, 150994944
  store i32 %35, ptr %9, align 4, !tbaa !9
  %36 = load i32, ptr %9, align 4, !tbaa !9
  %37 = call i32 @psa_key_agreement_try_support(i32 noundef %36)
  store i32 %37, ptr %6, align 4, !tbaa !9
  %38 = load i32, ptr %6, align 4, !tbaa !9
  %39 = icmp ne i32 %38, 0
  br i1 %39, label %40, label %42

40:                                               ; preds = %29
  %41 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %41, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %46

42:                                               ; preds = %29
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = load i32, ptr %8, align 4, !tbaa !9
  %45 = call i32 @psa_key_derivation_setup_kdf(ptr noundef %43, i32 noundef %44)
  store i32 %45, ptr %6, align 4, !tbaa !9
  store i32 0, ptr %7, align 4
  br label %46

46:                                               ; preds = %42, %40
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %47 = load i32, ptr %7, align 4
  switch i32 %47, label %69 [
    i32 0, label %48
  ]

48:                                               ; preds = %46
  br label %59

49:                                               ; preds = %25
  %50 = load i32, ptr %5, align 4, !tbaa !9
  %51 = and i32 %50, 2130706432
  %52 = icmp eq i32 %51, 134217728
  br i1 %52, label %53, label %57

53:                                               ; preds = %49
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = load i32, ptr %5, align 4, !tbaa !9
  %56 = call i32 @psa_key_derivation_setup_kdf(ptr noundef %54, i32 noundef %55)
  store i32 %56, ptr %6, align 4, !tbaa !9
  br label %58

57:                                               ; preds = %49
  store i32 -135, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %69

58:                                               ; preds = %53
  br label %59

59:                                               ; preds = %58, %48
  br label %60

60:                                               ; preds = %59
  %61 = load i32, ptr %6, align 4, !tbaa !9
  %62 = icmp eq i32 %61, 0
  br i1 %62, label %63, label %67

63:                                               ; preds = %60
  %64 = load i32, ptr %5, align 4, !tbaa !9
  %65 = load ptr, ptr %4, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.psa_key_derivation_s, ptr %65, i32 0, i32 0
  store i32 %64, ptr %66, align 8, !tbaa !11
  br label %67

67:                                               ; preds = %63, %60
  %68 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %68, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %69

69:                                               ; preds = %67, %57, %46, %24, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %70 = load i32, ptr %3, align 4
  ret i32 %70
}

; Function Attrs: nounwind uwtable
declare hidden i32 @psa_key_agreement_try_support(i32 noundef) #1

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

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 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"psa_key_derivation_s", !10, i64 0, !10, i64 4, !13, i64 8, !7, i64 16}
!13 = !{!"long", !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: nounwind uwtable
define dso_local i32 @psa_key_derivation_setup(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr %0, align 8, !tbaa !5
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %4, label %15

4:                                                ; preds = %2
  %5 = and i32 %1, -2013200385
  %6 = and i32 %1, -16711681
  %or.cond = icmp eq i32 %6, 150994944
  br i1 %or.cond, label %15, label %7

7:                                                ; preds = %4
  %8 = and i32 %1, 2130706432
  switch i32 %8, label %15 [
    i32 150994944, label %9
    i32 134217728, label %.thread
  ]

9:                                                ; preds = %7
  %10 = and i32 %1, -1979777024
  %11 = tail call i32 @psa_key_agreement_try_support(i32 noundef %10)
  %.not23 = icmp eq i32 %11, 0
  br i1 %.not23, label %.thread, label %15

.thread:                                          ; preds = %7, %9
  %.sink = phi i32 [ %5, %9 ], [ %1, %7 ]
  %12 = tail call i32 @psa_key_derivation_setup_kdf(ptr noundef nonnull %0, i32 noundef %.sink)
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %.thread
  store i32 %1, ptr %0, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %7, %9, %.thread, %14, %4, %2
  %.1 = phi i32 [ -137, %2 ], [ -135, %4 ], [ 0, %14 ], [ %12, %.thread ], [ %11, %9 ], [ -135, %7 ]
  ret i32 %.1
}

; Function Attrs: nounwind uwtable
declare hidden i32 @psa_key_agreement_try_support(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden i32 @psa_key_derivation_setup_kdf(ptr noundef, i32 noundef) local_unnamed_addr #0

attributes #0 = { 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}


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