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

%struct.TLS_CONNECT = type { i32, i32, [64 x i32], i64, i32, [18437 x i8], i64, [18437 x i8], [18437 x i8], ptr, i64, i32, [32 x i8], i64, [2048 x i8], i64, [2048 x i8], i64, [2048 x i8], i64, %struct.SM2_KEY, %struct.SM2_KEY, i32, [48 x i8], [96 x i8], %struct.SM3_HMAC_CTX, %struct.SM3_HMAC_CTX, %struct.SM4_KEY, %struct.SM4_KEY, [8 x i8], [8 x i8], [12 x i8], [12 x i8], %struct.BLOCK_CIPHER_KEY, %struct.BLOCK_CIPHER_KEY, i32 }
%struct.SM2_KEY = type { %struct.SM2_Z256_POINT, [4 x i64] }
%struct.SM2_Z256_POINT = type { [4 x i64], [4 x i64], [4 x i64] }
%struct.SM3_HMAC_CTX = type { %struct.SM3_CTX, [64 x i8] }
%struct.SM3_CTX = type { [8 x i32], i64, [64 x i8], i64 }
%struct.SM4_KEY = type { [32 x i32] }
%struct.BLOCK_CIPHER_KEY = type { %union.anon, ptr }
%union.anon = type { %struct.SM4_KEY }

@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_send_alert = external hidden unnamed_addr constant [15 x i8], align 1

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

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

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @tls_send_alert(ptr noundef %0, i32 noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca [7 x i8], align 1
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 7, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = icmp ne ptr %9, null
  br i1 %10, label %17, label %11

11:                                               ; preds = %2
  br label %12

12:                                               ; preds = %11
  %13 = load ptr, ptr @stderr, align 8, !tbaa !5
  %14 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %13, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 1596, ptr noundef @__FUNCTION__.tls_send_alert) #3
  br label %15

15:                                               ; preds = %12
  br label %16

16:                                               ; preds = %15
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %47

17:                                               ; preds = %2
  %18 = getelementptr inbounds [7 x i8], ptr %6, i64 0, i64 0
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.TLS_CONNECT, ptr %19, i32 0, i32 0
  %21 = load i32, ptr %20, align 8, !tbaa !11
  %22 = icmp eq i32 %21, 772
  br i1 %22, label %23, label %24

23:                                               ; preds = %17
  br label %28

24:                                               ; preds = %17
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.TLS_CONNECT, ptr %25, i32 0, i32 0
  %27 = load i32, ptr %26, align 8, !tbaa !11
  br label %28

28:                                               ; preds = %24, %23
  %29 = phi i32 [ 771, %23 ], [ %27, %24 ]
  %30 = call i32 @tls_record_set_protocol(ptr noundef %18, i32 noundef %29)
  %31 = getelementptr inbounds [7 x i8], ptr %6, i64 0, i64 0
  %32 = load i32, ptr %5, align 4, !tbaa !9
  %33 = call i32 @tls_record_set_alert(ptr noundef %31, ptr noundef %7, i32 noundef 2, i32 noundef %32)
  %34 = getelementptr inbounds [7 x i8], ptr %6, i64 0, i64 0
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.TLS_CONNECT, ptr %35, i32 0, i32 4
  %37 = load i32, ptr %36, align 8, !tbaa !20
  %38 = call i32 @tls_record_send(ptr noundef %34, i64 noundef 7, i32 noundef %37)
  %39 = icmp ne i32 %38, 1
  br i1 %39, label %40, label %46

40:                                               ; preds = %28
  br label %41

41:                                               ; preds = %40
  %42 = load ptr, ptr @stderr, align 8, !tbaa !5
  %43 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %42, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 1604, ptr noundef @__FUNCTION__.tls_send_alert) #3
  br label %44

44:                                               ; preds = %41
  br label %45

45:                                               ; preds = %44
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %47

46:                                               ; preds = %28
  store i32 1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %47

