; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

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

; 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 hidden void @tc_ctr_prng_update(ptr noundef, ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @tc_ctr_prng_reseed(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3, i32 noundef %4) #3 {
  %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 i32, align 4
  %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) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  store i32 0, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 32, ptr %13) #4
  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) #4
  %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 %15) #4
  %20 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %20, ptr %15, align 4, !tbaa !9
  %21 = load i32, ptr %15, 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 %15, 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 %15, 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 %15) #4
  br label %30

30:                                               ; preds = %25, %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #4
  store i32 32, ptr %16, align 4, !tbaa !9
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = icmp ne ptr null, %31
  br i1 %32, label %33, label %65

33:                                               ; preds = %30
  %34 = load i32, ptr %8, align 4, !tbaa !9
  %35 = load i32, ptr %16, align 4, !tbaa !9
  %36 = icmp uge i32 %34, %35
  br i1 %36, label %37, label %65

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

40:                                               ; preds = %57, %37
  %41 = load i32, ptr %11, align 4, !tbaa !9
  %42 = zext i32 %41 to i64
  %43 = icmp ult i64 %42, 32
  br i1 %43, label %44, label %60

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

57:                                               ; preds = %44
  %58 = load i32, ptr %11, align 4, !tbaa !9
  %59 = add i32 %58, 1
  store i32 %59, ptr %11, align 4, !tbaa !9
  br label %40, !llvm.loop !12

60:                                               ; preds = %40
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = getelementptr inbounds [32 x i8], ptr %14, i64 0, i64 0
  call void @tc_ctr_prng_update(ptr noundef %61, ptr noundef %62)
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.TCCtrPrng_t, ptr %63, i32 0, i32 2
  store i64 1, ptr %64, align 8, !tbaa !14
  store i32 1, ptr %12, align 4, !tbaa !9
  br label %65

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #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 = !{!"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: 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @tc_ctr_prng_reseed(ptr noundef %0, ptr nocapture noundef readonly %1, i32 noundef %2, ptr noundef readonly %3, i32 noundef %4) local_unnamed_addr #3 {
  %6 = alloca [32 x i8], align 16
  %7 = alloca [32 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) #5
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %10, label %8

8:                                                ; preds = %5
  %spec.store.select = tail call i32 @llvm.umin.i32(i32 %4, i32 32)
  %9 = 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 %9, i1 false)
  br label %10

10:                                               ; preds = %8, %5
  %.not19 = icmp ne ptr %0, null
  %11 = icmp ugt i32 %2, 31
  %or.cond = and i1 %.not19, %11
  br i1 %or.cond, label %12, label %19

12:                                               ; preds = %10
  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
  %13 = load <16 x i8>, ptr %7, align 16, !tbaa !5
  %14 = xor <16 x i8> %13, %.0.
  store <16 x i8> %14, ptr %7, align 16, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %7, i64 16
  %.16.sroa_idx22 = getelementptr inbounds i8, ptr %6, i64 16
  %.16. = load <16 x i8>, ptr %.16.sroa_idx22, align 16, !tbaa !5
  %16 = load <16 x i8>, ptr %15, align 16, !tbaa !5
  %17 = xor <16 x i8> %16, %.16.
  store <16 x i8> %17, ptr %15, align 16, !tbaa !5
  call void @tc_ctr_prng_update(ptr noundef nonnull %0, ptr noundef nonnull %7)
  %18 = getelementptr inbounds i8, ptr %0, i64 192
  store i64 1, ptr %18, align 8, !tbaa !8
  br label %19

19:                                               ; preds = %12, %10
  %.015 = phi i32 [ 1, %12 ], [ 0, %10 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %7) #5
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %6)
  ret i32 %.015
}

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"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