; 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_transform = type { ptr, i32, i64, i64, i64, i64, [16 x i8], [16 x i8], %struct.mbedtls_md_context_t, %struct.mbedtls_md_context_t, %struct.mbedtls_cipher_context_t, %struct.mbedtls_cipher_context_t }
%struct.mbedtls_md_context_t = type { ptr, ptr, ptr }
%struct.mbedtls_cipher_context_t = type { ptr, i32, i32, ptr, ptr, [16 x i8], i64, [16 x i8], i64, ptr }

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

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

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @mbedtls_ssl_hdr_len(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ssl_get_record_expansion(ptr noundef %0) #3 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.mbedtls_ssl_context, ptr %7, i32 0, i32 17
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  store ptr %9, ptr %5, align 8, !tbaa !5
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = icmp eq ptr %10, null
  br i1 %11, label %12, label %16

12:                                               ; preds = %1
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = call i64 @mbedtls_ssl_hdr_len(ptr noundef %13)
  %15 = trunc i64 %14 to i32
  store i32 %15, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %41

16:                                               ; preds = %1
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.mbedtls_ssl_transform, ptr %17, i32 0, i32 10
  %19 = call i32 @mbedtls_cipher_get_cipher_mode(ptr noundef %18)
  switch i32 %19, label %33 [
    i32 6, label %20
    i32 8, label %20
    i32 7, label %20
    i32 2, label %24
  ]

20:                                               ; preds = %16, %16, %16
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.mbedtls_ssl_transform, ptr %21, i32 0, i32 2
  %23 = load i64, ptr %22, align 8, !tbaa !14
  store i64 %23, ptr %4, align 8, !tbaa !18
  br label %35

24:                                               ; preds = %16
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.mbedtls_ssl_transform, ptr %25, i32 0, i32 5
  %27 = load i64, ptr %26, align 8, !tbaa !19
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.mbedtls_ssl_transform, ptr %28, i32 0, i32 10
  %30 = call i32 @mbedtls_cipher_get_block_size(ptr noundef %29)
  %31 = zext i32 %30 to i64
  %32 = add i64 %27, %31
  store i64 %32, ptr %4, align 8, !tbaa !18
  br label %35

33:                                               ; preds = %16
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  call void (ptr, i32, ptr, i32, ptr, ...) @mbedtls_debug_print_msg(ptr noundef %34, i32 noundef 1, ptr noundef @.str, i32 noundef 6470, ptr noundef @.str.4)
  store i32 -27648, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %41

35:                                               ; preds = %24, %20
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = call i64 @mbedtls_ssl_hdr_len(ptr noundef %36)
  %38 = load i64, ptr %4, align 8, !tbaa !18
  %39 = add i64 %37, %38
  %40 = trunc i64 %39 to i32
  store i32 %40, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %41

41:                                               ; preds = %35, %33, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %42 = load i32, ptr %2, align 4
  ret i32 %42
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @mbedtls_cipher_get_cipher_mode(ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @mbedtls_cipher_get_block_size(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { inlinehint 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 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 = { 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, !6, i64 112}
!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 = !{!15, !12, i64 16}
!15 = !{!"mbedtls_ssl_transform", !6, i64 0, !11, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40, !7, i64 48, !7, i64 64, !16, i64 80, !16, i64 104, !17, i64 128, !17, i64 216}
!16 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16}
!17 = !{!"", !6, i64 0, !11, i64 8, !11, i64 12, !6, i64 16, !6, i64 24, !7, i64 32, !12, i64 48, !7, i64 56, !12, i64 72, !6, i64 80}
!18 = !{!12, !12, i64 0}
!19 = !{!15, !12, i64 40}


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

@.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, ...) local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @mbedtls_ssl_hdr_len(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ssl_get_record_expansion(ptr noundef %0) local_unnamed_addr #2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 112
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  br i1 %4, label %5, label %8

5:                                                ; preds = %1
  %6 = tail call i64 @mbedtls_ssl_hdr_len(ptr noundef nonnull %0)
  %7 = trunc i64 %6 to i32
  br label %25

8:                                                ; preds = %1
  %9 = getelementptr inbounds i8, ptr %3, i64 128
  %10 = tail call i32 @mbedtls_cipher_get_cipher_mode(ptr noundef nonnull %9)
  switch i32 %10, label %20 [
    i32 6, label %11
    i32 8, label %11
    i32 7, label %11
    i32 2, label %14
  ]

11:                                               ; preds = %8, %8, %8
  %12 = getelementptr inbounds i8, ptr %3, i64 16
  %13 = load i64, ptr %12, align 8, !tbaa !13
  br label %21

14:                                               ; preds = %8
  %15 = getelementptr inbounds i8, ptr %3, i64 40
  %16 = load i64, ptr %15, align 8, !tbaa !17
  %17 = tail call i32 @mbedtls_cipher_get_block_size(ptr noundef nonnull %9)
  %18 = zext i32 %17 to i64
  %19 = add i64 %16, %18
  br label %21

20:                                               ; preds = %8
  tail call void (ptr, i32, ptr, i32, ptr, ...) @mbedtls_debug_print_msg(ptr noundef nonnull %0, i32 noundef 1, ptr noundef nonnull @.str, i32 noundef 6470, ptr noundef nonnull @.str.4) #3
  br label %25

21:                                               ; preds = %14, %11
  %.010 = phi i64 [ %19, %14 ], [ %13, %11 ]
  %22 = tail call i64 @mbedtls_ssl_hdr_len(ptr noundef nonnull %0)
  %23 = add i64 %22, %.010
  %24 = trunc i64 %23 to i32
  br label %25

25:                                               ; preds = %21, %20, %5
  %.0 = phi i32 [ %7, %5 ], [ -27648, %20 ], [ %24, %21 ]
  ret i32 %.0
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @mbedtls_cipher_get_cipher_mode(ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @mbedtls_cipher_get_block_size(ptr noundef) local_unnamed_addr #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 = { inlinehint 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 = { 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, !7, i64 112}
!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 = !{!14, !11, i64 16}
!14 = !{!"mbedtls_ssl_transform", !7, i64 0, !10, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !8, i64 48, !8, i64 64, !15, i64 80, !15, i64 104, !16, i64 128, !16, i64 216}
!15 = !{!"", !7, i64 0, !7, i64 8, !7, i64 16}
!16 = !{!"", !7, i64 0, !10, i64 8, !10, i64 12, !7, i64 16, !7, i64 24, !8, i64 32, !11, i64 48, !8, i64 56, !11, i64 72, !7, i64 80}
!17 = !{!14, !11, i64 40}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,TailCallElimPass