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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @tls_uint16_from_bytes(ptr noundef, 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
define dso_local i32 @tls_cipher_suites_select(ptr noundef %0, i64 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4) #1 {
  %6 = alloca i32, align 4
  %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 ptr, align 8
  %13 = alloca i64, align 8
  %14 = alloca i16, align 2
  %15 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i64 %1, ptr %8, align 8, !tbaa !9
  store ptr %2, ptr %9, align 8, !tbaa !5
  store i64 %3, ptr %10, align 8, !tbaa !9
  store ptr %4, ptr %11, align 8, !tbaa !5
  %16 = load ptr, ptr %7, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %30

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

21:                                               ; preds = %18
  %22 = load ptr, ptr %9, align 8, !tbaa !5
  %23 = icmp ne ptr %22, null
  br i1 %23, label %24, label %30

24:                                               ; preds = %21
  %25 = load i64, ptr %10, align 8, !tbaa !9
  %26 = icmp ne i64 %25, 0
  br i1 %26, label %27, label %30

27:                                               ; preds = %24
  %28 = load ptr, ptr %11, align 8, !tbaa !5
  %29 = icmp ne ptr %28, null
  br i1 %29, label %35, label %30

30:                                               ; preds = %27, %24, %21, %18, %5
  br label %31

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

34:                                               ; preds = %31
  store i32 -1, ptr %6, align 4
  br label %76

35:                                               ; preds = %27
  br label %36

36:                                               ; preds = %74, %35
  %37 = load i64, ptr %10, align 8, !tbaa !9
  %38 = add i64 %37, -1
  store i64 %38, ptr %10, align 8, !tbaa !9
  %39 = icmp ne i64 %37, 0
  br i1 %39, label %40, label %75

40:                                               ; preds = %36
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %41, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %42 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %42, ptr %13, align 8, !tbaa !9
  br label %43

43:                                               ; preds = %68, %40
  %44 = load i64, ptr %13, align 8, !tbaa !9
  %45 = icmp ne i64 %44, 0
  br i1 %45, label %46, label %69

46:                                               ; preds = %43
  call void @llvm.lifetime.start.p0(i64 2, ptr %14) #3
  %47 = call i32 @tls_uint16_from_bytes(ptr noundef %14, ptr noundef %12, ptr noundef %13)
  %48 = icmp ne i32 %47, 1
  br i1 %48, label %49, label %55

49:                                               ; preds = %46
  br label %50

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

53:                                               ; preds = %50
  br label %54

54:                                               ; preds = %53
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %66

55:                                               ; preds = %46
  %56 = load i16, ptr %14, align 2, !tbaa !11
  %57 = zext i16 %56 to i32
  %58 = load ptr, ptr %9, align 8, !tbaa !5
  %59 = load i32, ptr %58, align 4, !tbaa !13
  %60 = icmp eq i32 %57, %59
  br i1 %60, label %61, label %65

61:                                               ; preds = %55
  %62 = load ptr, ptr %9, align 8, !tbaa !5
  %63 = load i32, ptr %62, align 4, !tbaa !13
  %64 = load ptr, ptr %11, align 8, !tbaa !5
  store i32 %63, ptr %64, align 4, !tbaa !13
  store i32 1, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %66

65:                                               ; preds = %55
  store i32 0, ptr %15, align 4
  br label %66

66:                                               ; preds = %65, %61, %54
  call void @llvm.lifetime.end.p0(i64 2, ptr %14) #3
  %67 = load i32, ptr %15, align 4
  switch i32 %67, label %72 [
    i32 0, label %68
  ]

68:                                               ; preds = %66
  br label %43, !llvm.loop !15

69:                                               ; preds = %43
  %70 = load ptr, ptr %9, align 8, !tbaa !5
  %71 = getelementptr inbounds i32, ptr %70, i32 1
  store ptr %71, ptr %9, align 8, !tbaa !5
  store i32 0, ptr %15, align 4
  br label %72

72:                                               ; preds = %69, %66
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  %73 = load i32, ptr %15, align 4
  switch i32 %73, label %78 [
    i32 0, label %74
    i32 1, label %76
  ]

74:                                               ; preds = %72
  br label %36, !llvm.loop !17

75:                                               ; preds = %36
  store i32 0, ptr %6, align 4
  br label %76

76:                                               ; preds = %75, %72, %34
  %77 = load i32, ptr %6, align 4
  ret i32 %77

78:                                               ; preds = %72
  unreachable
}

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


; 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_cipher_suites_select = external hidden unnamed_addr constant [25 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: nounwind uwtable
declare dso_local i32 @tls_uint16_from_bytes(ptr noundef, ptr noundef, ptr noundef) 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
define dso_local noundef i32 @tls_cipher_suites_select(ptr noundef %0, i64 noundef %1, ptr noundef readonly %2, i64 noundef %3, ptr noundef writeonly %4) local_unnamed_addr #1 {
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i16, align 2
  %9 = icmp ne ptr %0, null
  %10 = icmp ne i64 %1, 0
  %or.cond = and i1 %9, %10
  %11 = icmp ne ptr %2, null
  %or.cond3 = and i1 %or.cond, %11
  %12 = icmp ne i64 %3, 0
  %or.cond5 = and i1 %or.cond3, %12
  %13 = icmp ne ptr %4, null
  %or.cond7 = and i1 %or.cond5, %13
  br i1 %or.cond7, label %.preheader, label %14

14:                                               ; preds = %5
  %15 = load ptr, ptr @stderr, align 8, !tbaa !5
  %16 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %15, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 2025, ptr noundef nonnull @__FUNCTION__.tls_cipher_suites_select) #3
  br label %.loopexit

.preheader:                                       ; preds = %5, %29
  %.in = phi i64 [ %17, %29 ], [ %3, %5 ]
  %.02437 = phi ptr [ %30, %29 ], [ %2, %5 ]
  %17 = add i64 %.in, -1
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #4
  store ptr %0, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %7) #4
  store i64 %1, ptr %7, align 8, !tbaa !9
  br label %18

thread-pre-split:                                 ; preds = %23
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %8) #4
  %.pr = load i64, ptr %7, align 8, !tbaa !9
  %.not29.not = icmp eq i64 %.pr, 0
  br i1 %.not29.not, label %29, label %18

18:                                               ; preds = %.preheader, %thread-pre-split
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %8) #4
  %19 = call i32 @tls_uint16_from_bytes(ptr noundef nonnull %8, ptr noundef nonnull %6, ptr noundef nonnull %7)
  %.not30 = icmp eq i32 %19, 1
  br i1 %.not30, label %23, label %20

20:                                               ; preds = %18
  %21 = load ptr, ptr @stderr, align 8, !tbaa !5
  %22 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %21, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 2034, ptr noundef nonnull @__FUNCTION__.tls_cipher_suites_select) #3
  br label %.thread33

23:                                               ; preds = %18
  %24 = load i16, ptr %8, align 2, !tbaa !11
  %25 = zext i16 %24 to i32
  %26 = load i32, ptr %.02437, align 4, !tbaa !13
  %27 = icmp eq i32 %26, %25
  br i1 %27, label %28, label %thread-pre-split

28:                                               ; preds = %23
  store i32 %25, ptr %4, align 4, !tbaa !13
  br label %.thread33

.thread33:                                        ; preds = %28, %20
  %.2.ph = phi i32 [ 1, %28 ], [ -1, %20 ]
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #4
  br label %.loopexit

29:                                               ; preds = %thread-pre-split
  %30 = getelementptr inbounds i8, ptr %.02437, i64 4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #4
  %.not = icmp eq i64 %17, 0
  br i1 %.not, label %.loopexit, label %.preheader

.loopexit:                                        ; preds = %29, %.thread33, %14
  %.4 = phi i32 [ -1, %14 ], [ %.2.ph, %.thread33 ], [ 0, %29 ]
  ret i32 %.4
}

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 = { 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 #2 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { cold nounwind }
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 = !{!"short", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass