; LLVM IR from tleonhardt/practical_cryptography_engineering :: mbedtls (pk)

; 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_pk_context = type { ptr, ptr }
%struct.mbedtls_pk_rsassa_pss_options = type { i32, i32 }

; 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
declare dso_local i32 @mbedtls_pk_can_do(ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_pk_verify(ptr noundef, i32 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_pk_verify_ext(i32 noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6, i64 noundef %7) #1 {
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca ptr, align 8
  %15 = alloca i64, align 8
  %16 = alloca ptr, align 8
  %17 = alloca i64, align 8
  %18 = alloca i32, align 4
  %19 = alloca ptr, align 8
  %20 = alloca i32, align 4
  store i32 %0, ptr %10, align 4, !tbaa !5
  store ptr %1, ptr %11, align 8, !tbaa !9
  store ptr %2, ptr %12, align 8, !tbaa !9
  store i32 %3, ptr %13, align 4, !tbaa !5
  store ptr %4, ptr %14, align 8, !tbaa !9
  store i64 %5, ptr %15, align 8, !tbaa !11
  store ptr %6, ptr %16, align 8, !tbaa !9
  store i64 %7, ptr %17, align 8, !tbaa !11
  %21 = load ptr, ptr %12, align 8, !tbaa !9
  %22 = icmp eq ptr %21, null
  br i1 %22, label %28, label %23

23:                                               ; preds = %8
  %24 = load ptr, ptr %12, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.mbedtls_pk_context, ptr %24, i32 0, i32 0
  %26 = load ptr, ptr %25, align 8, !tbaa !13
  %27 = icmp eq ptr %26, null
  br i1 %27, label %28, label %29

28:                                               ; preds = %23, %8
  store i32 -16000, ptr %9, align 4
  br label %99

29:                                               ; preds = %23
  %30 = load ptr, ptr %12, align 8, !tbaa !9
  %31 = load i32, ptr %10, align 4, !tbaa !5
  %32 = call i32 @mbedtls_pk_can_do(ptr noundef %30, i32 noundef %31)
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %35, label %34

34:                                               ; preds = %29
  store i32 -16128, ptr %9, align 4
  br label %99

35:                                               ; preds = %29
  %36 = load i32, ptr %10, align 4, !tbaa !5
  %37 = icmp eq i32 %36, 6
  br i1 %37, label %38, label %87

38:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 4, ptr %18) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %19) #4
  %39 = load i32, ptr %13, align 4, !tbaa !5
  %40 = icmp eq i32 %39, 0
  br i1 %40, label %41, label %45

41:                                               ; preds = %38
  %42 = load i64, ptr %15, align 8, !tbaa !11
  %43 = icmp ult i64 4294967295, %42
  br i1 %43, label %44, label %45

44:                                               ; preds = %41
  store i32 -16000, ptr %9, align 4
  store i32 1, ptr %20, align 4
  br label %86

45:                                               ; preds = %41, %38
  %46 = load ptr, ptr %11, align 8, !tbaa !9
  %47 = icmp eq ptr %46, null
  br i1 %47, label %48, label %49

48:                                               ; preds = %45
  store i32 -16000, ptr %9, align 4
  store i32 1, ptr %20, align 4
  br label %86

49:                                               ; preds = %45
  %50 = load ptr, ptr %11, align 8, !tbaa !9
  store ptr %50, ptr %19, align 8, !tbaa !9
  %51 = load i64, ptr %17, align 8, !tbaa !11
  %52 = load ptr, ptr %12, align 8, !tbaa !9
  %53 = call i64 @mbedtls_pk_get_len(ptr noundef %52)
  %54 = icmp ult i64 %51, %53
  br i1 %54, label %55, label %56

55:                                               ; preds = %49
  store i32 -17280, ptr %9, align 4
  store i32 1, ptr %20, align 4
  br label %86

56:                                               ; preds = %49
  %57 = load ptr, ptr %12, align 8, !tbaa !9
  %58 = getelementptr inbounds { ptr, ptr }, ptr %57, i32 0, i32 0
  %59 = load ptr, ptr %58, align 8
  %60 = getelementptr inbounds { ptr, ptr }, ptr %57, i32 0, i32 1
  %61 = load ptr, ptr %60, align 8
  %62 = call ptr @mbedtls_pk_rsa(ptr %59, ptr %61)
  %63 = load i32, ptr %13, align 4, !tbaa !5
  %64 = load i64, ptr %15, align 8, !tbaa !11
  %65 = trunc i64 %64 to i32
  %66 = load ptr, ptr %14, align 8, !tbaa !9
  %67 = load ptr, ptr %19, align 8, !tbaa !9
  %68 = getelementptr inbounds %struct.mbedtls_pk_rsassa_pss_options, ptr %67, i32 0, i32 0
  %69 = load i32, ptr %68, align 4, !tbaa !15
  %70 = load ptr, ptr %19, align 8, !tbaa !9
  %71 = getelementptr inbounds %struct.mbedtls_pk_rsassa_pss_options, ptr %70, i32 0, i32 1
  %72 = load i32, ptr %71, align 4, !tbaa !17
  %73 = load ptr, ptr %16, align 8, !tbaa !9
  %74 = call i32 @mbedtls_rsa_rsassa_pss_verify_ext(ptr noundef %62, ptr noundef null, ptr noundef null, i32 noundef 0, i32 noundef %63, i32 noundef %65, ptr noundef %66, i32 noundef %69, i32 noundef %72, ptr noundef %73)
  store i32 %74, ptr %18, align 4, !tbaa !5
  %75 = load i32, ptr %18, align 4, !tbaa !5
  %76 = icmp ne i32 %75, 0
  br i1 %76, label %77, label %79

77:                                               ; preds = %56
  %78 = load i32, ptr %18, align 4, !tbaa !5
  store i32 %78, ptr %9, align 4
  store i32 1, ptr %20, align 4
  br label %86

79:                                               ; preds = %56
  %80 = load i64, ptr %17, align 8, !tbaa !11
  %81 = load ptr, ptr %12, align 8, !tbaa !9
  %82 = call i64 @mbedtls_pk_get_len(ptr noundef %81)
  %83 = icmp ugt i64 %80, %82
  br i1 %83, label %84, label %85

84:                                               ; preds = %79
  store i32 -14592, ptr %9, align 4
  store i32 1, ptr %20, align 4
  br label %86

85:                                               ; preds = %79
  store i32 0, ptr %9, align 4
  store i32 1, ptr %20, align 4
  br label %86

86:                                               ; preds = %85, %84, %77, %55, %48, %44
  call void @llvm.lifetime.end.p0(i64 8, ptr %19) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %18) #4
  br label %99

87:                                               ; preds = %35
  %88 = load ptr, ptr %11, align 8, !tbaa !9
  %89 = icmp ne ptr %88, null
  br i1 %89, label %90, label %91

90:                                               ; preds = %87
  store i32 -16000, ptr %9, align 4
  br label %99

91:                                               ; preds = %87
  %92 = load ptr, ptr %12, align 8, !tbaa !9
  %93 = load i32, ptr %13, align 4, !tbaa !5
  %94 = load ptr, ptr %14, align 8, !tbaa !9
  %95 = load i64, ptr %15, align 8, !tbaa !11
  %96 = load ptr, ptr %16, align 8, !tbaa !9
  %97 = load i64, ptr %17, align 8, !tbaa !11
  %98 = call i32 @mbedtls_pk_verify(ptr noundef %92, i32 noundef %93, ptr noundef %94, i64 noundef %95, ptr noundef %96, i64 noundef %97)
  store i32 %98, ptr %9, align 4
  br label %99

99:                                               ; preds = %91, %90, %86, %34, %28
  %100 = load i32, ptr %9, align 4
  ret i32 %100
}

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

declare i32 @mbedtls_rsa_rsassa_pss_verify_ext(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef, i32 noundef, i32 noundef, ptr noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @mbedtls_pk_rsa(ptr, ptr) #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 = { 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 = { "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !10, i64 0}
!14 = !{!"", !10, i64 0, !10, i64 8}
!15 = !{!16, !6, i64 0}
!16 = !{!"", !6, i64 0, !6, i64 4}
!17 = !{!16, !6, i64 4}


; 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: nounwind uwtable
declare dso_local i32 @mbedtls_pk_can_do(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_pk_verify(ptr noundef, i32 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_pk_verify_ext(i32 noundef %0, ptr noundef readonly %1, ptr noundef %2, i32 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6, i64 noundef %7) local_unnamed_addr #0 {
  %9 = icmp eq ptr %2, null
  br i1 %9, label %40, label %10

10:                                               ; preds = %8
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = icmp eq ptr %11, null
  br i1 %12, label %40, label %13

13:                                               ; preds = %10
  %14 = tail call i32 @mbedtls_pk_can_do(ptr noundef nonnull %2, i32 noundef %0)
  %.not = icmp eq i32 %14, 0
  br i1 %.not, label %40, label %15

15:                                               ; preds = %13
  %16 = icmp eq i32 %0, 6
  br i1 %16, label %17, label %37

17:                                               ; preds = %15
  %18 = icmp eq i32 %3, 0
  %19 = icmp ugt i64 %5, 4294967295
  %or.cond = and i1 %18, %19
  %20 = icmp eq ptr %1, null
  %or.cond36 = or i1 %20, %or.cond
  br i1 %or.cond36, label %40, label %21

21:                                               ; preds = %17
  %22 = tail call i64 @mbedtls_pk_get_len(ptr noundef nonnull %2)
  %23 = icmp ugt i64 %22, %7
  br i1 %23, label %40, label %24

24:                                               ; preds = %21
  %25 = load ptr, ptr %2, align 8
  %26 = getelementptr inbounds i8, ptr %2, i64 8
  %27 = load ptr, ptr %26, align 8
  %28 = tail call ptr @mbedtls_pk_rsa(ptr %25, ptr %27)
  %29 = trunc i64 %5 to i32
  %30 = load i32, ptr %1, align 4, !tbaa !10
  %31 = getelementptr inbounds i8, ptr %1, i64 4
  %32 = load i32, ptr %31, align 4, !tbaa !13
  %33 = tail call i32 @mbedtls_rsa_rsassa_pss_verify_ext(ptr noundef %28, ptr noundef null, ptr noundef null, i32 noundef 0, i32 noundef %3, i32 noundef %29, ptr noundef %4, i32 noundef %30, i32 noundef %32, ptr noundef %6) #3
  %.not35 = icmp eq i32 %33, 0
  br i1 %.not35, label %34, label %40

34:                                               ; preds = %24
  %35 = tail call i64 @mbedtls_pk_get_len(ptr noundef nonnull %2)
  %36 = icmp ult i64 %35, %7
  %. = select i1 %36, i32 -14592, i32 0
  br label %40

37:                                               ; preds = %15
  %.not34 = icmp eq ptr %1, null
  br i1 %.not34, label %38, label %40

38:                                               ; preds = %37
  %39 = tail call i32 @mbedtls_pk_verify(ptr noundef nonnull %2, i32 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6, i64 noundef %7)
  br label %40

40:                                               ; preds = %37, %17, %21, %24, %34, %13, %8, %10, %38
  %.1 = phi i32 [ %39, %38 ], [ -16000, %10 ], [ -16000, %8 ], [ -16128, %13 ], [ -16000, %17 ], [ -17280, %21 ], [ %33, %24 ], [ %., %34 ], [ -16000, %37 ]
  ret i32 %.1
}

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

declare i32 @mbedtls_rsa_rsassa_pss_verify_ext(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @mbedtls_pk_rsa(ptr, ptr) 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 = { 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 = { "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 0}
!6 = !{!"", !7, i64 0, !7, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !12, i64 0}
!11 = !{!"", !12, i64 0, !12, i64 4}
!12 = !{!"int", !8, i64 0}
!13 = !{!11, !12, i64 4}


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