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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #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 ptr @tls_protocol_name(i32 noundef) #3

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

16:                                               ; preds = %5
  %17 = load ptr, ptr %8, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %25, label %19

19:                                               ; preds = %16, %5
  br label %20

20:                                               ; preds = %19
  %21 = load ptr, ptr @stderr, align 8, !tbaa !5
  %22 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %21, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 600, ptr noundef @__FUNCTION__.tls_record_set_handshake) #5
  br label %23

23:                                               ; preds = %20
  br label %24

24:                                               ; preds = %23
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %108

25:                                               ; preds = %16
  %26 = load i64, ptr %11, align 8, !tbaa !11
  %27 = icmp ugt i64 %26, 16380
  br i1 %27, label %28, label %34

28:                                               ; preds = %25
  br label %29

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

32:                                               ; preds = %29
  br label %33

33:                                               ; preds = %32
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %108

34:                                               ; preds = %25
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  %36 = getelementptr inbounds i8, ptr %35, i64 1
  %37 = load i8, ptr %36, align 1, !tbaa !13
  %38 = zext i8 %37 to i16
  %39 = zext i16 %38 to i32
  %40 = shl i32 %39, 8
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = getelementptr inbounds i8, ptr %41, i64 2
  %43 = load i8, ptr %42, align 1, !tbaa !13
  %44 = zext i8 %43 to i32
  %45 = or i32 %40, %44
  %46 = call ptr @tls_protocol_name(i32 noundef %45)
  %47 = icmp ne ptr %46, null
  br i1 %47, label %54, label %48

48:                                               ; preds = %34
  br label %49

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

52:                                               ; preds = %49
  br label %53

53:                                               ; preds = %52
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %108

54:                                               ; preds = %34
  %55 = load i32, ptr %9, align 4, !tbaa !9
  %56 = call ptr @tls_handshake_type_name(i32 noundef %55)
  %57 = icmp ne ptr %56, null
  br i1 %57, label %64, label %58

58:                                               ; preds = %54
  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 613, ptr noundef @__FUNCTION__.tls_record_set_handshake) #5
  br label %62

62:                                               ; preds = %59
  br label %63

63:                                               ; preds = %62
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %108

64:                                               ; preds = %54
  %65 = load i64, ptr %11, align 8, !tbaa !11
  %66 = add i64 4, %65
  store i64 %66, ptr %12, align 8, !tbaa !11
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = getelementptr inbounds i8, ptr %67, i64 0
  store i8 22, ptr %68, align 1, !tbaa !13
  %69 = load i64, ptr %12, align 8, !tbaa !11
  %70 = lshr i64 %69, 8
  %71 = trunc i64 %70 to i8
  %72 = load ptr, ptr %7, align 8, !tbaa !5
  %73 = getelementptr inbounds i8, ptr %72, i64 3
  store i8 %71, ptr %73, align 1, !tbaa !13
  %74 = load i64, ptr %12, align 8, !tbaa !11
  %75 = trunc i64 %74 to i8
  %76 = load ptr, ptr %7, align 8, !tbaa !5
  %77 = getelementptr inbounds i8, ptr %76, i64 4
  store i8 %75, ptr %77, align 1, !tbaa !13
  %78 = load i32, ptr %9, align 4, !tbaa !9
  %79 = trunc i32 %78 to i8
  %80 = load ptr, ptr %7, align 8, !tbaa !5
  %81 = getelementptr inbounds i8, ptr %80, i64 5
  store i8 %79, ptr %81, align 1, !tbaa !13
  %82 = load i64, ptr %11, align 8, !tbaa !11
  %83 = lshr i64 %82, 16
  %84 = trunc i64 %83 to i8
  %85 = load ptr, ptr %7, align 8, !tbaa !5
  %86 = getelementptr inbounds i8, ptr %85, i64 6
  store i8 %84, ptr %86, align 1, !tbaa !13
  %87 = load i64, ptr %11, align 8, !tbaa !11
  %88 = lshr i64 %87, 8
  %89 = trunc i64 %88 to i8
  %90 = load ptr, ptr %7, align 8, !tbaa !5
  %91 = getelementptr inbounds i8, ptr %90, i64 7
  store i8 %89, ptr %91, align 1, !tbaa !13
  %92 = load i64, ptr %11, align 8, !tbaa !11
  %93 = trunc i64 %92 to i8
  %94 = load ptr, ptr %7, align 8, !tbaa !5
  %95 = getelementptr inbounds i8, ptr %94, i64 8
  store i8 %93, ptr %95, align 1, !tbaa !13
  %96 = load ptr, ptr %10, align 8, !tbaa !5
  %97 = icmp ne ptr %96, null
  br i1 %97, label %98, label %104

98:                                               ; preds = %64
  %99 = load ptr, ptr %7, align 8, !tbaa !5
  %100 = getelementptr inbounds i8, ptr %99, i64 5
  %101 = getelementptr inbounds i8, ptr %100, i64 4
  %102 = load ptr, ptr %10, align 8, !tbaa !5
  %103 = load i64, ptr %11, align 8, !tbaa !11
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %101, ptr align 1 %102, i64 %103, i1 false)
  br label %104

104:                                              ; preds = %98, %64
  %105 = load i64, ptr %12, align 8, !tbaa !11
  %106 = add i64 5, %105
  %107 = load ptr, ptr %8, align 8, !tbaa !5
  store i64 %106, ptr %107, align 8, !tbaa !11
  store i32 1, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %108

108:                                              ; preds = %104, %63, %53, %33, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  %109 = load i32, ptr %6, align 4
  ret i32 %109
}

declare ptr @tls_handshake_type_name(i32 noundef) #3

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 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 #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, !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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, i64 0}


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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

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

declare ptr @tls_protocol_name(i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @tls_record_set_handshake(ptr noundef %0, ptr noundef writeonly %1, i32 noundef %2, ptr noundef readonly %3, i64 noundef %4) local_unnamed_addr #3 {
  %6 = icmp ne ptr %0, null
  %7 = icmp ne ptr %1, null
  %or.cond = and i1 %6, %7
  br i1 %or.cond, label %11, label %8

8:                                                ; preds = %5
  %9 = load ptr, ptr @stderr, align 8, !tbaa !5
  %10 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %9, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 600, ptr noundef nonnull @__FUNCTION__.tls_record_set_handshake) #4
  br label %53

11:                                               ; preds = %5
  %12 = icmp ugt i64 %4, 16380
  br i1 %12, label %13, label %16

13:                                               ; preds = %11
  %14 = load ptr, ptr @stderr, align 8, !tbaa !5
  %15 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %14, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 605, ptr noundef nonnull @__FUNCTION__.tls_record_set_handshake) #4
  br label %53

16:                                               ; preds = %11
  %17 = getelementptr inbounds i8, ptr %0, i64 1
  %18 = load i8, ptr %17, align 1, !tbaa !9
  %19 = zext i8 %18 to i32
  %20 = shl nuw nsw i32 %19, 8
  %21 = getelementptr inbounds i8, ptr %0, i64 2
  %22 = load i8, ptr %21, align 1, !tbaa !9
  %23 = zext i8 %22 to i32
  %24 = or disjoint i32 %20, %23
  %25 = tail call ptr @tls_protocol_name(i32 noundef %24) #5
  %.not = icmp eq ptr %25, null
  br i1 %.not, label %26, label %29

26:                                               ; preds = %16
  %27 = load ptr, ptr @stderr, align 8, !tbaa !5
  %28 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %27, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 609, ptr noundef nonnull @__FUNCTION__.tls_record_set_handshake) #4
  br label %53

29:                                               ; preds = %16
  %30 = tail call ptr @tls_handshake_type_name(i32 noundef %2) #5
  %.not31 = icmp eq ptr %30, null
  br i1 %.not31, label %31, label %34

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

34:                                               ; preds = %29
  %35 = add nuw nsw i64 %4, 4
  store i8 22, ptr %0, align 1, !tbaa !9
  %36 = lshr i64 %35, 8
  %37 = trunc nuw nsw i64 %36 to i8
  %38 = getelementptr inbounds i8, ptr %0, i64 3
  store i8 %37, ptr %38, align 1, !tbaa !9
  %39 = trunc i64 %35 to i8
  %40 = getelementptr inbounds i8, ptr %0, i64 4
  store i8 %39, ptr %40, align 1, !tbaa !9
  %41 = trunc i32 %2 to i8
  %42 = getelementptr inbounds i8, ptr %0, i64 5
  store i8 %41, ptr %42, align 1, !tbaa !9
  %43 = getelementptr inbounds i8, ptr %0, i64 6
  store i8 0, ptr %43, align 1, !tbaa !9
  %44 = lshr i64 %4, 8
  %45 = trunc nuw nsw i64 %44 to i8
  %46 = getelementptr inbounds i8, ptr %0, i64 7
  store i8 %45, ptr %46, align 1, !tbaa !9
  %47 = trunc i64 %4 to i8
  %48 = getelementptr inbounds i8, ptr %0, i64 8
  store i8 %47, ptr %48, align 1, !tbaa !9
  %.not32 = icmp eq ptr %3, null
  br i1 %.not32, label %51, label %49

49:                                               ; preds = %34
  %50 = getelementptr inbounds i8, ptr %0, i64 9
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %50, ptr nonnull align 1 %3, i64 %4, i1 false)
  br label %51

51:                                               ; preds = %49, %34
  %52 = add nuw nsw i64 %4, 9
  store i64 %52, ptr %1, align 8, !tbaa !10
  br label %53

53:                                               ; preds = %51, %31, %26, %13, %8
  %.0 = phi i32 [ -1, %13 ], [ 1, %51 ], [ -1, %31 ], [ -1, %26 ], [ -1, %8 ]
  ret i32 %.0
}

declare ptr @tls_handshake_type_name(i32 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { "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 = { cold nounwind }
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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !7, i64 0}


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