; LLVM IR from sca-secure-library-sca25519/sca25519 :: cw/sca25519_cw/crypto/support (randombytes)

; 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"

; Function Attrs: nounwind uwtable
declare dso_local i32 @rand32() #0

; Function Attrs: nounwind uwtable
define dso_local void @randombytes(ptr noundef %0, i64 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  br label %6

6:                                                ; preds = %9, %2
  %7 = load i64, ptr %4, align 8, !tbaa !9
  %8 = icmp uge i64 %7, 4
  br i1 %8, label %9, label %16

9:                                                ; preds = %6
  %10 = call i32 @rand32()
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  store i32 %10, ptr %11, align 4, !tbaa !11
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %12, i64 4
  store ptr %13, ptr %3, align 8, !tbaa !5
  %14 = load i64, ptr %4, align 8, !tbaa !9
  %15 = sub i64 %14, 4
  store i64 %15, ptr %4, align 8, !tbaa !9
  br label %6, !llvm.loop !13

16:                                               ; preds = %6
  %17 = load i64, ptr %4, align 8, !tbaa !9
  %18 = icmp eq i64 %17, 0
  br i1 %18, label %19, label %20

19:                                               ; preds = %16
  br label %36

20:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %21 = call i32 @rand32()
  store i32 %21, ptr %5, align 4, !tbaa !11
  br label %22

22:                                               ; preds = %25, %20
  %23 = load i64, ptr %4, align 8, !tbaa !9
  %24 = icmp ne i64 %23, 0
  br i1 %24, label %25, label %35

25:                                               ; preds = %22
  %26 = load i32, ptr %5, align 4, !tbaa !11
  %27 = trunc i32 %26 to i8
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  store i8 %27, ptr %28, align 1, !tbaa !15
  %29 = load i32, ptr %5, align 4, !tbaa !11
  %30 = lshr i32 %29, 8
  store i32 %30, ptr %5, align 4, !tbaa !11
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %31, i32 1
  store ptr %32, ptr %3, align 8, !tbaa !5
  %33 = load i64, ptr %4, align 8, !tbaa !9
  %34 = add i64 %33, -1
  store i64 %34, ptr %4, align 8, !tbaa !9
  br label %22, !llvm.loop !16

35:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %36

36:                                               ; preds = %35, %19
  ret void
}

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

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


; 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
declare dso_local i32 @rand32() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @randombytes(ptr nocapture noundef writeonly %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = icmp ugt i64 %1, 3
  br i1 %3, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.01014 = phi i64 [ %6, %.lr.ph ], [ %1, %2 ]
  %.01113 = phi ptr [ %5, %.lr.ph ], [ %0, %2 ]
  %4 = tail call i32 @rand32()
  store i32 %4, ptr %.01113, align 4, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %.01113, i64 4
  %6 = add i64 %.01014, -4
  %7 = icmp ugt i64 %6, 3
  br i1 %7, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %2
  %.011.lcssa = phi ptr [ %0, %2 ], [ %5, %.lr.ph ]
  %.010.lcssa = phi i64 [ %1, %2 ], [ %6, %.lr.ph ]
  %8 = icmp eq i64 %.010.lcssa, 0
  br i1 %8, label %.loopexit, label %.prol.preheader

.prol.preheader:                                  ; preds = %._crit_edge
  %9 = tail call i32 @rand32()
  br label %10

10:                                               ; preds = %10, %.prol.preheader
  %.018.prol = phi i32 [ %9, %.prol.preheader ], [ %12, %10 ]
  %.117.prol = phi i64 [ %.010.lcssa, %.prol.preheader ], [ %14, %10 ]
  %.11216.prol = phi ptr [ %.011.lcssa, %.prol.preheader ], [ %13, %10 ]
  %prol.iter = phi i64 [ 0, %.prol.preheader ], [ %prol.iter.next, %10 ]
  %11 = trunc i32 %.018.prol to i8
  store i8 %11, ptr %.11216.prol, align 1, !tbaa !11
  %12 = lshr i32 %.018.prol, 8
  %13 = getelementptr inbounds i8, ptr %.11216.prol, i64 1
  %14 = add nsw i64 %.117.prol, -1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %.010.lcssa
  br i1 %prol.iter.cmp.not, label %.prol.loopexit, label %10, !llvm.loop !12

.prol.loopexit:                                   ; preds = %10
  %15 = icmp ult i64 %.010.lcssa, 8
  br i1 %15, label %.loopexit, label %.new

.new:                                             ; preds = %.prol.loopexit, %.new
  %.018 = phi i32 [ 0, %.new ], [ %12, %.prol.loopexit ]
  %.117 = phi i64 [ %31, %.new ], [ %14, %.prol.loopexit ]
  %.11216 = phi ptr [ %30, %.new ], [ %13, %.prol.loopexit ]
  %16 = trunc i32 %.018 to i8
  store i8 %16, ptr %.11216, align 1, !tbaa !11
  %17 = lshr i32 %.018, 8
  %18 = getelementptr inbounds i8, ptr %.11216, i64 1
  %19 = trunc i32 %17 to i8
  store i8 %19, ptr %18, align 1, !tbaa !11
  %20 = lshr i32 %.018, 16
  %21 = getelementptr inbounds i8, ptr %.11216, i64 2
  %22 = trunc i32 %20 to i8
  store i8 %22, ptr %21, align 1, !tbaa !11
  %23 = lshr i32 %.018, 24
  %24 = getelementptr inbounds i8, ptr %.11216, i64 3
  %25 = trunc nuw i32 %23 to i8
  store i8 %25, ptr %24, align 1, !tbaa !11
  %26 = getelementptr inbounds i8, ptr %.11216, i64 4
  store i8 0, ptr %26, align 1, !tbaa !11
  %27 = getelementptr inbounds i8, ptr %.11216, i64 5
  store i8 0, ptr %27, align 1, !tbaa !11
  %28 = getelementptr inbounds i8, ptr %.11216, i64 6
  store i8 0, ptr %28, align 1, !tbaa !11
  %29 = getelementptr inbounds i8, ptr %.11216, i64 7
  store i8 0, ptr %29, align 1, !tbaa !11
  %30 = getelementptr inbounds i8, ptr %.11216, i64 8
  %31 = add nsw i64 %.117, -8
  %.not.7 = icmp eq i64 %31, 0
  br i1 %.not.7, label %.loopexit, label %.new, !llvm.loop !14

.loopexit:                                        ; preds = %.prol.loopexit, %.new, %._crit_edge
  ret void
}

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" }

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.unroll.disable"}
!14 = distinct !{!14, !10}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass