; 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_key = external hidden global i32, align 4
@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

declare zeroext i1 @s2n_is_initialized() #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @s2n_rand_cleanup_thread() #3 {
  %1 = alloca %struct.s2n_result, align 4
  %2 = alloca %struct.s2n_result, align 4
  %3 = alloca %struct.s2n_result, align 4
  br label %4

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

12:                                               ; preds = %4
  %13 = getelementptr inbounds %struct.s2n_result, ptr %1, i32 0, i32 0
  store i32 -1, ptr %13, align 4, !tbaa !5
  br label %36

14:                                               ; preds = %4
  br label %15

15:                                               ; preds = %14
  br label %16

16:                                               ; preds = %15
  %17 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @s2n_per_thread_rand_state)
  %18 = getelementptr inbounds %struct.s2n_rand_state, ptr %17, i32 0, i32 1
  %19 = call i32 @s2n_drbg_wipe(ptr noundef %18)
  %20 = getelementptr inbounds %struct.s2n_result, ptr %3, i32 0, i32 0
  store i32 %19, ptr %20, align 4
  %21 = getelementptr inbounds %struct.s2n_result, ptr %3, i32 0, i32 0
  %22 = load i32, ptr %21, align 4
  %23 = call zeroext i1 @s2n_result_is_ok(i32 %22)
  br i1 %23, label %26, label %24

24:                                               ; preds = %16
  %25 = getelementptr inbounds %struct.s2n_result, ptr %1, i32 0, i32 0
  store i32 -1, ptr %25, align 4, !tbaa !5
  br label %36

26:                                               ; preds = %16
  br label %27

27:                                               ; preds = %26
  %28 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @s2n_per_thread_rand_state)
  %29 = getelementptr inbounds %struct.s2n_rand_state, ptr %28, i32 0, i32 3
  store i8 0, ptr %29, align 8, !tbaa !10
  %30 = call zeroext i1 @s2n_is_initialized()
  br i1 %30, label %31, label %34

31:                                               ; preds = %27
  %32 = load i32, ptr @s2n_per_thread_rand_state_key, align 4, !tbaa !16
  %33 = call i32 @pthread_setspecific(i32 noundef %32, ptr noundef null) #5
  br label %34

34:                                               ; preds = %31, %27
  %35 = getelementptr inbounds %struct.s2n_result, ptr %1, i32 0, i32 0
  store i32 0, ptr %35, align 4, !tbaa !5
  br label %36

36:                                               ; preds = %34, %24, %12
  %37 = getelementptr inbounds %struct.s2n_result, ptr %1, i32 0, i32 0
  %38 = load i32, ptr %37, align 4
  ret i32 %38
}

declare i32 @s2n_drbg_wipe(ptr noundef) #1

; Function Attrs: nounwind
declare i32 @pthread_setspecific(i32 noundef, ptr noundef) #4

attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { "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 = { 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 = { 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 "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, !7, i64 0}
!6 = !{!"", !7, i64 0}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !15, i64 88}
!11 = !{!"s2n_rand_state", !12, i64 0, !13, i64 8, !13, i64 48, !15, i64 88}
!12 = !{!"long", !8, i64 0}
!13 = !{!"s2n_drbg", !12, i64 0, !14, i64 8, !8, i64 16, !12, i64 32}
!14 = !{!"any pointer", !8, i64 0}
!15 = !{!"_Bool", !8, i64 0}
!16 = !{!7, !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_key = external hidden local_unnamed_addr global i32, align 4
@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

declare zeroext i1 @s2n_is_initialized() local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @s2n_rand_cleanup_thread() local_unnamed_addr #3 {
  %1 = tail call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @s2n_per_thread_rand_state)
  %2 = getelementptr inbounds i8, ptr %1, i64 48
  %3 = tail call i32 @s2n_drbg_wipe(ptr noundef nonnull %2) #5
  %4 = tail call zeroext i1 @s2n_result_is_ok(i32 %3)
  br i1 %4, label %5, label %15

5:                                                ; preds = %0
  %6 = getelementptr inbounds i8, ptr %1, i64 8
  %7 = tail call i32 @s2n_drbg_wipe(ptr noundef nonnull %6) #5
  %8 = tail call zeroext i1 @s2n_result_is_ok(i32 %7)
  br i1 %8, label %9, label %15

9:                                                ; preds = %5
  %10 = getelementptr inbounds i8, ptr %1, i64 88
  store i8 0, ptr %10, align 8, !tbaa !5
  %11 = tail call zeroext i1 @s2n_is_initialized() #5
  br i1 %11, label %12, label %15

12:                                               ; preds = %9
  %13 = load i32, ptr @s2n_per_thread_rand_state_key, align 4, !tbaa !13
  %14 = tail call i32 @pthread_setspecific(i32 noundef %13, ptr noundef null) #5
  br label %15

15:                                               ; preds = %9, %12, %5, %0
  %.sroa.02.0 = phi i32 [ -1, %0 ], [ -1, %5 ], [ 0, %12 ], [ 0, %9 ]
  ret i32 %.sroa.02.0
}

declare i32 @s2n_drbg_wipe(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind
declare i32 @pthread_setspecific(i32 noundef, ptr noundef) local_unnamed_addr #4

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { "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 = { 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 = { 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 "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, !12, i64 88}
!6 = !{!"s2n_rand_state", !7, i64 0, !10, i64 8, !10, i64 48, !12, i64 88}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"s2n_drbg", !7, i64 0, !11, i64 8, !8, i64 16, !7, i64 32}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!"_Bool", !8, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !8, i64 0}


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