; 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_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 %5, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.psa_cipher_operation_s, ptr %12, i32 0, i32 0
  %14 = load i32, ptr %13, align 8, !tbaa !13
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %16, label %17

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

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

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

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

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

41:                                               ; preds = %33
  store i32 -138, ptr %9, align 4, !tbaa !11
  br label %58

42:                                               ; preds = %33
  %43 = load i64, ptr %11, align 8, !tbaa !9
  %44 = icmp ugt i64 %43, 16
  br i1 %44, label %45, label %46

45:                                               ; preds = %42
  store i32 -132, ptr %9, align 4, !tbaa !11
  br label %58

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

52:                                               ; preds = %46
  br label %58

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

58:                                               ; preds = %53, %52, %45, %41, %32, %16
  %59 = load i32, ptr %9, align 4, !tbaa !11
  %60 = icmp eq i32 %59, 0
  br i1 %60, label %61, label %72

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

72:                                               ; preds = %58
  %73 = load ptr, ptr %8, align 8, !tbaa !5
  store i64 0, ptr %73, align 8, !tbaa !9
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = call i32 @psa_cipher_abort(ptr noundef %74)
  br label %76

76:                                               ; preds = %72, %61
  %77 = 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 %77
}

; 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 nocapture 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 = load i32, ptr %0, align 8, !tbaa !5
  %7 = icmp eq i32 %6, 0
  br i1 %7, label %.thread, label %8

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

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

17:                                               ; preds = %12
  %18 = icmp ugt i8 %14, 16
  br i1 %18, label %.thread, label %19

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

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

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

.thread:                                          ; preds = %17, %12, %8, %4, %19, %21
  %.01927 = phi i32 [ %22, %21 ], [ -132, %17 ], [ -138, %12 ], [ -137, %8 ], [ -137, %4 ], [ %20, %19 ]
  store i64 0, ptr %3, align 8, !tbaa !11
  %27 = call i32 @psa_cipher_abort(ptr noundef nonnull %0)
  br label %28

28:                                               ; preds = %.thread, %24
  %.01926 = phi i32 [ %.01927, %.thread ], [ 0, %24 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %5) #4
  ret i32 %.01926
}

; 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