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

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

%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%struct.ByteArray_st = type { ptr, i64 }

@.str = external hidden unnamed_addr constant [14 x i8], align 1
@drbg_mutex = external hidden global %union.pthread_mutex_t, align 8
@drbg_Key = external hidden global ptr, align 8
@drbg_V = external hidden global ptr, align 8
@drbg_hmac_ctx = external hidden global ptr, align 8
@drbg_self_test.test_drbg_expected_bits = external hidden constant [256 x i8], align 16
@drbg_self_test.ba_test_drbg_init_entropy = external hidden constant %struct.ByteArray_st, align 8
@drbg_self_test.ba_test_reseed_entropy = external hidden constant %struct.ByteArray_st, align 8

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

declare void @stacktrace_add(ptr noundef, i64 noundef, i32 noundef) #1

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

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

; Function Attrs: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) #3

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

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr noundef) #3

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

; Function Attrs: nounwind uwtable
define dso_local i32 @drbg_self_test() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  store ptr null, ptr %3, align 8, !tbaa !9
  %5 = load ptr, ptr @drbg_Key, align 8, !tbaa !9
  %6 = icmp ne ptr %5, null
  br i1 %6, label %13, label %7

7:                                                ; preds = %0
  %8 = load ptr, ptr @drbg_V, align 8, !tbaa !9
  %9 = icmp ne ptr %8, null
  br i1 %9, label %13, label %10

10:                                               ; preds = %7
  %11 = load ptr, ptr @drbg_hmac_ctx, align 8, !tbaa !9
  %12 = icmp ne ptr %11, null
  br i1 %12, label %13, label %14

13:                                               ; preds = %10, %7, %0
  store i32 32, ptr %1, align 4
  store i32 1, ptr %4, align 4
  br label %77

14:                                               ; preds = %10
  %15 = call i32 @pthread_mutex_lock(ptr noundef @drbg_mutex) #5
  br label %16

16:                                               ; preds = %14
  %17 = call i32 @drbg_init_internal(ptr noundef @drbg_self_test.ba_test_drbg_init_entropy)
  store i32 %17, ptr %2, align 4, !tbaa !5
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = icmp ne i32 %18, 0
  br i1 %19, label %20, label %22

20:                                               ; preds = %16
  %21 = load i32, ptr %2, align 4, !tbaa !5
  call void @stacktrace_add(ptr noundef @.str, i64 noundef 287, i32 noundef %21)
  br label %73

22:                                               ; preds = %16
  br label %23

23:                                               ; preds = %22
  br label %24

24:                                               ; preds = %23
  br label %25

25:                                               ; preds = %24
  %26 = call i32 @drbg_reseed_internal(ptr noundef @drbg_self_test.ba_test_reseed_entropy)
  store i32 %26, ptr %2, align 4, !tbaa !5
  %27 = load i32, ptr %2, align 4, !tbaa !5
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %31

29:                                               ; preds = %25
  %30 = load i32, ptr %2, align 4, !tbaa !5
  call void @stacktrace_add(ptr noundef @.str, i64 noundef 288, i32 noundef %30)
  br label %73

31:                                               ; preds = %25
  br label %32

32:                                               ; preds = %31
  br label %33

33:                                               ; preds = %32
  br label %34

34:                                               ; preds = %33
  %35 = call ptr @ba_alloc_by_len(i64 noundef 256)
  store ptr %35, ptr %3, align 8, !tbaa !9
  %36 = icmp eq ptr null, %35
  br i1 %36, label %37, label %39

37:                                               ; preds = %34
  store i32 2, ptr %2, align 4, !tbaa !5
  %38 = load i32, ptr %2, align 4, !tbaa !5
  call void @stacktrace_add(ptr noundef @.str, i64 noundef 290, i32 noundef %38)
  br label %73

39:                                               ; preds = %34
  br label %40

40:                                               ; preds = %39
  br label %41

41:                                               ; preds = %40
  br label %42

42:                                               ; preds = %41
  %43 = load ptr, ptr %3, align 8, !tbaa !9
  %44 = call i32 @drbg_random_internal(ptr noundef %43)
  store i32 %44, ptr %2, align 4, !tbaa !5
  %45 = load i32, ptr %2, align 4, !tbaa !5
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %47, label %49

47:                                               ; preds = %42
  %48 = load i32, ptr %2, align 4, !tbaa !5
  call void @stacktrace_add(ptr noundef @.str, i64 noundef 292, i32 noundef %48)
  br label %73

49:                                               ; preds = %42
  br label %50

50:                                               ; preds = %49
  br label %51

51:                                               ; preds = %50
  br label %52

52:                                               ; preds = %51
  %53 = load ptr, ptr %3, align 8, !tbaa !9
  %54 = call i32 @drbg_random_internal(ptr noundef %53)
  store i32 %54, ptr %2, align 4, !tbaa !5
  %55 = load i32, ptr %2, align 4, !tbaa !5
  %56 = icmp ne i32 %55, 0
  br i1 %56, label %57, label %59

57:                                               ; preds = %52
  %58 = load i32, ptr %2, align 4, !tbaa !5
  call void @stacktrace_add(ptr noundef @.str, i64 noundef 293, i32 noundef %58)
  br label %73

59:                                               ; preds = %52
  br label %60

60:                                               ; preds = %59
  br label %61

61:                                               ; preds = %60
  %62 = load ptr, ptr %3, align 8, !tbaa !9
  %63 = getelementptr inbounds %struct.ByteArray_st, ptr %62, i32 0, i32 0
  %64 = load ptr, ptr %63, align 8, !tbaa !11
  %65 = call i32 @memcmp(ptr noundef %64, ptr noundef @drbg_self_test.test_drbg_expected_bits, i64 noundef 256) #6
  %66 = icmp ne i32 %65, 0
  br i1 %66, label %67, label %72

67:                                               ; preds = %61
  br label %68

68:                                               ; preds = %67
  store i32 33, ptr %2, align 4, !tbaa !5
  %69 = load i32, ptr %2, align 4, !tbaa !5
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 295, i32 noundef %69, ptr noundef null)
  br label %73

70:                                               ; No predecessors!
  br label %71

71:                                               ; preds = %70
  br label %72

72:                                               ; preds = %71, %61
  br label %73

73:                                               ; preds = %72, %68, %57, %47, %37, %29, %20
  call void @drbg_free_internal()
  %74 = call i32 @pthread_mutex_unlock(ptr noundef @drbg_mutex) #5
  %75 = load ptr, ptr %3, align 8, !tbaa !9
  call void @ba_free(ptr noundef %75)
  %76 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %76, ptr %1, align 4
  store i32 1, ptr %4, align 4
  br label %77

77:                                               ; preds = %73, %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  %78 = load i32, ptr %1, align 4
  ret i32 %78
}

declare ptr @ba_alloc_by_len(i64 noundef) #1

; Function Attrs: nounwind willreturn memory(read)
declare i32 @memcmp(ptr noundef, ptr noundef, i64 noundef) #4

declare void @stacktrace_create(ptr noundef, i64 noundef, i32 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @drbg_free_internal() #2

declare void @ba_free(ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 "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 willreturn memory(read) "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 }
attributes #6 = { nounwind willreturn memory(read) }

!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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"ByteArray_st", !10, i64 0, !13, i64 8}
!13 = !{!"long", !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"

%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%struct.ByteArray_st = type { ptr, i64 }

@.str = external hidden unnamed_addr constant [14 x i8], align 1
@drbg_mutex = external hidden global %union.pthread_mutex_t, align 8
@drbg_Key = external hidden local_unnamed_addr global ptr, align 8
@drbg_V = external hidden local_unnamed_addr global ptr, align 8
@drbg_hmac_ctx = external hidden local_unnamed_addr global ptr, align 8
@drbg_self_test.test_drbg_expected_bits = external hidden constant [256 x i8], align 16
@drbg_self_test.ba_test_drbg_init_entropy = external hidden constant %struct.ByteArray_st, align 8
@drbg_self_test.ba_test_reseed_entropy = external hidden constant %struct.ByteArray_st, align 8

declare void @stacktrace_add(ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) local_unnamed_addr #2

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

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr noundef) local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
define dso_local i32 @drbg_self_test() local_unnamed_addr #1 {
  %1 = load ptr, ptr @drbg_Key, align 8, !tbaa !5
  %2 = icmp ne ptr %1, null
  %3 = load ptr, ptr @drbg_V, align 8
  %4 = icmp ne ptr %3, null
  %or.cond = select i1 %2, i1 true, i1 %4
  %5 = load ptr, ptr @drbg_hmac_ctx, align 8
  %6 = icmp ne ptr %5, null
  %or.cond3 = select i1 %or.cond, i1 true, i1 %6
  br i1 %or.cond3, label %29, label %7

7:                                                ; preds = %0
  %8 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull @drbg_mutex) #4
  %9 = tail call i32 @drbg_init_internal(ptr noundef nonnull @drbg_self_test.ba_test_drbg_init_entropy)
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %11, label %10

10:                                               ; preds = %7
  tail call void @stacktrace_add(ptr noundef nonnull @.str, i64 noundef 287, i32 noundef %9) #4
  br label %27

11:                                               ; preds = %7
  %12 = tail call i32 @drbg_reseed_internal(ptr noundef nonnull @drbg_self_test.ba_test_reseed_entropy)
  %.not26 = icmp eq i32 %12, 0
  br i1 %.not26, label %14, label %13

13:                                               ; preds = %11
  tail call void @stacktrace_add(ptr noundef nonnull @.str, i64 noundef 288, i32 noundef %12) #4
  br label %27

14:                                               ; preds = %11
  %15 = tail call ptr @ba_alloc_by_len(i64 noundef 256) #4
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %18

17:                                               ; preds = %14
  tail call void @stacktrace_add(ptr noundef nonnull @.str, i64 noundef 290, i32 noundef 2) #4
  br label %27

18:                                               ; preds = %14
  %19 = tail call i32 @drbg_random_internal(ptr noundef nonnull %15)
  %.not27 = icmp eq i32 %19, 0
  br i1 %.not27, label %21, label %20

20:                                               ; preds = %18
  tail call void @stacktrace_add(ptr noundef nonnull @.str, i64 noundef 292, i32 noundef %19) #4
  br label %27

21:                                               ; preds = %18
  %22 = tail call i32 @drbg_random_internal(ptr noundef nonnull %15)
  %.not28 = icmp eq i32 %22, 0
  br i1 %.not28, label %24, label %23

23:                                               ; preds = %21
  tail call void @stacktrace_add(ptr noundef nonnull @.str, i64 noundef 293, i32 noundef %22) #4
  br label %27

24:                                               ; preds = %21
  %25 = load ptr, ptr %15, align 8, !tbaa !9
  %bcmp = tail call i32 @bcmp(ptr noundef nonnull dereferenceable(256) %25, ptr noundef nonnull dereferenceable(256) @drbg_self_test.test_drbg_expected_bits, i64 256)
  %.not29 = icmp eq i32 %bcmp, 0
  br i1 %.not29, label %27, label %26

26:                                               ; preds = %24
  tail call void @stacktrace_create(ptr noundef nonnull @.str, i64 noundef 295, i32 noundef 33, ptr noundef null) #4
  br label %27

27:                                               ; preds = %24, %26, %23, %20, %17, %13, %10
  %.018 = phi i32 [ %9, %10 ], [ %12, %13 ], [ 2, %17 ], [ %19, %20 ], [ %22, %23 ], [ 33, %26 ], [ 0, %24 ]
  %.0 = phi ptr [ null, %10 ], [ null, %13 ], [ null, %17 ], [ %15, %20 ], [ %15, %23 ], [ %15, %26 ], [ %15, %24 ]
  tail call void @drbg_free_internal()
  %28 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull @drbg_mutex) #4
  tail call void @ba_free(ptr noundef %.0) #4
  br label %29

29:                                               ; preds = %0, %27
  %.019 = phi i32 [ %.018, %27 ], [ 32, %0 ]
  ret i32 %.019
}

declare ptr @ba_alloc_by_len(i64 noundef) local_unnamed_addr #0

declare void @stacktrace_create(ptr noundef, i64 noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @drbg_free_internal() local_unnamed_addr #1

declare void @ba_free(ptr noundef) local_unnamed_addr #0

; Function Attrs: nofree nounwind willreturn memory(argmem: read)
declare i32 @bcmp(ptr nocapture, ptr nocapture, i64) local_unnamed_addr #3

attributes #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 = { 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 = { 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 #3 = { nofree nounwind willreturn memory(argmem: read) }
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)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"ByteArray_st", !6, i64 0, !11, i64 8}
!11 = !{!"long", !7, i64 0}


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