; LLVM IR from tleonhardt/practical_cryptography_engineering :: mbedtls (ssl_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.mbedtls_ssl_context = type { ptr, i32, i32, i32, i32, i32, i32, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32, i64, i64, i16, i64, i64, i64, i64, i32, i32, ptr, ptr, ptr, ptr, ptr, ptr, i32, i64, i64, i8, i32, ptr, ptr, ptr, i64, i32, i64, [12 x i8], [12 x i8] }
%struct.mbedtls_ssl_handshake_params = type { %struct.mbedtls_ssl_sig_hash_set_t, %struct.mbedtls_dhm_context, %struct.mbedtls_ecdh_context, ptr, ptr, i64, ptr, i32, ptr, ptr, ptr, i32, i32, ptr, i8, ptr, i32, i8, ptr, ptr, i32, ptr, [8 x i8], %struct.mbedtls_md5_context, %struct.mbedtls_sha1_context, %struct.mbedtls_sha256_context, %struct.mbedtls_sha512_context, ptr, ptr, ptr, ptr, i64, [64 x i8], [1060 x i8], i32, i32, i32, i32, i32, i32 }
%struct.mbedtls_ssl_sig_hash_set_t = type { i32, i32 }
%struct.mbedtls_dhm_context = type { i64, %struct.mbedtls_mpi, %struct.mbedtls_mpi, %struct.mbedtls_mpi, %struct.mbedtls_mpi, %struct.mbedtls_mpi, %struct.mbedtls_mpi, %struct.mbedtls_mpi, %struct.mbedtls_mpi, %struct.mbedtls_mpi, %struct.mbedtls_mpi }
%struct.mbedtls_mpi = type { i32, i64, ptr }
%struct.mbedtls_ecdh_context = type { %struct.mbedtls_ecp_group, %struct.mbedtls_mpi, %struct.mbedtls_ecp_point, %struct.mbedtls_ecp_point, %struct.mbedtls_mpi, i32, %struct.mbedtls_ecp_point, %struct.mbedtls_ecp_point, %struct.mbedtls_mpi }
%struct.mbedtls_ecp_group = type { i32, %struct.mbedtls_mpi, %struct.mbedtls_mpi, %struct.mbedtls_mpi, %struct.mbedtls_ecp_point, %struct.mbedtls_mpi, i64, i64, i32, ptr, ptr, ptr, ptr, ptr, i64 }
%struct.mbedtls_ecp_point = type { %struct.mbedtls_mpi, %struct.mbedtls_mpi, %struct.mbedtls_mpi }
%struct.mbedtls_md5_context = type { [2 x i32], [4 x i32], [64 x i8] }
%struct.mbedtls_sha1_context = type { [2 x i32], [5 x i32], [64 x i8] }
%struct.mbedtls_sha256_context = type { [2 x i32], [8 x i32], [64 x i8], i32 }
%struct.mbedtls_sha512_context = type { [2 x i64], [8 x i64], [128 x i8], i32 }
%struct.mbedtls_ssl_ciphersuite_t = type { i32, ptr, i32, i32, i32, i32, i32, i32, i32, i8 }

@.str = external hidden unnamed_addr constant [97 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [20 x i8], align 1

declare void @mbedtls_debug_print_msg(ptr noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef, ...) #0

; Function Attrs: nounwind uwtable
define dso_local void @mbedtls_ssl_optimize_checksum(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.mbedtls_ssl_context, ptr %6, i32 0, i32 5
  %8 = load i32, ptr %7, align 8, !tbaa !9
  %9 = icmp slt i32 %8, 3
  br i1 %9, label %10, label %15

10:                                               ; preds = %2
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.mbedtls_ssl_context, ptr %11, i32 0, i32 15
  %13 = load ptr, ptr %12, align 8, !tbaa !14
  %14 = getelementptr inbounds %struct.mbedtls_ssl_handshake_params, ptr %13, i32 0, i32 27
  store ptr @ssl_update_checksum_md5sha1, ptr %14, align 8, !tbaa !15
  br label %39

15:                                               ; preds = %2
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.mbedtls_ssl_ciphersuite_t, ptr %16, i32 0, i32 3
  %18 = load i32, ptr %17, align 4, !tbaa !27
  %19 = icmp eq i32 %18, 7
  br i1 %19, label %20, label %25

20:                                               ; preds = %15
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.mbedtls_ssl_context, ptr %21, i32 0, i32 15
  %23 = load ptr, ptr %22, align 8, !tbaa !14
  %24 = getelementptr inbounds %struct.mbedtls_ssl_handshake_params, ptr %23, i32 0, i32 27
  store ptr @ssl_update_checksum_sha384, ptr %24, align 8, !tbaa !15
  br label %38

25:                                               ; preds = %15
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.mbedtls_ssl_ciphersuite_t, ptr %26, i32 0, i32 3
  %28 = load i32, ptr %27, align 4, !tbaa !27
  %29 = icmp ne i32 %28, 7
  br i1 %29, label %30, label %35

30:                                               ; preds = %25
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.mbedtls_ssl_context, ptr %31, i32 0, i32 15
  %33 = load ptr, ptr %32, align 8, !tbaa !14
  %34 = getelementptr inbounds %struct.mbedtls_ssl_handshake_params, ptr %33, i32 0, i32 27
  store ptr @ssl_update_checksum_sha256, ptr %34, align 8, !tbaa !15
  br label %37

35:                                               ; preds = %25
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  call void (ptr, i32, ptr, i32, ptr, ...) @mbedtls_debug_print_msg(ptr noundef %36, i32 noundef 1, ptr noundef @.str, i32 noundef 4830, ptr noundef @.str.4)
  br label %39

37:                                               ; preds = %30
  br label %38

38:                                               ; preds = %37, %20
  br label %39

39:                                               ; preds = %38, %35, %10
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @ssl_update_checksum_md5sha1(ptr noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @ssl_update_checksum_sha384(ptr noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @ssl_update_checksum_sha256(ptr noundef, ptr noundef, i64 noundef) #1

attributes #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 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" }

!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 24}
!10 = !{!"mbedtls_ssl_context", !6, i64 0, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !6, i64 160, !6, i64 168, !6, i64 176, !6, i64 184, !6, i64 192, !6, i64 200, !6, i64 208, !11, i64 216, !12, i64 224, !12, i64 232, !13, i64 240, !12, i64 248, !12, i64 256, !12, i64 264, !12, i64 272, !11, i64 280, !11, i64 284, !6, i64 288, !6, i64 296, !6, i64 304, !6, i64 312, !6, i64 320, !6, i64 328, !11, i64 336, !12, i64 344, !12, i64 352, !7, i64 360, !11, i64 364, !6, i64 368, !6, i64 376, !6, i64 384, !12, i64 392, !11, i64 400, !12, i64 408, !7, i64 416, !7, i64 428}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = !{!10, !6, i64 96}
!15 = !{!16, !6, i64 1520}
!16 = !{!"mbedtls_ssl_handshake_params", !17, i64 0, !18, i64 8, !20, i64 256, !6, i64 872, !6, i64 880, !12, i64 888, !6, i64 896, !11, i64 904, !6, i64 912, !6, i64 920, !6, i64 928, !11, i64 936, !11, i64 940, !6, i64 944, !7, i64 952, !6, i64 960, !11, i64 968, !7, i64 972, !6, i64 976, !6, i64 984, !11, i64 992, !6, i64 1000, !7, i64 1008, !23, i64 1016, !24, i64 1104, !25, i64 1196, !26, i64 1304, !6, i64 1520, !6, i64 1528, !6, i64 1536, !6, i64 1544, !12, i64 1552, !7, i64 1560, !7, i64 1624, !11, i64 2684, !11, i64 2688, !11, i64 2692, !11, i64 2696, !11, i64 2700, !11, i64 2704}
!17 = !{!"mbedtls_ssl_sig_hash_set_t", !11, i64 0, !11, i64 4}
!18 = !{!"", !12, i64 0, !19, i64 8, !19, i64 32, !19, i64 56, !19, i64 80, !19, i64 104, !19, i64 128, !19, i64 152, !19, i64 176, !19, i64 200, !19, i64 224}
!19 = !{!"", !11, i64 0, !12, i64 8, !6, i64 16}
!20 = !{!"", !21, i64 0, !19, i64 248, !22, i64 272, !22, i64 344, !19, i64 416, !11, i64 440, !22, i64 448, !22, i64 520, !19, i64 592}
!21 = !{!"", !11, i64 0, !19, i64 8, !19, i64 32, !19, i64 56, !22, i64 80, !19, i64 152, !12, i64 176, !12, i64 184, !11, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !6, i64 224, !6, i64 232, !12, i64 240}
!22 = !{!"", !19, i64 0, !19, i64 24, !19, i64 48}
!23 = !{!"", !7, i64 0, !7, i64 8, !7, i64 24}
!24 = !{!"", !7, i64 0, !7, i64 8, !7, i64 28}
!25 = !{!"", !7, i64 0, !7, i64 8, !7, i64 40, !11, i64 104}
!26 = !{!"", !7, i64 0, !7, i64 16, !7, i64 80, !11, i64 208}
!27 = !{!28, !11, i64 20}
!28 = !{!"mbedtls_ssl_ciphersuite_t", !11, i64 0, !6, i64 8, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !7, i64 44}


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

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @mbedtls_ssl_optimize_checksum(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 24
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = icmp slt i32 %4, 3
  br i1 %5, label %6, label %10

6:                                                ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 96
  %8 = load ptr, ptr %7, align 8, !tbaa !13
  %9 = getelementptr inbounds i8, ptr %8, i64 1520
  store ptr @ssl_update_checksum_md5sha1, ptr %9, align 8, !tbaa !14
  br label %19

10:                                               ; preds = %2
  %11 = getelementptr inbounds i8, ptr %1, i64 20
  %12 = load i32, ptr %11, align 4, !tbaa !26
  %13 = icmp eq i32 %12, 7
  %14 = getelementptr inbounds i8, ptr %0, i64 96
  %15 = load ptr, ptr %14, align 8, !tbaa !13
  %16 = getelementptr inbounds i8, ptr %15, i64 1520
  br i1 %13, label %17, label %18

17:                                               ; preds = %10
  store ptr @ssl_update_checksum_sha384, ptr %16, align 8, !tbaa !14
  br label %19

18:                                               ; preds = %10
  store ptr @ssl_update_checksum_sha256, ptr %16, align 8, !tbaa !14
  br label %19

19:                                               ; preds = %17, %18, %6
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @ssl_update_checksum_md5sha1(ptr noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @ssl_update_checksum_sha384(ptr noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @ssl_update_checksum_sha256(ptr noundef, ptr noundef, i64 noundef) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: none) 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 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" }

!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, !10, i64 24}
!6 = !{!"mbedtls_ssl_context", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !7, i64 88, !7, i64 96, !7, i64 104, !7, i64 112, !7, i64 120, !7, i64 128, !7, i64 136, !7, i64 144, !7, i64 152, !7, i64 160, !7, i64 168, !7, i64 176, !7, i64 184, !7, i64 192, !7, i64 200, !7, i64 208, !10, i64 216, !11, i64 224, !11, i64 232, !12, i64 240, !11, i64 248, !11, i64 256, !11, i64 264, !11, i64 272, !10, i64 280, !10, i64 284, !7, i64 288, !7, i64 296, !7, i64 304, !7, i64 312, !7, i64 320, !7, i64 328, !10, i64 336, !11, i64 344, !11, i64 352, !8, i64 360, !10, i64 364, !7, i64 368, !7, i64 376, !7, i64 384, !11, i64 392, !10, i64 400, !11, i64 408, !8, i64 416, !8, i64 428}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!"short", !8, i64 0}
!13 = !{!6, !7, i64 96}
!14 = !{!15, !7, i64 1520}
!15 = !{!"mbedtls_ssl_handshake_params", !16, i64 0, !17, i64 8, !19, i64 256, !7, i64 872, !7, i64 880, !11, i64 888, !7, i64 896, !10, i64 904, !7, i64 912, !7, i64 920, !7, i64 928, !10, i64 936, !10, i64 940, !7, i64 944, !8, i64 952, !7, i64 960, !10, i64 968, !8, i64 972, !7, i64 976, !7, i64 984, !10, i64 992, !7, i64 1000, !8, i64 1008, !22, i64 1016, !23, i64 1104, !24, i64 1196, !25, i64 1304, !7, i64 1520, !7, i64 1528, !7, i64 1536, !7, i64 1544, !11, i64 1552, !8, i64 1560, !8, i64 1624, !10, i64 2684, !10, i64 2688, !10, i64 2692, !10, i64 2696, !10, i64 2700, !10, i64 2704}
!16 = !{!"mbedtls_ssl_sig_hash_set_t", !10, i64 0, !10, i64 4}
!17 = !{!"", !11, i64 0, !18, i64 8, !18, i64 32, !18, i64 56, !18, i64 80, !18, i64 104, !18, i64 128, !18, i64 152, !18, i64 176, !18, i64 200, !18, i64 224}
!18 = !{!"", !10, i64 0, !11, i64 8, !7, i64 16}
!19 = !{!"", !20, i64 0, !18, i64 248, !21, i64 272, !21, i64 344, !18, i64 416, !10, i64 440, !21, i64 448, !21, i64 520, !18, i64 592}
!20 = !{!"", !10, i64 0, !18, i64 8, !18, i64 32, !18, i64 56, !21, i64 80, !18, i64 152, !11, i64 176, !11, i64 184, !10, i64 192, !7, i64 200, !7, i64 208, !7, i64 216, !7, i64 224, !7, i64 232, !11, i64 240}
!21 = !{!"", !18, i64 0, !18, i64 24, !18, i64 48}
!22 = !{!"", !8, i64 0, !8, i64 8, !8, i64 24}
!23 = !{!"", !8, i64 0, !8, i64 8, !8, i64 28}
!24 = !{!"", !8, i64 0, !8, i64 8, !8, i64 40, !10, i64 104}
!25 = !{!"", !8, i64 0, !8, i64 16, !8, i64 80, !10, i64 208}
!26 = !{!27, !10, i64 20}
!27 = !{!"mbedtls_ssl_ciphersuite_t", !10, i64 0, !7, i64 8, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !8, i64 44}


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