; 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_global_data_t = type { i8, %struct.iotex_psa_external_random_context_t }
%struct.iotex_psa_external_random_context_t = type { [2 x i64] }

@global_data = external hidden global %struct.psa_global_data_t, align 8

; 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 void @iotex_psa_crypto_free() #1

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_crypto_init() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  %4 = load i8, ptr @global_data, align 8
  %5 = and i8 %4, 1
  %6 = zext i8 %5 to i32
  %7 = icmp ne i32 %6, 0
  br i1 %7, label %8, label %9

8:                                                ; preds = %0
  store i32 0, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %40

9:                                                ; preds = %0
  call void @iotex_psa_random_init(ptr noundef getelementptr inbounds (%struct.psa_global_data_t, ptr @global_data, i32 0, i32 1))
  %10 = load i8, ptr @global_data, align 8
  %11 = and i8 %10, -7
  %12 = or i8 %11, 2
  store i8 %12, ptr @global_data, align 8
  %13 = call i32 @iotex_psa_random_seed(ptr noundef getelementptr inbounds (%struct.psa_global_data_t, ptr @global_data, i32 0, i32 1))
  store i32 %13, ptr %2, align 4, !tbaa !5
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %9
  br label %34

17:                                               ; preds = %9
  %18 = load i8, ptr @global_data, align 8
  %19 = and i8 %18, -7
  %20 = or i8 %19, 4
  store i8 %20, ptr @global_data, align 8
  %21 = call i32 @psa_initialize_key_slots()
  store i32 %21, ptr %2, align 4, !tbaa !5
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %24, label %25

24:                                               ; preds = %17
  br label %34

25:                                               ; preds = %17
  %26 = call i32 @psa_driver_wrapper_init()
  store i32 %26, ptr %2, align 4, !tbaa !5
  %27 = load i32, ptr %2, align 4, !tbaa !5
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %30

29:                                               ; preds = %25
  br label %34

30:                                               ; preds = %25
  %31 = load i8, ptr @global_data, align 8
  %32 = and i8 %31, -2
  %33 = or i8 %32, 1
  store i8 %33, ptr @global_data, align 8
  br label %34

34:                                               ; preds = %30, %29, %24, %16
  %35 = load i32, ptr %2, align 4, !tbaa !5
  %36 = icmp ne i32 %35, 0
  br i1 %36, label %37, label %38

37:                                               ; preds = %34
  call void @iotex_psa_crypto_free()
  br label %38

38:                                               ; preds = %37, %34
  %39 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %39, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %40

40:                                               ; preds = %38, %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  %41 = load i32, ptr %1, align 4
  ret i32 %41
}

; Function Attrs: nounwind uwtable
declare hidden void @iotex_psa_random_init(ptr noundef) #1

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

declare i32 @psa_initialize_key_slots() #2

declare i32 @psa_driver_wrapper_init() #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 = { "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


; 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.psa_global_data_t = type { i8, %struct.iotex_psa_external_random_context_t }
%struct.iotex_psa_external_random_context_t = type { [2 x i64] }

@global_data = external hidden global %struct.psa_global_data_t, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @iotex_psa_crypto_free() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_crypto_init() local_unnamed_addr #0 {
  %1 = load i8, ptr @global_data, align 8
  %2 = and i8 %1, 1
  %.not = icmp eq i8 %2, 0
  br i1 %.not, label %3, label %19

3:                                                ; preds = %0
  tail call void @iotex_psa_random_init(ptr noundef nonnull getelementptr inbounds (i8, ptr @global_data, i64 8))
  %4 = load i8, ptr @global_data, align 8
  %5 = and i8 %4, -7
  %6 = or disjoint i8 %5, 2
  store i8 %6, ptr @global_data, align 8
  %7 = tail call i32 @iotex_psa_random_seed(ptr noundef nonnull getelementptr inbounds (i8, ptr @global_data, i64 8))
  %.not10 = icmp eq i32 %7, 0
  br i1 %.not10, label %8, label %18

8:                                                ; preds = %3
  %9 = load i8, ptr @global_data, align 8
  %10 = and i8 %9, -7
  %11 = or disjoint i8 %10, 4
  store i8 %11, ptr @global_data, align 8
  %12 = tail call i32 @psa_initialize_key_slots() #2
  %.not11 = icmp eq i32 %12, 0
  br i1 %.not11, label %13, label %18

13:                                               ; preds = %8
  %14 = tail call i32 @psa_driver_wrapper_init() #2
  %.not12 = icmp eq i32 %14, 0
  br i1 %.not12, label %15, label %18

15:                                               ; preds = %13
  %16 = load i8, ptr @global_data, align 8
  %17 = or i8 %16, 1
  store i8 %17, ptr @global_data, align 8
  br label %19

18:                                               ; preds = %3, %8, %13
  %.0.ph = phi i32 [ %14, %13 ], [ %12, %8 ], [ %7, %3 ]
  tail call void @iotex_psa_crypto_free()
  br label %19

19:                                               ; preds = %15, %18, %0
  %.06 = phi i32 [ 0, %0 ], [ %.0.ph, %18 ], [ 0, %15 ]
  ret i32 %.06
}

; Function Attrs: nounwind uwtable
declare hidden void @iotex_psa_random_init(ptr noundef) local_unnamed_addr #0

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

declare i32 @psa_initialize_key_slots() local_unnamed_addr #1

declare i32 @psa_driver_wrapper_init() local_unnamed_addr #1

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" }
attributes #1 = { "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)"}


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