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

; 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_rsassa_pss_options = type { i32, i32 }

@.str.5 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [23 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
declare i32 @snprintf(ptr noundef, i64 noundef, ptr noundef, ...) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_x509_sig_alg_gets(ptr noundef %0, i64 noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %4, ptr noundef %5) #2 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  %15 = alloca ptr, align 8
  %16 = alloca i64, align 8
  %17 = alloca ptr, align 8
  %18 = alloca i32, align 4
  %19 = alloca ptr, align 8
  %20 = alloca ptr, align 8
  %21 = alloca ptr, align 8
  store ptr %0, ptr %8, align 8, !tbaa !5
  store i64 %1, ptr %9, align 8, !tbaa !9
  store ptr %2, ptr %10, align 8, !tbaa !5
  store i32 %3, ptr %11, align 4, !tbaa !11
  store i32 %4, ptr %12, align 4, !tbaa !11
  store ptr %5, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #4
  %22 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %22, ptr %15, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  %23 = load i64, ptr %9, align 8, !tbaa !9
  store i64 %23, ptr %16, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #4
  store ptr null, ptr %17, align 8, !tbaa !5
  %24 = load ptr, ptr %10, align 8, !tbaa !5
  %25 = call i32 @mbedtls_oid_get_sig_alg_desc(ptr noundef %24, ptr noundef %17)
  store i32 %25, ptr %14, align 4, !tbaa !11
  %26 = load i32, ptr %14, align 4, !tbaa !11
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %32

28:                                               ; preds = %6
  %29 = load ptr, ptr %15, align 8, !tbaa !5
  %30 = load i64, ptr %16, align 8, !tbaa !9
  %31 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %29, i64 noundef %30, ptr noundef @.str.10) #4
  store i32 %31, ptr %14, align 4, !tbaa !11
  br label %37

32:                                               ; preds = %6
  %33 = load ptr, ptr %15, align 8, !tbaa !5
  %34 = load i64, ptr %16, align 8, !tbaa !9
  %35 = load ptr, ptr %17, align 8, !tbaa !5
  %36 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %33, i64 noundef %34, ptr noundef @.str.5, ptr noundef %35) #4
  store i32 %36, ptr %14, align 4, !tbaa !11
  br label %37

37:                                               ; preds = %32, %28
  br label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %14, align 4, !tbaa !11
  %40 = icmp slt i32 %39, 0
  br i1 %40, label %46, label %41

41:                                               ; preds = %38
  %42 = load i32, ptr %14, align 4, !tbaa !11
  %43 = sext i32 %42 to i64
  %44 = load i64, ptr %16, align 8, !tbaa !9
  %45 = icmp uge i64 %43, %44
  br i1 %45, label %46, label %47

46:                                               ; preds = %41, %38
  store i32 -10624, ptr %7, align 4
  store i32 1, ptr %18, align 4
  br label %118

47:                                               ; preds = %41
  %48 = load i32, ptr %14, align 4, !tbaa !11
  %49 = sext i32 %48 to i64
  %50 = load i64, ptr %16, align 8, !tbaa !9
  %51 = sub i64 %50, %49
  store i64 %51, ptr %16, align 8, !tbaa !9
  %52 = load i32, ptr %14, align 4, !tbaa !11
  %53 = sext i32 %52 to i64
  %54 = load ptr, ptr %15, align 8, !tbaa !5
  %55 = getelementptr inbounds i8, ptr %54, i64 %53
  store ptr %55, ptr %15, align 8, !tbaa !5
  br label %56

56:                                               ; preds = %47
  br label %57

57:                                               ; preds = %56
  %58 = load i32, ptr %11, align 4, !tbaa !11
  %59 = icmp eq i32 %58, 6
  br i1 %59, label %60, label %113

60:                                               ; preds = %57
  call void @llvm.lifetime.start.p0(i64 8, ptr %19) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %20) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %21) #4
  %61 = load ptr, ptr %13, align 8, !tbaa !5
  store ptr %61, ptr %19, align 8, !tbaa !5
  %62 = load i32, ptr %12, align 4, !tbaa !11
  %63 = call ptr @mbedtls_md_info_from_type(i32 noundef %62)
  store ptr %63, ptr %20, align 8, !tbaa !5
  %64 = load ptr, ptr %19, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.mbedtls_pk_rsassa_pss_options, ptr %64, i32 0, i32 0
  %66 = load i32, ptr %65, align 4, !tbaa !13
  %67 = call ptr @mbedtls_md_info_from_type(i32 noundef %66)
  store ptr %67, ptr %21, align 8, !tbaa !5
  %68 = load ptr, ptr %15, align 8, !tbaa !5
  %69 = load i64, ptr %16, align 8, !tbaa !9
  %70 = load ptr, ptr %20, align 8, !tbaa !5
  %71 = icmp ne ptr %70, null
  br i1 %71, label %72, label %75

72:                                               ; preds = %60
  %73 = load ptr, ptr %20, align 8, !tbaa !5
  %74 = call ptr @mbedtls_md_get_name(ptr noundef %73)
  br label %76

75:                                               ; preds = %60
  br label %76

76:                                               ; preds = %75, %72
  %77 = phi ptr [ %74, %72 ], [ @.str.10, %75 ]
  %78 = load ptr, ptr %21, align 8, !tbaa !5
  %79 = icmp ne ptr %78, null
  br i1 %79, label %80, label %83

80:                                               ; preds = %76
  %81 = load ptr, ptr %21, align 8, !tbaa !5
  %82 = call ptr @mbedtls_md_get_name(ptr noundef %81)
  br label %84

83:                                               ; preds = %76
  br label %84

84:                                               ; preds = %83, %80
  %85 = phi ptr [ %82, %80 ], [ @.str.10, %83 ]
  %86 = load ptr, ptr %19, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.mbedtls_pk_rsassa_pss_options, ptr %86, i32 0, i32 1
  %88 = load i32, ptr %87, align 4, !tbaa !15
  %89 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %68, i64 noundef %69, ptr noundef @.str.11, ptr noundef %77, ptr noundef %85, i32 noundef %88) #4
  store i32 %89, ptr %14, align 4, !tbaa !11
  br label %90

90:                                               ; preds = %84
  %91 = load i32, ptr %14, align 4, !tbaa !11
  %92 = icmp slt i32 %91, 0
  br i1 %92, label %98, label %93

93:                                               ; preds = %90
  %94 = load i32, ptr %14, align 4, !tbaa !11
  %95 = sext i32 %94 to i64
  %96 = load i64, ptr %16, align 8, !tbaa !9
  %97 = icmp uge i64 %95, %96
  br i1 %97, label %98, label %99

98:                                               ; preds = %93, %90
  store i32 -10624, ptr %7, align 4
  store i32 1, ptr %18, align 4
  br label %110

99:                                               ; preds = %93
  %100 = load i32, ptr %14, align 4, !tbaa !11
  %101 = sext i32 %100 to i64
  %102 = load i64, ptr %16, align 8, !tbaa !9
  %103 = sub i64 %102, %101
  store i64 %103, ptr %16, align 8, !tbaa !9
  %104 = load i32, ptr %14, align 4, !tbaa !11
  %105 = sext i32 %104 to i64
  %106 = load ptr, ptr %15, align 8, !tbaa !5
  %107 = getelementptr inbounds i8, ptr %106, i64 %105
  store ptr %107, ptr %15, align 8, !tbaa !5
  br label %108

108:                                              ; preds = %99
  br label %109

109:                                              ; preds = %108
  store i32 0, ptr %18, align 4
  br label %110

110:                                              ; preds = %109, %98
  call void @llvm.lifetime.end.p0(i64 8, ptr %21) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %20) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %19) #4
  %111 = load i32, ptr %18, align 4
  switch i32 %111, label %118 [
    i32 0, label %112
  ]

112:                                              ; preds = %110
  br label %113

113:                                              ; preds = %112, %57
  %114 = load i64, ptr %9, align 8, !tbaa !9
  %115 = load i64, ptr %16, align 8, !tbaa !9
  %116 = sub i64 %114, %115
  %117 = trunc i64 %116 to i32
  store i32 %117, ptr %7, align 4
  store i32 1, ptr %18, align 4
  br label %118

118:                                              ; preds = %113, %110, %46
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  %119 = load i32, ptr %7, align 4
  ret i32 %119
}

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

declare ptr @mbedtls_md_info_from_type(i32 noundef) #3

declare ptr @mbedtls_md_get_name(ptr noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind "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 = { "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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"", !12, i64 0, !12, i64 4}
!15 = !{!14, !12, 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"

@.str.5 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [23 x i8], align 1

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

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

; Function Attrs: nofree nounwind
declare noundef i32 @snprintf(ptr noalias nocapture noundef writeonly, i64 noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_x509_sig_alg_gets(ptr nocapture noundef writeonly %0, i64 noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %4, ptr nocapture noundef readonly %5) local_unnamed_addr #2 {
  %7 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %7) #4
  store ptr null, ptr %7, align 8, !tbaa !5
  %8 = call i32 @mbedtls_oid_get_sig_alg_desc(ptr noundef %2, ptr noundef nonnull %7) #4
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %11, label %9

9:                                                ; preds = %6
  %10 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %0, i64 noundef %1, ptr noundef nonnull @.str.10) #4
  br label %14

11:                                               ; preds = %6
  %12 = load ptr, ptr %7, align 8, !tbaa !5
  %13 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %0, i64 noundef %1, ptr noundef nonnull @.str.5, ptr noundef %12) #4
  br label %14

14:                                               ; preds = %9, %11
  %.036 = phi i32 [ %10, %9 ], [ %13, %11 ]
  %15 = icmp slt i32 %.036, 0
  br i1 %15, label %43, label %16

16:                                               ; preds = %14
  %17 = zext nneg i32 %.036 to i64
  %.not46 = icmp ult i64 %17, %1
  br i1 %.not46, label %18, label %43

18:                                               ; preds = %16
  %19 = sub i64 %1, %17
  %20 = getelementptr inbounds i8, ptr %0, i64 %17
  %21 = icmp eq i32 %3, 6
  br i1 %21, label %22, label %40

22:                                               ; preds = %18
  %23 = call ptr @mbedtls_md_info_from_type(i32 noundef %4) #4
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = call ptr @mbedtls_md_info_from_type(i32 noundef %24) #4
  %.not47 = icmp eq ptr %23, null
  br i1 %.not47, label %28, label %26

26:                                               ; preds = %22
  %27 = call ptr @mbedtls_md_get_name(ptr noundef nonnull %23) #4
  br label %28

28:                                               ; preds = %22, %26
  %29 = phi ptr [ %27, %26 ], [ @.str.10, %22 ]
  %.not48 = icmp eq ptr %25, null
  br i1 %.not48, label %32, label %30

30:                                               ; preds = %28
  %31 = call ptr @mbedtls_md_get_name(ptr noundef nonnull %25) #4
  br label %32

32:                                               ; preds = %28, %30
  %33 = phi ptr [ %31, %30 ], [ @.str.10, %28 ]
  %34 = getelementptr inbounds i8, ptr %5, i64 4
  %35 = load i32, ptr %34, align 4, !tbaa !12
  %36 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %20, i64 noundef %19, ptr noundef nonnull @.str.11, ptr noundef %29, ptr noundef %33, i32 noundef %35) #4
  %37 = icmp sgt i32 %36, -1
  %38 = zext nneg i32 %36 to i64
  %.not49 = icmp ugt i64 %19, %38
  %39 = select i1 %37, i1 %.not49, i1 false
  %spec.select = select i1 %39, i64 %38, i64 0
  %.038 = sub i64 %19, %spec.select
  br i1 %39, label %40, label %43

40:                                               ; preds = %32, %18
  %.139 = phi i64 [ %.038, %32 ], [ %19, %18 ]
  %41 = sub i64 %1, %.139
  %42 = trunc i64 %41 to i32
  br label %43

43:                                               ; preds = %14, %16, %32, %40
  %.1 = phi i32 [ %42, %40 ], [ -10624, %32 ], [ -10624, %16 ], [ -10624, %14 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7) #4
  ret i32 %.1
}

declare i32 @mbedtls_oid_get_sig_alg_desc(ptr noundef, ptr noundef) local_unnamed_addr #3

declare ptr @mbedtls_md_info_from_type(i32 noundef) local_unnamed_addr #3

declare ptr @mbedtls_md_get_name(ptr noundef) local_unnamed_addr #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nofree nounwind "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 = { "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, !11, i64 0}
!10 = !{!"", !11, i64 0, !11, i64 4}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 4}


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