; LLVM IR from microsoft/vOW4SIKE_on_HW :: SIKE_vOW_hw-sw/ref_c/random (random)

; 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.AES256_CTR_DRBG_struct = type { [32 x i8], [16 x i8], i32 }

@DRBG_ctx = external dso_local global %struct.AES256_CTR_DRBG_struct, align 4

; 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: nounwind uwtable
declare dso_local void @AES256_ECB(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @AES256_CTR_DRBG_Update(ptr noundef, ptr noundef, ptr noundef) #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: nounwind uwtable
define dso_local void @randombytes(ptr noundef %0, i64 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca [16 x i8], align 16
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !11
  br label %9

9:                                                ; preds = %60, %2
  %10 = load i64, ptr %4, align 8, !tbaa !9
  %11 = icmp ugt i64 %10, 0
  br i1 %11, label %12, label %61

12:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 15, ptr %7, align 4, !tbaa !11
  br label %13

13:                                               ; preds = %35, %12
  %14 = load i32, ptr %7, align 4, !tbaa !11
  %15 = icmp sge i32 %14, 0
  br i1 %15, label %17, label %16

16:                                               ; preds = %13
  store i32 4, ptr %8, align 4
  br label %38

17:                                               ; preds = %13
  %18 = load i32, ptr %7, align 4, !tbaa !11
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [16 x i8], ptr getelementptr inbounds (%struct.AES256_CTR_DRBG_struct, ptr @DRBG_ctx, i32 0, i32 1), i64 0, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !13
  %22 = zext i8 %21 to i32
  %23 = icmp eq i32 %22, 255
  br i1 %23, label %24, label %28

24:                                               ; preds = %17
  %25 = load i32, ptr %7, align 4, !tbaa !11
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [16 x i8], ptr getelementptr inbounds (%struct.AES256_CTR_DRBG_struct, ptr @DRBG_ctx, i32 0, i32 1), i64 0, i64 %26
  store i8 0, ptr %27, align 1, !tbaa !13
  br label %34

28:                                               ; preds = %17
  %29 = load i32, ptr %7, align 4, !tbaa !11
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [16 x i8], ptr getelementptr inbounds (%struct.AES256_CTR_DRBG_struct, ptr @DRBG_ctx, i32 0, i32 1), i64 0, i64 %30
  %32 = load i8, ptr %31, align 1, !tbaa !13
  %33 = add i8 %32, 1
  store i8 %33, ptr %31, align 1, !tbaa !13
  store i32 4, ptr %8, align 4
  br label %38

34:                                               ; preds = %24
  br label %35

35:                                               ; preds = %34
  %36 = load i32, ptr %7, align 4, !tbaa !11
  %37 = add nsw i32 %36, -1
  store i32 %37, ptr %7, align 4, !tbaa !11
  br label %13, !llvm.loop !14

38:                                               ; preds = %28, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %39

39:                                               ; preds = %38
  %40 = getelementptr inbounds [16 x i8], ptr %5, i64 0, i64 0
  call void @AES256_ECB(ptr noundef @DRBG_ctx, ptr noundef getelementptr inbounds (%struct.AES256_CTR_DRBG_struct, ptr @DRBG_ctx, i32 0, i32 1), ptr noundef %40)
  %41 = load i64, ptr %4, align 8, !tbaa !9
  %42 = icmp ugt i64 %41, 15
  br i1 %42, label %43, label %53

43:                                               ; preds = %39
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = load i32, ptr %6, align 4, !tbaa !11
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i8, ptr %44, i64 %46
  %48 = getelementptr inbounds [16 x i8], ptr %5, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %47, ptr align 16 %48, i64 16, i1 false)
  %49 = load i32, ptr %6, align 4, !tbaa !11
  %50 = add nsw i32 %49, 16
  store i32 %50, ptr %6, align 4, !tbaa !11
  %51 = load i64, ptr %4, align 8, !tbaa !9
  %52 = sub i64 %51, 16
  store i64 %52, ptr %4, align 8, !tbaa !9
  br label %60

53:                                               ; preds = %39
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = load i32, ptr %6, align 4, !tbaa !11
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds i8, ptr %54, i64 %56
  %58 = getelementptr inbounds [16 x i8], ptr %5, i64 0, i64 0
  %59 = load i64, ptr %4, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %57, ptr align 16 %58, i64 %59, i1 false)
  store i64 0, ptr %4, align 8, !tbaa !9
  br label %60

60:                                               ; preds = %53, %43
  br label %9, !llvm.loop !16

61:                                               ; preds = %9
  call void @AES256_CTR_DRBG_Update(ptr noundef null, ptr noundef @DRBG_ctx, ptr noundef getelementptr inbounds (%struct.AES256_CTR_DRBG_struct, ptr @DRBG_ctx, i32 0, i32 1))
  %62 = load i32, ptr getelementptr inbounds (%struct.AES256_CTR_DRBG_struct, ptr @DRBG_ctx, i32 0, i32 2), align 4, !tbaa !17
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr getelementptr inbounds (%struct.AES256_CTR_DRBG_struct, ptr @DRBG_ctx, i32 0, i32 2), align 4, !tbaa !17
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %5) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = !{!18, !12, i64 48}
!18 = !{!"", !7, i64 0, !7, i64 32, !12, i64 48}


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

%struct.AES256_CTR_DRBG_struct = type { [32 x i8], [16 x i8], i32 }

@DRBG_ctx = external dso_local global %struct.AES256_CTR_DRBG_struct, align 4

; 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: nounwind uwtable
declare dso_local void @AES256_ECB(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @AES256_CTR_DRBG_Update(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #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: nounwind uwtable
define dso_local void @randombytes(ptr nocapture noundef writeonly %0, i64 noundef %1) local_unnamed_addr #1 {
  %3 = alloca [16 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #3
  %.not18 = icmp eq i64 %1, 0
  br i1 %.not18, label %._crit_edge, label %.preheader

.preheader:                                       ; preds = %2, %56
  %.01320 = phi i32 [ %57, %56 ], [ 0, %2 ]
  %.01419 = phi i64 [ %58, %56 ], [ %1, %2 ]
  %4 = load i8, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 47), align 1, !tbaa !5
  %5 = icmp eq i8 %4, -1
  br i1 %5, label %6, label %51

6:                                                ; preds = %.preheader
  store i8 0, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 47), align 1, !tbaa !5
  %7 = load i8, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 46), align 2, !tbaa !5
  %8 = icmp eq i8 %7, -1
  br i1 %8, label %9, label %51

9:                                                ; preds = %6
  store i8 0, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 46), align 2, !tbaa !5
  %10 = load i8, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 45), align 1, !tbaa !5
  %11 = icmp eq i8 %10, -1
  br i1 %11, label %12, label %51

12:                                               ; preds = %9
  store i8 0, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 45), align 1, !tbaa !5
  %13 = load i8, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 44), align 4, !tbaa !5
  %14 = icmp eq i8 %13, -1
  br i1 %14, label %15, label %51

15:                                               ; preds = %12
  store i8 0, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 44), align 4, !tbaa !5
  %16 = load i8, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 43), align 1, !tbaa !5
  %17 = icmp eq i8 %16, -1
  br i1 %17, label %18, label %51

18:                                               ; preds = %15
  store i8 0, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 43), align 1, !tbaa !5
  %19 = load i8, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 42), align 2, !tbaa !5
  %20 = icmp eq i8 %19, -1
  br i1 %20, label %21, label %51

21:                                               ; preds = %18
  store i8 0, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 42), align 2, !tbaa !5
  %22 = load i8, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 41), align 1, !tbaa !5
  %23 = icmp eq i8 %22, -1
  br i1 %23, label %24, label %51

24:                                               ; preds = %21
  store i8 0, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 41), align 1, !tbaa !5
  %25 = load i8, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 40), align 4, !tbaa !5
  %26 = icmp eq i8 %25, -1
  br i1 %26, label %27, label %51

27:                                               ; preds = %24
  store i8 0, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 40), align 4, !tbaa !5
  %28 = load i8, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 39), align 1, !tbaa !5
  %29 = icmp eq i8 %28, -1
  br i1 %29, label %30, label %51

30:                                               ; preds = %27
  store i8 0, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 39), align 1, !tbaa !5
  %31 = load i8, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 38), align 2, !tbaa !5
  %32 = icmp eq i8 %31, -1
  br i1 %32, label %33, label %51

33:                                               ; preds = %30
  store i8 0, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 38), align 2, !tbaa !5
  %34 = load i8, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 37), align 1, !tbaa !5
  %35 = icmp eq i8 %34, -1
  br i1 %35, label %36, label %51

36:                                               ; preds = %33
  store i8 0, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 37), align 1, !tbaa !5
  %37 = load i8, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 36), align 4, !tbaa !5
  %38 = icmp eq i8 %37, -1
  br i1 %38, label %39, label %51

39:                                               ; preds = %36
  store i8 0, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 36), align 4, !tbaa !5
  %40 = load i8, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 35), align 1, !tbaa !5
  %41 = icmp eq i8 %40, -1
  br i1 %41, label %42, label %51

42:                                               ; preds = %39
  store i8 0, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 35), align 1, !tbaa !5
  %43 = load i8, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 34), align 2, !tbaa !5
  %44 = icmp eq i8 %43, -1
  br i1 %44, label %45, label %51

45:                                               ; preds = %42
  store i8 0, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 34), align 2, !tbaa !5
  %46 = load i8, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 33), align 1, !tbaa !5
  %47 = icmp eq i8 %46, -1
  br i1 %47, label %48, label %51

48:                                               ; preds = %45
  store i8 0, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 33), align 1, !tbaa !5
  %49 = load i8, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 32), align 4, !tbaa !5
  %50 = icmp eq i8 %49, -1
  br i1 %50, label %.loopexit.loopexit, label %51

.loopexit.loopexit:                               ; preds = %48
  store i8 0, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 32), align 4, !tbaa !5
  br label %.loopexit

51:                                               ; preds = %48, %45, %42, %39, %36, %33, %30, %27, %24, %21, %18, %15, %12, %9, %6, %.preheader
  %.lcssa22 = phi ptr [ getelementptr inbounds (i8, ptr @DRBG_ctx, i64 47), %.preheader ], [ getelementptr inbounds (i8, ptr @DRBG_ctx, i64 46), %6 ], [ getelementptr inbounds (i8, ptr @DRBG_ctx, i64 45), %9 ], [ getelementptr inbounds (i8, ptr @DRBG_ctx, i64 44), %12 ], [ getelementptr inbounds (i8, ptr @DRBG_ctx, i64 43), %15 ], [ getelementptr inbounds (i8, ptr @DRBG_ctx, i64 42), %18 ], [ getelementptr inbounds (i8, ptr @DRBG_ctx, i64 41), %21 ], [ getelementptr inbounds (i8, ptr @DRBG_ctx, i64 40), %24 ], [ getelementptr inbounds (i8, ptr @DRBG_ctx, i64 39), %27 ], [ getelementptr inbounds (i8, ptr @DRBG_ctx, i64 38), %30 ], [ getelementptr inbounds (i8, ptr @DRBG_ctx, i64 37), %33 ], [ getelementptr inbounds (i8, ptr @DRBG_ctx, i64 36), %36 ], [ getelementptr inbounds (i8, ptr @DRBG_ctx, i64 35), %39 ], [ getelementptr inbounds (i8, ptr @DRBG_ctx, i64 34), %42 ], [ getelementptr inbounds (i8, ptr @DRBG_ctx, i64 33), %45 ], [ getelementptr inbounds (i8, ptr @DRBG_ctx, i64 32), %48 ]
  %.lcssa = phi i8 [ %4, %.preheader ], [ %7, %6 ], [ %10, %9 ], [ %13, %12 ], [ %16, %15 ], [ %19, %18 ], [ %22, %21 ], [ %25, %24 ], [ %28, %27 ], [ %31, %30 ], [ %34, %33 ], [ %37, %36 ], [ %40, %39 ], [ %43, %42 ], [ %46, %45 ], [ %49, %48 ]
  %52 = add nuw i8 %.lcssa, 1
  store i8 %52, ptr %.lcssa22, align 1, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit, %51
  call void @AES256_ECB(ptr noundef nonnull @DRBG_ctx, ptr noundef nonnull getelementptr inbounds (i8, ptr @DRBG_ctx, i64 32), ptr noundef nonnull %3)
  %53 = icmp ugt i64 %.01419, 15
  %54 = zext nneg i32 %.01320 to i64
  %55 = getelementptr inbounds i8, ptr %0, i64 %54
  br i1 %53, label %56, label %.thread

.thread:                                          ; preds = %.loopexit
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(1) %55, ptr noundef nonnull align 16 dereferenceable(1) %3, i64 %.01419, i1 false)
  br label %._crit_edge

56:                                               ; preds = %.loopexit
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(16) %55, ptr noundef nonnull align 16 dereferenceable(16) %3, i64 16, i1 false)
  %57 = add nuw nsw i32 %.01320, 16
  %58 = add i64 %.01419, -16
  %.not = icmp eq i64 %58, 0
  br i1 %.not, label %._crit_edge, label %.preheader, !llvm.loop !8

._crit_edge:                                      ; preds = %56, %.thread, %2
  call void @AES256_CTR_DRBG_Update(ptr noundef null, ptr noundef nonnull @DRBG_ctx, ptr noundef nonnull getelementptr inbounds (i8, ptr @DRBG_ctx, i64 32))
  %59 = load i32, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 48), align 4, !tbaa !10
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr getelementptr inbounds (i8, ptr @DRBG_ctx, i64 48), align 4, !tbaa !10
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #3
  ret void
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!11, !12, i64 48}
!11 = !{!"", !6, i64 0, !6, i64 32, !12, i64 48}
!12 = !{!"int", !6, i64 0}


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