; 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_config = type { [4 x ptr], ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, %struct.mbedtls_mpi, %struct.mbedtls_mpi, ptr, i64, ptr, i64, ptr, i32, i32, i32, i32, [8 x i8], i32, i32, i8, i8, i8, i8, i32 }
%struct.mbedtls_mpi = type { i32, i64, ptr }

@mfl_code_to_length = external hidden global [5 x i32], align 16

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ssl_conf_max_frag_len(ptr noundef %0, i8 noundef zeroext %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i8, align 1
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i8 %1, ptr %5, align 1, !tbaa !9
  %6 = load i8, ptr %5, align 1, !tbaa !9
  %7 = zext i8 %6 to i32
  %8 = icmp sge i32 %7, 5
  br i1 %8, label %15, label %9

9:                                                ; preds = %2
  %10 = load i8, ptr %5, align 1, !tbaa !9
  %11 = zext i8 %10 to i64
  %12 = getelementptr inbounds [5 x i32], ptr @mfl_code_to_length, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !10
  %14 = icmp ugt i32 %13, 16384
  br i1 %14, label %15, label %16

15:                                               ; preds = %9, %2
  store i32 -28928, ptr %3, align 4
  br label %26

16:                                               ; preds = %9
  %17 = load i8, ptr %5, align 1, !tbaa !9
  %18 = zext i8 %17 to i32
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %19, i32 0, i32 46
  %21 = load i32, ptr %20, align 4
  %22 = and i32 %18, 7
  %23 = shl i32 %22, 7
  %24 = and i32 %21, -897
  %25 = or i32 %24, %23
  store i32 %25, ptr %20, align 4
  store i32 0, ptr %3, align 4
  br label %26

26:                                               ; preds = %16, %15
  %27 = load i32, ptr %3, align 4
  ret i32 %27
}

attributes #0 = { 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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !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"

@mfl_code_to_length = external hidden local_unnamed_addr global [5 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local range(i32 -28928, 1) i32 @mbedtls_ssl_conf_max_frag_len(ptr nocapture noundef %0, i8 noundef zeroext %1) local_unnamed_addr #0 {
  %3 = zext i8 %1 to i32
  %4 = icmp ugt i8 %1, 4
  br i1 %4, label %16, label %5

5:                                                ; preds = %2
  %6 = zext nneg i8 %1 to i64
  %7 = getelementptr inbounds [5 x i32], ptr @mfl_code_to_length, i64 0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = icmp ugt i32 %8, 16384
  br i1 %9, label %16, label %10

10:                                               ; preds = %5
  %11 = getelementptr inbounds i8, ptr %0, i64 372
  %12 = load i32, ptr %11, align 4
  %13 = shl nuw nsw i32 %3, 7
  %14 = and i32 %12, -897
  %15 = or disjoint i32 %14, %13
  store i32 %15, ptr %11, align 4
  br label %16

16:                                               ; preds = %2, %5, %10
  %.0 = phi i32 [ 0, %10 ], [ -28928, %5 ], [ -28928, %2 ]
  ret i32 %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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" }

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


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