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

; 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_ssl_ciphersuite_t = type { i32, ptr, i32, i32, i32, i32, i32, i32, i32, i8 }

@supported_init = external hidden global i32, align 4
@ciphersuite_preference = external hidden constant [163 x i32], align 16
@supported_ciphersuites = external hidden global [141 x i32], align 16

; Function Attrs: nounwind uwtable
define dso_local ptr @mbedtls_ssl_list_ciphersuites() #0 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = load i32, ptr @supported_init, align 4, !tbaa !5
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %6, label %37

6:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  store ptr @ciphersuite_preference, ptr %1, align 8, !tbaa !9
  store ptr @supported_ciphersuites, ptr %2, align 8, !tbaa !9
  br label %7

7:                                                ; preds = %32, %6
  %8 = load ptr, ptr %1, align 8, !tbaa !9
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %11, label %14

11:                                               ; preds = %7
  %12 = load ptr, ptr %2, align 8, !tbaa !9
  %13 = icmp ult ptr %12, getelementptr inbounds (i32, ptr getelementptr inbounds (i32, ptr @supported_ciphersuites, i64 141), i64 -1)
  br label %14

14:                                               ; preds = %11, %7
  %15 = phi i1 [ false, %7 ], [ %13, %11 ]
  br i1 %15, label %16, label %35

16:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %17 = load ptr, ptr %1, align 8, !tbaa !9
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = call ptr @mbedtls_ssl_ciphersuite_from_id(i32 noundef %18)
  store ptr %19, ptr %3, align 8, !tbaa !9
  %20 = icmp ne ptr %19, null
  br i1 %20, label %21, label %31

21:                                               ; preds = %16
  %22 = load ptr, ptr %3, align 8, !tbaa !9
  %23 = getelementptr inbounds %struct.mbedtls_ssl_ciphersuite_t, ptr %22, i32 0, i32 2
  %24 = load i32, ptr %23, align 8, !tbaa !11
  %25 = icmp ne i32 %24, 42
  br i1 %25, label %26, label %31

26:                                               ; preds = %21
  %27 = load ptr, ptr %1, align 8, !tbaa !9
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = load ptr, ptr %2, align 8, !tbaa !9
  %30 = getelementptr inbounds i32, ptr %29, i32 1
  store ptr %30, ptr %2, align 8, !tbaa !9
  store i32 %28, ptr %29, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %26, %21, %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  br label %32

32:                                               ; preds = %31
  %33 = load ptr, ptr %1, align 8, !tbaa !9
  %34 = getelementptr inbounds i32, ptr %33, i32 1
  store ptr %34, ptr %1, align 8, !tbaa !9
  br label %7, !llvm.loop !13

35:                                               ; preds = %14
  %36 = load ptr, ptr %2, align 8, !tbaa !9
  store i32 0, ptr %36, align 4, !tbaa !5
  store i32 1, ptr @supported_init, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  br label %37

37:                                               ; preds = %35, %0
  ret ptr @supported_ciphersuites
}

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

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

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

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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"mbedtls_ssl_ciphersuite_t", !6, i64 0, !10, i64 8, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36, !6, i64 40, !7, i64 44}
!13 = distinct !{!13, !14}
!14 = !{!"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"

@supported_init = external hidden local_unnamed_addr global i32, align 4
@ciphersuite_preference = external hidden local_unnamed_addr constant [163 x i32], align 16
@supported_ciphersuites = external hidden global [141 x i32], align 16

; Function Attrs: nounwind uwtable
define dso_local noundef nonnull ptr @mbedtls_ssl_list_ciphersuites() local_unnamed_addr #0 {
  %1 = load i32, ptr @supported_init, align 4, !tbaa !5
  %2 = icmp eq i32 %1, 0
  br i1 %2, label %.preheader, label %16

.preheader:                                       ; preds = %0
  %3 = load i32, ptr @ciphersuite_preference, align 16, !tbaa !5
  %.not15 = icmp eq i32 %3, 0
  br i1 %.not15, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %10
  %4 = phi i32 [ %12, %10 ], [ %3, %.preheader ]
  %.08.ptr14 = phi ptr [ %.08.ptr, %10 ], [ @supported_ciphersuites, %.preheader ]
  %.013 = phi ptr [ %11, %10 ], [ @ciphersuite_preference, %.preheader ]
  %.08.idx12 = phi i64 [ %.1.idx, %10 ], [ 0, %.preheader ]
  %5 = tail call ptr @mbedtls_ssl_ciphersuite_from_id(i32 noundef %4)
  %.not = icmp eq ptr %5, null
  br i1 %.not, label %10, label %6

6:                                                ; preds = %.lr.ph
  %7 = getelementptr inbounds i8, ptr %5, i64 16
  %8 = load i32, ptr %7, align 8, !tbaa !9
  %.not11 = icmp eq i32 %8, 42
  br i1 %.not11, label %10, label %9

9:                                                ; preds = %6
  %.08.add = add nsw i64 %.08.idx12, 4
  store i32 %4, ptr %.08.ptr14, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %9, %6, %.lr.ph
  %.1.idx = phi i64 [ %.08.add, %9 ], [ %.08.idx12, %6 ], [ %.08.idx12, %.lr.ph ]
  %11 = getelementptr inbounds i8, ptr %.013, i64 4
  %.08.ptr = getelementptr inbounds i8, ptr @supported_ciphersuites, i64 %.1.idx
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = icmp ne i32 %12, 0
  %14 = icmp slt i64 %.1.idx, 560
  %15 = select i1 %13, i1 %14, i1 false
  br i1 %15, label %.lr.ph, label %._crit_edge, !llvm.loop !12

._crit_edge:                                      ; preds = %10, %.preheader
  %.08.ptr.lcssa = phi ptr [ @supported_ciphersuites, %.preheader ], [ %.08.ptr, %10 ]
  store i32 0, ptr %.08.ptr.lcssa, align 4, !tbaa !5
  store i32 1, ptr @supported_init, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %._crit_edge, %0
  ret ptr @supported_ciphersuites
}

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

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

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 16}
!10 = !{!"mbedtls_ssl_ciphersuite_t", !6, i64 0, !11, i64 8, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36, !6, i64 40, !7, i64 44}
!11 = !{!"any pointer", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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