; LLVM IR from aws/s2n-tls :: utils (s2n_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.s2n_rand_state = type { i64, %struct.s2n_drbg, %struct.s2n_drbg, i8 }
%struct.s2n_drbg = type { i64, ptr, [16 x i8], i64 }
%struct.s2n_result = type { i32 }

@s2n_per_thread_rand_state = external hidden thread_local global %struct.s2n_rand_state, align 8

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @s2n_result_is_ok(i32) #1

declare i32 @s2n_drbg_bytes_used(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @s2n_get_private_random_bytes_used(ptr noundef %0) #3 {
  %2 = alloca %struct.s2n_result, align 4
  %3 = alloca ptr, align 8
  %4 = alloca %struct.s2n_result, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  br label %5

5:                                                ; preds = %1
  %6 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @s2n_per_thread_rand_state)
  %7 = getelementptr inbounds %struct.s2n_rand_state, ptr %6, i32 0, i32 2
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = call i32 @s2n_drbg_bytes_used(ptr noundef %7, ptr noundef %8)
  %10 = getelementptr inbounds %struct.s2n_result, ptr %4, i32 0, i32 0
  store i32 %9, ptr %10, align 4
  %11 = getelementptr inbounds %struct.s2n_result, ptr %4, i32 0, i32 0
  %12 = load i32, ptr %11, align 4
  %13 = call zeroext i1 @s2n_result_is_ok(i32 %12)
  br i1 %13, label %16, label %14

14:                                               ; preds = %5
  %15 = getelementptr inbounds %struct.s2n_result, ptr %2, i32 0, i32 0
  store i32 -1, ptr %15, align 4, !tbaa !9
  br label %19

16:                                               ; preds = %5
  br label %17

17:                                               ; preds = %16
  %18 = getelementptr inbounds %struct.s2n_result, ptr %2, i32 0, i32 0
  store i32 0, ptr %18, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %17, %14
  %20 = getelementptr inbounds %struct.s2n_result, ptr %2, i32 0, i32 0
  %21 = load i32, ptr %20, align 4
  ret i32 %21
}

attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"", !11, i64 0}
!11 = !{!"int", !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"

%struct.s2n_rand_state = type { i64, %struct.s2n_drbg, %struct.s2n_drbg, i8 }
%struct.s2n_drbg = type { i64, ptr, [16 x i8], i64 }

@s2n_per_thread_rand_state = external hidden thread_local global %struct.s2n_rand_state, align 8

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @s2n_result_is_ok(i32) local_unnamed_addr #1

declare i32 @s2n_drbg_bytes_used(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @s2n_get_private_random_bytes_used(ptr noundef %0) local_unnamed_addr #3 {
  %2 = tail call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @s2n_per_thread_rand_state)
  %3 = getelementptr inbounds i8, ptr %2, i64 48
  %4 = tail call i32 @s2n_drbg_bytes_used(ptr noundef nonnull %3, ptr noundef %0) #4
  %5 = tail call zeroext i1 @s2n_result_is_ok(i32 %4)
  %not. = xor i1 %5, true
  %. = sext i1 %not. to i32
  ret i32 %.
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = { 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)"}


; Effective O3 passes: GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass