; LLVM IR from PacktPublishing/Demystifying-Cryptography-with-OpenSSL-3 :: Chapter10 (crl-check)

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

@.str.14 = external hidden unnamed_addr constant [38 x i8], align 1
@.str.15 = external hidden unnamed_addr constant [35 x i8], align 1
@.str.16 = external hidden unnamed_addr constant [2 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: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #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 @lookup_crls(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca ptr, align 8
  %17 = alloca ptr, align 8
  %18 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  %19 = call i32 @SSL_get_ex_data_X509_STORE_CTX_idx()
  store i32 %19, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = call ptr @X509_STORE_CTX_get_ex_data(ptr noundef %20, i32 noundef %21)
  store ptr %22, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = call ptr @SSL_get_ex_data(ptr noundef %23, i32 noundef 0)
  store ptr %24, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = call ptr @X509_STORE_CTX_get_current_cert(ptr noundef %25)
  store ptr %26, ptr %9, align 8, !tbaa !5
  %27 = load ptr, ptr %9, align 8, !tbaa !5
  %28 = icmp ne ptr %27, null
  br i1 %28, label %30, label %29

29:                                               ; preds = %2
  store ptr null, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %88

30:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = call i32 @X509_STORE_CTX_get_error_depth(ptr noundef %31)
  store i32 %32, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %33 = load ptr, ptr %9, align 8, !tbaa !5
  %34 = call ptr @X509_get_subject_name(ptr noundef %33)
  store ptr %34, ptr %12, align 8, !tbaa !5
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  %36 = load i32, ptr %11, align 4, !tbaa !9
  %37 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %35, ptr noundef @.str.14, i32 noundef %36) #5
  %38 = load ptr, ptr %8, align 8, !tbaa !5
  %39 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %38, ptr noundef @.str.15) #5
  %40 = load ptr, ptr %8, align 8, !tbaa !5
  %41 = load ptr, ptr %12, align 8, !tbaa !5
  %42 = call i32 @X509_NAME_print_ex_fp(ptr noundef %40, ptr noundef %41, i32 noundef 0, i64 noundef 8520475)
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %43, ptr noundef @.str.16) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  %45 = load ptr, ptr %9, align 8, !tbaa !5
  %46 = call ptr @X509_get_ext_d2i(ptr noundef %45, i32 noundef 103, ptr noundef null, ptr noundef null)
  store ptr %46, ptr %13, align 8, !tbaa !5
  %47 = load ptr, ptr %13, align 8, !tbaa !5
  %48 = icmp ne ptr %47, null
  br i1 %48, label %50, label %49

49:                                               ; preds = %30
  store ptr null, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %87

50:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #5
  %51 = load ptr, ptr %13, align 8, !tbaa !5
  %52 = call ptr @ossl_check_const_DIST_POINT_sk_type(ptr noundef %51)
  %53 = call i32 @OPENSSL_sk_num(ptr noundef %52)
  store i32 %53, ptr %14, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #5
  store i32 0, ptr %15, align 4, !tbaa !9
  br label %54

54:                                               ; preds = %80, %50
  %55 = load i32, ptr %15, align 4, !tbaa !9
  %56 = load i32, ptr %14, align 4, !tbaa !9
  %57 = icmp slt i32 %55, %56
  br i1 %57, label %59, label %58

58:                                               ; preds = %54
  store i32 2, ptr %10, align 4
  br label %83

59:                                               ; preds = %54
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #5
  %60 = load ptr, ptr %13, align 8, !tbaa !5
  %61 = call ptr @ossl_check_const_DIST_POINT_sk_type(ptr noundef %60)
  %62 = load i32, ptr %15, align 4, !tbaa !9
  %63 = call ptr @OPENSSL_sk_value(ptr noundef %61, i32 noundef %62)
  store ptr %63, ptr %16, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #5
  %64 = load ptr, ptr %16, align 8, !tbaa !5
  %65 = load ptr, ptr %8, align 8, !tbaa !5
  %66 = call ptr @download_crl_from_dist_point(ptr noundef %64, ptr noundef %65)
  store ptr %66, ptr %17, align 8, !tbaa !5
  %67 = load ptr, ptr %17, align 8, !tbaa !5
  %68 = icmp ne ptr %67, null
  br i1 %68, label %70, label %69

69:                                               ; preds = %59
  store i32 4, ptr %10, align 4
  br label %78

70:                                               ; preds = %59
  call void @llvm.lifetime.start.p0(i64 8, ptr %18) #5
  %71 = call ptr @OPENSSL_sk_new_null()
  store ptr %71, ptr %18, align 8, !tbaa !5
  %72 = load ptr, ptr %18, align 8, !tbaa !5
  %73 = call ptr @ossl_check_X509_CRL_sk_type(ptr noundef %72)
  %74 = load ptr, ptr %17, align 8, !tbaa !5
  %75 = call ptr @ossl_check_X509_CRL_type(ptr noundef %74)
  %76 = call i32 @OPENSSL_sk_push(ptr noundef %73, ptr noundef %75)
  %77 = load ptr, ptr %18, align 8, !tbaa !5
  store ptr %77, ptr %3, align 8
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #5
  br label %78

78:                                               ; preds = %70, %69
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #5
  %79 = load i32, ptr %10, align 4
  switch i32 %79, label %83 [
    i32 4, label %80
  ]

80:                                               ; preds = %78
  %81 = load i32, ptr %15, align 4, !tbaa !9
  %82 = add nsw i32 %81, 1
  store i32 %82, ptr %15, align 4, !tbaa !9
  br label %54, !llvm.loop !11

83:                                               ; preds = %78, %58
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #5
  %84 = load i32, ptr %10, align 4
  switch i32 %84, label %86 [
    i32 2, label %85
  ]

85:                                               ; preds = %83
  store ptr null, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %86

86:                                               ; preds = %85, %83
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #5
  br label %87

87:                                               ; preds = %86, %49
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  br label %88

88:                                               ; preds = %87, %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  %89 = load ptr, ptr %3, align 8
  ret ptr %89
}

declare i32 @SSL_get_ex_data_X509_STORE_CTX_idx() #3

declare ptr @X509_STORE_CTX_get_ex_data(ptr noundef, i32 noundef) #3

declare ptr @SSL_get_ex_data(ptr noundef, i32 noundef) #3

declare ptr @X509_STORE_CTX_get_current_cert(ptr noundef) #3

declare i32 @X509_STORE_CTX_get_error_depth(ptr noundef) #3

declare ptr @X509_get_subject_name(ptr noundef) #3

declare i32 @X509_NAME_print_ex_fp(ptr noundef, ptr noundef, i32 noundef, i64 noundef) #3

declare ptr @X509_get_ext_d2i(ptr noundef, i32 noundef, ptr noundef, ptr noundef) #3

declare i32 @OPENSSL_sk_num(ptr noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @ossl_check_const_DIST_POINT_sk_type(ptr noundef) #4

declare ptr @OPENSSL_sk_value(ptr noundef, i32 noundef) #3

; Function Attrs: nounwind uwtable
declare dso_local ptr @download_crl_from_dist_point(ptr noundef, ptr noundef) #2

declare ptr @OPENSSL_sk_new_null() #3

declare i32 @OPENSSL_sk_push(ptr noundef, ptr noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @ossl_check_X509_CRL_sk_type(ptr noundef) #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @ossl_check_X509_CRL_type(ptr noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = { inlinehint 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 #5 = { 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 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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.14 = external hidden unnamed_addr constant [38 x i8], align 1
@.str.15 = external hidden unnamed_addr constant [35 x i8], align 1
@.str.16 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @lookup_crls(ptr noundef %0, ptr nocapture noundef readnone %1) local_unnamed_addr #1 {
  %3 = tail call i32 @SSL_get_ex_data_X509_STORE_CTX_idx() #4
  %4 = tail call ptr @X509_STORE_CTX_get_ex_data(ptr noundef %0, i32 noundef %3) #4
  %5 = tail call ptr @SSL_get_ex_data(ptr noundef %4, i32 noundef 0) #4
  %6 = tail call ptr @X509_STORE_CTX_get_current_cert(ptr noundef %0) #4
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %.loopexit, label %7

7:                                                ; preds = %2
  %8 = tail call i32 @X509_STORE_CTX_get_error_depth(ptr noundef %0) #4
  %9 = tail call ptr @X509_get_subject_name(ptr noundef nonnull %6) #4
  %10 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %5, ptr noundef nonnull @.str.14, i32 noundef %8) #4
  %11 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %5, ptr noundef nonnull @.str.15) #4
  %12 = tail call i32 @X509_NAME_print_ex_fp(ptr noundef %5, ptr noundef %9, i32 noundef 0, i64 noundef 8520475) #4
  %13 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %5, ptr noundef nonnull @.str.16) #4
  %14 = tail call ptr @X509_get_ext_d2i(ptr noundef nonnull %6, i32 noundef 103, ptr noundef null, ptr noundef null) #4
  %.not36 = icmp eq ptr %14, null
  br i1 %.not36, label %.loopexit, label %15

15:                                               ; preds = %7
  %16 = tail call ptr @ossl_check_const_DIST_POINT_sk_type(ptr noundef nonnull %14)
  %17 = tail call i32 @OPENSSL_sk_num(ptr noundef %16) #4
  %.not3841 = icmp sgt i32 %17, 0
  br i1 %.not3841, label %.lr.ph, label %.loopexit

18:                                               ; preds = %.lr.ph
  %19 = add nuw nsw i32 %.03242, 1
  %exitcond.not = icmp eq i32 %19, %17
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph, !llvm.loop !5

.lr.ph:                                           ; preds = %15, %18
  %.03242 = phi i32 [ %19, %18 ], [ 0, %15 ]
  %20 = tail call ptr @ossl_check_const_DIST_POINT_sk_type(ptr noundef nonnull %14)
  %21 = tail call ptr @OPENSSL_sk_value(ptr noundef %20, i32 noundef %.03242) #4
  %22 = tail call ptr @download_crl_from_dist_point(ptr noundef %21, ptr noundef %5)
  %.not37 = icmp eq ptr %22, null
  br i1 %.not37, label %18, label %23

23:                                               ; preds = %.lr.ph
  %24 = tail call ptr @OPENSSL_sk_new_null() #4
  %25 = tail call ptr @ossl_check_X509_CRL_sk_type(ptr noundef %24)
  %26 = tail call ptr @ossl_check_X509_CRL_type(ptr noundef nonnull %22)
  %27 = tail call i32 @OPENSSL_sk_push(ptr noundef %25, ptr noundef %26) #4
  br label %.loopexit

.loopexit:                                        ; preds = %18, %15, %23, %7, %2
  %.5 = phi ptr [ null, %2 ], [ null, %7 ], [ %24, %23 ], [ null, %15 ], [ null, %18 ]
  ret ptr %.5
}

declare i32 @SSL_get_ex_data_X509_STORE_CTX_idx() local_unnamed_addr #2

declare ptr @X509_STORE_CTX_get_ex_data(ptr noundef, i32 noundef) local_unnamed_addr #2

declare ptr @SSL_get_ex_data(ptr noundef, i32 noundef) local_unnamed_addr #2

declare ptr @X509_STORE_CTX_get_current_cert(ptr noundef) local_unnamed_addr #2

declare i32 @X509_STORE_CTX_get_error_depth(ptr noundef) local_unnamed_addr #2

declare ptr @X509_get_subject_name(ptr noundef) local_unnamed_addr #2

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

declare ptr @X509_get_ext_d2i(ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

declare i32 @OPENSSL_sk_num(ptr noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @ossl_check_const_DIST_POINT_sk_type(ptr noundef) local_unnamed_addr #3

declare ptr @OPENSSL_sk_value(ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local ptr @download_crl_from_dist_point(ptr noundef, ptr noundef) local_unnamed_addr #1

declare ptr @OPENSSL_sk_new_null() local_unnamed_addr #2

declare i32 @OPENSSL_sk_push(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @ossl_check_X509_CRL_sk_type(ptr noundef) local_unnamed_addr #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @ossl_check_X509_CRL_type(ptr noundef) local_unnamed_addr #3

attributes #0 = { nofree 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 #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 = { inlinehint 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 #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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass