; LLVM IR from specinfo-ua/UAPKI :: library/uapkic/src (jitterentropy)

; 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.rand_data = type { ptr, ptr, i64, i64, i64, i32, ptr, i32, i32, i32, i32, i32, i32, i32, i32, i8, i8, i64, i64, i64 }

; Function Attrs: nounwind uwtable
define dso_local i32 @jent_read_entropy(ptr noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %12, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store i32 0, ptr %9, align 4, !tbaa !11
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = icmp eq ptr null, %13
  br i1 %14, label %15, label %16

15:                                               ; preds = %3
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %64

16:                                               ; preds = %3
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = call i32 @jent_notime_settick(ptr noundef %17)
  %19 = icmp ne i32 %18, 0
  br i1 %19, label %20, label %21

20:                                               ; preds = %16
  store i32 -4, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %64

21:                                               ; preds = %16
  br label %22

22:                                               ; preds = %58, %21
  %23 = load i64, ptr %7, align 8, !tbaa !9
  %24 = icmp ugt i64 %23, 0
  br i1 %24, label %25, label %59

25:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  call void @jent_random_data(ptr noundef %26)
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = call i32 @jent_health_failure(ptr noundef %27)
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %37

30:                                               ; preds = %25
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = call i32 @jent_rct_failure(ptr noundef %31)
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %35

34:                                               ; preds = %30
  store i32 -2, ptr %9, align 4, !tbaa !11
  br label %36

35:                                               ; preds = %30
  store i32 -3, ptr %9, align 4, !tbaa !11
  br label %36

36:                                               ; preds = %35, %34
  store i32 4, ptr %10, align 4
  br label %56

37:                                               ; preds = %25
  %38 = load i64, ptr %7, align 8, !tbaa !9
  %39 = icmp ult i64 32, %38
  br i1 %39, label %40, label %41

40:                                               ; preds = %37
  store i64 32, ptr %11, align 8, !tbaa !9
  br label %43

41:                                               ; preds = %37
  %42 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %42, ptr %11, align 8, !tbaa !9
  br label %43

43:                                               ; preds = %41, %40
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.rand_data, ptr %45, i32 0, i32 1
  %47 = load ptr, ptr %46, align 8, !tbaa !13
  %48 = call ptr @ba_get_buf_const(ptr noundef %47)
  %49 = load i64, ptr %11, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %44, ptr align 1 %48, i64 %49, i1 false)
  %50 = load i64, ptr %11, align 8, !tbaa !9
  %51 = load i64, ptr %7, align 8, !tbaa !9
  %52 = sub i64 %51, %50
  store i64 %52, ptr %7, align 8, !tbaa !9
  %53 = load i64, ptr %11, align 8, !tbaa !9
  %54 = load ptr, ptr %8, align 8, !tbaa !5
  %55 = getelementptr inbounds i8, ptr %54, i64 %53
  store ptr %55, ptr %8, align 8, !tbaa !5
  store i32 0, ptr %10, align 4
  br label %56

56:                                               ; preds = %43, %36
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  %57 = load i32, ptr %10, align 4
  switch i32 %57, label %64 [
    i32 0, label %58
    i32 4, label %61
  ]

58:                                               ; preds = %56
  br label %22, !llvm.loop !15

59:                                               ; preds = %22
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  call void @jent_random_data(ptr noundef %60)
  br label %61

61:                                               ; preds = %59, %56
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  call void @jent_notime_unsettick(ptr noundef %62)
  %63 = load i32, ptr %9, align 4, !tbaa !11
  store i32 %63, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %64

64:                                               ; preds = %61, %56, %20, %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %65 = load i32, ptr %4, align 4
  ret i32 %65
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @jent_notime_settick(ptr noundef) #2

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

; Function Attrs: nounwind uwtable
declare hidden i32 @jent_health_failure(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden i32 @jent_rct_failure(ptr noundef) #0

declare ptr @ba_get_buf_const(ptr noundef) #3

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @jent_notime_unsettick(ptr noundef) #2

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 = { inlinehint 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 #3 = { "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 nounwind willreturn memory(argmem: readwrite) }
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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 8}
!14 = !{!"rand_data", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !12, i64 40, !6, i64 48, !12, i64 56, !12, i64 60, !12, i64 64, !12, i64 68, !12, i64 72, !12, i64 76, !12, i64 80, !12, i64 84, !12, i64 88, !12, i64 88, !12, i64 88, !12, i64 88, !7, i64 89, !10, i64 96, !10, i64 104, !10, i64 112}
!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: nounwind uwtable
define dso_local i32 @jent_read_entropy(ptr noundef %0, ptr nocapture noundef writeonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq ptr %0, null
  br i1 %4, label %17, label %5

5:                                                ; preds = %3
  %6 = tail call i32 @jent_notime_settick(ptr noundef nonnull %0)
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %.preheader, label %17

.preheader:                                       ; preds = %5
  %.not2837 = icmp eq i64 %2, 0
  br i1 %.not2837, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  br label %8

8:                                                ; preds = %.lr.ph, %11
  %.02239 = phi ptr [ %1, %.lr.ph ], [ %15, %11 ]
  %.02438 = phi i64 [ %2, %.lr.ph ], [ %14, %11 ]
  tail call void @jent_random_data(ptr noundef nonnull %0)
  %9 = tail call i32 @jent_health_failure(ptr noundef nonnull %0)
  %.not29 = icmp eq i32 %9, 0
  br i1 %.not29, label %11, label %.thread

.thread:                                          ; preds = %8
  %10 = tail call i32 @jent_rct_failure(ptr noundef nonnull %0)
  %.not30 = icmp eq i32 %10, 0
  %. = select i1 %.not30, i32 -3, i32 -2
  br label %16

11:                                               ; preds = %8
  %..024 = tail call i64 @llvm.umin.i64(i64 %.02438, i64 32)
  %12 = load ptr, ptr %7, align 8, !tbaa !5
  %13 = tail call ptr @ba_get_buf_const(ptr noundef %12) #5
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.02239, ptr align 1 %13, i64 %..024, i1 false)
  %14 = sub i64 %.02438, %..024
  %15 = getelementptr inbounds i8, ptr %.02239, i64 %..024
  %.not28 = icmp eq i64 %14, 0
  br i1 %.not28, label %._crit_edge, label %8

._crit_edge:                                      ; preds = %11, %.preheader
  tail call void @jent_random_data(ptr noundef nonnull %0)
  br label %16

16:                                               ; preds = %.thread, %._crit_edge
  %.3 = phi i32 [ 0, %._crit_edge ], [ %., %.thread ]
  tail call void @jent_notime_unsettick(ptr noundef nonnull %0)
  br label %17

17:                                               ; preds = %5, %3, %16
  %.026 = phi i32 [ %.3, %16 ], [ -1, %3 ], [ -4, %5 ]
  ret i32 %.026
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @jent_notime_settick(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
declare hidden i32 @jent_health_failure(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden i32 @jent_rct_failure(ptr noundef) local_unnamed_addr #0

declare ptr @ba_get_buf_const(ptr noundef) local_unnamed_addr #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: inlinehint nounwind uwtable
declare hidden void @jent_notime_unsettick(ptr noundef) local_unnamed_addr #1

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

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 = { inlinehint 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 = { "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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !7, i64 8}
!6 = !{!"rand_data", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !11, i64 40, !7, i64 48, !11, i64 56, !11, i64 60, !11, i64 64, !11, i64 68, !11, i64 72, !11, i64 76, !11, i64 80, !11, i64 84, !11, i64 88, !11, i64 88, !11, i64 88, !11, i64 88, !8, i64 89, !10, i64 96, !10, i64 104, !10, i64 112}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"int", !8, i64 0}


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