; LLVM IR from machinefi/psa-crypto-esp32 :: crypto (ctr_prng)

; 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.TCCtrPrng_t = type { [16 x i8], %struct.tc_aes_key_sched_struct, i64 }
%struct.tc_aes_key_sched_struct = type { [44 x i32] }

; Function Attrs: nounwind uwtable
define dso_local i32 @tc_ctr_prng_init(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3, i32 noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca [32 x i8], align 16
  %14 = alloca [32 x i8], align 16
  %15 = alloca [16 x i8], align 16
  %16 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  store i32 %4, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  store i32 0, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  call void @llvm.lifetime.start.p0(i64 32, ptr %13) #5
  call void @llvm.memset.p0.i64(ptr align 16 %13, i8 0, i64 32, i1 false)
  call void @llvm.lifetime.start.p0(i64 32, ptr %14) #5
  call void @llvm.lifetime.start.p0(i64 16, ptr %15) #5
  call void @llvm.memset.p0.i64(ptr align 16 %15, i8 0, i64 16, i1 false)
  %17 = load ptr, ptr %9, align 8, !tbaa !5
  %18 = icmp ne ptr null, %17
  br i1 %18, label %19, label %30

19:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #5
  %20 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %20, ptr %16, align 4, !tbaa !9
  %21 = load i32, ptr %16, align 4, !tbaa !9
  %22 = zext i32 %21 to i64
  %23 = icmp ugt i64 %22, 32
  br i1 %23, label %24, label %25

24:                                               ; preds = %19
  store i32 32, ptr %16, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %24, %19
  %26 = getelementptr inbounds [32 x i8], ptr %13, i64 0, i64 0
  %27 = load ptr, ptr %9, align 8, !tbaa !5
  %28 = load i32, ptr %16, align 4, !tbaa !9
  %29 = zext i32 %28 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %26, ptr align 1 %27, i64 %29, i1 false)
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #5
  br label %30

30:                                               ; preds = %25, %5
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = icmp ne ptr null, %31
  br i1 %32, label %33, label %75

33:                                               ; preds = %30
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = icmp ne ptr null, %34
  br i1 %35, label %36, label %75

36:                                               ; preds = %33
  %37 = load i32, ptr %8, align 4, !tbaa !9
  %38 = zext i32 %37 to i64
  %39 = icmp uge i64 %38, 32
  br i1 %39, label %40, label %75

40:                                               ; preds = %36
  %41 = getelementptr inbounds [32 x i8], ptr %14, i64 0, i64 0
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %41, ptr align 1 %42, i64 32, i1 false)
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %60, %40
  %44 = load i32, ptr %12, align 4, !tbaa !9
  %45 = zext i32 %44 to i64
  %46 = icmp ult i64 %45, 32
  br i1 %46, label %47, label %63

47:                                               ; preds = %43
  %48 = load i32, ptr %12, align 4, !tbaa !9
  %49 = zext i32 %48 to i64
  %50 = getelementptr inbounds [32 x i8], ptr %13, i64 0, i64 %49
  %51 = load i8, ptr %50, align 1, !tbaa !11
  %52 = zext i8 %51 to i32
  %53 = load i32, ptr %12, align 4, !tbaa !9
  %54 = zext i32 %53 to i64
  %55 = getelementptr inbounds [32 x i8], ptr %14, i64 0, i64 %54
  %56 = load i8, ptr %55, align 1, !tbaa !11
  %57 = zext i8 %56 to i32
  %58 = xor i32 %57, %52
  %59 = trunc i32 %58 to i8
  store i8 %59, ptr %55, align 1, !tbaa !11
  br label %60

60:                                               ; preds = %47
  %61 = load i32, ptr %12, align 4, !tbaa !9
  %62 = add i32 %61, 1
  store i32 %62, ptr %12, align 4, !tbaa !9
  br label %43, !llvm.loop !12

63:                                               ; preds = %43
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.TCCtrPrng_t, ptr %64, i32 0, i32 1
  %66 = getelementptr inbounds [16 x i8], ptr %15, i64 0, i64 0
  %67 = call i32 @tc_aes128_set_encrypt_key(ptr noundef %65, ptr noundef %66)
  %68 = load ptr, ptr %6, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.TCCtrPrng_t, ptr %68, i32 0, i32 0
  %70 = getelementptr inbounds [16 x i8], ptr %69, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 8 %70, i8 0, i64 16, i1 false)
  %71 = load ptr, ptr %6, align 8, !tbaa !5
  %72 = getelementptr inbounds [32 x i8], ptr %14, i64 0, i64 0
  call void @tc_ctr_prng_update(ptr noundef %71, ptr noundef %72)
  %73 = load ptr, ptr %6, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.TCCtrPrng_t, ptr %73, i32 0, i32 2
  store i64 1, ptr %74, align 8, !tbaa !14
  store i32 1, ptr %11, align 4, !tbaa !9
  br label %75

75:                                               ; preds = %63, %36, %33, %30
  %76 = load i32, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 16, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 32, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 32, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  ret i32 %76
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; 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) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

declare i32 @tc_aes128_set_encrypt_key(ptr noundef, ptr noundef) #4

; Function Attrs: nounwind uwtable
declare hidden void @tc_ctr_prng_update(ptr noundef, ptr noundef) #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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { "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 #5 = { 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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !17, i64 192}
!15 = !{!"", !7, i64 0, !16, i64 16, !17, i64 192}
!16 = !{!"tc_aes_key_sched_struct", !7, i64 0}
!17 = !{!"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 range(i32 0, 2) i32 @tc_ctr_prng_init(ptr noundef %0, ptr noundef readonly %1, i32 noundef %2, ptr noundef readonly %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = alloca [32 x i8], align 16
  %7 = alloca [32 x i8], align 16
  %8 = alloca [16 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %6)
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %6, i8 0, i64 32, i1 false)
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %7) #6
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %8) #6
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %8, i8 0, i64 16, i1 false)
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %11, label %9

9:                                                ; preds = %5
  %spec.store.select = tail call i32 @llvm.umin.i32(i32 %4, i32 32)
  %10 = zext nneg i32 %spec.store.select to i64
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 %6, ptr nonnull align 1 %3, i64 %10, i1 false)
  br label %11

11:                                               ; preds = %9, %5
  %12 = icmp ne ptr %0, null
  %13 = icmp ne ptr %1, null
  %or.cond = and i1 %12, %13
  %14 = icmp ugt i32 %2, 31
  %or.cond4 = and i1 %or.cond, %14
  br i1 %or.cond4, label %15, label %24

15:                                               ; preds = %11
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %7, ptr noundef nonnull align 1 dereferenceable(32) %1, i64 32, i1 false)
  %.0. = load <16 x i8>, ptr %6, align 16, !tbaa !5
  %16 = load <16 x i8>, ptr %7, align 16, !tbaa !5
  %17 = xor <16 x i8> %16, %.0.
  store <16 x i8> %17, ptr %7, align 16, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %7, i64 16
  %.16.sroa_idx28 = getelementptr inbounds i8, ptr %6, i64 16
  %.16. = load <16 x i8>, ptr %.16.sroa_idx28, align 16, !tbaa !5
  %19 = load <16 x i8>, ptr %18, align 16, !tbaa !5
  %20 = xor <16 x i8> %19, %.16.
  store <16 x i8> %20, ptr %18, align 16, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %0, i64 16
  %22 = call i32 @tc_aes128_set_encrypt_key(ptr noundef nonnull %21, ptr noundef nonnull %8) #6
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %0, i8 0, i64 16, i1 false)
  call void @tc_ctr_prng_update(ptr noundef nonnull %0, ptr noundef nonnull %7)
  %23 = getelementptr inbounds i8, ptr %0, i64 192
  store i64 1, ptr %23, align 8, !tbaa !8
  br label %24

24:                                               ; preds = %15, %11
  %.0 = phi i32 [ 1, %15 ], [ 0, %11 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %8) #6
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %7) #6
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %6)
  ret i32 %.0
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; 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) #3

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

declare i32 @tc_aes128_set_encrypt_key(ptr noundef, ptr noundef) local_unnamed_addr #4

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #5

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { "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 #5 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #6 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !11, i64 192}
!9 = !{!"", !6, i64 0, !10, i64 16, !11, i64 192}
!10 = !{!"tc_aes_key_sched_struct", !6, i64 0}
!11 = !{!"long", !6, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,PostOrderFunctionAttrsPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass