; LLVM IR from guanzhi/GmSSL :: src (tls)

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

@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [13 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [57 x i8], align 1
@__FUNCTION__.tls_authorities_from_certs = external hidden unnamed_addr constant [27 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @tls_uint16_to_bytes(i16 noundef zeroext, ptr noundef, ptr noundef) #0

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #1

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

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

declare i32 @x509_cert_from_der(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @tls_authorities_from_certs(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4) #0 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i64, align 8
  %14 = alloca ptr, align 8
  %15 = alloca i64, align 8
  %16 = alloca i64, align 8
  %17 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i64 %2, ptr %9, align 8, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !5
  store i64 %4, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #4
  %18 = load ptr, ptr %8, align 8, !tbaa !5
  store i64 0, ptr %18, align 8, !tbaa !9
  br label %19

19:                                               ; preds = %82, %5
  %20 = load i64, ptr %11, align 8, !tbaa !9
  %21 = icmp ne i64 %20, 0
  br i1 %21, label %22, label %83

22:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  store i64 0, ptr %16, align 8, !tbaa !9
  %23 = call i32 @x509_cert_from_der(ptr noundef %12, ptr noundef %13, ptr noundef %10, ptr noundef %11)
  %24 = icmp ne i32 %23, 1
  br i1 %24, label %35, label %25

25:                                               ; preds = %22
  %26 = load ptr, ptr %12, align 8, !tbaa !5
  %27 = load i64, ptr %13, align 8, !tbaa !9
  %28 = call i32 @x509_cert_get_subject(ptr noundef %26, i64 noundef %27, ptr noundef %14, ptr noundef %15)
  %29 = icmp ne i32 %28, 1
  br i1 %29, label %35, label %30

30:                                               ; preds = %25
  %31 = load ptr, ptr %14, align 8, !tbaa !5
  %32 = load i64, ptr %15, align 8, !tbaa !9
  %33 = call i32 @asn1_type_to_der(i32 noundef 48, ptr noundef %31, i64 noundef %32, ptr noundef null, ptr noundef %16)
  %34 = icmp ne i32 %33, 1
  br i1 %34, label %35, label %41

35:                                               ; preds = %30, %25, %22
  br label %36

36:                                               ; preds = %35
  %37 = load ptr, ptr @stderr, align 8, !tbaa !5
  %38 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %37, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 1862, ptr noundef @__FUNCTION__.tls_authorities_from_certs) #4
  br label %39

39:                                               ; preds = %36
  br label %40

40:                                               ; preds = %39
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %80

41:                                               ; preds = %30
  %42 = load i64, ptr %16, align 8, !tbaa !9
  %43 = add i64 2, %42
  %44 = load i64, ptr %9, align 8, !tbaa !9
  %45 = icmp ugt i64 %43, %44
  br i1 %45, label %46, label %52

46:                                               ; preds = %41
  br label %47

47:                                               ; preds = %46
  %48 = load ptr, ptr @stderr, align 8, !tbaa !5
  %49 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %48, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 1866, ptr noundef @__FUNCTION__.tls_authorities_from_certs) #4
  br label %50

50:                                               ; preds = %47
  br label %51

51:                                               ; preds = %50
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %80

52:                                               ; preds = %41
  %53 = load i64, ptr %16, align 8, !tbaa !9
  %54 = icmp ugt i64 %53, 65535
  br i1 %54, label %55, label %61

55:                                               ; preds = %52
  br label %56

56:                                               ; preds = %55
  %57 = load ptr, ptr @stderr, align 8, !tbaa !5
  %58 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %57, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 1870, ptr noundef @__FUNCTION__.tls_authorities_from_certs) #4
  br label %59

59:                                               ; preds = %56
  br label %60

60:                                               ; preds = %59
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %80

61:                                               ; preds = %52
  %62 = load i64, ptr %16, align 8, !tbaa !9
  %63 = trunc i64 %62 to i16
  %64 = load ptr, ptr %8, align 8, !tbaa !5
  call void @tls_uint16_to_bytes(i16 noundef zeroext %63, ptr noundef %7, ptr noundef %64)
  %65 = load ptr, ptr %14, align 8, !tbaa !5
  %66 = load i64, ptr %15, align 8, !tbaa !9
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  %68 = call i32 @asn1_type_to_der(i32 noundef 48, ptr noundef %65, i64 noundef %66, ptr noundef %7, ptr noundef %67)
  %69 = icmp ne i32 %68, 1
  br i1 %69, label %70, label %76

70:                                               ; preds = %61
  br label %71

71:                                               ; preds = %70
  %72 = load ptr, ptr @stderr, align 8, !tbaa !5
  %73 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %72, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 1875, ptr noundef @__FUNCTION__.tls_authorities_from_certs) #4
  br label %74

74:                                               ; preds = %71
  br label %75

75:                                               ; preds = %74
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %80

76:                                               ; preds = %61
  %77 = load i64, ptr %16, align 8, !tbaa !9
  %78 = load i64, ptr %9, align 8, !tbaa !9
  %79 = sub i64 %78, %77
  store i64 %79, ptr %9, align 8, !tbaa !9
  store i32 0, ptr %17, align 4
  br label %80

80:                                               ; preds = %76, %75, %60, %51, %40
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  %81 = load i32, ptr %17, align 4
  switch i32 %81, label %84 [
    i32 0, label %82
  ]

82:                                               ; preds = %80
  br label %19, !llvm.loop !11

83:                                               ; preds = %19
  store i32 1, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %84

84:                                               ; preds = %83, %80
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  %85 = load i32, ptr %6, align 4
  ret i32 %85
}

declare i32 @x509_cert_get_subject(ptr noundef, i64 noundef, ptr noundef, ptr noundef) #3

declare i32 @asn1_type_to_der(i32 noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef) #3

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 = { 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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

@stderr = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [13 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [57 x i8], align 1
@__FUNCTION__.tls_authorities_from_certs = external hidden unnamed_addr constant [27 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @tls_uint16_to_bytes(i16 noundef zeroext, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #1

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

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

declare i32 @x509_cert_from_der(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @tls_authorities_from_certs(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4) local_unnamed_addr #0 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %3, ptr %7, align 8, !tbaa !5
  store i64 %4, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %9) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %10) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %11) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %12) #4
  store i64 0, ptr %1, align 8, !tbaa !9
  %.not16 = icmp eq i64 %4, 0
  br i1 %.not16, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %5, %36
  %.0617 = phi i64 [ %38, %36 ], [ %2, %5 ]
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %13) #4
  store i64 0, ptr %13, align 8, !tbaa !9
  %14 = call i32 @x509_cert_from_der(ptr noundef nonnull %9, ptr noundef nonnull %10, ptr noundef nonnull %7, ptr noundef nonnull %8) #4
  %.not10 = icmp eq i32 %14, 1
  br i1 %.not10, label %15, label %.thread

15:                                               ; preds = %.lr.ph
  %16 = load ptr, ptr %9, align 8, !tbaa !5
  %17 = load i64, ptr %10, align 8, !tbaa !9
  %18 = call i32 @x509_cert_get_subject(ptr noundef %16, i64 noundef %17, ptr noundef nonnull %11, ptr noundef nonnull %12) #4
  %.not11 = icmp eq i32 %18, 1
  br i1 %.not11, label %19, label %.thread

19:                                               ; preds = %15
  %20 = load ptr, ptr %11, align 8, !tbaa !5
  %21 = load i64, ptr %12, align 8, !tbaa !9
  %22 = call i32 @asn1_type_to_der(i32 noundef 48, ptr noundef %20, i64 noundef %21, ptr noundef null, ptr noundef nonnull %13) #4
  %.not12 = icmp eq i32 %22, 1
  br i1 %.not12, label %23, label %.thread

23:                                               ; preds = %19
  %24 = load i64, ptr %13, align 8, !tbaa !9
  %25 = add i64 %24, 2
  %26 = icmp ugt i64 %25, %.0617
  br i1 %26, label %.thread, label %27

27:                                               ; preds = %23
  %28 = icmp ugt i64 %24, 65535
  br i1 %28, label %.thread, label %29

29:                                               ; preds = %27
  %30 = trunc nuw i64 %24 to i16
  call void @tls_uint16_to_bytes(i16 noundef zeroext %30, ptr noundef nonnull %6, ptr noundef nonnull %1)
  %31 = load ptr, ptr %11, align 8, !tbaa !5
  %32 = load i64, ptr %12, align 8, !tbaa !9
  %33 = call i32 @asn1_type_to_der(i32 noundef 48, ptr noundef %31, i64 noundef %32, ptr noundef nonnull %6, ptr noundef nonnull %1) #4
  %.not13 = icmp eq i32 %33, 1
  br i1 %.not13, label %36, label %.thread

.thread:                                          ; preds = %29, %27, %23, %19, %15, %.lr.ph
  %.sink18 = phi i32 [ 1862, %.lr.ph ], [ 1862, %15 ], [ 1862, %19 ], [ 1866, %23 ], [ 1870, %27 ], [ 1875, %29 ]
  %34 = load ptr, ptr @stderr, align 8, !tbaa !5
  %35 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %34, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef %.sink18, ptr noundef nonnull @__FUNCTION__.tls_authorities_from_certs) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %13) #4
  br label %.loopexit

36:                                               ; preds = %29
  %37 = load i64, ptr %13, align 8, !tbaa !9
  %38 = sub i64 %.0617, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %13) #4
  %39 = load i64, ptr %8, align 8, !tbaa !9
  %.not = icmp eq i64 %39, 0
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !11

.loopexit:                                        ; preds = %36, %5, %.thread
  %.2 = phi i32 [ -1, %.thread ], [ 1, %5 ], [ 1, %36 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %9) #4
  ret i32 %.2
}

declare i32 @x509_cert_get_subject(ptr noundef, i64 noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

declare i32 @asn1_type_to_der(i32 noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

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 = { nofree 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 #2 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nounwind }
attributes #5 = { cold 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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass