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

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

@_s2n_debug_info = external thread_local global %struct.s2n_debug_info, align 8
@s2n_errno = external thread_local global i32, align 4
@.str.25 = external hidden unnamed_addr constant [95 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: nounwind uwtable
declare dso_local i32 @s2n_cert_chain_and_key_set_cert_chain_bytes(ptr noundef, ptr noundef, i32 noundef) #3

; Function Attrs: nounwind uwtable
declare dso_local i32 @s2n_cert_chain_and_key_set_private_key_bytes(ptr noundef, ptr noundef, i32 noundef) #3

; Function Attrs: nounwind uwtable
declare dso_local i32 @s2n_cert_chain_and_key_load(ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @s2n_cert_chain_and_key_load_pem_bytes(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3, i32 noundef %4) #3 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i32 %2, ptr %9, align 4, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !5
  store i32 %4, ptr %11, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %5
  %13 = load ptr, ptr %7, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %46, label %15

15:                                               ; preds = %12
  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.25, ptr %19, align 8, !tbaa !11
  %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 !11
  %23 = getelementptr inbounds i8, ptr %22, i64 21
  %24 = call ptr @strrchr(ptr noundef %23, i32 noundef 47) #4
  %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 !11
  %30 = getelementptr inbounds i8, ptr %29, i64 21
  %31 = call ptr @strrchr(ptr noundef %30, i32 noundef 47) #4
  %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 !11
  %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 !13
  %42 = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @s2n_errno)
  store i32 402653193, ptr %42, align 4, !tbaa !9
  %43 = call i32 @s2n_calculate_stacktrace()
  br label %44

44:                                               ; preds = %38
  store i32 -1, ptr %6, align 4
  br label %73

45:                                               ; No predecessors!
  br label %46

46:                                               ; preds = %45, %12
  br label %47

47:                                               ; preds = %46
  br label %48

48:                                               ; preds = %47
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = load ptr, ptr %8, align 8, !tbaa !5
  %51 = load i32, ptr %9, align 4, !tbaa !9
  %52 = call i32 @s2n_cert_chain_and_key_set_cert_chain_bytes(ptr noundef %49, ptr noundef %50, i32 noundef %51)
  %53 = icmp sgt i32 %52, -1
  br i1 %53, label %55, label %54

54:                                               ; preds = %48
  store i32 -1, ptr %6, align 4
  br label %73

55:                                               ; preds = %48
  br label %56

56:                                               ; preds = %55
  br label %57

57:                                               ; preds = %56
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = load ptr, ptr %10, align 8, !tbaa !5
  %60 = load i32, ptr %11, align 4, !tbaa !9
  %61 = call i32 @s2n_cert_chain_and_key_set_private_key_bytes(ptr noundef %58, ptr noundef %59, i32 noundef %60)
  %62 = icmp sgt i32 %61, -1
  br i1 %62, label %64, label %63

63:                                               ; preds = %57
  store i32 -1, ptr %6, align 4
  br label %73

64:                                               ; preds = %57
  br label %65

65:                                               ; preds = %64
  br label %66

66:                                               ; preds = %65
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = call i32 @s2n_cert_chain_and_key_load(ptr noundef %67)
  %69 = icmp sgt i32 %68, -1
  br i1 %69, label %71, label %70

70:                                               ; preds = %66
  store i32 -1, ptr %6, align 4
  br label %73

71:                                               ; preds = %66
  br label %72

72:                                               ; preds = %71
  store i32 0, ptr %6, align 4
  br label %73

73:                                               ; preds = %72, %70, %63, %54, %44
  %74 = load i32, ptr %6, align 4
  ret i32 %74
}

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 = { 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) }

!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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"s2n_debug_info", !6, i64 0, !6, i64 8}
!13 = !{!12, !6, 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_debug_info = type { ptr, ptr }

@_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.25 = external hidden unnamed_addr constant [95 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
declare dso_local i32 @s2n_cert_chain_and_key_set_cert_chain_bytes(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare dso_local i32 @s2n_cert_chain_and_key_set_private_key_bytes(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare dso_local i32 @s2n_cert_chain_and_key_load(ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @s2n_cert_chain_and_key_load_pem_bytes(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3, i32 noundef %4) local_unnamed_addr #3 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %6, label %14

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

14:                                               ; preds = %5
  %15 = tail call i32 @s2n_cert_chain_and_key_set_cert_chain_bytes(ptr noundef nonnull %0, ptr noundef %1, i32 noundef %2)
  %16 = icmp sgt i32 %15, -1
  br i1 %16, label %17, label %22

17:                                               ; preds = %14
  %18 = tail call i32 @s2n_cert_chain_and_key_set_private_key_bytes(ptr noundef nonnull %0, ptr noundef %3, i32 noundef %4)
  %19 = icmp sgt i32 %18, -1
  br i1 %19, label %20, label %22

20:                                               ; preds = %17
  %21 = tail call i32 @s2n_cert_chain_and_key_load(ptr noundef nonnull %0)
  %.lobit = ashr i32 %21, 31
  br label %22

22:                                               ; preds = %20, %17, %14, %6
  %.0 = phi i32 [ -1, %6 ], [ -1, %14 ], [ -1, %17 ], [ %.lobit, %20 ]
  ret i32 %.0
}

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