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

; 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.libmdbx_globals = type { %union.bin128, i32, i32, i8, i8, i8, i8, i32, %union.logger_union, %union.pthread_mutex_t, i64, ptr }
%union.bin128 = type { %struct.anon }
%struct.anon = type { i64, i64 }
%union.logger_union = type { ptr }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%struct.MDBX_txn = type { i32, i32, i64, i64, ptr, ptr, %struct.geo, i64, i64, ptr, ptr, ptr, ptr, ptr, ptr, %struct.MDBX_canary, ptr, %union.anon.2 }
%struct.geo = type { i16, i16, i32, i32, %union.anon, %union.anon.1 }
%union.anon = type { i32 }
%union.anon.1 = type { i32 }
%struct.MDBX_canary = type { i64, i64, i64, i64 }
%union.anon.2 = type { %struct.anon.4 }
%struct.anon.4 = type { %struct.troika, ptr, %struct.anon.5, i8, i32, i32, i64, ptr, ptr, ptr, i64, %union.anon.6 }
%struct.troika = type { i8, i8, i8, i8, i32, [3 x i64] }
%struct.anon.5 = type { %struct.MDBX_rkl, i64, %struct.MDBX_rkl }
%struct.MDBX_rkl = type { i64, i64, i32, i32, ptr, [12 x i64] }
%union.anon.6 = type { %struct.anon.7 }
%struct.anon.7 = type { i64, ptr }

@globals = external global %struct.libmdbx_globals, align 8
@.str.1 = external hidden unnamed_addr constant [34 x i8], align 1
@__func__.txn_done_cursors = external hidden unnamed_addr constant [17 x i8], align 1

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

