; LLVM IR from erthink/libmdbx :: src (chk)

; 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.MDBX_chk_context = type { ptr, ptr, ptr, ptr, i8, %struct.anon.7 }
%struct.anon.7 = type { i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, ptr }
%struct.MDBX_chk_internal = type { ptr, ptr, i64, ptr, i8, i8, i8, i8, %struct.MDBX_chk_table, %struct.MDBX_chk_table, ptr, ptr, ptr, [12 x %struct.MDBX_chk_scope], [32767 x ptr], %struct.MDBX_envinfo, %struct.troika, %struct.iovec }
%struct.MDBX_chk_table = type { ptr, %struct.iovec, i32, i32, i64, i64, %struct.anon, %struct.anon.0 }
%struct.anon = type { i64, i64, i64, i64, i64, i64, i64, i64 }
%struct.anon.0 = type { %struct.MDBX_chk_histogram, %struct.MDBX_chk_histogram, %struct.MDBX_chk_histogram, %struct.MDBX_chk_histogram, %struct.MDBX_chk_histogram, %struct.MDBX_chk_histogram }
%struct.MDBX_chk_histogram = type { i64, i64, i64, i64, [9 x %struct.anon.1] }
%struct.anon.1 = type { i64, i64, i64, i64 }
%struct.MDBX_chk_scope = type { ptr, ptr, ptr, i32, i32, i64, %union.anon, %union.anon, %union.anon }
%union.anon = type { ptr }
%struct.MDBX_envinfo = type { %struct.anon.2, i64, i64, i64, i64, i64, [3 x i64], [3 x i64], i32, i32, i32, i32, %struct.anon.3, i64, i64, i32, i32, i32, i32, %struct.anon.5, %struct.anon.6 }
%struct.anon.2 = type { i64, i64, i64, i64, i64 }
%struct.anon.3 = type { %struct.anon.4, [3 x %struct.anon.4] }
%struct.anon.4 = type { i64, i64 }
%struct.anon.5 = type { i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64 }
%struct.anon.6 = type { i64, i64 }
%struct.troika = type { i8, i8, i8, i8, i32, [3 x i64] }
%struct.iovec = type { ptr, i64 }

; Function Attrs: cold nounwind optsize uwtable
define dso_local i32 @mdbx_env_chk_encount_problem(ptr noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = icmp ne ptr %4, null
  br i1 %5, label %6, label %31

6:                                                ; preds = %1
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.MDBX_chk_context, ptr %7, i32 0, i32 0
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = icmp ne ptr %9, null
  br i1 %10, label %11, label %31

11:                                               ; preds = %6
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.MDBX_chk_context, ptr %12, i32 0, i32 0
  %14 = load ptr, ptr %13, align 8, !tbaa !9
  %15 = getelementptr inbounds %struct.MDBX_chk_internal, ptr %14, i32 0, i32 0
  %16 = load ptr, ptr %15, align 8, !tbaa !13
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = icmp eq ptr %16, %17
  br i1 %18, label %19, label %31

19:                                               ; preds = %11
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.MDBX_chk_context, ptr %20, i32 0, i32 0
  %22 = load ptr, ptr %21, align 8, !tbaa !9
  %23 = getelementptr inbounds %struct.MDBX_chk_internal, ptr %22, i32 0, i32 3
  %24 = load ptr, ptr %23, align 8, !tbaa !28
  %25 = icmp ne ptr %24, null
  br i1 %25, label %26, label %31

26:                                               ; preds = %19
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.MDBX_chk_context, ptr %27, i32 0, i32 3
  %29 = load ptr, ptr %28, align 8, !tbaa !29
  %30 = icmp ne ptr %29, null
  br label %31

31:                                               ; preds = %26, %19, %11, %6, %1
  %32 = phi i1 [ false, %19 ], [ false, %11 ], [ false, %6 ], [ false, %1 ], [ %30, %26 ]
  %33 = xor i1 %32, true
  %34 = xor i1 %33, true
  %35 = zext i1 %34 to i32
  %36 = sext i32 %35 to i64
  %37 = call i64 @llvm.expect.i64(i64 %36, i64 1)
  %38 = icmp ne i64 %37, 0
  br i1 %38, label %39, label %53

39:                                               ; preds = %31
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.MDBX_chk_context, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.MDBX_chk_internal, ptr %42, i32 0, i32 3
  %44 = load ptr, ptr %43, align 8, !tbaa !28
  %45 = load i64, ptr %44, align 8, !tbaa !30
  %46 = add i64 %45, 1
  store i64 %46, ptr %44, align 8, !tbaa !30
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.MDBX_chk_context, ptr %47, i32 0, i32 3
  %49 = load ptr, ptr %48, align 8, !tbaa !29
  %50 = getelementptr inbounds %struct.MDBX_chk_scope, ptr %49, i32 0, i32 5
  %51 = load i64, ptr %50, align 8, !tbaa !31
  %52 = add i64 %51, 1
  store i64 %52, ptr %50, align 8, !tbaa !31
  store i32 0, ptr %2, align 4
  br label %54

53:                                               ; preds = %31
  store i32 22, ptr %2, align 4
  br label %54

54:                                               ; preds = %53, %39
  %55 = load i32, ptr %2, align 4
  ret i32 %55
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #1

attributes #0 = { cold nounwind optsize 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(none) }

!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, !6, i64 0}
!10 = !{!"MDBX_chk_context", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !7, i64 32, !11, i64 40}
!11 = !{!"", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !12, i64 56, !12, i64 64, !12, i64 72, !12, i64 80, !12, i64 88, !12, i64 96, !12, i64 104, !12, i64 112, !12, i64 120, !12, i64 128, !12, i64 136, !12, i64 144, !6, i64 152}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"MDBX_chk_internal", !6, i64 0, !6, i64 8, !12, i64 16, !6, i64 24, !7, i64 32, !15, i64 33, !15, i64 34, !7, i64 35, !16, i64 40, !16, i64 2072, !6, i64 4104, !6, i64 4112, !6, i64 4120, !7, i64 4128, !7, i64 4896, !22, i64 267032, !27, i64 267384, !17, i64 267416}
!15 = !{!"_Bool", !7, i64 0}
!16 = !{!"MDBX_chk_table", !6, i64 0, !17, i64 8, !18, i64 24, !18, i64 28, !12, i64 32, !12, i64 40, !19, i64 48, !20, i64 112}
!17 = !{!"iovec", !6, i64 0, !12, i64 8}
!18 = !{!"int", !7, i64 0}
!19 = !{!"", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !12, i64 56}
!20 = !{!"", !21, i64 0, !21, i64 320, !21, i64 640, !21, i64 960, !21, i64 1280, !21, i64 1600}
!21 = !{!"MDBX_chk_histogram", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !7, i64 32}
!22 = !{!"MDBX_envinfo", !23, i64 0, !12, i64 40, !12, i64 48, !12, i64 56, !12, i64 64, !12, i64 72, !7, i64 80, !7, i64 104, !18, i64 128, !18, i64 132, !18, i64 136, !18, i64 140, !24, i64 144, !12, i64 208, !12, i64 216, !18, i64 224, !18, i64 228, !18, i64 232, !18, i64 236, !26, i64 240, !25, i64 336}
!23 = !{!"", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32}
!24 = !{!"", !25, i64 0, !7, i64 16}
!25 = !{!"", !12, i64 0, !12, i64 8}
!26 = !{!"", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !12, i64 56, !12, i64 64, !12, i64 72, !12, i64 80, !12, i64 88}
!27 = !{!"troika", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !18, i64 4, !7, i64 8}
!28 = !{!14, !6, i64 24}
!29 = !{!10, !6, i64 24}
!30 = !{!12, !12, i64 0}
!31 = !{!32, !12, i64 32}
!32 = !{!"MDBX_chk_scope", !6, i64 0, !6, i64 8, !6, i64 16, !18, i64 24, !18, i64 28, !12, i64 32, !7, i64 40, !7, i64 48, !7, i64 56}


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

; Function Attrs: cold mustprogress nofree norecurse nosync nounwind optsize willreturn memory(readwrite, inaccessiblemem: none) uwtable
define dso_local range(i32 0, 23) i32 @mdbx_env_chk_encount_problem(ptr noundef readonly %0) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.critedge, label %2, !prof !5

2:                                                ; preds = %1
  %3 = load ptr, ptr %0, align 8, !tbaa !6
  %.not10 = icmp eq ptr %3, null
  br i1 %.not10, label %.critedge, label %4, !prof !5

4:                                                ; preds = %2
  %5 = load ptr, ptr %3, align 8, !tbaa !13
  %6 = icmp eq ptr %5, %0
  br i1 %6, label %7, label %.critedge, !prof !28

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %3, i64 24
  %9 = load ptr, ptr %8, align 8, !tbaa !29
  %.not11 = icmp eq ptr %9, null
  br i1 %.not11, label %.critedge, label %10, !prof !5

