; LLVM IR from machinefi/psa-crypto-arduino :: src/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_cipher_operation_s = type { i32, i8, i8, %union.psa_driver_cipher_context_t }
%union.psa_driver_cipher_context_t = type { %struct.iotex_psa_cipher_operation_t }
%struct.iotex_psa_cipher_operation_t = type { i32, i8, i8, %union.anon.1 }
%union.anon.1 = 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: 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
define dso_local i32 @psa_cipher_generate_iv(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 [16 x i8], align 16
  %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) #4
  store i32 -151, ptr %9, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 16, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %15

14:                                               ; preds = %4
  store i32 -135, ptr %9, align 4, !tbaa !11
  br label %62

15:                                               ; preds = %4
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.psa_cipher_operation_s, ptr %16, i32 0, i32 0
  %18 = load i32, ptr %17, align 8, !tbaa !13
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %20, label %21

20:                                               ; preds = %15
  store i32 -137, ptr %9, align 4, !tbaa !11
  br label %62

21:                                               ; preds = %15
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.psa_cipher_operation_s, ptr %22, i32 0, i32 1
  %24 = load i8, ptr %23, align 4
  %25 = lshr i8 %24, 1
  %26 = and i8 %25, 1
  %27 = zext i8 %26 to i32
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %36, label %29

29:                                               ; preds = %21
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.psa_cipher_operation_s, ptr %30, i32 0, i32 1
  %32 = load i8, ptr %31, align 4
  %33 = and i8 %32, 1
  %34 = zext i8 %33 to i32
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %37, label %36

36:                                               ; preds = %29, %21
  store i32 -137, ptr %9, align 4, !tbaa !11
  br label %62

37:                                               ; preds = %29
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.psa_cipher_operation_s, ptr %38, i32 0, i32 2
  %40 = load i8, ptr %39, align 1, !tbaa !15
  %41 = zext i8 %40 to i64
  store i64 %41, ptr %11, align 8, !tbaa !9
  %42 = load i64, ptr %7, align 8, !tbaa !9
  %43 = load i64, ptr %11, align 8, !tbaa !9
  %44 = icmp ult i64 %42, %43
  br i1 %44, label %45, label %46

45:                                               ; preds = %37
  store i32 -138, ptr %9, align 4, !tbaa !11
  br label %62

46:                                               ; preds = %37
  %47 = load i64, ptr %11, align 8, !tbaa !9
  %48 = icmp ugt i64 %47, 16
  br i1 %48, label %49, label %50

49:                                               ; preds = %46
  store i32 -132, ptr %9, align 4, !tbaa !11
  br label %62

50:                                               ; preds = %46
  %51 = getelementptr inbounds [16 x i8], ptr %10, i64 0, i64 0
  %52 = load i64, ptr %11, align 8, !tbaa !9
  %53 = call i32 @psa_generate_random(ptr noundef %51, i64 noundef %52)
  store i32 %53, ptr %9, align 4, !tbaa !11
  %54 = load i32, ptr %9, align 4, !tbaa !11
  %55 = icmp ne i32 %54, 0
  br i1 %55, label %56, label %57

56:                                               ; preds = %50
  br label %62

57:                                               ; preds = %50
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds [16 x i8], ptr %10, i64 0, i64 0
  %60 = load i64, ptr %11, align 8, !tbaa !9
  %61 = call i32 @psa_driver_wrapper_cipher_set_iv(ptr noundef %58, ptr noundef %59, i64 noundef %60)
  store i32 %61, ptr %9, align 4, !tbaa !11
  br label %62

62:                                               ; preds = %57, %56, %49, %45, %36, %20, %14
  %63 = load i32, ptr %9, align 4, !tbaa !11
  %64 = icmp eq i32 %63, 0
  br i1 %64, label %65, label %76

65:                                               ; preds = %62
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  %67 = getelementptr inbounds [16 x i8], ptr %10, i64 0, i64 0
  %68 = load i64, ptr %11, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %66, ptr align 16 %67, i64 %68, i1 false)
  %69 = load i64, ptr %11, align 8, !tbaa !9
  %70 = load ptr, ptr %8, align 8, !tbaa !5
  store i64 %69, ptr %70, align 8, !tbaa !9
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.psa_cipher_operation_s, ptr %71, i32 0, i32 1
  %73 = load i8, ptr %72, align 4
  %74 = and i8 %73, -3
  %75 = or i8 %74, 2
  store i8 %75, ptr %72, align 4
  br label %80

76:                                               ; preds = %62
  %77 = load ptr, ptr %8, align 8, !tbaa !5
  store i64 0, ptr %77, align 8, !tbaa !9
  %78 = load ptr, ptr %5, align 8, !tbaa !5
  %79 = call i32 @psa_cipher_abort(ptr noundef %78)
  br label %80

80:                                               ; preds = %76, %65
  %81 = load i32, ptr %9, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  ret i32 %81
}

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

declare i32 @psa_driver_wrapper_cipher_set_iv(ptr noundef, ptr noundef, i64 noundef) #3

; Function Attrs: nounwind uwtable
declare dso_local i32 @psa_cipher_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 = { "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 #4 = { 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_cipher_operation_s", !12, i64 0, !12, i64 4, !12, i64 4, !7, i64 5, !7, i64 8}
!15 = !{!14, !7, i64 5}


; 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
define dso_local i32 @psa_cipher_generate_iv(ptr noundef %0, ptr noundef writeonly %1, i64 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #2 {
  %5 = alloca [16 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %5) #4
  %6 = icmp eq ptr %1, null
  br i1 %6, label %.thread, label %7

7:                                                ; preds = %4
  %8 = load i32, ptr %0, align 8, !tbaa !5
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %.thread, label %10

10:                                               ; preds = %7
  %11 = getelementptr inbounds i8, ptr %0, i64 4
  %12 = load i8, ptr %11, align 4
  %13 = and i8 %12, 3
  %or.cond.not = icmp eq i8 %13, 1
  br i1 %or.cond.not, label %14, label %.thread

14:                                               ; preds = %10
  %15 = getelementptr inbounds i8, ptr %0, i64 5
  %16 = load i8, ptr %15, align 1, !tbaa !10
  %17 = zext i8 %16 to i64
  %18 = icmp ugt i64 %17, %2
  br i1 %18, label %.thread, label %19

19:                                               ; preds = %14
  %20 = icmp ugt i8 %16, 16
  br i1 %20, label %.thread, label %21

21:                                               ; preds = %19
  %22 = call i32 @psa_generate_random(ptr noundef nonnull %5, i64 noundef %17)
  %.not24 = icmp eq i32 %22, 0
  br i1 %.not24, label %23, label %.thread

23:                                               ; preds = %21
  %24 = call i32 @psa_driver_wrapper_cipher_set_iv(ptr noundef nonnull %0, ptr noundef nonnull %5, i64 noundef %17) #4
  %25 = icmp eq i32 %24, 0
  br i1 %25, label %26, label %.thread

26:                                               ; preds = %23
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %1, ptr nonnull align 16 %5, i64 %17, i1 false)
  store i64 %17, ptr %3, align 8, !tbaa !11
  %27 = load i8, ptr %11, align 4
  %28 = or i8 %27, 2
  store i8 %28, ptr %11, align 4
  br label %30

.thread:                                          ; preds = %19, %14, %10, %7, %4, %21, %23
  %.02028 = phi i32 [ %24, %23 ], [ -132, %19 ], [ -138, %14 ], [ -137, %10 ], [ -137, %7 ], [ -135, %4 ], [ %22, %21 ]
  store i64 0, ptr %3, align 8, !tbaa !11
  %29 = call i32 @psa_cipher_abort(ptr noundef %0)
  br label %30

30:                                               ; preds = %.thread, %26
  %.02027 = phi i32 [ %.02028, %.thread ], [ 0, %26 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %5) #4
  ret i32 %.02027
}

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

declare i32 @psa_driver_wrapper_cipher_set_iv(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare dso_local i32 @psa_cipher_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 = { "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 #4 = { 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, !7, i64 0}
!6 = !{!"psa_cipher_operation_s", !7, i64 0, !7, i64 4, !7, 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 = !{!6, !8, i64 5}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !8, i64 0}


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