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

; 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_asn1_named_data = type { %struct.mbedtls_asn1_buf, %struct.mbedtls_asn1_buf, ptr, i8 }
%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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; 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 ptr @mbedtls_asn1_store_named_data(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4) #2 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca ptr, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i64 %2, ptr %9, align 8, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !5
  store i64 %4, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #6
  %15 = load ptr, ptr %7, align 8, !tbaa !5
  %16 = load ptr, ptr %15, align 8, !tbaa !5
  %17 = load ptr, ptr %8, align 8, !tbaa !5
  %18 = load i64, ptr %9, align 8, !tbaa !9
  %19 = call ptr @mbedtls_asn1_find_named_data(ptr noundef %16, ptr noundef %17, i64 noundef %18)
  store ptr %19, ptr %12, align 8, !tbaa !5
  %20 = icmp eq ptr %19, null
  br i1 %20, label %21, label %77

21:                                               ; preds = %5
  %22 = call noalias ptr @calloc(i64 noundef 1, i64 noundef 64) #7
  store ptr %22, ptr %12, align 8, !tbaa !5
  %23 = load ptr, ptr %12, align 8, !tbaa !5
  %24 = icmp eq ptr %23, null
  br i1 %24, label %25, label %26

25:                                               ; preds = %21
  store ptr null, ptr %6, align 8
  store i32 1, ptr %13, align 4
  br label %119

26:                                               ; preds = %21
  %27 = load i64, ptr %9, align 8, !tbaa !9
  %28 = load ptr, ptr %12, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %28, i32 0, i32 0
  %30 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %29, i32 0, i32 1
  store i64 %27, ptr %30, align 8, !tbaa !11
  %31 = load i64, ptr %9, align 8, !tbaa !9
  %32 = call noalias ptr @calloc(i64 noundef 1, i64 noundef %31) #7
  %33 = load ptr, ptr %12, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %33, i32 0, i32 0
  %35 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %34, i32 0, i32 2
  store ptr %32, ptr %35, align 8, !tbaa !15
  %36 = load ptr, ptr %12, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %36, i32 0, i32 0
  %38 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %37, i32 0, i32 2
  %39 = load ptr, ptr %38, align 8, !tbaa !15
  %40 = icmp eq ptr %39, null
  br i1 %40, label %41, label %43

41:                                               ; preds = %26
  %42 = load ptr, ptr %12, align 8, !tbaa !5
  call void @free(ptr noundef %42) #6
  store ptr null, ptr %6, align 8
  store i32 1, ptr %13, align 4
  br label %119

43:                                               ; preds = %26
  %44 = load ptr, ptr %12, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %44, i32 0, i32 0
  %46 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %45, i32 0, i32 2
  %47 = load ptr, ptr %46, align 8, !tbaa !15
  %48 = load ptr, ptr %8, align 8, !tbaa !5
  %49 = load i64, ptr %9, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %47, ptr align 1 %48, i64 %49, i1 false)
  %50 = load i64, ptr %11, align 8, !tbaa !9
  %51 = load ptr, ptr %12, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %51, i32 0, i32 1
  %53 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %52, i32 0, i32 1
  store i64 %50, ptr %53, align 8, !tbaa !16
  %54 = load i64, ptr %11, align 8, !tbaa !9
  %55 = call noalias ptr @calloc(i64 noundef 1, i64 noundef %54) #7
  %56 = load ptr, ptr %12, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %56, i32 0, i32 1
  %58 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %57, i32 0, i32 2
  store ptr %55, ptr %58, align 8, !tbaa !17
  %59 = load ptr, ptr %12, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %59, i32 0, i32 1
  %61 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %60, i32 0, i32 2
  %62 = load ptr, ptr %61, align 8, !tbaa !17
  %63 = icmp eq ptr %62, null
  br i1 %63, label %64, label %70

64:                                               ; preds = %43
  %65 = load ptr, ptr %12, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %65, i32 0, i32 0
  %67 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %66, i32 0, i32 2
  %68 = load ptr, ptr %67, align 8, !tbaa !15
  call void @free(ptr noundef %68) #6
  %69 = load ptr, ptr %12, align 8, !tbaa !5
  call void @free(ptr noundef %69) #6
  store ptr null, ptr %6, align 8
  store i32 1, ptr %13, align 4
  br label %119

70:                                               ; preds = %43
  %71 = load ptr, ptr %7, align 8, !tbaa !5
  %72 = load ptr, ptr %71, align 8, !tbaa !5
  %73 = load ptr, ptr %12, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %73, i32 0, i32 2
  store ptr %72, ptr %74, align 8, !tbaa !18
  %75 = load ptr, ptr %12, align 8, !tbaa !5
  %76 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %75, ptr %76, align 8, !tbaa !5
  br label %107

77:                                               ; preds = %5
  %78 = load ptr, ptr %12, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %78, i32 0, i32 1
  %80 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %79, i32 0, i32 1
  %81 = load i64, ptr %80, align 8, !tbaa !16
  %82 = load i64, ptr %11, align 8, !tbaa !9
  %83 = icmp ult i64 %81, %82
  br i1 %83, label %84, label %106

84:                                               ; preds = %77
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #6
  %85 = load i64, ptr %11, align 8, !tbaa !9
  %86 = call noalias ptr @calloc(i64 noundef 1, i64 noundef %85) #7
  store ptr %86, ptr %14, align 8, !tbaa !5
  %87 = load ptr, ptr %14, align 8, !tbaa !5
  %88 = icmp eq ptr %87, null
  br i1 %88, label %89, label %90

89:                                               ; preds = %84
  store ptr null, ptr %6, align 8
  store i32 1, ptr %13, align 4
  br label %103

90:                                               ; preds = %84
  %91 = load ptr, ptr %12, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %91, i32 0, i32 1
  %93 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %92, i32 0, i32 2
  %94 = load ptr, ptr %93, align 8, !tbaa !17
  call void @free(ptr noundef %94) #6
  %95 = load ptr, ptr %14, align 8, !tbaa !5
  %96 = load ptr, ptr %12, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %96, i32 0, i32 1
  %98 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %97, i32 0, i32 2
  store ptr %95, ptr %98, align 8, !tbaa !17
  %99 = load i64, ptr %11, align 8, !tbaa !9
  %100 = load ptr, ptr %12, align 8, !tbaa !5
  %101 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %100, i32 0, i32 1
  %102 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %101, i32 0, i32 1
  store i64 %99, ptr %102, align 8, !tbaa !16
  store i32 0, ptr %13, align 4
  br label %103

103:                                              ; preds = %90, %89
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #6
  %104 = load i32, ptr %13, align 4
  switch i32 %104, label %119 [
    i32 0, label %105
  ]

105:                                              ; preds = %103
  br label %106

106:                                              ; preds = %105, %77
  br label %107

107:                                              ; preds = %106, %70
  %108 = load ptr, ptr %10, align 8, !tbaa !5
  %109 = icmp ne ptr %108, null
  br i1 %109, label %110, label %117

110:                                              ; preds = %107
  %111 = load ptr, ptr %12, align 8, !tbaa !5
  %112 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %111, i32 0, i32 1
  %113 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %112, i32 0, i32 2
  %114 = load ptr, ptr %113, align 8, !tbaa !17
  %115 = load ptr, ptr %10, align 8, !tbaa !5
  %116 = load i64, ptr %11, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %114, ptr align 1 %115, i64 %116, i1 false)
  br label %117

117:                                              ; preds = %110, %107
  %118 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %118, ptr %6, align 8
  store i32 1, ptr %13, align 4
  br label %119

119:                                              ; preds = %117, %103, %64, %41, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #6
  %120 = load ptr, ptr %6, align 8
  ret ptr %120
}

declare ptr @mbedtls_asn1_find_named_data(ptr noundef, ptr noundef, i64 noundef) #3

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

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 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 #5 = { 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 #6 = { nounwind }
attributes #7 = { 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, !10, i64 8}
!12 = !{!"mbedtls_asn1_named_data", !13, i64 0, !13, i64 24, !6, i64 48, !7, i64 56}
!13 = !{!"mbedtls_asn1_buf", !14, i64 0, !10, i64 8, !6, i64 16}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !6, i64 16}
!16 = !{!12, !10, i64 32}
!17 = !{!12, !6, i64 40}
!18 = !{!12, !6, i64 48}


; 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: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @mbedtls_asn1_store_named_data(ptr nocapture noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef readonly %3, i64 noundef %4) local_unnamed_addr #1 {
  %6 = load ptr, ptr %0, align 8, !tbaa !5
  %7 = tail call ptr @mbedtls_asn1_find_named_data(ptr noundef %6, ptr noundef %1, i64 noundef %2) #5
  %8 = icmp eq ptr %7, null
  br i1 %8, label %9, label %27

9:                                                ; preds = %5
  %10 = tail call noalias dereferenceable_or_null(64) ptr @calloc(i64 noundef 1, i64 noundef 64) #6
  %11 = icmp eq ptr %10, null
  br i1 %11, label %.critedge, label %12

12:                                               ; preds = %9
  %13 = getelementptr inbounds i8, ptr %10, i64 8
  store i64 %2, ptr %13, align 8, !tbaa !9
  %14 = tail call noalias ptr @calloc(i64 noundef 1, i64 noundef %2) #6
  %15 = getelementptr inbounds i8, ptr %10, i64 16
  store ptr %14, ptr %15, align 8, !tbaa !14
  %16 = icmp eq ptr %14, null
  br i1 %16, label %17, label %18

17:                                               ; preds = %12
  tail call void @free(ptr noundef nonnull %10) #5
  br label %.critedge

18:                                               ; preds = %12
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %14, ptr align 1 %1, i64 %2, i1 false)
  %19 = getelementptr inbounds i8, ptr %10, i64 32
  store i64 %4, ptr %19, align 8, !tbaa !15
  %20 = tail call noalias ptr @calloc(i64 noundef 1, i64 noundef %4) #6
  %21 = getelementptr inbounds i8, ptr %10, i64 40
  store ptr %20, ptr %21, align 8, !tbaa !16
  %22 = icmp eq ptr %20, null
  br i1 %22, label %23, label %24

23:                                               ; preds = %18
  tail call void @free(ptr noundef nonnull %14) #5
  tail call void @free(ptr noundef nonnull %10) #5
  br label %.critedge

24:                                               ; preds = %18
  %25 = load ptr, ptr %0, align 8, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %10, i64 48
  store ptr %25, ptr %26, align 8, !tbaa !17
  store ptr %10, ptr %0, align 8, !tbaa !5
  br label %36

27:                                               ; preds = %5
  %28 = getelementptr inbounds i8, ptr %7, i64 32
  %29 = load i64, ptr %28, align 8, !tbaa !15
  %30 = icmp ult i64 %29, %4
  br i1 %30, label %31, label %36

31:                                               ; preds = %27
  %32 = tail call noalias ptr @calloc(i64 noundef 1, i64 noundef %4) #6
  %.not = icmp eq ptr %32, null
  br i1 %.not, label %.critedge, label %33

33:                                               ; preds = %31
  %34 = getelementptr inbounds i8, ptr %7, i64 40
  %35 = load ptr, ptr %34, align 8, !tbaa !16
  tail call void @free(ptr noundef %35) #5
  store ptr %32, ptr %34, align 8, !tbaa !16
  store i64 %4, ptr %28, align 8, !tbaa !15
  br label %36

36:                                               ; preds = %33, %27, %24
  %.040 = phi ptr [ %10, %24 ], [ %7, %33 ], [ %7, %27 ]
  %.not46 = icmp eq ptr %3, null
  br i1 %.not46, label %.critedge, label %37

37:                                               ; preds = %36
  %38 = getelementptr inbounds i8, ptr %.040, i64 40
  %39 = load ptr, ptr %38, align 8, !tbaa !16
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %39, ptr nonnull align 1 %3, i64 %4, i1 false)
  br label %.critedge

.critedge:                                        ; preds = %31, %36, %37, %9, %23, %17
  %.1 = phi ptr [ null, %17 ], [ null, %23 ], [ null, %9 ], [ %.040, %37 ], [ %.040, %36 ], [ null, %31 ]
  ret ptr %.1
}

declare ptr @mbedtls_asn1_find_named_data(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

; 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 #3

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

attributes #0 = { mustprogress nocallback nofree 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 = { 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 #4 = { 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 #5 = { nounwind }
attributes #6 = { 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, !13, i64 8}
!10 = !{!"mbedtls_asn1_named_data", !11, i64 0, !11, i64 24, !6, i64 48, !7, i64 56}
!11 = !{!"mbedtls_asn1_buf", !12, i64 0, !13, i64 8, !6, i64 16}
!12 = !{!"int", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!10, !6, i64 16}
!15 = !{!10, !13, i64 32}
!16 = !{!10, !6, i64 40}
!17 = !{!10, !6, i64 48}


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