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

; 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_x509_crt = type { %struct.mbedtls_asn1_buf, %struct.mbedtls_asn1_buf, i32, %struct.mbedtls_asn1_buf, %struct.mbedtls_asn1_buf, %struct.mbedtls_asn1_buf, %struct.mbedtls_asn1_buf, %struct.mbedtls_asn1_named_data, %struct.mbedtls_asn1_named_data, %struct.mbedtls_x509_time, %struct.mbedtls_x509_time, %struct.mbedtls_pk_context, %struct.mbedtls_asn1_buf, %struct.mbedtls_asn1_buf, %struct.mbedtls_asn1_buf, %struct.mbedtls_asn1_sequence, i32, i32, i32, i32, %struct.mbedtls_asn1_sequence, i8, %struct.mbedtls_asn1_buf, i32, i32, ptr, ptr }
%struct.mbedtls_asn1_named_data = type { %struct.mbedtls_asn1_buf, %struct.mbedtls_asn1_buf, ptr, i8 }
%struct.mbedtls_x509_time = type { i32, i32, i32, i32, i32, i32 }
%struct.mbedtls_pk_context = type { ptr, ptr }
%struct.mbedtls_asn1_sequence = type { %struct.mbedtls_asn1_buf, ptr }
%struct.mbedtls_asn1_buf = type { i32, i64, ptr }

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

15:                                               ; preds = %3
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %19

18:                                               ; preds = %15, %3
  store i32 -10240, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %85

19:                                               ; preds = %15
  br label %20

20:                                               ; preds = %32, %19
  %21 = load ptr, ptr %9, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.mbedtls_x509_crt, ptr %21, i32 0, i32 2
  %23 = load i32, ptr %22, align 8, !tbaa !11
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %30

25:                                               ; preds = %20
  %26 = load ptr, ptr %9, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.mbedtls_x509_crt, ptr %26, i32 0, i32 26
  %28 = load ptr, ptr %27, align 8, !tbaa !19
  %29 = icmp ne ptr %28, null
  br label %30

30:                                               ; preds = %25, %20
  %31 = phi i1 [ false, %20 ], [ %29, %25 ]
  br i1 %31, label %32, label %37

32:                                               ; preds = %30
  %33 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %33, ptr %10, align 8, !tbaa !5
  %34 = load ptr, ptr %9, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.mbedtls_x509_crt, ptr %34, i32 0, i32 26
  %36 = load ptr, ptr %35, align 8, !tbaa !19
  store ptr %36, ptr %9, align 8, !tbaa !5
  br label %20, !llvm.loop !20

37:                                               ; preds = %30
  %38 = load ptr, ptr %9, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.mbedtls_x509_crt, ptr %38, i32 0, i32 2
  %40 = load i32, ptr %39, align 8, !tbaa !11
  %41 = icmp ne i32 %40, 0
  br i1 %41, label %42, label %64

42:                                               ; preds = %37
  %43 = load ptr, ptr %9, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.mbedtls_x509_crt, ptr %43, i32 0, i32 26
  %45 = load ptr, ptr %44, align 8, !tbaa !19
  %46 = icmp eq ptr %45, null
  br i1 %46, label %47, label %64

47:                                               ; preds = %42
  %48 = call noalias ptr @calloc(i64 noundef 1, i64 noundef 552) #5
  %49 = load ptr, ptr %9, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.mbedtls_x509_crt, ptr %49, i32 0, i32 26
  store ptr %48, ptr %50, align 8, !tbaa !19
  %51 = load ptr, ptr %9, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.mbedtls_x509_crt, ptr %51, i32 0, i32 26
  %53 = load ptr, ptr %52, align 8, !tbaa !19
  %54 = icmp eq ptr %53, null
  br i1 %54, label %55, label %56

55:                                               ; preds = %47
  store i32 -10368, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %85

56:                                               ; preds = %47
  %57 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %57, ptr %10, align 8, !tbaa !5
  %58 = load ptr, ptr %9, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.mbedtls_x509_crt, ptr %58, i32 0, i32 26
  %60 = load ptr, ptr %59, align 8, !tbaa !19
  call void @mbedtls_x509_crt_init(ptr noundef %60)
  %61 = load ptr, ptr %9, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.mbedtls_x509_crt, ptr %61, i32 0, i32 26
  %63 = load ptr, ptr %62, align 8, !tbaa !19
  store ptr %63, ptr %9, align 8, !tbaa !5
  br label %64

64:                                               ; preds = %56, %42, %37
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  %67 = load i64, ptr %7, align 8, !tbaa !9
  %68 = call i32 @x509_crt_parse_der_core(ptr noundef %65, ptr noundef %66, i64 noundef %67)
  store i32 %68, ptr %8, align 4, !tbaa !22
  %69 = icmp ne i32 %68, 0
  br i1 %69, label %70, label %84

70:                                               ; preds = %64
  %71 = load ptr, ptr %10, align 8, !tbaa !5
  %72 = icmp ne ptr %71, null
  br i1 %72, label %73, label %76

73:                                               ; preds = %70
  %74 = load ptr, ptr %10, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.mbedtls_x509_crt, ptr %74, i32 0, i32 26
  store ptr null, ptr %75, align 8, !tbaa !19
  br label %76

76:                                               ; preds = %73, %70
  %77 = load ptr, ptr %9, align 8, !tbaa !5
  %78 = load ptr, ptr %5, align 8, !tbaa !5
  %79 = icmp ne ptr %77, %78
  br i1 %79, label %80, label %82

80:                                               ; preds = %76
  %81 = load ptr, ptr %9, align 8, !tbaa !5
  call void @free(ptr noundef %81) #4
  br label %82

82:                                               ; preds = %80, %76
  %83 = load i32, ptr %8, align 4, !tbaa !22
  store i32 %83, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %85

84:                                               ; preds = %64
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %85

85:                                               ; preds = %84, %82, %55, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  %86 = load i32, ptr %4, align 4
  ret i32 %86
}

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

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_x509_crt_init(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden i32 @x509_crt_parse_der_core(ptr noundef, ptr noundef, i64 noundef) #0

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind allocsize(0,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 #3 = { 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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0,1) }

!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, !14, i64 48}
!12 = !{!"mbedtls_x509_crt", !13, i64 0, !13, i64 24, !14, i64 48, !13, i64 56, !13, i64 80, !13, i64 104, !13, i64 128, !15, i64 152, !15, i64 216, !16, i64 280, !16, i64 304, !17, i64 328, !13, i64 344, !13, i64 368, !13, i64 392, !18, i64 416, !14, i64 448, !14, i64 452, !14, i64 456, !14, i64 460, !18, i64 464, !7, i64 496, !13, i64 504, !14, i64 528, !14, i64 532, !6, i64 536, !6, i64 544}
!13 = !{!"mbedtls_asn1_buf", !14, i64 0, !10, i64 8, !6, i64 16}
!14 = !{!"int", !7, i64 0}
!15 = !{!"mbedtls_asn1_named_data", !13, i64 0, !13, i64 24, !6, i64 48, !7, i64 56}
!16 = !{!"mbedtls_x509_time", !14, i64 0, !14, i64 4, !14, i64 8, !14, i64 12, !14, i64 16, !14, i64 20}
!17 = !{!"", !6, i64 0, !6, i64 8}
!18 = !{!"mbedtls_asn1_sequence", !13, i64 0, !6, i64 24}
!19 = !{!12, !6, i64 544}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!14, !14, 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"

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_x509_crt_parse_der(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq ptr %0, null
  %5 = icmp eq ptr %1, null
  %or.cond = or i1 %4, %5
  br i1 %or.cond, label %23, label %.preheader.preheader

.preheader.preheader:                             ; preds = %3
  %6 = getelementptr inbounds i8, ptr %0, i64 48
  %7 = load i32, ptr %6, align 8, !tbaa !5
  %.not45 = icmp eq i32 %7, 0
  br i1 %.not45, label %.critedge.thread, label %.lr.ph

.preheader:                                       ; preds = %.lr.ph
  %8 = getelementptr inbounds i8, ptr %11, i64 48
  %9 = load i32, ptr %8, align 8, !tbaa !5
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %.critedge.thread, label %.lr.ph, !llvm.loop !17

.lr.ph:                                           ; preds = %.preheader.preheader, %.preheader
  %.02446 = phi ptr [ %11, %.preheader ], [ %0, %.preheader.preheader ]
  %10 = getelementptr inbounds i8, ptr %.02446, i64 544
  %11 = load ptr, ptr %10, align 8, !tbaa !19
  %.not31 = icmp eq ptr %11, null
  br i1 %.not31, label %.critedge, label %.preheader, !llvm.loop !17

.critedge:                                        ; preds = %.lr.ph
  %12 = getelementptr inbounds i8, ptr %.02446, i64 544
  %13 = tail call noalias dereferenceable_or_null(552) ptr @calloc(i64 noundef 1, i64 noundef 552) #3
  store ptr %13, ptr %12, align 8, !tbaa !19
  %14 = icmp eq ptr %13, null
  br i1 %14, label %23, label %15

15:                                               ; preds = %.critedge
  tail call void @mbedtls_x509_crt_init(ptr noundef nonnull %13)
  %16 = load ptr, ptr %12, align 8, !tbaa !19
  br label %.critedge.thread

.critedge.thread:                                 ; preds = %.preheader, %.preheader.preheader, %15
  %.125 = phi ptr [ %16, %15 ], [ %0, %.preheader.preheader ], [ %11, %.preheader ]
  %.1 = phi ptr [ %.02446, %15 ], [ null, %.preheader.preheader ], [ %.02446, %.preheader ]
  %17 = tail call i32 @x509_crt_parse_der_core(ptr noundef %.125, ptr noundef %1, i64 noundef %2)
  %.not33 = icmp eq i32 %17, 0
  br i1 %.not33, label %23, label %18

18:                                               ; preds = %.critedge.thread
  %.not34 = icmp eq ptr %.1, null
  br i1 %.not34, label %21, label %19

19:                                               ; preds = %18
  %20 = getelementptr inbounds i8, ptr %.1, i64 544
  store ptr null, ptr %20, align 8, !tbaa !19
  br label %21

21:                                               ; preds = %19, %18
  %.not35 = icmp eq ptr %.125, %0
  br i1 %.not35, label %23, label %22

22:                                               ; preds = %21
  tail call void @free(ptr noundef %.125) #4
  br label %23

23:                                               ; preds = %.critedge.thread, %21, %22, %.critedge, %3
  %.026 = phi i32 [ -10240, %3 ], [ -10368, %.critedge ], [ %17, %22 ], [ %17, %21 ], [ 0, %.critedge.thread ]
  ret i32 %.026
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @calloc(i64 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_x509_crt_init(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

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 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(0,1) }
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, !8, i64 48}
!6 = !{!"mbedtls_x509_crt", !7, i64 0, !7, i64 24, !8, i64 48, !7, i64 56, !7, i64 80, !7, i64 104, !7, i64 128, !13, i64 152, !13, i64 216, !14, i64 280, !14, i64 304, !15, i64 328, !7, i64 344, !7, i64 368, !7, i64 392, !16, i64 416, !8, i64 448, !8, i64 452, !8, i64 456, !8, i64 460, !16, i64 464, !9, i64 496, !7, i64 504, !8, i64 528, !8, i64 532, !12, i64 536, !12, i64 544}
!7 = !{!"mbedtls_asn1_buf", !8, i64 0, !11, i64 8, !12, i64 16}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"long", !9, i64 0}
!12 = !{!"any pointer", !9, i64 0}
!13 = !{!"mbedtls_asn1_named_data", !7, i64 0, !7, i64 24, !12, i64 48, !9, i64 56}
!14 = !{!"mbedtls_x509_time", !8, i64 0, !8, i64 4, !8, i64 8, !8, i64 12, !8, i64 16, !8, i64 20}
!15 = !{!"", !12, i64 0, !12, i64 8}
!16 = !{!"mbedtls_asn1_sequence", !7, i64 0, !12, i64 24}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!6, !12, i64 544}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass