; 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_record_set_handshake_certificate = external hidden unnamed_addr constant [37 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @tls_uint24_to_bytes(i32 noundef, ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @tls_uint24array_to_bytes(ptr noundef, i64 noundef, 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

; Function Attrs: nounwind uwtable
declare dso_local i32 @tls_record_set_handshake(ptr noundef, ptr noundef, i32 noundef, ptr noundef, i64 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @tls_record_set_handshake_certificate(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i64, align 8
  %15 = alloca i32, align 4
  %16 = alloca ptr, align 8
  %17 = alloca i64, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  store i32 11, ptr %10, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  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
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = icmp ne ptr %18, null
  br i1 %19, label %20, label %29

20:                                               ; preds = %4
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = icmp ne ptr %21, null
  br i1 %22, label %23, label %29

23:                                               ; preds = %20
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = icmp ne ptr %24, null
  br i1 %25, label %26, label %29

26:                                               ; preds = %23
  %27 = load i64, ptr %9, align 8, !tbaa !9
  %28 = icmp ne i64 %27, 0
  br i1 %28, label %35, label %29

29:                                               ; preds = %26, %23, %20, %4
  br label %30

30:                                               ; preds = %29
  %31 = load ptr, ptr @stderr, align 8, !tbaa !5
  %32 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %31, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 987, ptr noundef @__FUNCTION__.tls_record_set_handshake_certificate) #4
  br label %33

33:                                               ; preds = %30
  br label %34

34:                                               ; preds = %33
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %78

35:                                               ; preds = %26
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds i8, ptr %36, i64 5
  %38 = getelementptr inbounds i8, ptr %37, i64 4
  store ptr %38, ptr %11, align 8, !tbaa !5
  %39 = load ptr, ptr %11, align 8, !tbaa !5
  %40 = getelementptr inbounds i8, ptr %39, i64 3
  store ptr %40, ptr %13, align 8, !tbaa !5
  store i64 3, ptr %12, align 8, !tbaa !9
  store i64 0, ptr %14, align 8, !tbaa !9
  br label %41

41:                                               ; preds = %69, %35
  %42 = load i64, ptr %9, align 8, !tbaa !9
  %43 = icmp ne i64 %42, 0
  br i1 %43, label %44, label %70

44:                                               ; preds = %41
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #4
  %45 = call i32 @x509_cert_from_der(ptr noundef %16, ptr noundef %17, ptr noundef %8, ptr noundef %9)
  %46 = icmp ne i32 %45, 1
  br i1 %46, label %47, label %53

47:                                               ; preds = %44
  br label %48

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

51:                                               ; preds = %48
  br label %52

52:                                               ; preds = %51
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %67

53:                                               ; preds = %44
  %54 = load ptr, ptr %16, align 8, !tbaa !5
  %55 = load i64, ptr %17, align 8, !tbaa !9
  call void @tls_uint24array_to_bytes(ptr noundef %54, i64 noundef %55, ptr noundef null, ptr noundef %12)
  %56 = load i64, ptr %12, align 8, !tbaa !9
  %57 = icmp ugt i64 %56, 16380
  br i1 %57, label %58, label %64

58:                                               ; preds = %53
  br label %59

59:                                               ; preds = %58
  %60 = load ptr, ptr @stderr, align 8, !tbaa !5
  %61 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %60, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 1005, ptr noundef @__FUNCTION__.tls_record_set_handshake_certificate) #4
  br label %62

62:                                               ; preds = %59
  br label %63

63:                                               ; preds = %62
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %67

64:                                               ; preds = %53
  %65 = load ptr, ptr %16, align 8, !tbaa !5
  %66 = load i64, ptr %17, align 8, !tbaa !9
  call void @tls_uint24array_to_bytes(ptr noundef %65, i64 noundef %66, ptr noundef %13, ptr noundef %14)
  store i32 0, ptr %15, align 4
  br label %67

67:                                               ; preds = %64, %63, %52
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  %68 = load i32, ptr %15, align 4
  switch i32 %68, label %78 [
    i32 0, label %69
  ]

69:                                               ; preds = %67
  br label %41, !llvm.loop !13

70:                                               ; preds = %41
  %71 = load i64, ptr %14, align 8, !tbaa !9
  %72 = trunc i64 %71 to i32
  call void @tls_uint24_to_bytes(i32 noundef %72, ptr noundef %11, ptr noundef %14)
  %73 = load ptr, ptr %6, align 8, !tbaa !5
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  %75 = load i32, ptr %10, align 4, !tbaa !11
  %76 = load i64, ptr %12, align 8, !tbaa !9
  %77 = call i32 @tls_record_set_handshake(ptr noundef %73, ptr noundef %74, i32 noundef %75, ptr noundef null, i64 noundef %76)
  store i32 1, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %78

78:                                               ; preds = %70, %67, %34
  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
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  %79 = load i32, ptr %5, align 4
  ret i32 %79
}

declare i32 @x509_cert_from_der(ptr noundef, ptr 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 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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_record_set_handshake_certificate = external hidden unnamed_addr constant [37 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @tls_uint24_to_bytes(i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @tls_uint24array_to_bytes(ptr noundef, i64 noundef, 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

; Function Attrs: nounwind uwtable
declare dso_local i32 @tls_record_set_handshake(ptr noundef, ptr noundef, i32 noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @tls_record_set_handshake_certificate(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = alloca ptr, align 8
  %6 = alloca i64, 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
  store ptr %2, ptr %5, align 8, !tbaa !5
  store i64 %3, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %7) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %9) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %10) #4
  %13 = icmp ne ptr %0, null
  %14 = icmp ne ptr %1, null
  %or.cond = and i1 %13, %14
  %15 = icmp ne ptr %2, null
  %or.cond3 = and i1 %or.cond, %15
  %16 = icmp ne i64 %3, 0
  %or.cond5 = and i1 %or.cond3, %16
  br i1 %or.cond5, label %.lr.ph.preheader, label %17

17:                                               ; preds = %4
  %18 = load ptr, ptr @stderr, align 8, !tbaa !5
  %19 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %18, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 987, ptr noundef nonnull @__FUNCTION__.tls_record_set_handshake_certificate) #5
  br label %37

.lr.ph.preheader:                                 ; preds = %4
  %20 = getelementptr inbounds i8, ptr %0, i64 9
  store ptr %20, ptr %7, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %0, i64 12
  store ptr %21, ptr %9, align 8, !tbaa !5
  store i64 3, ptr %8, align 8, !tbaa !9
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %28
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %11) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %12) #4
  %22 = call i32 @x509_cert_from_der(ptr noundef nonnull %11, ptr noundef nonnull %12, ptr noundef nonnull %5, ptr noundef nonnull %6) #4
  %.not15 = icmp eq i32 %22, 1
  br i1 %.not15, label %23, label %.critedge

23:                                               ; preds = %.lr.ph
  %24 = load ptr, ptr %11, align 8, !tbaa !5
  %25 = load i64, ptr %12, align 8, !tbaa !9
  call void @tls_uint24array_to_bytes(ptr noundef %24, i64 noundef %25, ptr noundef null, ptr noundef nonnull %8)
  %26 = load i64, ptr %8, align 8, !tbaa !9
  %27 = icmp ugt i64 %26, 16380
  br i1 %27, label %.critedge, label %28

28:                                               ; preds = %23
  %29 = load ptr, ptr %11, align 8, !tbaa !5
  %30 = load i64, ptr %12, align 8, !tbaa !9
  call void @tls_uint24array_to_bytes(ptr noundef %29, i64 noundef %30, ptr noundef nonnull %9, ptr noundef nonnull %10)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %11) #4
  %31 = load i64, ptr %6, align 8, !tbaa !9
  %.not = icmp eq i64 %31, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !11

._crit_edge:                                      ; preds = %28
  %.pre = load i64, ptr %10, align 8, !tbaa !9
  %32 = trunc i64 %.pre to i32
  call void @tls_uint24_to_bytes(i32 noundef %32, ptr noundef nonnull %7, ptr noundef nonnull %10)
  %33 = load i64, ptr %8, align 8, !tbaa !9
  %34 = call i32 @tls_record_set_handshake(ptr noundef nonnull %0, ptr noundef nonnull %1, i32 noundef 11, ptr noundef null, i64 noundef %33)
  br label %37

.critedge:                                        ; preds = %23, %.lr.ph
  %.sink17 = phi i32 [ 1000, %.lr.ph ], [ 1005, %23 ]
  %35 = load ptr, ptr @stderr, align 8, !tbaa !5
  %36 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %35, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef %.sink17, ptr noundef nonnull @__FUNCTION__.tls_record_set_handshake_certificate) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %11) #4
  br label %37

37:                                               ; preds = %.critedge, %._crit_edge, %17
  %.2 = phi i32 [ 1, %._crit_edge ], [ -1, %17 ], [ -1, %.critedge ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7) #4
  ret i32 %.2
}

declare i32 @x509_cert_from_der(ptr noundef, ptr 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,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass