; 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_aead_operation_s = type { i32, i32, i16, i64, i64, i8, %union.psa_driver_aead_context_t }
%union.psa_driver_aead_context_t = type { %struct.iotex_psa_aead_operation_t }
%struct.iotex_psa_aead_operation_t = type { i32, i16, i8, i8, %union.anon.2 }
%union.anon.2 = type { i32 }

; 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: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

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

17:                                               ; preds = %4
  store i32 -137, ptr %9, align 4, !tbaa !11
  br label %110

18:                                               ; preds = %4
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.psa_aead_operation_s, ptr %19, i32 0, i32 5
  %21 = load i8, ptr %20, align 8
  %22 = and i8 %21, 1
  %23 = zext i8 %22 to i32
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %33, label %25

25:                                               ; preds = %18
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.psa_aead_operation_s, ptr %26, i32 0, i32 5
  %28 = load i8, ptr %27, align 8
  %29 = lshr i8 %28, 4
  %30 = and i8 %29, 1
  %31 = zext i8 %30 to i32
  %32 = icmp ne i32 %31, 0
  br i1 %32, label %34, label %33

33:                                               ; preds = %25, %18
  store i32 -137, ptr %9, align 4, !tbaa !11
  br label %110

34:                                               ; preds = %25
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.psa_aead_operation_s, ptr %35, i32 0, i32 2
  %37 = load i16, ptr %36, align 8, !tbaa !16
  %38 = zext i16 %37 to i32
  %39 = and i32 %38, 28672
  %40 = icmp eq i32 %39, 8192
  br i1 %40, label %41, label %49

41:                                               ; preds = %34
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.psa_aead_operation_s, ptr %42, i32 0, i32 2
  %44 = load i16, ptr %43, align 8, !tbaa !16
  %45 = zext i16 %44 to i32
  %46 = ashr i32 %45, 8
  %47 = and i32 %46, 7
  %48 = shl i32 1, %47
  br label %50

49:                                               ; preds = %34
  br label %50

50:                                               ; preds = %49, %41
  %51 = phi i32 [ %48, %41 ], [ 0, %49 ]
  %52 = icmp eq i32 %51, 16
  br i1 %52, label %53, label %73

53:                                               ; preds = %50
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.psa_aead_operation_s, ptr %54, i32 0, i32 1
  %56 = load i32, ptr %55, align 4, !tbaa !17
  %57 = xor i32 %56, 89129216
  %58 = and i32 %57, -4161537
  %59 = icmp ne i32 %58, 0
  br i1 %59, label %61, label %60

60:                                               ; preds = %53
  br label %71

61:                                               ; preds = %53
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.psa_aead_operation_s, ptr %62, i32 0, i32 1
  %64 = load i32, ptr %63, align 4, !tbaa !17
  %65 = xor i32 %64, 89129472
  %66 = and i32 %65, -4161537
  %67 = icmp ne i32 %66, 0
  %68 = xor i1 %67, true
  %69 = zext i1 %68 to i64
  %70 = select i1 %68, i32 12, i32 0
  br label %71

71:                                               ; preds = %61, %60
  %72 = phi i32 [ 13, %60 ], [ %70, %61 ]
  br label %91

73:                                               ; preds = %50
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.psa_aead_operation_s, ptr %74, i32 0, i32 2
  %76 = load i16, ptr %75, align 8, !tbaa !16
  %77 = zext i16 %76 to i32
  %78 = icmp eq i32 %77, 8196
  br i1 %78, label %79, label %87

79:                                               ; preds = %73
  %80 = load ptr, ptr %5, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.psa_aead_operation_s, ptr %80, i32 0, i32 1
  %82 = load i32, ptr %81, align 4, !tbaa !17
  %83 = xor i32 %82, 84935936
  %84 = and i32 %83, -4161537
  %85 = icmp ne i32 %84, 0
  %86 = xor i1 %85, true
  br label %87

87:                                               ; preds = %79, %73
  %88 = phi i1 [ false, %73 ], [ %86, %79 ]
  %89 = zext i1 %88 to i64
  %90 = select i1 %88, i32 12, i32 0
  br label %91

91:                                               ; preds = %87, %71
  %92 = phi i32 [ %72, %71 ], [ %90, %87 ]
  %93 = sext i32 %92 to i64
  store i64 %93, ptr %11, align 8, !tbaa !9
  %94 = load i64, ptr %7, align 8, !tbaa !9
  %95 = load i64, ptr %11, align 8, !tbaa !9
  %96 = icmp ult i64 %94, %95
  br i1 %96, label %97, label %98

97:                                               ; preds = %91
  store i32 -138, ptr %9, align 4, !tbaa !11
  br label %110

98:                                               ; preds = %91
  %99 = getelementptr inbounds [13 x i8], ptr %10, i64 0, i64 0
  %100 = load i64, ptr %11, align 8, !tbaa !9
  %101 = call i32 @psa_generate_random(ptr noundef %99, i64 noundef %100)
  store i32 %101, ptr %9, align 4, !tbaa !11
  %102 = load i32, ptr %9, align 4, !tbaa !11
  %103 = icmp ne i32 %102, 0
  br i1 %103, label %104, label %105

104:                                              ; preds = %98
  br label %110

105:                                              ; preds = %98
  %106 = load ptr, ptr %5, align 8, !tbaa !5
  %107 = getelementptr inbounds [13 x i8], ptr %10, i64 0, i64 0
  %108 = load i64, ptr %11, align 8, !tbaa !9
  %109 = call i32 @psa_aead_set_nonce(ptr noundef %106, ptr noundef %107, i64 noundef %108)
  store i32 %109, ptr %9, align 4, !tbaa !11
  br label %110

110:                                              ; preds = %105, %104, %97, %33, %17
  %111 = load i32, ptr %9, align 4, !tbaa !11
  %112 = icmp eq i32 %111, 0
  br i1 %112, label %113, label %119

113:                                              ; preds = %110
  %114 = load ptr, ptr %6, align 8, !tbaa !5
  %115 = getelementptr inbounds [13 x i8], ptr %10, i64 0, i64 0
  %116 = load i64, ptr %11, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %114, ptr align 1 %115, i64 %116, i1 false)
  %117 = load i64, ptr %11, align 8, !tbaa !9
  %118 = load ptr, ptr %8, align 8, !tbaa !5
  store i64 %117, ptr %118, align 8, !tbaa !9
  br label %122

119:                                              ; preds = %110
  %120 = load ptr, ptr %5, align 8, !tbaa !5
  %121 = call i32 @psa_aead_abort(ptr noundef %120)
  br label %122

122:                                              ; preds = %119, %113
  %123 = load i32, ptr %9, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 13, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  ret i32 %123
}

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @psa_aead_abort(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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_aead_operation_s", !12, i64 0, !12, i64 4, !15, i64 8, !10, i64 16, !10, i64 24, !12, i64 32, !12, i64 32, !12, i64 32, !12, i64 32, !12, i64 32, !7, i64 36}
!15 = !{!"short", !7, i64 0}
!16 = !{!14, !15, i64 8}
!17 = !{!14, !12, i64 4}


; 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 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: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_aead_generate_nonce(ptr noundef %0, ptr nocapture noundef writeonly %1, i64 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #2 {
  %5 = alloca [13 x i8], align 1
  call void @llvm.lifetime.start.p0(i64 13, ptr nonnull %5) #3
  store i64 0, ptr %3, align 8, !tbaa !5
  %6 = load i32, ptr %0, align 8, !tbaa !9
  %7 = icmp eq i32 %6, 0
  br i1 %7, label %.thread30, label %8

8:                                                ; preds = %4
  %9 = getelementptr inbounds i8, ptr %0, i64 32
  %10 = load i8, ptr %9, align 8
  %11 = and i8 %10, 17
  %or.cond.not = icmp eq i8 %11, 16
  br i1 %or.cond.not, label %12, label %.thread30

12:                                               ; preds = %8
  %13 = getelementptr inbounds i8, ptr %0, i64 8
  %14 = load i16, ptr %13, align 8, !tbaa !13
  %15 = and i16 %14, 30464
  %16 = icmp eq i16 %15, 9216
  br i1 %16, label %17, label %23

17:                                               ; preds = %12
  %18 = getelementptr inbounds i8, ptr %0, i64 4
  %19 = load i32, ptr %18, align 4, !tbaa !14
  %20 = and i32 %19, -4161537
  %.not27 = icmp eq i32 %20, 88080640
  br i1 %.not27, label %30, label %21

21:                                               ; preds = %17
  %.not28 = icmp eq i32 %20, 88080896
  %22 = select i1 %.not28, i64 12, i64 0
  br label %30

23:                                               ; preds = %12
  %24 = icmp eq i16 %14, 8196
  br i1 %24, label %25, label %.thread

25:                                               ; preds = %23
  %26 = getelementptr inbounds i8, ptr %0, i64 4
  %27 = load i32, ptr %26, align 4, !tbaa !14
  %28 = and i32 %27, -4161537
  %.not26 = icmp eq i32 %28, 83887360
  %29 = select i1 %.not26, i64 12, i64 0
  br label %30

30:                                               ; preds = %25, %21, %17
  %31 = phi i64 [ %22, %21 ], [ 13, %17 ], [ %29, %25 ]
  %32 = icmp ugt i64 %31, %2
  br i1 %32, label %.thread30, label %.thread

.thread:                                          ; preds = %23, %30
  %33 = phi i64 [ %31, %30 ], [ 0, %23 ]
  %34 = call i32 @psa_generate_random(ptr noundef nonnull %5, i64 noundef %33)
  %.not29 = icmp eq i32 %34, 0
  br i1 %.not29, label %35, label %.thread30

35:                                               ; preds = %.thread
  %36 = call i32 @psa_aead_set_nonce(ptr noundef nonnull %0, ptr noundef nonnull %5, i64 noundef %33)
  %37 = icmp eq i32 %36, 0
  br i1 %37, label %38, label %.thread30

38:                                               ; preds = %35
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %1, ptr nonnull align 1 %5, i64 %33, i1 false)
  store i64 %33, ptr %3, align 8, !tbaa !5
  br label %40

.thread30:                                        ; preds = %30, %8, %4, %.thread, %35
  %.02234 = phi i32 [ %36, %35 ], [ -138, %30 ], [ -137, %8 ], [ -137, %4 ], [ %34, %.thread ]
  %39 = call i32 @psa_aead_abort(ptr noundef nonnull %0)
  br label %40

40:                                               ; preds = %.thread30, %38
  %.02233 = phi i32 [ %.02234, %.thread30 ], [ 0, %38 ]
  call void @llvm.lifetime.end.p0(i64 13, ptr nonnull %5) #3
  ret i32 %.02233
}

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

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"psa_aead_operation_s", !11, i64 0, !11, i64 4, !12, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !11, i64 32, !11, i64 32, !11, i64 32, !11, i64 32, !7, i64 36}
!11 = !{!"int", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!10, !12, i64 8}
!14 = !{!10, !11, i64 4}


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