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

@.str.23 = external hidden unnamed_addr constant [5 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 uwtable
define dso_local i32 @mbedtls_x509_crt_check_extended_key_usage(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  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 8, ptr %8) #3
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.mbedtls_x509_crt, ptr %11, i32 0, i32 16
  %13 = load i32, ptr %12, align 8, !tbaa !11
  %14 = and i32 %13, 2048
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %3
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %68

17:                                               ; preds = %3
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.mbedtls_x509_crt, ptr %18, i32 0, i32 20
  store ptr %19, ptr %8, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %63, %17
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = icmp ne ptr %21, null
  br i1 %22, label %23, label %67

23:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.mbedtls_asn1_sequence, ptr %24, i32 0, i32 0
  store ptr %25, ptr %10, align 8, !tbaa !5
  %26 = load ptr, ptr %10, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %26, i32 0, i32 1
  %28 = load i64, ptr %27, align 8, !tbaa !19
  %29 = load i64, ptr %7, align 8, !tbaa !9
  %30 = icmp eq i64 %28, %29
  br i1 %30, label %31, label %40

31:                                               ; preds = %23
  %32 = load ptr, ptr %10, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %32, i32 0, i32 2
  %34 = load ptr, ptr %33, align 8, !tbaa !20
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = load i64, ptr %7, align 8, !tbaa !9
  %37 = call i32 @memcmp(ptr noundef %34, ptr noundef %35, i64 noundef %36) #4
  %38 = icmp eq i32 %37, 0
  br i1 %38, label %39, label %40

39:                                               ; preds = %31
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %60

40:                                               ; preds = %31, %23
  %41 = load ptr, ptr %10, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %41, i32 0, i32 1
  %43 = load i64, ptr %42, align 8, !tbaa !19
  %44 = icmp ne i64 4, %43
  br i1 %44, label %54, label %45

45:                                               ; preds = %40
  %46 = load ptr, ptr %10, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %46, i32 0, i32 2
  %48 = load ptr, ptr %47, align 8, !tbaa !20
  %49 = load ptr, ptr %10, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %49, i32 0, i32 1
  %51 = load i64, ptr %50, align 8, !tbaa !19
  %52 = call i32 @memcmp(ptr noundef @.str.23, ptr noundef %48, i64 noundef %51) #4
  %53 = icmp ne i32 %52, 0
  br label %54

54:                                               ; preds = %45, %40
  %55 = phi i1 [ true, %40 ], [ %53, %45 ]
  %56 = zext i1 %55 to i32
  %57 = icmp eq i32 %56, 0
  br i1 %57, label %58, label %59

58:                                               ; preds = %54
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %60

59:                                               ; preds = %54
  store i32 0, ptr %9, align 4
  br label %60

60:                                               ; preds = %59, %58, %39
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %61 = load i32, ptr %9, align 4
  switch i32 %61, label %68 [
    i32 0, label %62
  ]

62:                                               ; preds = %60
  br label %63

63:                                               ; preds = %62
  %64 = load ptr, ptr %8, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.mbedtls_asn1_sequence, ptr %64, i32 0, i32 1
  %66 = load ptr, ptr %65, align 8, !tbaa !21
  store ptr %66, ptr %8, align 8, !tbaa !5
  br label %20, !llvm.loop !22

67:                                               ; preds = %20
  store i32 -10240, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %68

68:                                               ; preds = %67, %60, %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %69 = load i32, ptr %4, align 4
  ret i32 %69
}

; Function Attrs: nounwind willreturn memory(read)
declare i32 @memcmp(ptr noundef, ptr noundef, i64 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 willreturn memory(read) "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 }
attributes #4 = { nounwind willreturn memory(read) }

!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 448}
!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 = !{!13, !10, i64 8}
!20 = !{!13, !6, i64 16}
!21 = !{!18, !6, i64 24}
!22 = distinct !{!22, !23}
!23 = !{!"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"

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

; Function Attrs: nofree nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local noundef i32 @mbedtls_x509_crt_check_extended_key_usage(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 448
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %6 = and i32 %5, 2048
  %7 = icmp eq i32 %6, 0
  br i1 %7, label %.critedge, label %8

8:                                                ; preds = %3
  %9 = getelementptr inbounds i8, ptr %0, i64 464
  br label %10

10:                                               ; preds = %8, %.thread
  %.01521 = phi ptr [ %9, %8 ], [ %23, %.thread ]
  %11 = getelementptr inbounds i8, ptr %.01521, i64 8
  %12 = load i64, ptr %11, align 8, !tbaa !17
  %13 = icmp eq i64 %12, %2
  br i1 %13, label %14, label %18

14:                                               ; preds = %10
  %15 = getelementptr inbounds i8, ptr %.01521, i64 16
  %16 = load ptr, ptr %15, align 8, !tbaa !18
  %bcmp = tail call i32 @bcmp(ptr %16, ptr %1, i64 %2)
  %17 = icmp eq i32 %bcmp, 0
  br i1 %17, label %.critedge, label %18

18:                                               ; preds = %14, %10
  %.not18 = icmp eq i64 %12, 4
  br i1 %.not18, label %19, label %.thread

19:                                               ; preds = %18
  %20 = getelementptr inbounds i8, ptr %.01521, i64 16
  %21 = load ptr, ptr %20, align 8, !tbaa !18
  %bcmp19 = tail call i32 @bcmp(ptr noundef nonnull dereferenceable(4) @.str.23, ptr noundef nonnull dereferenceable(4) %21, i64 4)
  %.not20 = icmp eq i32 %bcmp19, 0
  br i1 %.not20, label %.critedge, label %.thread

.thread:                                          ; preds = %18, %19
  %22 = getelementptr inbounds i8, ptr %.01521, i64 24
  %23 = load ptr, ptr %22, align 8, !tbaa !19
  %.not = icmp eq ptr %23, null
  br i1 %.not, label %.critedge, label %10, !llvm.loop !20

.critedge:                                        ; preds = %14, %.thread, %19, %3
  %.2 = phi i32 [ 0, %3 ], [ 0, %14 ], [ -10240, %.thread ], [ 0, %19 ]
  ret i32 %.2
}

; Function Attrs: nofree nounwind willreturn memory(argmem: read)
declare i32 @bcmp(ptr nocapture, ptr nocapture, i64) local_unnamed_addr #1

attributes #0 = { nofree nounwind memory(read, inaccessiblemem: none) 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 = { nofree nounwind willreturn memory(argmem: read) }

!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 448}
!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 = !{!7, !11, i64 8}
!18 = !{!7, !12, i64 16}
!19 = !{!16, !12, i64 24}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


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