10:                                               ; preds = %7
  %11 = getelementptr inbounds i8, ptr %0, i64 24
  %12 = load ptr, ptr %11, align 8, !tbaa !30
  %.not12 = icmp eq ptr %12, null
  br i1 %.not12, label %.critedge, label %13, !prof !5

13:                                               ; preds = %10
  %14 = load i64, ptr %9, align 8, !tbaa !31
  %15 = add i64 %14, 1
  store i64 %15, ptr %9, align 8, !tbaa !31
  %16 = getelementptr inbounds i8, ptr %12, i64 32
  %17 = load i64, ptr %16, align 8, !tbaa !32
  %18 = add i64 %17, 1
  store i64 %18, ptr %16, align 8, !tbaa !32
  br label %.critedge

.critedge:                                        ; preds = %10, %1, %2, %4, %7, %13
  %.0 = phi i32 [ 0, %13 ], [ 22, %7 ], [ 22, %4 ], [ 22, %2 ], [ 22, %1 ], [ 22, %10 ]
  ret i32 %.0
}

attributes #0 = { cold mustprogress nofree norecurse nosync nounwind optsize willreturn memory(readwrite, 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" }

!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 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!6 = !{!7, !8, i64 0}
!7 = !{!"MDBX_chk_context", !8, i64 0, !8, i64 8, !8, i64 16, !8, i64 24, !9, i64 32, !11, i64 40}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !12, i64 56, !12, i64 64, !12, i64 72, !12, i64 80, !12, i64 88, !12, i64 96, !12, i64 104, !12, i64 112, !12, i64 120, !12, i64 128, !12, i64 136, !12, i64 144, !8, i64 152}
!12 = !{!"long", !9, i64 0}
!13 = !{!14, !8, i64 0}
!14 = !{!"MDBX_chk_internal", !8, i64 0, !8, i64 8, !12, i64 16, !8, i64 24, !9, i64 32, !15, i64 33, !15, i64 34, !9, i64 35, !16, i64 40, !16, i64 2072, !8, i64 4104, !8, i64 4112, !8, i64 4120, !9, i64 4128, !9, i64 4896, !22, i64 267032, !27, i64 267384, !17, i64 267416}
!15 = !{!"_Bool", !9, i64 0}
!16 = !{!"MDBX_chk_table", !8, i64 0, !17, i64 8, !18, i64 24, !18, i64 28, !12, i64 32, !12, i64 40, !19, i64 48, !20, i64 112}
!17 = !{!"iovec", !8, i64 0, !12, i64 8}
!18 = !{!"int", !9, i64 0}
!19 = !{!"", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !12, i64 56}
!20 = !{!"", !21, i64 0, !21, i64 320, !21, i64 640, !21, i64 960, !21, i64 1280, !21, i64 1600}
!21 = !{!"MDBX_chk_histogram", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !9, i64 32}
!22 = !{!"MDBX_envinfo", !23, i64 0, !12, i64 40, !12, i64 48, !12, i64 56, !12, i64 64, !12, i64 72, !9, i64 80, !9, i64 104, !18, i64 128, !18, i64 132, !18, i64 136, !18, i64 140, !24, i64 144, !12, i64 208, !12, i64 216, !18, i64 224, !18, i64 228, !18, i64 232, !18, i64 236, !26, i64 240, !25, i64 336}
!23 = !{!"", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32}
!24 = !{!"", !25, i64 0, !9, i64 16}
!25 = !{!"", !12, i64 0, !12, i64 8}
!26 = !{!"", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !12, i64 56, !12, i64 64, !12, i64 72, !12, i64 80, !12, i64 88}
!27 = !{!"troika", !9, i64 0, !9, i64 1, !9, i64 2, !9, i64 3, !18, i64 4, !9, i64 8}
!28 = !{!"branch_weights", !"expected", i32 2000, i32 1}
!29 = !{!14, !8, i64 24}
!30 = !{!7, !8, i64 24}
!31 = !{!12, !12, i64 0}
!32 = !{!33, !12, i64 32}
!33 = !{!"MDBX_chk_scope", !8, i64 0, !8, i64 8, !8, i64 16, !18, i64 24, !18, i64 28, !12, i64 32, !9, i64 40, !9, i64 48, !9, i64 56}


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