; LLVM IR from aws/s2n-tls :: crypto (s2n_libcrypto)

; 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_debug_info = type { ptr, ptr }
%struct.s2n_result = type { i32 }

@.str = external hidden unnamed_addr constant [93 x i8], align 1
@_s2n_debug_info = external thread_local global %struct.s2n_debug_info, align 8
@s2n_errno = external thread_local global i32, align 4
@.str.1 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [7 x i8], align 1

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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @s2n_libcrypto_is_boringssl() #1

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @s2n_libcrypto_is_awslc() #1

; 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 i64 @s2n_libcrypto_awslc_api_version() #1

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

7:                                                ; preds = %0
  %8 = call zeroext i1 @s2n_libcrypto_is_boringssl()
  br i1 %8, label %9, label %11

9:                                                ; preds = %7
  %10 = call zeroext i1 @s2n_libcrypto_is_awslc()
  br label %11

11:                                               ; preds = %9, %7
  %12 = phi i1 [ false, %7 ], [ %10, %9 ]
  %13 = zext i1 %12 to i32
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %47, label %15

15:                                               ; preds = %11
  br label %16

16:                                               ; preds = %15
  br label %17

17:                                               ; preds = %16
  %18 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  %19 = getelementptr inbounds %struct.s2n_debug_info, ptr %18, i32 0, i32 0
  store ptr @.str, ptr %19, align 8, !tbaa !5
  %20 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  %21 = getelementptr inbounds %struct.s2n_debug_info, ptr %20, i32 0, i32 0
  %22 = load ptr, ptr %21, align 8, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %22, i64 21
  %24 = call ptr @strrchr(ptr noundef %23, i32 noundef 47) #6
  %25 = icmp ne ptr %24, null
  br i1 %25, label %26, label %33

26:                                               ; preds = %17
  %27 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  %28 = getelementptr inbounds %struct.s2n_debug_info, ptr %27, i32 0, i32 0
  %29 = load ptr, ptr %28, align 8, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %29, i64 21
  %31 = call ptr @strrchr(ptr noundef %30, i32 noundef 47) #6
  %32 = getelementptr inbounds i8, ptr %31, i64 1
  br label %38

33:                                               ; preds = %17
  %34 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  %35 = getelementptr inbounds %struct.s2n_debug_info, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !5
  %37 = getelementptr inbounds i8, ptr %36, i64 21
  br label %38

38:                                               ; preds = %33, %26
  %39 = phi ptr [ %32, %26 ], [ %37, %33 ]
  %40 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  %41 = getelementptr inbounds %struct.s2n_debug_info, ptr %40, i32 0, i32 1
  store ptr %39, ptr %41, align 8, !tbaa !10
  %42 = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @s2n_errno)
  store i32 402653194, ptr %42, align 4, !tbaa !11
  %43 = call i32 @s2n_calculate_stacktrace()
  br label %44

44:                                               ; preds = %38
  %45 = getelementptr inbounds %struct.s2n_result, ptr %1, i32 0, i32 0
  store i32 -1, ptr %45, align 4, !tbaa !13
  br label %97

46:                                               ; No predecessors!
  br label %47

47:                                               ; preds = %46, %11
  br label %48

48:                                               ; preds = %47
  %49 = call zeroext i1 @s2n_libcrypto_is_awslc()
  br i1 %49, label %50, label %71

50:                                               ; preds = %48
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #7
  store ptr null, ptr %2, align 8, !tbaa !15
  %51 = call i64 @s2n_libcrypto_awslc_api_version()
  %52 = icmp ult i64 %51, 17
  br i1 %52, label %53, label %54

53:                                               ; preds = %50
  store ptr @.str.1, ptr %2, align 8, !tbaa !15
  br label %55

54:                                               ; preds = %50
  store ptr @.str.2, ptr %2, align 8, !tbaa !15
  br label %55

55:                                               ; preds = %54, %53
  br label %56

56:                                               ; preds = %55
  %57 = load ptr, ptr %2, align 8, !tbaa !15
  %58 = call i32 @s2n_libcrypto_validate_expected_version_prefix(ptr noundef %57)
  %59 = getelementptr inbounds %struct.s2n_result, ptr %3, i32 0, i32 0
  store i32 %58, ptr %59, align 4
  %60 = getelementptr inbounds %struct.s2n_result, ptr %3, i32 0, i32 0
  %61 = load i32, ptr %60, align 4
  %62 = call zeroext i1 @s2n_result_is_ok(i32 %61)
  br i1 %62, label %65, label %63

63:                                               ; preds = %56
  %64 = getelementptr inbounds %struct.s2n_result, ptr %1, i32 0, i32 0
  store i32 -1, ptr %64, align 4, !tbaa !13
  store i32 1, ptr %4, align 4
  br label %68

65:                                               ; preds = %56
  br label %66

66:                                               ; preds = %65
  br label %67

67:                                               ; preds = %66
  store i32 0, ptr %4, align 4
  br label %68

68:                                               ; preds = %67, %63
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #7
  %69 = load i32, ptr %4, align 4
  switch i32 %69, label %100 [
    i32 0, label %70
    i32 1, label %97
  ]

70:                                               ; preds = %68
  br label %85

71:                                               ; preds = %48
  %72 = call zeroext i1 @s2n_libcrypto_is_boringssl()
  br i1 %72, label %73, label %84

73:                                               ; preds = %71
  br label %74

74:                                               ; preds = %73
  %75 = call i32 @s2n_libcrypto_validate_expected_version_prefix(ptr noundef @.str.1)
  %76 = getelementptr inbounds %struct.s2n_result, ptr %5, i32 0, i32 0
  store i32 %75, ptr %76, align 4
  %77 = getelementptr inbounds %struct.s2n_result, ptr %5, i32 0, i32 0
  %78 = load i32, ptr %77, align 4
  %79 = call zeroext i1 @s2n_result_is_ok(i32 %78)
  br i1 %79, label %82, label %80

80:                                               ; preds = %74
  %81 = getelementptr inbounds %struct.s2n_result, ptr %1, i32 0, i32 0
  store i32 -1, ptr %81, align 4, !tbaa !13
  br label %97

82:                                               ; preds = %74
  br label %83

83:                                               ; preds = %82
  br label %84

84:                                               ; preds = %83, %71
  br label %85

85:                                               ; preds = %84, %70
  br label %86

86:                                               ; preds = %85
  %87 = call i32 @s2n_libcrypto_validate_expected_version_number()
  %88 = getelementptr inbounds %struct.s2n_result, ptr %6, i32 0, i32 0
  store i32 %87, ptr %88, align 4
  %89 = getelementptr inbounds %struct.s2n_result, ptr %6, i32 0, i32 0
  %90 = load i32, ptr %89, align 4
  %91 = call zeroext i1 @s2n_result_is_ok(i32 %90)
  br i1 %91, label %94, label %92

92:                                               ; preds = %86
  %93 = getelementptr inbounds %struct.s2n_result, ptr %1, i32 0, i32 0
  store i32 -1, ptr %93, align 4, !tbaa !13
  br label %97

94:                                               ; preds = %86
  br label %95

95:                                               ; preds = %94
  %96 = getelementptr inbounds %struct.s2n_result, ptr %1, i32 0, i32 0
  store i32 0, ptr %96, align 4, !tbaa !13
  br label %97

97:                                               ; preds = %95, %92, %80, %68, %44
  %98 = getelementptr inbounds %struct.s2n_result, ptr %1, i32 0, i32 0
  %99 = load i32, ptr %98, align 4
  ret i32 %99

100:                                              ; preds = %68
  unreachable
}

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

; Function Attrs: nounwind willreturn memory(read)
declare ptr @strrchr(ptr noundef, i32 noundef) #3

declare i32 @s2n_calculate_stacktrace() #4

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

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

; Function Attrs: nounwind uwtable
declare hidden i32 @s2n_libcrypto_validate_expected_version_number() #1

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 nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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 #4 = { "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 = { 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 #6 = { nounwind willreturn memory(read) }
attributes #7 = { 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 = !{!"s2n_debug_info", !7, i64 0, !7, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !8, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"", !12, i64 0}
!15 = !{!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_debug_info = type { ptr, ptr }

@.str = external hidden unnamed_addr constant [93 x i8], align 1
@_s2n_debug_info = external thread_local local_unnamed_addr global %struct.s2n_debug_info, align 8
@s2n_errno = external thread_local local_unnamed_addr global i32, align 4
@.str.1 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [7 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @s2n_libcrypto_is_boringssl() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @s2n_libcrypto_is_awslc() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i64 @s2n_libcrypto_awslc_api_version() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @s2n_libcrypto_validate_runtime() local_unnamed_addr #0 {
  %1 = tail call zeroext i1 @s2n_libcrypto_is_boringssl()
  br i1 %1, label %2, label %.critedge

2:                                                ; preds = %0
  %3 = tail call zeroext i1 @s2n_libcrypto_is_awslc()
  br i1 %3, label %4, label %.critedge

4:                                                ; preds = %2
  %5 = tail call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  store ptr @.str, ptr %5, align 8, !tbaa !5
  %6 = tail call ptr @strrchr(ptr noundef nonnull dereferenceable(1) getelementptr inbounds (i8, ptr @.str, i64 21), i32 noundef 47) #5
  %.not = icmp eq ptr %6, null
  %7 = getelementptr inbounds i8, ptr %6, i64 1
  %8 = select i1 %.not, ptr getelementptr inbounds (i8, ptr @.str, i64 21), ptr %7
  %9 = getelementptr inbounds i8, ptr %5, i64 8
  store ptr %8, ptr %9, align 8, !tbaa !10
  %10 = tail call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @s2n_errno)
  store i32 402653194, ptr %10, align 4, !tbaa !11
  %11 = tail call i32 @s2n_calculate_stacktrace() #6
  br label %26

.critedge:                                        ; preds = %0, %2
  %12 = tail call zeroext i1 @s2n_libcrypto_is_awslc()
  br i1 %12, label %13, label %18

13:                                               ; preds = %.critedge
  %14 = tail call i64 @s2n_libcrypto_awslc_api_version()
  %15 = icmp ult i64 %14, 17
  %.str.1..str.2 = select i1 %15, ptr @.str.1, ptr @.str.2
  %16 = tail call i32 @s2n_libcrypto_validate_expected_version_prefix(ptr noundef nonnull %.str.1..str.2)
  %17 = tail call zeroext i1 @s2n_result_is_ok(i32 %16)
  br i1 %17, label %23, label %26

18:                                               ; preds = %.critedge
  %19 = tail call zeroext i1 @s2n_libcrypto_is_boringssl()
  br i1 %19, label %20, label %23

20:                                               ; preds = %18
  %21 = tail call i32 @s2n_libcrypto_validate_expected_version_prefix(ptr noundef nonnull @.str.1)
  %22 = tail call zeroext i1 @s2n_result_is_ok(i32 %21)
  br i1 %22, label %23, label %26

23:                                               ; preds = %13, %20, %18
  %24 = tail call i32 @s2n_libcrypto_validate_expected_version_number()
  %25 = tail call zeroext i1 @s2n_result_is_ok(i32 %24)
  %not. = xor i1 %25, true
  %. = sext i1 %not. to i32
  br label %26

26:                                               ; preds = %13, %23, %20, %4
  %.sroa.03.1 = phi i32 [ -1, %13 ], [ -1, %4 ], [ -1, %20 ], [ %., %23 ]
  ret i32 %.sroa.03.1
}

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare ptr @strrchr(ptr noundef, i32 noundef) local_unnamed_addr #2

declare i32 @s2n_calculate_stacktrace() local_unnamed_addr #3

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

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

; Function Attrs: nounwind uwtable
declare hidden i32 @s2n_libcrypto_validate_expected_version_number() local_unnamed_addr #0

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


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