; Function Attrs: noreturn
declare void @mdbx_assert_fail(ptr noundef, ptr noundef, ptr noundef, i32 noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local void @txn_done_cursors(ptr noundef %0) #3 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %1
  %7 = load i8, ptr getelementptr inbounds (%struct.libmdbx_globals, ptr @globals, i32 0, i32 4), align 1, !tbaa !9
  %8 = zext i8 %7 to i32
  %9 = and i32 %8, 1
  %10 = icmp ne i32 %9, 0
  %11 = xor i1 %10, true
  %12 = xor i1 %11, true
  %13 = zext i1 %12 to i32
  %14 = sext i32 %13 to i64
  %15 = call i64 @llvm.expect.i64(i64 %14, i64 1)
  %16 = icmp ne i64 %15, 0
  br i1 %16, label %17, label %37

17:                                               ; preds = %6
  br label %18

18:                                               ; preds = %17
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.MDBX_txn, ptr %19, i32 0, i32 1
  %21 = load i32, ptr %20, align 4, !tbaa !14
  %22 = and i32 %21, 256
  %23 = icmp ne i32 %22, 0
  %24 = xor i1 %23, true
  %25 = xor i1 %24, true
  %26 = xor i1 %25, true
  %27 = zext i1 %26 to i32
  %28 = sext i32 %27 to i64
  %29 = call i64 @llvm.expect.i64(i64 %28, i64 0)
  %30 = icmp ne i64 %29, 0
  br i1 %30, label %31, label %35

31:                                               ; preds = %18
  %32 = load ptr, ptr %2, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.MDBX_txn, ptr %32, i32 0, i32 9
  %34 = load ptr, ptr %33, align 8, !tbaa !19
  call void @mdbx_assert_fail(ptr noundef %34, ptr noundef @.str.1, ptr noundef @__func__.txn_done_cursors, i32 noundef 21) #6
  unreachable

35:                                               ; preds = %18
  br label %36

36:                                               ; preds = %35
  br label %37

37:                                               ; preds = %36, %6
  br label %38

38:                                               ; preds = %37
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #7
  %39 = load ptr, ptr %2, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.MDBX_txn, ptr %39, i32 0, i32 11
  %41 = load ptr, ptr %40, align 8, !tbaa !20
  %42 = getelementptr inbounds i32, ptr %41, i64 0
  %43 = load i32, ptr %42, align 4, !tbaa !21
  %44 = lshr i32 %43, 0
  %45 = zext i32 %44 to i64
  store i64 %45, ptr %3, align 8, !tbaa !22
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #7
  store i64 0, ptr %4, align 8, !tbaa !22
  br label %46

46:                                               ; preds = %81, %38
  %47 = load i64, ptr %4, align 8, !tbaa !22
  %48 = load ptr, ptr %2, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.MDBX_txn, ptr %48, i32 0, i32 2
  %50 = load i64, ptr %49, align 8, !tbaa !23
  %51 = icmp ult i64 %47, %50
  br i1 %51, label %53, label %52

52:                                               ; preds = %46
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #7
  br label %84

53:                                               ; preds = %46
  %54 = load ptr, ptr %2, align 8, !tbaa !5
  %55 = call zeroext i1 @dbi_foreach_step(ptr noundef %54, ptr noundef %3, ptr noundef %4)
  br i1 %55, label %56, label %80

56:                                               ; preds = %53
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #7
  %57 = load ptr, ptr %2, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.MDBX_txn, ptr %57, i32 0, i32 14
  %59 = load ptr, ptr %58, align 8, !tbaa !24
  %60 = load i64, ptr %4, align 8, !tbaa !22
  %61 = getelementptr inbounds ptr, ptr %59, i64 %60
  %62 = load ptr, ptr %61, align 8, !tbaa !5
  store ptr %62, ptr %5, align 8, !tbaa !5
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = icmp ne ptr %63, null
  br i1 %64, label %65, label %79

65:                                               ; preds = %56
  %66 = load ptr, ptr %2, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.MDBX_txn, ptr %66, i32 0, i32 14
  %68 = load ptr, ptr %67, align 8, !tbaa !24
  %69 = load i64, ptr %4, align 8, !tbaa !22
  %70 = getelementptr inbounds ptr, ptr %68, i64 %69
  store ptr null, ptr %70, align 8, !tbaa !5
  br label %71

71:                                               ; preds = %75, %65
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  %73 = load ptr, ptr %2, align 8, !tbaa !5
  %74 = call ptr @cursor_eot(ptr noundef %72, ptr noundef %73)
  store ptr %74, ptr %5, align 8, !tbaa !5
  br label %75

75:                                               ; preds = %71
  %76 = load ptr, ptr %5, align 8, !tbaa !5
  %77 = icmp ne ptr %76, null
  br i1 %77, label %71, label %78, !llvm.loop !25

78:                                               ; preds = %75
  br label %79

79:                                               ; preds = %78, %56
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #7
  br label %80

80:                                               ; preds = %79, %53
  br label %81

81:                                               ; preds = %80
  %82 = load i64, ptr %4, align 8, !tbaa !22
  %83 = add i64 %82, 1
  store i64 %83, ptr %4, align 8, !tbaa !22
  br label %46, !llvm.loop !27

84:                                               ; preds = %52
  %85 = load ptr, ptr %2, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.MDBX_txn, ptr %85, i32 0, i32 1
  %87 = load i32, ptr %86, align 4, !tbaa !14
  %88 = and i32 %87, -257
  store i32 %88, ptr %86, align 4, !tbaa !14
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @dbi_foreach_step(ptr noundef, ptr noundef, ptr noundef) #4

declare ptr @cursor_eot(ptr noundef, ptr noundef) #5

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #1 = { noreturn "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = { 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 = { "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 #6 = { noreturn }
attributes #7 = { 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, !7, i64 25}
!10 = !{!"libmdbx_globals", !7, i64 0, !11, i64 16, !11, i64 20, !7, i64 24, !7, i64 25, !7, i64 26, !12, i64 27, !11, i64 28, !7, i64 32, !7, i64 40, !13, i64 80, !6, i64 88}
!11 = !{!"int", !7, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!15, !11, i64 4}
!15 = !{!"MDBX_txn", !11, i64 0, !11, i64 4, !13, i64 8, !13, i64 16, !6, i64 24, !6, i64 32, !16, i64 40, !13, i64 64, !13, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !18, i64 128, !6, i64 160, !7, i64 168}
!16 = !{!"geo", !17, i64 0, !17, i64 2, !11, i64 4, !11, i64 8, !7, i64 12, !7, i64 16}
!17 = !{!"short", !7, i64 0}
!18 = !{!"MDBX_canary", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24}
!19 = !{!15, !6, i64 80}
!20 = !{!15, !6, i64 96}
!21 = !{!11, !11, i64 0}
!22 = !{!13, !13, i64 0}
!23 = !{!15, !13, i64 8}
!24 = !{!15, !6, i64 120}
!25 = distinct !{!25, !26}
!26 = !{!"llvm.loop.mustprogress"}
!27 = distinct !{!27, !26}


; 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.libmdbx_globals = type { %union.bin128, i32, i32, i8, i8, i8, i8, i32, %union.logger_union, %union.pthread_mutex_t, i64, ptr }
%union.bin128 = type { %struct.anon }
%struct.anon = type { i64, i64 }
%union.logger_union = type { ptr }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@globals = external local_unnamed_addr global %struct.libmdbx_globals, align 8
@.str.1 = external hidden unnamed_addr constant [34 x i8], align 1
@__func__.txn_done_cursors = external hidden unnamed_addr constant [17 x i8], align 1

; Function Attrs: noreturn
declare void @mdbx_assert_fail(ptr noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
define dso_local void @txn_done_cursors(ptr noundef %0) local_unnamed_addr #2 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = load i8, ptr getelementptr inbounds (i8, ptr @globals, i64 25), align 1, !tbaa !5
  %5 = and i8 %4, 1
  %.not = icmp eq i8 %5, 0
  br i1 %.not, label %13, label %6, !prof !13

6:                                                ; preds = %1
  %7 = getelementptr inbounds i8, ptr %0, i64 4
  %8 = load i32, ptr %7, align 4, !tbaa !14
  %9 = and i32 %8, 256
  %.not13 = icmp eq i32 %9, 0
  br i1 %.not13, label %10, label %13, !prof !13

10:                                               ; preds = %6
  %11 = getelementptr inbounds i8, ptr %0, i64 80
  %12 = load ptr, ptr %11, align 8, !tbaa !19
  tail call void @mdbx_assert_fail(ptr noundef %12, ptr noundef nonnull @.str.1, ptr noundef nonnull @__func__.txn_done_cursors, i32 noundef 21) #5
  unreachable

13:                                               ; preds = %1, %6
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #6
  %14 = getelementptr inbounds i8, ptr %0, i64 96
  %15 = load ptr, ptr %14, align 8, !tbaa !20
  %16 = load i32, ptr %15, align 4, !tbaa !21
  %17 = zext i32 %16 to i64
  store i64 %17, ptr %2, align 8, !tbaa !22
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #6
  %18 = getelementptr inbounds i8, ptr %0, i64 8
  store i64 0, ptr %3, align 8, !tbaa !22
  %19 = load i64, ptr %18, align 8, !tbaa !23
  %.not16 = icmp eq i64 %19, 0
  br i1 %.not16, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %13
  %20 = getelementptr inbounds i8, ptr %0, i64 120
  br label %24

._crit_edge:                                      ; preds = %.loopexit, %13
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #6
  %21 = getelementptr inbounds i8, ptr %0, i64 4
  %22 = load i32, ptr %21, align 4, !tbaa !14
  %23 = and i32 %22, -257
  store i32 %23, ptr %21, align 4, !tbaa !14
  ret void

24:                                               ; preds = %.lr.ph, %.loopexit
  %25 = call zeroext i1 @dbi_foreach_step(ptr noundef nonnull %0, ptr noundef nonnull %2, ptr noundef nonnull %3)
  %.pre17 = load i64, ptr %3, align 8, !tbaa !22
  br i1 %25, label %26, label %.loopexit

26:                                               ; preds = %24
  %27 = load ptr, ptr %20, align 8, !tbaa !24
  %28 = getelementptr inbounds ptr, ptr %27, i64 %.pre17
  %29 = load ptr, ptr %28, align 8, !tbaa !25
  %.not14 = icmp eq ptr %29, null
  br i1 %.not14, label %.loopexit, label %30

30:                                               ; preds = %26
  store ptr null, ptr %28, align 8, !tbaa !25
  br label %31

31:                                               ; preds = %31, %30
  %.0 = phi ptr [ %29, %30 ], [ %32, %31 ]
  %32 = call ptr @cursor_eot(ptr noundef nonnull %.0, ptr noundef %0) #6
  %.not15 = icmp eq ptr %32, null
  br i1 %.not15, label %.loopexit.loopexit, label %31, !llvm.loop !26

.loopexit.loopexit:                               ; preds = %31
  %.pre = load i64, ptr %3, align 8, !tbaa !22
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit, %26, %24
  %33 = phi i64 [ %.pre, %.loopexit.loopexit ], [ %.pre17, %26 ], [ %.pre17, %24 ]
  %34 = add i64 %33, 1
  store i64 %34, ptr %3, align 8, !tbaa !22
  %35 = load i64, ptr %18, align 8, !tbaa !23
  %36 = icmp ult i64 %34, %35
  br i1 %36, label %24, label %._crit_edge, !llvm.loop !28
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @dbi_foreach_step(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

declare ptr @cursor_eot(ptr noundef, ptr noundef) local_unnamed_addr #4

attributes #0 = { noreturn "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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = { "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 = { noreturn nounwind }
attributes #6 = { 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, !7, i64 25}
!6 = !{!"libmdbx_globals", !7, i64 0, !9, i64 16, !9, i64 20, !7, i64 24, !7, i64 25, !7, i64 26, !10, i64 27, !9, i64 28, !7, i64 32, !7, i64 40, !11, i64 80, !12, i64 88}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"_Bool", !7, i64 0}
!11 = !{!"long", !7, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!14 = !{!15, !9, i64 4}
!15 = !{!"MDBX_txn", !9, i64 0, !9, i64 4, !11, i64 8, !11, i64 16, !12, i64 24, !12, i64 32, !16, i64 40, !11, i64 64, !11, i64 72, !12, i64 80, !12, i64 88, !12, i64 96, !12, i64 104, !12, i64 112, !12, i64 120, !18, i64 128, !12, i64 160, !7, i64 168}
!16 = !{!"geo", !17, i64 0, !17, i64 2, !9, i64 4, !9, i64 8, !7, i64 12, !7, i64 16}
!17 = !{!"short", !7, i64 0}
!18 = !{!"MDBX_canary", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24}
!19 = !{!15, !12, i64 80}
!20 = !{!15, !12, i64 96}
!21 = !{!9, !9, i64 0}
!22 = !{!11, !11, i64 0}
!23 = !{!15, !11, i64 8}
!24 = !{!15, !12, i64 120}
!25 = !{!12, !12, i64 0}
!26 = distinct !{!26, !27}
!27 = !{!"llvm.loop.mustprogress"}
!28 = distinct !{!28, !27}


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