; LLVM IR from aws/s2n-tls :: utils (s2n_map)

; 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.s2n_result = type { i32 }
%struct.s2n_map = type { i32, i32, i32, ptr }
%struct.s2n_map_entry = type { %struct.s2n_blob, %struct.s2n_blob }
%struct.s2n_blob = type { ptr, i32, i32, i8 }

; 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

declare i32 @s2n_free(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @s2n_map_free(ptr noundef %0) #2 {
  %2 = alloca %struct.s2n_result, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %10

8:                                                ; preds = %1
  %9 = getelementptr inbounds %struct.s2n_result, ptr %2, i32 0, i32 0
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %88

10:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store i64 0, ptr %4, align 8, !tbaa !12
  br label %11

11:                                               ; preds = %59, %10
  %12 = load i64, ptr %4, align 8, !tbaa !12
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.s2n_map, ptr %13, i32 0, i32 0
  %15 = load i32, ptr %14, align 8, !tbaa !14
  %16 = zext i32 %15 to i64
  %17 = icmp ult i64 %12, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %11
  store i32 2, ptr %5, align 4
  br label %62

19:                                               ; preds = %11
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.s2n_map, ptr %20, i32 0, i32 3
  %22 = load ptr, ptr %21, align 8, !tbaa !16
  %23 = load i64, ptr %4, align 8, !tbaa !12
  %24 = getelementptr inbounds %struct.s2n_map_entry, ptr %22, i64 %23
  %25 = getelementptr inbounds %struct.s2n_map_entry, ptr %24, i32 0, i32 0
  %26 = getelementptr inbounds %struct.s2n_blob, ptr %25, i32 0, i32 1
  %27 = load i32, ptr %26, align 8, !tbaa !17
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %58

29:                                               ; preds = %19
  br label %30

30:                                               ; preds = %29
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.s2n_map, ptr %31, i32 0, i32 3
  %33 = load ptr, ptr %32, align 8, !tbaa !16
  %34 = load i64, ptr %4, align 8, !tbaa !12
  %35 = getelementptr inbounds %struct.s2n_map_entry, ptr %33, i64 %34
  %36 = getelementptr inbounds %struct.s2n_map_entry, ptr %35, i32 0, i32 0
  %37 = call i32 @s2n_free(ptr noundef %36)
  %38 = icmp sgt i32 %37, -1
  br i1 %38, label %41, label %39

39:                                               ; preds = %30
  %40 = getelementptr inbounds %struct.s2n_result, ptr %2, i32 0, i32 0
  store i32 -1, ptr %40, align 4, !tbaa !9
  store i32 1, ptr %5, align 4
  br label %62

41:                                               ; preds = %30
  br label %42

42:                                               ; preds = %41
  br label %43

43:                                               ; preds = %42
  br label %44

44:                                               ; preds = %43
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.s2n_map, ptr %45, i32 0, i32 3
  %47 = load ptr, ptr %46, align 8, !tbaa !16
  %48 = load i64, ptr %4, align 8, !tbaa !12
  %49 = getelementptr inbounds %struct.s2n_map_entry, ptr %47, i64 %48
  %50 = getelementptr inbounds %struct.s2n_map_entry, ptr %49, i32 0, i32 1
  %51 = call i32 @s2n_free(ptr noundef %50)
  %52 = icmp sgt i32 %51, -1
  br i1 %52, label %55, label %53

53:                                               ; preds = %44
  %54 = getelementptr inbounds %struct.s2n_result, ptr %2, i32 0, i32 0
  store i32 -1, ptr %54, align 4, !tbaa !9
  store i32 1, ptr %5, align 4
  br label %62

55:                                               ; preds = %44
  br label %56

56:                                               ; preds = %55
  br label %57

57:                                               ; preds = %56
  br label %58

58:                                               ; preds = %57, %19
  br label %59

59:                                               ; preds = %58
  %60 = load i64, ptr %4, align 8, !tbaa !12
  %61 = add i64 %60, 1
  store i64 %61, ptr %4, align 8, !tbaa !12
  br label %11, !llvm.loop !20

62:                                               ; preds = %53, %39, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %63 = load i32, ptr %5, align 4
  switch i32 %63, label %91 [
    i32 2, label %64
    i32 1, label %88
  ]

64:                                               ; preds = %62
  br label %65

65:                                               ; preds = %64
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.s2n_map, ptr %66, i32 0, i32 3
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.s2n_map, ptr %68, i32 0, i32 0
  %70 = load i32, ptr %69, align 8, !tbaa !14
  %71 = zext i32 %70 to i64
  %72 = mul i64 %71, 48
  %73 = trunc i64 %72 to i32
  %74 = call i32 @s2n_free_object(ptr noundef %67, i32 noundef %73)
  %75 = icmp sgt i32 %74, -1
  br i1 %75, label %78, label %76

76:                                               ; preds = %65
  %77 = getelementptr inbounds %struct.s2n_result, ptr %2, i32 0, i32 0
  store i32 -1, ptr %77, align 4, !tbaa !9
  br label %88

78:                                               ; preds = %65
  br label %79

79:                                               ; preds = %78
  br label %80

80:                                               ; preds = %79
  %81 = call i32 @s2n_free_object(ptr noundef %3, i32 noundef 24)
  %82 = icmp sgt i32 %81, -1
  br i1 %82, label %85, label %83

83:                                               ; preds = %80
  %84 = getelementptr inbounds %struct.s2n_result, ptr %2, i32 0, i32 0
  store i32 -1, ptr %84, align 4, !tbaa !9
  br label %88

85:                                               ; preds = %80
  br label %86

86:                                               ; preds = %85
  %87 = getelementptr inbounds %struct.s2n_result, ptr %2, i32 0, i32 0
  store i32 0, ptr %87, align 4, !tbaa !9
  br label %88

88:                                               ; preds = %86, %83, %76, %62, %8
  %89 = getelementptr inbounds %struct.s2n_result, ptr %2, i32 0, i32 0
  %90 = load i32, ptr %89, align 4
  ret i32 %90

91:                                               ; preds = %62
  unreachable
}

