; 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_decrypt_recv = external hidden unnamed_addr constant [17 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_decrypt(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef) #2

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @tls_decrypt_recv(ptr noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %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
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.TLS_CONNECT, ptr %11, i32 0, i32 7
  %13 = getelementptr inbounds [18437 x i8], ptr %12, i64 0, i64 0
  store ptr %13, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.TLS_CONNECT, ptr %14, i32 0, i32 1
  %16 = load i32, ptr %15, align 4, !tbaa !9
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %26

18:                                               ; preds = %1
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.TLS_CONNECT, ptr %19, i32 0, i32 26
  store ptr %20, ptr %5, align 8, !tbaa !5
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.TLS_CONNECT, ptr %21, i32 0, i32 28
  store ptr %22, ptr %6, align 8, !tbaa !5
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.TLS_CONNECT, ptr %23, i32 0, i32 30
  %25 = getelementptr inbounds [8 x i8], ptr %24, i64 0, i64 0
  store ptr %25, ptr %7, align 8, !tbaa !5
  br label %34

26:                                               ; preds = %1
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.TLS_CONNECT, ptr %27, i32 0, i32 25
  store ptr %28, ptr %5, align 8, !tbaa !5
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.TLS_CONNECT, ptr %29, i32 0, i32 27
  store ptr %30, ptr %6, align 8, !tbaa !5
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.TLS_CONNECT, ptr %31, i32 0, i32 29
  %33 = getelementptr inbounds [8 x i8], ptr %32, i64 0, i64 0
  store ptr %33, ptr %7, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %26, %18
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.TLS_CONNECT, ptr %36, i32 0, i32 4
  %38 = load i32, ptr %37, align 8, !tbaa !19
  %39 = call i32 @tls_record_recv(ptr noundef %35, ptr noundef %9, i32 noundef %38)
  store i32 %39, ptr %4, align 4, !tbaa !20
  %40 = icmp ne i32 %39, 1
  br i1 %40, label %41, label %55

41:                                               ; preds = %34
  %42 = load i32, ptr %4, align 4, !tbaa !20
  %43 = icmp slt i32 %42, 0
  br i1 %43, label %44, label %53

44:                                               ; preds = %41
  %45 = load i32, ptr %4, align 4, !tbaa !20
  %46 = icmp ne i32 %45, -11
  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 1743, ptr noundef @__FUNCTION__.tls_decrypt_recv) #3
  br label %51

51:                                               ; preds = %48
  br label %52

52:                                               ; preds = %51
  br label %53

53:                                               ; preds = %52, %44, %41
  %54 = load i32, ptr %4, align 4, !tbaa !20
  store i32 %54, ptr %2, align 4
  store i32 1, ptr %10, align 4
  br label %99

55:                                               ; preds = %34
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = load ptr, ptr %8, align 8, !tbaa !5
  %60 = load i64, ptr %9, align 8, !tbaa !21
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.TLS_CONNECT, ptr %61, i32 0, i32 8
  %63 = getelementptr inbounds [18437 x i8], ptr %62, i64 0, i64 0
  %64 = load ptr, ptr %3, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.TLS_CONNECT, ptr %64, i32 0, i32 10
  %66 = call i32 @tls_record_decrypt(ptr noundef %56, ptr noundef %57, ptr noundef %58, ptr noundef %59, i64 noundef %60, ptr noundef %63, ptr noundef %65)
  %67 = icmp ne i32 %66, 1
  br i1 %67, label %68, label %74

68:                                               ; preds = %55
  br label %69

69:                                               ; preds = %68
  %70 = load ptr, ptr @stderr, align 8, !tbaa !5
  %71 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %70, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 1751, ptr noundef @__FUNCTION__.tls_decrypt_recv) #3
  br label %72

72:                                               ; preds = %69
  br label %73

73:                                               ; preds = %72
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %10, align 4
  br label %99

74:                                               ; preds = %55
  %75 = load ptr, ptr %7, align 8, !tbaa !5
  %76 = call i32 @tls_seq_num_incr(ptr noundef %75)
  %77 = load ptr, ptr %3, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.TLS_CONNECT, ptr %77, i32 0, i32 8
  %79 = getelementptr inbounds [18437 x i8], ptr %78, i64 0, i64 0
  %80 = getelementptr inbounds i8, ptr %79, i64 5
  %81 = load ptr, ptr %3, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.TLS_CONNECT, ptr %81, i32 0, i32 9
  store ptr %80, ptr %82, align 8, !tbaa !22
  %83 = load ptr, ptr %3, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.TLS_CONNECT, ptr %83, i32 0, i32 8
  %85 = getelementptr inbounds [18437 x i8], ptr %84, i64 0, i64 3
  %86 = load i8, ptr %85, align 1, !tbaa !23
  %87 = zext i8 %86 to i16
  %88 = zext i16 %87 to i32
  %89 = shl i32 %88, 8
  %90 = load ptr, ptr %3, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.TLS_CONNECT, ptr %90, i32 0, i32 8
  %92 = getelementptr inbounds [18437 x i8], ptr %91, i64 0, i64 4
  %93 = load i8, ptr %92, align 1, !tbaa !23
  %94 = zext i8 %93 to i32
  %95 = or i32 %89, %94
  %96 = sext i32 %95 to i64
  %97 = load ptr, ptr %3, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.TLS_CONNECT, ptr %97, i32 0, i32 10
  store i64 %96, ptr %98, align 8, !tbaa !24
  store i32 1, ptr %2, align 4
  store i32 1, ptr %10, align 4
  br label %99

99:                                               ; preds = %74, %73, %53
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %100 = load i32, ptr %2, align 4
  ret i32 %100
}

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, !11, i64 4}
!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}
!20 = !{!11, !11, i64 0}
!21 = !{!12, !12, i64 0}
!22 = !{!10, !6, i64 55608}
!23 = !{!7, !7, i64 0}
!24 = !{!10, !12, i64 55616}


; 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_decrypt_recv = external hidden unnamed_addr constant [17 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_decrypt(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @tls_decrypt_recv(ptr noundef %0) local_unnamed_addr #2 {
  %2 = alloca i64, align 8
  %3 = getelementptr inbounds i8, ptr %0, i64 18728
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #3
  %4 = getelementptr inbounds i8, ptr %0, i64 4
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %.not = icmp eq i32 %5, 0
  %.026.v = select i1 %.not, i64 62856, i64 62864
  %.026 = getelementptr inbounds i8, ptr %0, i64 %.026.v
  %6 = getelementptr inbounds i8, ptr %0, i64 272
  %7 = load i32, ptr %6, align 8, !tbaa !18
  %8 = call i32 @tls_record_recv(ptr noundef nonnull %3, ptr noundef nonnull %2, i32 noundef %7)
  %.not30 = icmp eq i32 %8, 1
  br i1 %.not30, label %15, label %9

9:                                                ; preds = %1
  %10 = icmp slt i32 %8, 0
  %11 = icmp ne i32 %8, -11
  %or.cond = and i1 %10, %11
  br i1 %or.cond, label %12, label %35

12:                                               ; preds = %9
  %13 = load ptr, ptr @stderr, align 8, !tbaa !19
  %14 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %13, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 1743, ptr noundef nonnull @__FUNCTION__.tls_decrypt_recv) #4
  br label %35

15:                                               ; preds = %1
  %.027.v = select i1 %.not, i64 62600, i64 62728
  %.027 = getelementptr inbounds i8, ptr %0, i64 %.027.v
  %.028.v = select i1 %.not, i64 62248, i64 62424
  %.028 = getelementptr inbounds i8, ptr %0, i64 %.028.v
  %16 = load i64, ptr %2, align 8, !tbaa !20
  %17 = getelementptr inbounds i8, ptr %0, i64 37165
  %18 = getelementptr inbounds i8, ptr %0, i64 55616
  %19 = call i32 @tls_record_decrypt(ptr noundef nonnull %.028, ptr noundef nonnull %.027, ptr noundef nonnull %.026, ptr noundef nonnull %3, i64 noundef %16, ptr noundef nonnull %17, ptr noundef nonnull %18)
  %.not31 = icmp eq i32 %19, 1
  br i1 %.not31, label %23, label %20

20:                                               ; preds = %15
  %21 = load ptr, ptr @stderr, align 8, !tbaa !19
  %22 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %21, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 1751, ptr noundef nonnull @__FUNCTION__.tls_decrypt_recv) #4
  br label %35

23:                                               ; preds = %15
  %24 = call i32 @tls_seq_num_incr(ptr noundef nonnull %.026)
  %25 = getelementptr inbounds i8, ptr %0, i64 37170
  %26 = getelementptr inbounds i8, ptr %0, i64 55608
  store ptr %25, ptr %26, align 8, !tbaa !21
  %27 = getelementptr inbounds i8, ptr %0, i64 37168
  %28 = load i8, ptr %27, align 1, !tbaa !22
  %29 = zext i8 %28 to i64
  %30 = shl nuw nsw i64 %29, 8
  %31 = getelementptr inbounds i8, ptr %0, i64 37169
  %32 = load i8, ptr %31, align 1, !tbaa !22
  %33 = zext i8 %32 to i64
  %34 = or disjoint i64 %30, %33
  store i64 %34, ptr %18, align 8, !tbaa !23
  br label %35

35:                                               ; preds = %9, %12, %23, %20
  %.0 = phi i32 [ -1, %20 ], [ 1, %23 ], [ %8, %12 ], [ %8, %9 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #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, !7, i64 4}
!6 = !{!"", !7, i64 0, !7, i64 4, !8, i64 8, !10, i64 264, !7, i64 272, !8, i64 276, !10, i64 18720, !8, i64 18728, !8, i64 37165, !11, i64 55608, !10, i64 55616, !7, i64 55624, !8, i64 55628, !10, i64 55664, !8, i64 55672, !10, i64 57720, !8, i64 57728, !10, i64 59776, !8, i64 59784, !10, i64 61832, !12, i64 61840, !12, i64 61968, !7, i64 62096, !8, i64 62100, !8, i64 62148, !14, i64 62248, !14, i64 62424, !16, i64 62600, !16, i64 62728, !8, i64 62856, !8, i64 62864, !8, i64 62872, !8, i64 62884, !17, i64 62896, !17, i64 63032, !7, i64 63168}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!"", !13, i64 0, !8, i64 96}
!13 = !{!"", !8, i64 0, !8, i64 32, !8, i64 64}
!14 = !{!"", !15, i64 0, !8, i64 112}
!15 = !{!"", !8, i64 0, !10, i64 32, !8, i64 40, !10, i64 104}
!16 = !{!"", !8, i64 0}
!17 = !{!"BLOCK_CIPHER_KEY", !8, i64 0, !11, i64 128}
!18 = !{!6, !7, i64 272}
!19 = !{!11, !11, i64 0}
!20 = !{!10, !10, i64 0}
!21 = !{!6, !11, i64 55608}
!22 = !{!8, !8, i64 0}
!23 = !{!6, !10, i64 55616}


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