47:                                               ; preds = %46, %45, %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 7, ptr %6) #3
  %48 = load i32, ptr %3, align 4
  ret i32 %48
}

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"", !10, i64 0, !10, i64 4, !7, i64 8, !13, i64 264, !10, i64 272, !7, i64 276, !13, i64 18720, !7, i64 18728, !7, i64 37165, !6, i64 55608, !13, i64 55616, !10, i64 55624, !7, i64 55628, !13, i64 55664, !7, i64 55672, !13, i64 57720, !7, i64 57728, !13, i64 59776, !7, i64 59784, !13, i64 61832, !14, i64 61840, !14, i64 61968, !10, i64 62096, !7, i64 62100, !7, i64 62148, !16, i64 62248, !16, i64 62424, !18, i64 62600, !18, i64 62728, !7, i64 62856, !7, i64 62864, !7, i64 62872, !7, i64 62884, !19, i64 62896, !19, i64 63032, !10, i64 63168}
!13 = !{!"long", !7, i64 0}
!14 = !{!"", !15, i64 0, !7, i64 96}
!15 = !{!"", !7, i64 0, !7, i64 32, !7, i64 64}
!16 = !{!"", !17, i64 0, !7, i64 112}
!17 = !{!"", !7, i64 0, !13, i64 32, !7, i64 40, !13, i64 104}
!18 = !{!"", !7, i64 0}
!19 = !{!"BLOCK_CIPHER_KEY", !7, i64 0, !6, i64 128}
!20 = !{!12, !10, i64 272}


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

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

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

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @tls_send_alert(ptr noundef readonly %0, i32 noundef %1) local_unnamed_addr #2 {
  %3 = alloca [7 x i8], align 1
  %4 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 7, ptr nonnull %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %5, label %8

5:                                                ; preds = %2
  %6 = load ptr, ptr @stderr, align 8, !tbaa !5
  %7 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %6, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 1596, ptr noundef nonnull @__FUNCTION__.tls_send_alert) #4
  br label %19

8:                                                ; preds = %2
  %9 = load i32, ptr %0, align 8, !tbaa !9
  %10 = icmp eq i32 %9, 772
  %spec.select = select i1 %10, i32 771, i32 %9
  %11 = call i32 @tls_record_set_protocol(ptr noundef nonnull %3, i32 noundef %spec.select)
  %12 = call i32 @tls_record_set_alert(ptr noundef nonnull %3, ptr noundef nonnull %4, i32 noundef 2, i32 noundef %1)
  %13 = getelementptr inbounds i8, ptr %0, i64 272
  %14 = load i32, ptr %13, align 8, !tbaa !19
  %15 = call i32 @tls_record_send(ptr noundef nonnull %3, i64 noundef 7, i32 noundef %14)
  %.not7 = icmp eq i32 %15, 1
  br i1 %.not7, label %19, label %16

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

19:                                               ; preds = %8, %16, %5
  %.0 = phi i32 [ -1, %16 ], [ -1, %5 ], [ 1, %8 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 7, ptr nonnull %3) #3
  ret i32 %.0
}

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { nounwind }
attributes #4 = { 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, !11, i64 0}
!10 = !{!"", !11, i64 0, !11, i64 4, !7, i64 8, !12, i64 264, !11, i64 272, !7, i64 276, !12, i64 18720, !7, i64 18728, !7, i64 37165, !6, i64 55608, !12, i64 55616, !11, i64 55624, !7, i64 55628, !12, i64 55664, !7, i64 55672, !12, i64 57720, !7, i64 57728, !12, i64 59776, !7, i64 59784, !12, i64 61832, !13, i64 61840, !13, i64 61968, !11, i64 62096, !7, i64 62100, !7, i64 62148, !15, i64 62248, !15, i64 62424, !17, i64 62600, !17, i64 62728, !7, i64 62856, !7, i64 62864, !7, i64 62872, !7, i64 62884, !18, i64 62896, !18, i64 63032, !11, i64 63168}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!"", !14, i64 0, !7, i64 96}
!14 = !{!"", !7, i64 0, !7, i64 32, !7, i64 64}
!15 = !{!"", !16, i64 0, !7, i64 112}
!16 = !{!"", !7, i64 0, !12, i64 32, !7, i64 40, !12, i64 104}
!17 = !{!"", !7, i64 0}
!18 = !{!"BLOCK_CIPHER_KEY", !7, i64 0, !6, i64 128}
!19 = !{!10, !11, i64 272}


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