declare i32 @s2n_free_object(ptr noundef, i32 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { 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, !11, i64 0}
!10 = !{!"", !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!15, !11, i64 0}
!15 = !{!"s2n_map", !11, i64 0, !11, i64 4, !11, i64 8, !6, i64 16}
!16 = !{!15, !6, i64 16}
!17 = !{!18, !11, i64 8}
!18 = !{!"s2n_map_entry", !19, i64 0, !19, i64 24}
!19 = !{!"s2n_blob", !6, i64 0, !11, i64 8, !11, i64 12, !11, i64 16}
!20 = distinct !{!20, !21}
!21 = !{!"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.s2n_map_entry = type { %struct.s2n_blob, %struct.s2n_blob }
%struct.s2n_blob = type { ptr, i32, i32, i8 }

declare i32 @s2n_free(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @s2n_map_free(ptr noundef %0) local_unnamed_addr #1 {
  %2 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %3 = icmp eq ptr %0, null
  br i1 %3, label %.thread, label %.preheader

.preheader:                                       ; preds = %1
  %4 = load i32, ptr %0, align 8, !tbaa !9
  %.not11 = icmp eq i32 %4, 0
  br i1 %.not11, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  br label %6

6:                                                ; preds = %.lr.ph, %20
  %7 = phi i32 [ %4, %.lr.ph ], [ %21, %20 ]
  %.0610 = phi i64 [ 0, %.lr.ph ], [ %22, %20 ]
  %8 = load ptr, ptr %5, align 8, !tbaa !12
  %9 = getelementptr inbounds %struct.s2n_map_entry, ptr %8, i64 %.0610
  %10 = getelementptr inbounds i8, ptr %9, i64 8
  %11 = load i32, ptr %10, align 8, !tbaa !13
  %.not = icmp eq i32 %11, 0
  br i1 %.not, label %20, label %12

12:                                               ; preds = %6
  %13 = tail call i32 @s2n_free(ptr noundef nonnull %9) #2
  %14 = icmp sgt i32 %13, -1
  br i1 %14, label %15, label %.thread

15:                                               ; preds = %12
  %16 = load ptr, ptr %5, align 8, !tbaa !12
  %17 = getelementptr inbounds %struct.s2n_map_entry, ptr %16, i64 %.0610, i32 1
  %18 = tail call i32 @s2n_free(ptr noundef nonnull %17) #2
  %19 = icmp sgt i32 %18, -1
  br i1 %19, label %._crit_edge12, label %.thread

._crit_edge12:                                    ; preds = %15
  %.pre = load i32, ptr %0, align 8, !tbaa !9
  br label %20

20:                                               ; preds = %._crit_edge12, %6
  %21 = phi i32 [ %.pre, %._crit_edge12 ], [ %7, %6 ]
  %22 = add nuw nsw i64 %.0610, 1
  %23 = zext i32 %21 to i64
  %24 = icmp ult i64 %22, %23
  br i1 %24, label %6, label %._crit_edge.loopexit, !llvm.loop !16

._crit_edge.loopexit:                             ; preds = %20
  %25 = mul i32 %21, 48
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %26 = phi i32 [ %25, %._crit_edge.loopexit ], [ 0, %.preheader ]
  %27 = getelementptr inbounds i8, ptr %0, i64 16
  %28 = tail call i32 @s2n_free_object(ptr noundef nonnull %27, i32 noundef %26) #2
  %29 = icmp sgt i32 %28, -1
  br i1 %29, label %30, label %.thread

30:                                               ; preds = %._crit_edge
  %31 = call i32 @s2n_free_object(ptr noundef nonnull %2, i32 noundef 24) #2
  %.lobit = ashr i32 %31, 31
  br label %.thread

.thread:                                          ; preds = %15, %12, %30, %._crit_edge, %1
  %.sroa.0.1 = phi i32 [ 0, %1 ], [ -1, %._crit_edge ], [ %.lobit, %30 ], [ -1, %12 ], [ -1, %15 ]
  ret i32 %.sroa.0.1
}

declare i32 @s2n_free_object(ptr noundef, i32 noundef) local_unnamed_addr #0

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

!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, !11, i64 0}
!10 = !{!"s2n_map", !11, i64 0, !11, i64 4, !11, i64 8, !6, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 16}
!13 = !{!14, !11, i64 8}
!14 = !{!"s2n_map_entry", !15, i64 0, !15, i64 24}
!15 = !{!"s2n_blob", !6, i64 0, !11, i64 8, !11, i64 12, !11, i64 16}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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