; LLVM IR from PacktPublishing/Demystifying-Cryptography-with-OpenSSL-3 :: Chapter11 (tls-cert-pinning)

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

26:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %27 = load ptr, ptr %9, align 8, !tbaa !5
  %28 = call ptr @X509_get_subject_name(ptr noundef %27)
  store ptr %28, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %29 = load ptr, ptr %10, align 8, !tbaa !5
  %30 = call ptr @X509_get_subject_name(ptr noundef %29)
  store ptr %30, ptr %12, align 8, !tbaa !5
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %31, ptr noundef @.str.22) #4
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = load ptr, ptr %11, align 8, !tbaa !5
  %35 = call i32 @X509_NAME_print_ex_fp(ptr noundef %33, ptr noundef %34, i32 noundef 2, i64 noundef 8520475)
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %36, ptr noundef @.str.23) #4
  %38 = load ptr, ptr %8, align 8, !tbaa !5
  %39 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %38, ptr noundef @.str.24) #4
  %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 2, i64 noundef 8520475)
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %43, ptr noundef @.str.23) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  br label %45

45:                                               ; preds = %26, %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  %46 = load ptr, ptr %9, align 8, !tbaa !5
  %47 = load ptr, ptr %10, align 8, !tbaa !5
  %48 = call i32 @X509_cmp(ptr noundef %46, ptr noundef %47)
  store i32 %48, ptr %13, align 4, !tbaa !9
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = load ptr, ptr %10, align 8, !tbaa !5
  call void @X509_STORE_CTX_set_current_cert(ptr noundef %49, ptr noundef %50)
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  call void @X509_STORE_CTX_set_depth(ptr noundef %51, i32 noundef 0)
  %52 = load i32, ptr %13, align 4, !tbaa !9
  %53 = icmp eq i32 %52, 0
  br i1 %53, label %54, label %62

54:                                               ; preds = %45
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  %56 = icmp ne ptr %55, null
  br i1 %56, label %57, label %60

57:                                               ; preds = %54
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %58, ptr noundef @.str.25) #4
  br label %60

60:                                               ; preds = %57, %54
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  call void @X509_STORE_CTX_set_error(ptr noundef %61, i32 noundef 0)
  store i32 1, ptr %3, align 4
  store i32 1, ptr %14, align 4
  br label %70

62:                                               ; preds = %45
  %63 = load ptr, ptr %8, align 8, !tbaa !5
  %64 = icmp ne ptr %63, null
  br i1 %64, label %65, label %68

65:                                               ; preds = %62
  %66 = load ptr, ptr %8, align 8, !tbaa !5
  %67 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %66, ptr noundef @.str.26) #4
  br label %68

68:                                               ; preds = %65, %62
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  call void @X509_STORE_CTX_set_error(ptr noundef %69, i32 noundef 50)
  store i32 0, ptr %3, align 4
  store i32 1, ptr %14, align 4
  br label %70

70:                                               ; preds = %68, %60
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  %71 = load i32, ptr %3, align 4
  ret i32 %71
}

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_get0_cert(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 i32 @X509_cmp(ptr noundef, ptr noundef) #3

declare void @X509_STORE_CTX_set_current_cert(ptr noundef, ptr noundef) #3

declare void @X509_STORE_CTX_set_depth(ptr noundef, i32 noundef) #3

declare void @X509_STORE_CTX_set_error(ptr noundef, i32 noundef) #3

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 = { 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}


; 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.22 = external hidden unnamed_addr constant [72 x i8], align 1
@.str.23 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.24 = external hidden unnamed_addr constant [51 x i8], align 1
@.str.25 = external hidden unnamed_addr constant [63 x i8], align 1
@.str.26 = external hidden unnamed_addr constant [63 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 range(i32 0, 2) i32 @cert_verify_callback(ptr noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = tail call i32 @SSL_get_ex_data_X509_STORE_CTX_idx() #3
  %4 = tail call ptr @X509_STORE_CTX_get_ex_data(ptr noundef %0, i32 noundef %3) #3
  %5 = tail call ptr @SSL_get_ex_data(ptr noundef %4, i32 noundef 0) #3
  %6 = tail call ptr @X509_STORE_CTX_get0_cert(ptr noundef %0) #3
  %.not = icmp eq ptr %5, null
  br i1 %.not, label %16, label %7

7:                                                ; preds = %2
  %8 = tail call ptr @X509_get_subject_name(ptr noundef %1) #3
  %9 = tail call ptr @X509_get_subject_name(ptr noundef %6) #3
  %10 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef nonnull %5, ptr noundef nonnull @.str.22) #3
  %11 = tail call i32 @X509_NAME_print_ex_fp(ptr noundef nonnull %5, ptr noundef %8, i32 noundef 2, i64 noundef 8520475) #3
  %12 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef nonnull %5, ptr noundef nonnull @.str.23) #3
  %13 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef nonnull %5, ptr noundef nonnull @.str.24) #3
  %14 = tail call i32 @X509_NAME_print_ex_fp(ptr noundef nonnull %5, ptr noundef %9, i32 noundef 2, i64 noundef 8520475) #3
  %15 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef nonnull %5, ptr noundef nonnull @.str.23) #3
  br label %16

16:                                               ; preds = %7, %2
  %17 = tail call i32 @X509_cmp(ptr noundef %1, ptr noundef %6) #3
  tail call void @X509_STORE_CTX_set_current_cert(ptr noundef %0, ptr noundef %6) #3
  tail call void @X509_STORE_CTX_set_depth(ptr noundef %0, i32 noundef 0) #3
  %18 = icmp eq i32 %17, 0
  br i1 %.not, label %20, label %.sink.split

.sink.split:                                      ; preds = %16
  %.str.25..str.26 = select i1 %18, ptr @.str.25, ptr @.str.26
  %19 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef nonnull %5, ptr noundef nonnull %.str.25..str.26) #3
  br label %20

20:                                               ; preds = %16, %.sink.split
  %.29 = zext i1 %18 to i32
  %. = select i1 %18, i32 0, i32 50
  tail call void @X509_STORE_CTX_set_error(ptr noundef %0, i32 noundef %.) #3
  ret i32 %.29
}

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_get0_cert(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 i32 @X509_cmp(ptr noundef, ptr noundef) local_unnamed_addr #2

declare void @X509_STORE_CTX_set_current_cert(ptr noundef, ptr noundef) local_unnamed_addr #2

declare void @X509_STORE_CTX_set_depth(ptr noundef, i32 noundef) local_unnamed_addr #2

declare void @X509_STORE_CTX_set_error(ptr noundef, i32 noundef) local_unnamed_addr #2

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 = { 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)"}


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