; 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_ciphersuite_t = type { i32, ptr, i32, i32, i32, i32, i32, i32, i32, i8 }

@.str.153 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.154 = external hidden unnamed_addr constant [9 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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ssl_check_cert_usage(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %13 = load i32, ptr %7, align 4, !tbaa !9
  %14 = icmp eq i32 %13, 1
  br i1 %14, label %15, label %24

15:                                               ; preds = %4
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.mbedtls_ssl_ciphersuite_t, ptr %16, i32 0, i32 4
  %18 = load i32, ptr %17, align 8, !tbaa !11
  switch i32 %18, label %23 [
    i32 1, label %19
    i32 7, label %19
    i32 2, label %20
    i32 3, label %20
    i32 4, label %20
    i32 9, label %21
    i32 10, label %21
    i32 0, label %22
    i32 5, label %22
    i32 6, label %22
    i32 8, label %22
    i32 11, label %22
  ]

19:                                               ; preds = %15, %15
  store i32 32, ptr %10, align 4, !tbaa !9
  br label %23

20:                                               ; preds = %15, %15, %15
  store i32 128, ptr %10, align 4, !tbaa !9
  br label %23

21:                                               ; preds = %15, %15
  store i32 8, ptr %10, align 4, !tbaa !9
  br label %23

22:                                               ; preds = %15, %15, %15, %15, %15
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %23

23:                                               ; preds = %22, %21, %20, %19, %15
  br label %25

24:                                               ; preds = %4
  store i32 128, ptr %10, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %24, %23
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = load i32, ptr %10, align 4, !tbaa !9
  %28 = call i32 @mbedtls_x509_crt_check_key_usage(ptr noundef %26, i32 noundef %27)
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %34

30:                                               ; preds = %25
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = load i32, ptr %31, align 4, !tbaa !9
  %33 = or i32 %32, 2048
  store i32 %33, ptr %31, align 4, !tbaa !9
  store i32 -1, ptr %9, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %30, %25
  %35 = load i32, ptr %7, align 4, !tbaa !9
  %36 = icmp eq i32 %35, 1
  br i1 %36, label %37, label %38

37:                                               ; preds = %34
  store ptr @.str.153, ptr %11, align 8, !tbaa !5
  store i64 8, ptr %12, align 8, !tbaa !13
  br label %39

38:                                               ; preds = %34
  store ptr @.str.154, ptr %11, align 8, !tbaa !5
  store i64 8, ptr %12, align 8, !tbaa !13
  br label %39

39:                                               ; preds = %38, %37
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = load ptr, ptr %11, align 8, !tbaa !5
  %42 = load i64, ptr %12, align 8, !tbaa !13
  %43 = call i32 @mbedtls_x509_crt_check_extended_key_usage(ptr noundef %40, ptr noundef %41, i64 noundef %42)
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %49

45:                                               ; preds = %39
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  %47 = load i32, ptr %46, align 4, !tbaa !9
  %48 = or i32 %47, 4096
  store i32 %48, ptr %46, align 4, !tbaa !9
  store i32 -1, ptr %9, align 4, !tbaa !9
  br label %49

49:                                               ; preds = %45, %39
  %50 = load i32, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  ret i32 %50
}

declare i32 @mbedtls_x509_crt_check_key_usage(ptr noundef, i32 noundef) #2

declare i32 @mbedtls_x509_crt_check_extended_key_usage(ptr noundef, ptr noundef, i64 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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, !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, !10, i64 24}
!12 = !{!"mbedtls_ssl_ciphersuite_t", !10, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !7, i64 44}
!13 = !{!14, !14, i64 0}
!14 = !{!"long", !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"

@.str.153 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.154 = external hidden unnamed_addr constant [9 x i8], align 1
@switch.table.mbedtls_ssl_check_cert_usage = private unnamed_addr constant [10 x i32] [i32 32, i32 128, i32 128, i32 128, i32 0, i32 0, i32 32, i32 0, i32 8, i32 8], align 4

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @mbedtls_ssl_check_cert_usage(ptr noundef %0, ptr nocapture noundef readonly %1, i32 noundef %2, ptr nocapture noundef %3) local_unnamed_addr #0 {
  %5 = icmp eq i32 %2, 1
  br i1 %5, label %6, label %11

6:                                                ; preds = %4
  %7 = getelementptr inbounds i8, ptr %1, i64 24
  %8 = load i32, ptr %7, align 8, !tbaa !5
  %switch.tableidx = add i32 %8, -1
  %9 = icmp ult i32 %switch.tableidx, 10
  br i1 %9, label %switch.lookup, label %11

switch.lookup:                                    ; preds = %6
  %10 = zext nneg i32 %switch.tableidx to i64
  %switch.gep = getelementptr inbounds [10 x i32], ptr @switch.table.mbedtls_ssl_check_cert_usage, i64 0, i64 %10
  %switch.load = load i32, ptr %switch.gep, align 4
  br label %11

11:                                               ; preds = %switch.lookup, %6, %4
  %.011 = phi i32 [ 0, %6 ], [ 128, %4 ], [ %switch.load, %switch.lookup ]
  %12 = tail call i32 @mbedtls_x509_crt_check_key_usage(ptr noundef %0, i32 noundef %.011) #2
  %.not = icmp eq i32 %12, 0
  br i1 %.not, label %16, label %13

13:                                               ; preds = %11
  %14 = load i32, ptr %3, align 4, !tbaa !11
  %15 = or i32 %14, 2048
  store i32 %15, ptr %3, align 4, !tbaa !11
  br label %16

16:                                               ; preds = %13, %11
  %.012 = phi i32 [ -1, %13 ], [ 0, %11 ]
  %.str.153..str.154 = select i1 %5, ptr @.str.153, ptr @.str.154
  %17 = tail call i32 @mbedtls_x509_crt_check_extended_key_usage(ptr noundef %0, ptr noundef nonnull %.str.153..str.154, i64 noundef 8) #2
  %.not13 = icmp eq i32 %17, 0
  br i1 %.not13, label %21, label %18

18:                                               ; preds = %16
  %19 = load i32, ptr %3, align 4, !tbaa !11
  %20 = or i32 %19, 4096
  store i32 %20, ptr %3, align 4, !tbaa !11
  br label %21

21:                                               ; preds = %18, %16
  %.1 = phi i32 [ -1, %18 ], [ %.012, %16 ]
  ret i32 %.1
}

declare i32 @mbedtls_x509_crt_check_key_usage(ptr noundef, i32 noundef) local_unnamed_addr #1

declare i32 @mbedtls_x509_crt_check_extended_key_usage(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

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" }
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 = { 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 24}
!6 = !{!"mbedtls_ssl_ciphersuite_t", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 20, !7, i64 24, !7, i64 28, !7, i64 32, !7, i64 36, !7, i64 40, !8, i64 44}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!7, !7, i64 0}


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