; 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_generate(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 [32 x i8], align 16
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca [16 x i8], align 16
  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
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = icmp ne ptr null, %17
  br i1 %18, label %19, label %88

19:                                               ; preds = %5
  %20 = load ptr, ptr %9, align 8, !tbaa !5
  %21 = icmp ne ptr null, %20
  br i1 %21, label %22, label %88

22:                                               ; preds = %19
  %23 = load i32, ptr %10, align 4, !tbaa !9
  %24 = icmp ult i32 %23, 65536
  br i1 %24, label %25, label %88

25:                                               ; preds = %22
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.TCCtrPrng_t, ptr %26, i32 0, i32 2
  %28 = load i64, ptr %27, align 8, !tbaa !11
  %29 = icmp ugt i64 %28, 281474976710656
  br i1 %29, label %30, label %31

30:                                               ; preds = %25
  store i32 -1, ptr %11, align 4, !tbaa !9
  br label %87

31:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 32, ptr %12) #5
  call void @llvm.memset.p0.i64(ptr align 16 %12, i8 0, i64 32, i1 false)
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  %33 = icmp ne ptr null, %32
  br i1 %33, label %34, label %47

34:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  %35 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %35, ptr %13, align 4, !tbaa !9
  %36 = load i32, ptr %13, align 4, !tbaa !9
  %37 = zext i32 %36 to i64
  %38 = icmp ugt i64 %37, 32
  br i1 %38, label %39, label %40

39:                                               ; preds = %34
  store i32 32, ptr %13, align 4, !tbaa !9
  br label %40

40:                                               ; preds = %39, %34
  %41 = getelementptr inbounds [32 x i8], ptr %12, i64 0, i64 0
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = load i32, ptr %13, align 4, !tbaa !9
  %44 = zext i32 %43 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %41, ptr align 1 %42, i64 %44, i1 false)
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = getelementptr inbounds [32 x i8], ptr %12, i64 0, i64 0
  call void @tc_ctr_prng_update(ptr noundef %45, ptr noundef %46)
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  br label %47

47:                                               ; preds = %40, %31
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #5
  store i32 0, ptr %14, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %69, %47
  %49 = load i32, ptr %14, align 4, !tbaa !9
  %50 = load i32, ptr %10, align 4, !tbaa !9
  %51 = icmp ult i32 %49, %50
  br i1 %51, label %52, label %80

52:                                               ; preds = %48
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #5
  %53 = load i32, ptr %10, align 4, !tbaa !9
  %54 = load i32, ptr %14, align 4, !tbaa !9
  %55 = sub i32 %53, %54
  store i32 %55, ptr %15, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %16) #5
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.TCCtrPrng_t, ptr %56, i32 0, i32 0
  %58 = getelementptr inbounds [16 x i8], ptr %57, i64 0, i64 0
  call void @arrInc(ptr noundef %58, i32 noundef 16)
  %59 = getelementptr inbounds [16 x i8], ptr %16, i64 0, i64 0
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.TCCtrPrng_t, ptr %60, i32 0, i32 0
  %62 = getelementptr inbounds [16 x i8], ptr %61, i64 0, i64 0
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.TCCtrPrng_t, ptr %63, i32 0, i32 1
  %65 = call i32 @tc_aes_encrypt(ptr noundef %59, ptr noundef %62, ptr noundef %64)
  %66 = load i32, ptr %15, align 4, !tbaa !9
  %67 = icmp ugt i32 %66, 16
  br i1 %67, label %68, label %69

68:                                               ; preds = %52
  store i32 16, ptr %15, align 4, !tbaa !9
  br label %69

69:                                               ; preds = %68, %52
  %70 = load ptr, ptr %9, align 8, !tbaa !5
  %71 = load i32, ptr %14, align 4, !tbaa !9
  %72 = zext i32 %71 to i64
  %73 = getelementptr inbounds i8, ptr %70, i64 %72
  %74 = getelementptr inbounds [16 x i8], ptr %16, i64 0, i64 0
  %75 = load i32, ptr %15, align 4, !tbaa !9
  %76 = zext i32 %75 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %73, ptr align 16 %74, i64 %76, i1 false)
  %77 = load i32, ptr %15, align 4, !tbaa !9
  %78 = load i32, ptr %14, align 4, !tbaa !9
  %79 = add i32 %78, %77
  store i32 %79, ptr %14, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 16, ptr %16) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #5
  br label %48, !llvm.loop !15

80:                                               ; preds = %48
  %81 = load ptr, ptr %6, align 8, !tbaa !5
  %82 = getelementptr inbounds [32 x i8], ptr %12, i64 0, i64 0
  call void @tc_ctr_prng_update(ptr noundef %81, ptr noundef %82)
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.TCCtrPrng_t, ptr %83, i32 0, i32 2
  %85 = load i64, ptr %84, align 8, !tbaa !11
  %86 = add i64 %85, 1
  store i64 %86, ptr %84, align 8, !tbaa !11
  store i32 1, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 32, ptr %12) #5
  br label %87

87:                                               ; preds = %80, %30
  br label %88

88:                                               ; preds = %87, %22, %19, %5
  %89 = load i32, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  ret i32 %89
}

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

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

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 = { "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 = !{!12, !14, i64 192}
!12 = !{!"", !7, i64 0, !13, i64 16, !14, i64 192}
!13 = !{!"tc_aes_key_sched_struct", !7, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


; 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 -1, 2) i32 @tc_ctr_prng_generate(ptr noundef %0, ptr noundef readonly %1, i32 noundef %2, ptr noundef writeonly %3, i32 noundef %4) local_unnamed_addr #3 {
  %6 = alloca [32 x i8], align 16
  %7 = alloca [16 x i8], align 16
  %8 = icmp ne ptr %0, null
  %9 = icmp ne ptr %3, null
  %or.cond = and i1 %8, %9
  %10 = icmp ult i32 %4, 65536
  %or.cond3 = and i1 %or.cond, %10
  br i1 %or.cond3, label %11, label %30

11:                                               ; preds = %5
  %12 = getelementptr inbounds i8, ptr %0, i64 192
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %14 = icmp ugt i64 %13, 281474976710656
  br i1 %14, label %30, label %15

15:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %6) #6
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %6, i8 0, i64 32, i1 false)
  %.not = icmp eq ptr %1, null
  br i1 %.not, label %18, label %16

16:                                               ; preds = %15
  %spec.store.select = tail call i32 @llvm.umin.i32(i32 %2, i32 32)
  %17 = zext nneg i32 %spec.store.select to i64
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 %6, ptr nonnull align 1 %1, i64 %17, i1 false)
  call void @tc_ctr_prng_update(ptr noundef nonnull %0, ptr noundef nonnull %6)
  br label %18

18:                                               ; preds = %16, %15
  %.not35 = icmp eq i32 %4, 0
  br i1 %.not35, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %18
  %19 = getelementptr inbounds i8, ptr %0, i64 16
  br label %20

20:                                               ; preds = %.lr.ph, %20
  %.02934 = phi i32 [ 0, %.lr.ph ], [ %26, %20 ]
  %21 = sub nsw i32 %4, %.02934
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %7) #6
  call void @arrInc(ptr noundef nonnull %0, i32 noundef 16)
  %22 = call i32 @tc_aes_encrypt(ptr noundef nonnull %7, ptr noundef nonnull %0, ptr noundef nonnull %19) #6
  %spec.store.select4 = call i32 @llvm.umin.i32(i32 %21, i32 16)
  %23 = zext nneg i32 %.02934 to i64
  %24 = getelementptr inbounds i8, ptr %3, i64 %23
  %25 = zext nneg i32 %spec.store.select4 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %24, ptr nonnull align 16 %7, i64 %25, i1 false)
  %26 = add nuw nsw i32 %spec.store.select4, %.02934
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %7) #6
  %27 = icmp ult i32 %26, %4
  br i1 %27, label %20, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %20, %18
  call void @tc_ctr_prng_update(ptr noundef nonnull %0, ptr noundef nonnull %6)
  %28 = load i64, ptr %12, align 8, !tbaa !5
  %29 = add i64 %28, 1
  store i64 %29, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %6) #6
  br label %30

30:                                               ; preds = %11, %._crit_edge, %5
  %.0 = phi i32 [ 1, %._crit_edge ], [ 0, %5 ], [ -1, %11 ]
  ret i32 %.0
}

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

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

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

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 = { "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, !10, i64 192}
!6 = !{!"", !7, i64 0, !9, i64 16, !10, i64 192}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"tc_aes_key_sched_struct", !7, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass