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

; 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_iana_to_kem = type { [2 x i8], ptr, i8 }
%struct.s2n_debug_info = type { ptr, ptr }
%struct.s2n_kem = type { ptr, i32, i16, i16, i16, i16, i16, ptr, ptr, ptr }

@kem_mapping = external dso_local constant [1 x %struct.s2n_iana_to_kem], align 16
@_s2n_debug_info = external thread_local global %struct.s2n_debug_info, align 8
@s2n_errno = external thread_local global i32, align 4
@.str.39 = external hidden unnamed_addr constant [84 x i8], align 1

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

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

declare i32 @s2n_calculate_stacktrace() #2

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @s2n_get_kem_from_extension_id(i16 noundef zeroext %0, ptr noundef %1) #4 {
  %3 = alloca i32, align 4
  %4 = alloca i16, align 2
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  store i16 %0, ptr %4, align 2, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  store i64 0, ptr %6, align 8, !tbaa !11
  br label %11

11:                                               ; preds = %57, %2
  %12 = load i64, ptr %6, align 8, !tbaa !11
  %13 = icmp ult i64 %12, 1
  br i1 %13, label %15, label %14

14:                                               ; preds = %11
  store i32 2, ptr %7, align 4
  br label %60

15:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %16 = load i64, ptr %6, align 8, !tbaa !11
  %17 = getelementptr inbounds [1 x %struct.s2n_iana_to_kem], ptr @kem_mapping, i64 0, i64 %16
  store ptr %17, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store i32 0, ptr %9, align 4, !tbaa !13
  br label %18

18:                                               ; preds = %48, %15
  %19 = load i32, ptr %9, align 4, !tbaa !13
  %20 = load ptr, ptr %8, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.s2n_iana_to_kem, ptr %20, i32 0, i32 2
  %22 = load i8, ptr %21, align 8, !tbaa !15
  %23 = zext i8 %22 to i32
  %24 = icmp slt i32 %19, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %18
  store i32 5, ptr %7, align 4
  br label %51

26:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %27 = load ptr, ptr %8, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.s2n_iana_to_kem, ptr %27, i32 0, i32 1
  %29 = load ptr, ptr %28, align 8, !tbaa !17
  %30 = load i32, ptr %9, align 4, !tbaa !13
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds ptr, ptr %29, i64 %31
  %33 = load ptr, ptr %32, align 8, !tbaa !9
  store ptr %33, ptr %10, align 8, !tbaa !9
  %34 = load ptr, ptr %10, align 8, !tbaa !9
  %35 = getelementptr inbounds %struct.s2n_kem, ptr %34, i32 0, i32 2
  %36 = load i16, ptr %35, align 4, !tbaa !18
  %37 = zext i16 %36 to i32
  %38 = load i16, ptr %4, align 2, !tbaa !5
  %39 = zext i16 %38 to i32
  %40 = icmp eq i32 %37, %39
  br i1 %40, label %41, label %44

41:                                               ; preds = %26
  %42 = load ptr, ptr %10, align 8, !tbaa !9
  %43 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %42, ptr %43, align 8, !tbaa !9
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %45

44:                                               ; preds = %26
  store i32 0, ptr %7, align 4
  br label %45

45:                                               ; preds = %44, %41
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  %46 = load i32, ptr %7, align 4
  switch i32 %46, label %51 [
    i32 0, label %47
  ]

47:                                               ; preds = %45
  br label %48

48:                                               ; preds = %47
  %49 = load i32, ptr %9, align 4, !tbaa !13
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %9, align 4, !tbaa !13
  br label %18, !llvm.loop !20

51:                                               ; preds = %45, %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  %52 = load i32, ptr %7, align 4
  switch i32 %52, label %54 [
    i32 5, label %53
  ]

53:                                               ; preds = %51
  store i32 0, ptr %7, align 4
  br label %54

54:                                               ; preds = %53, %51
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %55 = load i32, ptr %7, align 4
  switch i32 %55, label %60 [
    i32 0, label %56
  ]

56:                                               ; preds = %54
  br label %57

57:                                               ; preds = %56
  %58 = load i64, ptr %6, align 8, !tbaa !11
  %59 = add i64 %58, 1
  store i64 %59, ptr %6, align 8, !tbaa !11
  br label %11, !llvm.loop !22

60:                                               ; preds = %54, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  %61 = load i32, ptr %7, align 4
  switch i32 %61, label %94 [
    i32 2, label %62
    i32 1, label %92
  ]

62:                                               ; preds = %60
  br label %63

63:                                               ; preds = %62
  br label %64

64:                                               ; preds = %63
  %65 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  %66 = getelementptr inbounds %struct.s2n_debug_info, ptr %65, i32 0, i32 0
  store ptr @.str.39, ptr %66, align 8, !tbaa !23
  %67 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  %68 = getelementptr inbounds %struct.s2n_debug_info, ptr %67, i32 0, i32 0
  %69 = load ptr, ptr %68, align 8, !tbaa !23
  %70 = getelementptr inbounds i8, ptr %69, i64 21
  %71 = call ptr @strrchr(ptr noundef %70, i32 noundef 47) #6
  %72 = icmp ne ptr %71, null
  br i1 %72, label %73, label %80

73:                                               ; preds = %64
  %74 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  %75 = getelementptr inbounds %struct.s2n_debug_info, ptr %74, i32 0, i32 0
  %76 = load ptr, ptr %75, align 8, !tbaa !23
  %77 = getelementptr inbounds i8, ptr %76, i64 21
  %78 = call ptr @strrchr(ptr noundef %77, i32 noundef 47) #6
  %79 = getelementptr inbounds i8, ptr %78, i64 1
  br label %85

80:                                               ; preds = %64
  %81 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  %82 = getelementptr inbounds %struct.s2n_debug_info, ptr %81, i32 0, i32 0
  %83 = load ptr, ptr %82, align 8, !tbaa !23
  %84 = getelementptr inbounds i8, ptr %83, i64 21
  br label %85

85:                                               ; preds = %80, %73
  %86 = phi ptr [ %79, %73 ], [ %84, %80 ]
  %87 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  %88 = getelementptr inbounds %struct.s2n_debug_info, ptr %87, i32 0, i32 1
  store ptr %86, ptr %88, align 8, !tbaa !25
  %89 = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @s2n_errno)
  store i32 335544361, ptr %89, align 4, !tbaa !13
  %90 = call i32 @s2n_calculate_stacktrace()
  br label %91

91:                                               ; preds = %85
  store i32 -1, ptr %3, align 4
  br label %92

92:                                               ; preds = %91, %60
  %93 = load i32, ptr %3, align 4
  ret i32 %93

94:                                               ; preds = %60
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { 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 #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 }
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 = !{!"short", !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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!16, !7, i64 16}
!16 = !{!"s2n_iana_to_kem", !7, i64 0, !10, i64 8, !7, i64 16}
!17 = !{!16, !10, i64 8}
!18 = !{!19, !6, i64 12}
!19 = !{!"s2n_kem", !10, i64 0, !14, i64 8, !6, i64 12, !6, i64 14, !6, i64 16, !6, i64 18, !6, i64 20, !10, i64 24, !10, i64 32, !10, i64 40}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = distinct !{!22, !21}
!23 = !{!24, !10, i64 0}
!24 = !{!"s2n_debug_info", !10, i64 0, !10, i64 8}
!25 = !{!24, !10, i64 8}


; 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_iana_to_kem = type { [2 x i8], ptr, i8 }
%struct.s2n_debug_info = type { ptr, ptr }

@kem_mapping = external dso_local local_unnamed_addr constant [1 x %struct.s2n_iana_to_kem], align 16
@_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.39 = external hidden unnamed_addr constant [84 x i8], align 1

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

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

declare i32 @s2n_calculate_stacktrace() local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @s2n_get_kem_from_extension_id(i16 noundef zeroext %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #3 {
.critedge:
  %2 = load i8, ptr getelementptr inbounds (i8, ptr @kem_mapping, i64 16), align 16, !tbaa !5
  %.not2432.not = icmp eq i8 %2, 0
  br i1 %.not2432.not, label %._crit_edge, label %.critedge27.lr.ph

.critedge27.lr.ph:                                ; preds = %.critedge
  %3 = load ptr, ptr getelementptr inbounds (i8, ptr @kem_mapping, i64 8), align 8, !tbaa !10
  %wide.trip.count = zext i8 %2 to i64
  br label %.critedge27

4:                                                ; preds = %.critedge27
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.critedge27, !llvm.loop !11

.critedge27:                                      ; preds = %.critedge27.lr.ph, %4
  %indvars.iv = phi i64 [ 0, %.critedge27.lr.ph ], [ %indvars.iv.next, %4 ]
  %5 = getelementptr inbounds ptr, ptr %3, i64 %indvars.iv
  %6 = load ptr, ptr %5, align 8, !tbaa !13
  %7 = getelementptr inbounds i8, ptr %6, i64 12
  %8 = load i16, ptr %7, align 4, !tbaa !14
  %.not = icmp eq i16 %8, %0
  br i1 %.not, label %9, label %4

9:                                                ; preds = %.critedge27
  store ptr %6, ptr %1, align 8, !tbaa !13
  br label %17

._crit_edge:                                      ; preds = %4, %.critedge
  %10 = tail call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  store ptr @.str.39, ptr %10, align 8, !tbaa !18
  %11 = tail call ptr @strrchr(ptr noundef nonnull dereferenceable(1) getelementptr inbounds (i8, ptr @.str.39, i64 21), i32 noundef 47) #4
  %.not25 = icmp eq ptr %11, null
  %12 = getelementptr inbounds i8, ptr %11, i64 1
  %13 = select i1 %.not25, ptr getelementptr inbounds (i8, ptr @.str.39, i64 21), ptr %12
  %14 = getelementptr inbounds i8, ptr %10, i64 8
  store ptr %13, ptr %14, align 8, !tbaa !20
  %15 = tail call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @s2n_errno)
  store i32 335544361, ptr %15, align 4, !tbaa !21
  %16 = tail call i32 @s2n_calculate_stacktrace() #5
  br label %17

17:                                               ; preds = %9, %._crit_edge
  %.5 = phi i32 [ 0, %9 ], [ -1, %._crit_edge ]
  ret i32 %.5
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { 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 #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 willreturn memory(read) }
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 16}
!6 = !{!"s2n_iana_to_kem", !7, i64 0, !9, i64 8, !7, i64 16}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"any pointer", !7, i64 0}
!10 = !{!6, !9, i64 8}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!9, !9, i64 0}
!14 = !{!15, !17, i64 12}
!15 = !{!"s2n_kem", !9, i64 0, !16, i64 8, !17, i64 12, !17, i64 14, !17, i64 16, !17, i64 18, !17, i64 20, !9, i64 24, !9, i64 32, !9, i64 40}
!16 = !{!"int", !7, i64 0}
!17 = !{!"short", !7, i64 0}
!18 = !{!19, !9, i64 0}
!19 = !{!"s2n_debug_info", !9, i64 0, !9, i64 8}
!20 = !{!19, !9, i64 8}
!21 = !{!16, !16, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass