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

; 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_txn = type { i32, i32, i64, i64, ptr, ptr, %struct.geo, i64, i64, ptr, ptr, ptr, ptr, ptr, ptr, %struct.MDBX_canary, ptr, %union.anon.4 }
%struct.geo = type { i16, i16, i32, i32, %union.anon.2, %union.anon.3 }
%union.anon.2 = type { i32 }
%union.anon.3 = type { i32 }
%struct.MDBX_canary = type { i64, i64, i64, i64 }
%union.anon.4 = type { %struct.anon.6 }
%struct.anon.6 = type { %struct.troika, ptr, %struct.anon.7, i8, i32, i32, i64, ptr, ptr, ptr, i64, %union.anon.8 }
%struct.troika = type { i8, i8, i8, i8, i32, [3 x i64] }
%struct.anon.7 = type { %struct.MDBX_rkl, i64, %struct.MDBX_rkl }
%struct.MDBX_rkl = type { i64, i64, i32, i32, ptr, [12 x i64] }
%union.anon.8 = type { %struct.anon.9 }
%struct.anon.9 = type { i64, ptr }

@__func__.mdbx_canary_put = external hidden unnamed_addr constant [16 x i8], align 1

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @log_if_error(i32 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 i32 @mdbx_canary_put(ptr noundef %0, ptr noundef %1) #3 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = 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
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = call i32 @check_txn_rw(ptr noundef %8, i32 noundef 51)
  store i32 %9, ptr %6, align 4, !tbaa !9
  %10 = load i32, ptr %6, align 4, !tbaa !9
  %11 = icmp ne i32 %10, 0
  %12 = xor i1 %11, true
  %13 = xor i1 %12, true
  %14 = zext i1 %13 to i32
  %15 = sext i32 %14 to i64
  %16 = call i64 @llvm.expect.i64(i64 %15, i64 0)
  %17 = icmp ne i64 %16, 0
  br i1 %17, label %18, label %21

18:                                               ; preds = %2
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = call i32 @log_if_error(i32 noundef %19, ptr noundef @__func__.mdbx_canary_put, i32 noundef 141)
  store i32 %20, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %88

21:                                               ; preds = %2
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = icmp ne ptr %22, null
  %24 = xor i1 %23, true
  %25 = xor i1 %24, true
  %26 = zext i1 %25 to i32
  %27 = sext i32 %26 to i64
  %28 = call i64 @llvm.expect.i64(i64 %27, i64 1)
  %29 = icmp ne i64 %28, 0
  br i1 %29, label %30, label %77

30:                                               ; preds = %21
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.MDBX_txn, ptr %31, i32 0, i32 15
  %33 = getelementptr inbounds %struct.MDBX_canary, ptr %32, i32 0, i32 0
  %34 = load i64, ptr %33, align 8, !tbaa !11
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.MDBX_canary, ptr %35, i32 0, i32 0
  %37 = load i64, ptr %36, align 8, !tbaa !17
  %38 = icmp eq i64 %34, %37
  br i1 %38, label %39, label %58

39:                                               ; preds = %30
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.MDBX_txn, ptr %40, i32 0, i32 15
  %42 = getelementptr inbounds %struct.MDBX_canary, ptr %41, i32 0, i32 1
  %43 = load i64, ptr %42, align 8, !tbaa !18
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.MDBX_canary, ptr %44, i32 0, i32 1
  %46 = load i64, ptr %45, align 8, !tbaa !19
  %47 = icmp eq i64 %43, %46
  br i1 %47, label %48, label %58

48:                                               ; preds = %39
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.MDBX_txn, ptr %49, i32 0, i32 15
  %51 = getelementptr inbounds %struct.MDBX_canary, ptr %50, i32 0, i32 2
  %52 = load i64, ptr %51, align 8, !tbaa !20
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.MDBX_canary, ptr %53, i32 0, i32 2
  %55 = load i64, ptr %54, align 8, !tbaa !21
  %56 = icmp eq i64 %52, %55
  br i1 %56, label %57, label %58

57:                                               ; preds = %48
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %88

58:                                               ; preds = %48, %39, %30
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.MDBX_canary, ptr %59, i32 0, i32 0
  %61 = load i64, ptr %60, align 8, !tbaa !17
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.MDBX_txn, ptr %62, i32 0, i32 15
  %64 = getelementptr inbounds %struct.MDBX_canary, ptr %63, i32 0, i32 0
  store i64 %61, ptr %64, align 8, !tbaa !11
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.MDBX_canary, ptr %65, i32 0, i32 1
  %67 = load i64, ptr %66, align 8, !tbaa !19
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.MDBX_txn, ptr %68, i32 0, i32 15
  %70 = getelementptr inbounds %struct.MDBX_canary, ptr %69, i32 0, i32 1
  store i64 %67, ptr %70, align 8, !tbaa !18
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.MDBX_canary, ptr %71, i32 0, i32 2
  %73 = load i64, ptr %72, align 8, !tbaa !21
  %74 = load ptr, ptr %4, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.MDBX_txn, ptr %74, i32 0, i32 15
  %76 = getelementptr inbounds %struct.MDBX_canary, ptr %75, i32 0, i32 2
  store i64 %73, ptr %76, align 8, !tbaa !20
  br label %77

77:                                               ; preds = %58, %21
  %78 = load ptr, ptr %4, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.MDBX_txn, ptr %78, i32 0, i32 7
  %80 = load i64, ptr %79, align 8, !tbaa !22
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.MDBX_txn, ptr %81, i32 0, i32 15
  %83 = getelementptr inbounds %struct.MDBX_canary, ptr %82, i32 0, i32 3
  store i64 %80, ptr %83, align 8, !tbaa !23
  %84 = load ptr, ptr %4, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.MDBX_txn, ptr %84, i32 0, i32 1
  %86 = load i32, ptr %85, align 4, !tbaa !24
  %87 = or i32 %86, 4
  store i32 %87, ptr %85, align 4, !tbaa !24
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %88

88:                                               ; preds = %77, %57, %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  %89 = load i32, ptr %3, align 4
  ret i32 %89
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @check_txn_rw(ptr noundef, i32 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #1 = { 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 #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 = { 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}
!11 = !{!12, !13, i64 128}
!12 = !{!"MDBX_txn", !10, i64 0, !10, i64 4, !13, i64 8, !13, i64 16, !6, i64 24, !6, i64 32, !14, 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, !16, i64 128, !6, i64 160, !7, i64 168}
!13 = !{!"long", !7, i64 0}
!14 = !{!"geo", !15, i64 0, !15, i64 2, !10, i64 4, !10, i64 8, !7, i64 12, !7, i64 16}
!15 = !{!"short", !7, i64 0}
!16 = !{!"MDBX_canary", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24}
!17 = !{!16, !13, i64 0}
!18 = !{!12, !13, i64 136}
!19 = !{!16, !13, i64 8}
!20 = !{!12, !13, i64 144}
!21 = !{!16, !13, i64 16}
!22 = !{!12, !13, i64 64}
!23 = !{!12, !13, i64 152}
!24 = !{!12, !10, i64 4}


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

@__func__.mdbx_canary_put = external hidden unnamed_addr constant [16 x i8], align 1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @log_if_error(i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @mdbx_canary_put(ptr noundef %0, ptr noundef readonly %1) local_unnamed_addr #1 {
  %3 = tail call i32 @check_txn_rw(ptr noundef %0, i32 noundef 51)
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %6, label %4, !prof !5

4:                                                ; preds = %2
  %5 = tail call i32 @log_if_error(i32 noundef %3, ptr noundef nonnull @__func__.mdbx_canary_put, i32 noundef 141)
  br label %37

6:                                                ; preds = %2
  %.not21 = icmp eq ptr %1, null
  br i1 %.not21, label %30, label %7, !prof !6

7:                                                ; preds = %6
  %8 = getelementptr inbounds i8, ptr %0, i64 128
  %9 = load i64, ptr %8, align 8, !tbaa !7
  %10 = load i64, ptr %1, align 8, !tbaa !17
  %11 = icmp eq i64 %9, %10
  br i1 %11, label %12, label %._crit_edge

._crit_edge:                                      ; preds = %7
  %.phi.trans.insert = getelementptr inbounds i8, ptr %1, i64 8
  %.pre = load i64, ptr %.phi.trans.insert, align 8, !tbaa !18
  br label %24

12:                                               ; preds = %7
  %13 = getelementptr inbounds i8, ptr %0, i64 136
  %14 = load i64, ptr %13, align 8, !tbaa !19
  %15 = getelementptr inbounds i8, ptr %1, i64 8
  %16 = load i64, ptr %15, align 8, !tbaa !18
  %17 = icmp eq i64 %14, %16
  br i1 %17, label %18, label %24

18:                                               ; preds = %12
  %19 = getelementptr inbounds i8, ptr %0, i64 144
  %20 = load i64, ptr %19, align 8, !tbaa !20
  %21 = getelementptr inbounds i8, ptr %1, i64 16
  %22 = load i64, ptr %21, align 8, !tbaa !21
  %23 = icmp eq i64 %20, %22
  br i1 %23, label %37, label %24

24:                                               ; preds = %._crit_edge, %18, %12
  %25 = phi i64 [ %.pre, %._crit_edge ], [ %14, %18 ], [ %16, %12 ]
  store i64 %10, ptr %8, align 8, !tbaa !7
  %26 = getelementptr inbounds i8, ptr %0, i64 136
  store i64 %25, ptr %26, align 8, !tbaa !19
  %27 = getelementptr inbounds i8, ptr %1, i64 16
  %28 = load i64, ptr %27, align 8, !tbaa !21
  %29 = getelementptr inbounds i8, ptr %0, i64 144
  store i64 %28, ptr %29, align 8, !tbaa !20
  br label %30

30:                                               ; preds = %24, %6
  %31 = getelementptr inbounds i8, ptr %0, i64 64
  %32 = load i64, ptr %31, align 8, !tbaa !22
  %33 = getelementptr inbounds i8, ptr %0, i64 152
  store i64 %32, ptr %33, align 8, !tbaa !23
  %34 = getelementptr inbounds i8, ptr %0, i64 4
  %35 = load i32, ptr %34, align 4, !tbaa !24
  %36 = or i32 %35, 4
  store i32 %36, ptr %34, align 4, !tbaa !24
  br label %37

37:                                               ; preds = %18, %30, %4
  %.0 = phi i32 [ %5, %4 ], [ 0, %30 ], [ 0, %18 ]
  ret i32 %.0
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @check_txn_rw(ptr noundef, i32 noundef) local_unnamed_addr #0

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

!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 2000, i32 1}
!6 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!7 = !{!8, !12, i64 128}
!8 = !{!"MDBX_txn", !9, i64 0, !9, i64 4, !12, i64 8, !12, i64 16, !13, i64 24, !13, i64 32, !14, i64 40, !12, i64 64, !12, i64 72, !13, i64 80, !13, i64 88, !13, i64 96, !13, i64 104, !13, i64 112, !13, i64 120, !16, i64 128, !13, i64 160, !10, i64 168}
!9 = !{!"int", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C/C++ TBAA"}
!12 = !{!"long", !10, i64 0}
!13 = !{!"any pointer", !10, i64 0}
!14 = !{!"geo", !15, i64 0, !15, i64 2, !9, i64 4, !9, i64 8, !10, i64 12, !10, i64 16}
!15 = !{!"short", !10, i64 0}
!16 = !{!"MDBX_canary", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24}
!17 = !{!16, !12, i64 0}
!18 = !{!16, !12, i64 8}
!19 = !{!8, !12, i64 136}
!20 = !{!8, !12, i64 144}
!21 = !{!16, !12, i64 16}
!22 = !{!8, !12, i64 64}
!23 = !{!8, !12, i64 152}
!24 = !{!8, !9, i64 4}


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