; 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_pem_context = type { ptr, i64, ptr }

@.str = external hidden unnamed_addr constant [25 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [23 x i8], align 1

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_x509_crl_parse(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 i64, align 8
  %10 = alloca %struct.mbedtls_pem_context, align 8
  %11 = alloca i32, align 4
  %12 = 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) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 24, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !11
  %13 = load ptr, ptr %5, 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 %12, align 4
  br label %79

19:                                               ; preds = %15
  br label %20

20:                                               ; preds = %68, %19
  call void @mbedtls_pem_init(ptr noundef %10)
  %21 = load i64, ptr %7, align 8, !tbaa !9
  %22 = icmp eq i64 %21, 0
  br i1 %22, label %31, label %23

23:                                               ; preds = %20
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = load i64, ptr %7, align 8, !tbaa !9
  %26 = sub i64 %25, 1
  %27 = getelementptr inbounds i8, ptr %24, i64 %26
  %28 = load i8, ptr %27, align 1, !tbaa !13
  %29 = zext i8 %28 to i32
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %32

31:                                               ; preds = %23, %20
  store i32 -4224, ptr %8, align 4, !tbaa !11
  br label %35

32:                                               ; preds = %23
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = call i32 @mbedtls_pem_read_buffer(ptr noundef %10, ptr noundef @.str, ptr noundef @.str.1, ptr noundef %33, ptr noundef null, i64 noundef 0, ptr noundef %9)
  store i32 %34, ptr %8, align 4, !tbaa !11
  br label %35

35:                                               ; preds = %32, %31
  %36 = load i32, ptr %8, align 4, !tbaa !11
  %37 = icmp eq i32 %36, 0
  br i1 %37, label %38, label %55

38:                                               ; preds = %35
  store i32 1, ptr %11, align 4, !tbaa !11
  %39 = load i64, ptr %9, align 8, !tbaa !9
  %40 = load i64, ptr %7, align 8, !tbaa !9
  %41 = sub i64 %40, %39
  store i64 %41, ptr %7, align 8, !tbaa !9
  %42 = load i64, ptr %9, align 8, !tbaa !9
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds i8, ptr %43, i64 %42
  store ptr %44, ptr %6, align 8, !tbaa !5
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.mbedtls_pem_context, ptr %10, i32 0, i32 0
  %47 = load ptr, ptr %46, align 8, !tbaa !14
  %48 = getelementptr inbounds %struct.mbedtls_pem_context, ptr %10, i32 0, i32 1
  %49 = load i64, ptr %48, align 8, !tbaa !16
  %50 = call i32 @mbedtls_x509_crl_parse_der(ptr noundef %45, ptr noundef %47, i64 noundef %49)
  store i32 %50, ptr %8, align 4, !tbaa !11
  %51 = icmp ne i32 %50, 0
  br i1 %51, label %52, label %54

52:                                               ; preds = %38
  call void @mbedtls_pem_free(ptr noundef %10)
  %53 = load i32, ptr %8, align 4, !tbaa !11
  store i32 %53, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %79

54:                                               ; preds = %38
  br label %61

55:                                               ; preds = %35
  %56 = load i32, ptr %11, align 4, !tbaa !11
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %58, label %60

58:                                               ; preds = %55
  call void @mbedtls_pem_free(ptr noundef %10)
  %59 = load i32, ptr %8, align 4, !tbaa !11
  store i32 %59, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %79

60:                                               ; preds = %55
  br label %61

61:                                               ; preds = %60, %54
  call void @mbedtls_pem_free(ptr noundef %10)
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %11, align 4, !tbaa !11
  %64 = icmp ne i32 %63, 0
  br i1 %64, label %65, label %68

65:                                               ; preds = %62
  %66 = load i64, ptr %7, align 8, !tbaa !9
  %67 = icmp ugt i64 %66, 1
  br label %68

68:                                               ; preds = %65, %62
  %69 = phi i1 [ false, %62 ], [ %67, %65 ]
  br i1 %69, label %20, label %70, !llvm.loop !17

70:                                               ; preds = %68
  %71 = load i32, ptr %11, align 4, !tbaa !11
  %72 = icmp ne i32 %71, 0
  br i1 %72, label %73, label %74

73:                                               ; preds = %70
  store i32 0, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %79

74:                                               ; preds = %70
  %75 = load ptr, ptr %5, align 8, !tbaa !5
  %76 = load ptr, ptr %6, align 8, !tbaa !5
  %77 = load i64, ptr %7, align 8, !tbaa !9
  %78 = call i32 @mbedtls_x509_crl_parse_der(ptr noundef %75, ptr noundef %76, i64 noundef %77)
  store i32 %78, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %79

79:                                               ; preds = %74, %73, %58, %52, %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 24, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %80 = load i32, ptr %4, align 4
  ret i32 %80
}

declare void @mbedtls_pem_init(ptr noundef) #2

declare i32 @mbedtls_pem_read_buffer(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef) #2

declare void @mbedtls_pem_free(ptr noundef) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !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 = !{!7, !7, i64 0}
!14 = !{!15, !6, i64 0}
!15 = !{!"", !6, i64 0, !10, i64 8, !6, i64 16}
!16 = !{!15, !10, i64 8}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; 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"

%struct.mbedtls_pem_context = type { ptr, i64, ptr }

@.str = external hidden unnamed_addr constant [25 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [23 x i8], align 1

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_x509_crl_parse(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = alloca i64, align 8
  %5 = alloca %struct.mbedtls_pem_context, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %5) #3
  %6 = icmp eq ptr %0, null
  %7 = icmp eq ptr %1, null
  %or.cond = or i1 %6, %7
  br i1 %or.cond, label %.thread37, label %.preheader

.preheader:                                       ; preds = %3
  call void @mbedtls_pem_init(ptr noundef nonnull %5) #3
  %8 = icmp eq i64 %2, 0
  br i1 %8, label %44, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %9 = getelementptr inbounds i8, ptr %5, i64 8
  %10 = getelementptr i8, ptr %1, i64 %2
  %11 = getelementptr i8, ptr %10, i64 -1
  %12 = load i8, ptr %11, align 1, !tbaa !5
  %.not.peel = icmp eq i8 %12, 0
  br i1 %.not.peel, label %13, label %44

13:                                               ; preds = %.lr.ph
  %14 = call i32 @mbedtls_pem_read_buffer(ptr noundef nonnull %5, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, ptr noundef nonnull %1, ptr noundef null, i64 noundef 0, ptr noundef nonnull %4) #3
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %16, label %44

16:                                               ; preds = %13
  %17 = load i64, ptr %4, align 8, !tbaa !8
  %18 = load ptr, ptr %5, align 8, !tbaa !10
  %19 = load i64, ptr %9, align 8, !tbaa !13
  %20 = call i32 @mbedtls_x509_crl_parse_der(ptr noundef nonnull %0, ptr noundef %18, i64 noundef %19)
  %.not29.peel = icmp eq i32 %20, 0
  br i1 %.not29.peel, label %21, label %.loopexit

21:                                               ; preds = %16
  %22 = sub i64 %2, %17
  call void @mbedtls_pem_free(ptr noundef nonnull %5) #3
  %23 = icmp ugt i64 %22, 1
  br i1 %23, label %.peel.next, label %.thread37, !llvm.loop !14

.peel.next:                                       ; preds = %21
  call void @mbedtls_pem_init(ptr noundef nonnull %5) #3
  %24 = getelementptr i8, ptr %1, i64 %2
  %25 = getelementptr i8, ptr %24, i64 -1
  %26 = load i8, ptr %25, align 1, !tbaa !5
  %.not75 = icmp eq i8 %26, 0
  br i1 %.not75, label %.lr.ph78, label %.thread

.lr.ph78:                                         ; preds = %.peel.next
  %27 = getelementptr inbounds i8, ptr %1, i64 %17
  br label %33

28:                                               ; preds = %41
  %29 = getelementptr inbounds i8, ptr %.0234777, i64 %37
  call void @mbedtls_pem_init(ptr noundef nonnull %5) #3
  %30 = getelementptr i8, ptr %.0234777, i64 %.0214876
  %31 = getelementptr i8, ptr %30, i64 -1
  %32 = load i8, ptr %31, align 1, !tbaa !5
  %.not = icmp eq i8 %32, 0
  br i1 %.not, label %33, label %.thread

33:                                               ; preds = %.lr.ph78, %28
  %.0234777 = phi ptr [ %27, %.lr.ph78 ], [ %29, %28 ]
  %.0214876 = phi i64 [ %22, %.lr.ph78 ], [ %42, %28 ]
  %34 = call i32 @mbedtls_pem_read_buffer(ptr noundef nonnull %5, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, ptr noundef nonnull %.0234777, ptr noundef null, i64 noundef 0, ptr noundef nonnull %4) #3
  %35 = icmp eq i32 %34, 0
  br i1 %35, label %36, label %.thread

36:                                               ; preds = %33
  %37 = load i64, ptr %4, align 8, !tbaa !8
  %38 = load ptr, ptr %5, align 8, !tbaa !10
  %39 = load i64, ptr %9, align 8, !tbaa !13
  %40 = call i32 @mbedtls_x509_crl_parse_der(ptr noundef %0, ptr noundef %38, i64 noundef %39)
  %.not29 = icmp eq i32 %40, 0
  br i1 %.not29, label %41, label %.loopexit

.loopexit:                                        ; preds = %36, %16
  %.lcssa = phi i32 [ %20, %16 ], [ %40, %36 ]
  call void @mbedtls_pem_free(ptr noundef nonnull %5) #3
  br label %.thread37

.thread:                                          ; preds = %33, %28, %.peel.next
  %.02031 = phi i32 [ -4224, %.peel.next ], [ %34, %33 ], [ -4224, %28 ]
  call void @mbedtls_pem_free(ptr noundef nonnull %5) #3
  br label %.thread37

41:                                               ; preds = %36
  %42 = sub i64 %.0214876, %37
  call void @mbedtls_pem_free(ptr noundef nonnull %5) #3
  %43 = icmp ugt i64 %42, 1
  br i1 %43, label %28, label %.thread37, !llvm.loop !14

44:                                               ; preds = %.preheader, %.lr.ph, %13
  call void @mbedtls_pem_free(ptr noundef nonnull %5) #3
  %45 = call i32 @mbedtls_x509_crl_parse_der(ptr noundef nonnull %0, ptr noundef nonnull %1, i64 noundef %2)
  br label %.thread37

.thread37:                                        ; preds = %41, %21, %3, %44, %.thread, %.loopexit
  %.025 = phi i32 [ %.lcssa, %.loopexit ], [ %45, %44 ], [ %.02031, %.thread ], [ -10240, %3 ], [ 0, %21 ], [ 0, %41 ]
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  ret i32 %.025
}

declare void @mbedtls_pem_init(ptr noundef) local_unnamed_addr #2

declare i32 @mbedtls_pem_read_buffer(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

declare void @mbedtls_pem_free(ptr 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"long", !6, i64 0}
!10 = !{!11, !12, i64 0}
!11 = !{!"", !12, i64 0, !9, i64 8, !12, i64 16}
!12 = !{!"any pointer", !6, i64 0}
!13 = !{!11, !9, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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