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

; 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_crl = type { %struct.mbedtls_asn1_buf, %struct.mbedtls_asn1_buf, i32, %struct.mbedtls_asn1_buf, %struct.mbedtls_asn1_buf, %struct.mbedtls_asn1_named_data, %struct.mbedtls_x509_time, %struct.mbedtls_x509_time, %struct.mbedtls_x509_crl_entry, %struct.mbedtls_asn1_buf, %struct.mbedtls_asn1_buf, %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_x509_crl_entry = type { %struct.mbedtls_asn1_buf, %struct.mbedtls_asn1_buf, %struct.mbedtls_x509_time, %struct.mbedtls_asn1_buf, ptr }
%struct.mbedtls_asn1_buf = type { i32, i64, ptr }

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

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

13:                                               ; preds = %1
  store i32 1, ptr %9, align 4
  br label %92

14:                                               ; preds = %1
  br label %15

15:                                               ; preds = %71, %14
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.mbedtls_x509_crl, ptr %16, i32 0, i32 14
  %18 = load ptr, ptr %17, align 8, !tbaa !9
  call void @free(ptr noundef %18) #3
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.mbedtls_x509_crl, ptr %19, i32 0, i32 5
  %21 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %20, i32 0, i32 2
  %22 = load ptr, ptr %21, align 8, !tbaa !17
  store ptr %22, ptr %5, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %26, %15
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = icmp ne ptr %24, null
  br i1 %25, label %26, label %33

26:                                               ; preds = %23
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %27, ptr %6, align 8, !tbaa !5
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %28, i32 0, i32 2
  %30 = load ptr, ptr %29, align 8, !tbaa !18
  store ptr %30, ptr %5, align 8, !tbaa !5
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  call void @mbedtls_zeroize(ptr noundef %31, i64 noundef 64)
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  call void @free(ptr noundef %32) #3
  br label %23, !llvm.loop !19

33:                                               ; preds = %23
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.mbedtls_x509_crl, ptr %34, i32 0, i32 8
  %36 = getelementptr inbounds %struct.mbedtls_x509_crl_entry, ptr %35, i32 0, i32 4
  %37 = load ptr, ptr %36, align 8, !tbaa !21
  store ptr %37, ptr %7, align 8, !tbaa !5
  br label %38

38:                                               ; preds = %41, %33
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = icmp ne ptr %39, null
  br i1 %40, label %41, label %48

41:                                               ; preds = %38
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %42, ptr %8, align 8, !tbaa !5
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.mbedtls_x509_crl_entry, ptr %43, i32 0, i32 4
  %45 = load ptr, ptr %44, align 8, !tbaa !22
  store ptr %45, ptr %7, align 8, !tbaa !5
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  call void @mbedtls_zeroize(ptr noundef %46, i64 noundef 104)
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  call void @free(ptr noundef %47) #3
  br label %38, !llvm.loop !23

48:                                               ; preds = %38
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.mbedtls_x509_crl, ptr %49, i32 0, i32 0
  %51 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %50, i32 0, i32 2
  %52 = load ptr, ptr %51, align 8, !tbaa !24
  %53 = icmp ne ptr %52, null
  br i1 %53, label %54, label %67

54:                                               ; preds = %48
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.mbedtls_x509_crl, ptr %55, i32 0, i32 0
  %57 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %56, i32 0, i32 2
  %58 = load ptr, ptr %57, align 8, !tbaa !24
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.mbedtls_x509_crl, ptr %59, i32 0, i32 0
  %61 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %60, i32 0, i32 1
  %62 = load i64, ptr %61, align 8, !tbaa !25
  call void @mbedtls_zeroize(ptr noundef %58, i64 noundef %62)
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.mbedtls_x509_crl, ptr %63, i32 0, i32 0
  %65 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %64, i32 0, i32 2
  %66 = load ptr, ptr %65, align 8, !tbaa !24
  call void @free(ptr noundef %66) #3
  br label %67

67:                                               ; preds = %54, %48
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.mbedtls_x509_crl, ptr %68, i32 0, i32 15
  %70 = load ptr, ptr %69, align 8, !tbaa !26
  store ptr %70, ptr %3, align 8, !tbaa !5
  br label %71

71:                                               ; preds = %67
  %72 = load ptr, ptr %3, align 8, !tbaa !5
  %73 = icmp ne ptr %72, null
  br i1 %73, label %15, label %74, !llvm.loop !27

74:                                               ; preds = %71
  %75 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %75, ptr %3, align 8, !tbaa !5
  br label %76

76:                                               ; preds = %88, %74
  %77 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %77, ptr %4, align 8, !tbaa !5
  %78 = load ptr, ptr %3, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.mbedtls_x509_crl, ptr %78, i32 0, i32 15
  %80 = load ptr, ptr %79, align 8, !tbaa !26
  store ptr %80, ptr %3, align 8, !tbaa !5
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  call void @mbedtls_zeroize(ptr noundef %81, i64 noundef 416)
  %82 = load ptr, ptr %4, align 8, !tbaa !5
  %83 = load ptr, ptr %2, align 8, !tbaa !5
  %84 = icmp ne ptr %82, %83
  br i1 %84, label %85, label %87

85:                                               ; preds = %76
  %86 = load ptr, ptr %4, align 8, !tbaa !5
  call void @free(ptr noundef %86) #3
  br label %87

87:                                               ; preds = %85, %76
  br label %88

88:                                               ; preds = %87
  %89 = load ptr, ptr %3, align 8, !tbaa !5
  %90 = icmp ne ptr %89, null
  br i1 %90, label %76, label %91, !llvm.loop !28

91:                                               ; preds = %88
  store i32 0, ptr %9, align 4
  br label %92

92:                                               ; preds = %91, %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  %93 = load i32, ptr %9, align 4
  switch i32 %93, label %95 [
    i32 0, label %94
    i32 1, label %94
  ]

94:                                               ; preds = %92, %92
  ret void

95:                                               ; preds = %92
  unreachable
}

; 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 hidden void @mbedtls_zeroize(ptr noundef, i64 noundef) #1

; Function Attrs: nounwind
declare void @free(ptr 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 = { 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 #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, !6, i64 400}
!10 = !{!"mbedtls_x509_crl", !11, i64 0, !11, i64 24, !12, i64 48, !11, i64 56, !11, i64 80, !14, i64 104, !15, i64 168, !15, i64 192, !16, i64 216, !11, i64 320, !11, i64 344, !11, i64 368, !12, i64 392, !12, i64 396, !6, i64 400, !6, i64 408}
!11 = !{!"mbedtls_asn1_buf", !12, i64 0, !13, i64 8, !6, i64 16}
!12 = !{!"int", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!"mbedtls_asn1_named_data", !11, i64 0, !11, i64 24, !6, i64 48, !7, i64 56}
!15 = !{!"mbedtls_x509_time", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !12, i64 16, !12, i64 20}
!16 = !{!"mbedtls_x509_crl_entry", !11, i64 0, !11, i64 24, !15, i64 48, !11, i64 72, !6, i64 96}
!17 = !{!10, !6, i64 152}
!18 = !{!14, !6, i64 48}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!10, !6, i64 312}
!22 = !{!16, !6, i64 96}
!23 = distinct !{!23, !20}
!24 = !{!10, !6, i64 16}
!25 = !{!10, !13, i64 8}
!26 = !{!10, !6, i64 408}
!27 = distinct !{!27, !20}
!28 = distinct !{!28, !20}


; 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 void @mbedtls_x509_crl_free(ptr noundef %0) local_unnamed_addr #0 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %.loopexit, label %.preheader40

.preheader40:                                     ; preds = %1, %19
  %.0 = phi ptr [ %21, %19 ], [ %0, %1 ]
  %3 = getelementptr inbounds i8, ptr %.0, i64 400
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  tail call void @free(ptr noundef %4) #2
  %5 = getelementptr inbounds i8, ptr %.0, i64 152
  %6 = load ptr, ptr %5, align 8, !tbaa !16
  %.not41 = icmp eq ptr %6, null
  br i1 %.not41, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader40, %.lr.ph
  %.03042 = phi ptr [ %8, %.lr.ph ], [ %6, %.preheader40 ]
  %7 = getelementptr inbounds i8, ptr %.03042, i64 48
  %8 = load ptr, ptr %7, align 8, !tbaa !17
  tail call void @mbedtls_zeroize(ptr noundef nonnull %.03042, i64 noundef 64)
  tail call void @free(ptr noundef nonnull %.03042) #2
  %.not = icmp eq ptr %8, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !18

._crit_edge:                                      ; preds = %.lr.ph, %.preheader40
  %9 = getelementptr inbounds i8, ptr %.0, i64 312
  %10 = load ptr, ptr %9, align 8, !tbaa !20
  %.not3543 = icmp eq ptr %10, null
  br i1 %.not3543, label %._crit_edge47, label %.lr.ph46

.lr.ph46:                                         ; preds = %._crit_edge, %.lr.ph46
  %.02944 = phi ptr [ %12, %.lr.ph46 ], [ %10, %._crit_edge ]
  %11 = getelementptr inbounds i8, ptr %.02944, i64 96
  %12 = load ptr, ptr %11, align 8, !tbaa !21
  tail call void @mbedtls_zeroize(ptr noundef nonnull %.02944, i64 noundef 104)
  tail call void @free(ptr noundef nonnull %.02944) #2
  %.not35 = icmp eq ptr %12, null
  br i1 %.not35, label %._crit_edge47, label %.lr.ph46, !llvm.loop !22

._crit_edge47:                                    ; preds = %.lr.ph46, %._crit_edge
  %13 = getelementptr inbounds i8, ptr %.0, i64 16
  %14 = load ptr, ptr %13, align 8, !tbaa !23
  %.not36 = icmp eq ptr %14, null
  br i1 %.not36, label %19, label %15

15:                                               ; preds = %._crit_edge47
  %16 = getelementptr inbounds i8, ptr %.0, i64 8
  %17 = load i64, ptr %16, align 8, !tbaa !24
  tail call void @mbedtls_zeroize(ptr noundef nonnull %14, i64 noundef %17)
  %18 = load ptr, ptr %13, align 8, !tbaa !23
  tail call void @free(ptr noundef %18) #2
  br label %19

19:                                               ; preds = %15, %._crit_edge47
  %20 = getelementptr inbounds i8, ptr %.0, i64 408
  %21 = load ptr, ptr %20, align 8, !tbaa !25
  %.not37 = icmp eq ptr %21, null
  br i1 %.not37, label %.preheader, label %.preheader40, !llvm.loop !26

.preheader:                                       ; preds = %19, %25
  %.1 = phi ptr [ %23, %25 ], [ %0, %19 ]
  %22 = getelementptr inbounds i8, ptr %.1, i64 408
  %23 = load ptr, ptr %22, align 8, !tbaa !25
  tail call void @mbedtls_zeroize(ptr noundef nonnull %.1, i64 noundef 416)
  %.not38 = icmp eq ptr %.1, %0
  br i1 %.not38, label %25, label %24

24:                                               ; preds = %.preheader
  tail call void @free(ptr noundef nonnull %.1) #2
  br label %25

25:                                               ; preds = %.preheader, %24
  %.not39 = icmp eq ptr %23, null
  br i1 %.not39, label %.loopexit, label %.preheader, !llvm.loop !27

.loopexit:                                        ; preds = %25, %1
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @mbedtls_zeroize(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 #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 = { 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 #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, !12, i64 400}
!6 = !{!"mbedtls_x509_crl", !7, i64 0, !7, i64 24, !8, i64 48, !7, i64 56, !7, i64 80, !13, i64 104, !14, i64 168, !14, i64 192, !15, i64 216, !7, i64 320, !7, i64 344, !7, i64 368, !8, i64 392, !8, i64 396, !12, i64 400, !12, i64 408}
!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 = !{!"mbedtls_x509_crl_entry", !7, i64 0, !7, i64 24, !14, i64 48, !7, i64 72, !12, i64 96}
!16 = !{!6, !12, i64 152}
!17 = !{!13, !12, i64 48}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!6, !12, i64 312}
!21 = !{!15, !12, i64 96}
!22 = distinct !{!22, !19}
!23 = !{!6, !12, i64 16}
!24 = !{!6, !11, i64 8}
!25 = !{!6, !12, i64 408}
!26 = distinct !{!26, !19}
!27 = distinct !{!27, !19}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass