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

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

; Function Attrs: cold nounwind optsize uwtable
define dso_local ptr @txn_basal_create(i64 noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store i64 %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #6
  store ptr null, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %9 = load i64, ptr %3, align 8, !tbaa !5
  %10 = call i64 @ceil_powerof2(i64 noundef %9, i64 noundef 32) #7
  %11 = udiv i64 %10, 8
  store i64 %11, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  store i64 1368, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #6
  %12 = load i64, ptr %5, align 8, !tbaa !5
  %13 = add i64 1368, %12
  %14 = load i64, ptr %3, align 8, !tbaa !5
  %15 = mul i64 %14, 61
  %16 = add i64 %13, %15
  store i64 %16, ptr %7, align 8, !tbaa !5
  %17 = load i64, ptr %7, align 8, !tbaa !5
  %18 = call noalias ptr @calloc(i64 noundef 1, i64 noundef %17) #8
  store ptr %18, ptr %4, align 8, !tbaa !9
  %19 = load ptr, ptr %4, align 8, !tbaa !9
  %20 = icmp ne ptr %19, null
  %21 = xor i1 %20, true
  %22 = xor i1 %21, true
  %23 = xor i1 %22, true
  %24 = zext i1 %23 to i32
  %25 = sext i32 %24 to i64
  %26 = call i64 @llvm.expect.i64(i64 %25, i64 0)
  %27 = icmp ne i64 %26, 0
  br i1 %27, label %28, label %30

28:                                               ; preds = %1
  %29 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %29, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %119

30:                                               ; preds = %1
  %31 = load ptr, ptr %4, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.MDBX_txn, ptr %31, i32 0, i32 17
  %33 = getelementptr inbounds %struct.anon.2, ptr %32, i32 0, i32 2
  %34 = getelementptr inbounds %struct.anon.3, ptr %33, i32 0, i32 0
  call void @rkl_init(ptr noundef %34)
  %35 = load ptr, ptr %4, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct.MDBX_txn, ptr %35, i32 0, i32 17
  %37 = getelementptr inbounds %struct.anon.2, ptr %36, i32 0, i32 2
  %38 = getelementptr inbounds %struct.anon.3, ptr %37, i32 0, i32 2
  call void @rkl_init(ptr noundef %38)
  %39 = load ptr, ptr %4, align 8, !tbaa !9
  %40 = ptrtoint ptr %39 to i64
  %41 = add nsw i64 %40, 1368
  %42 = inttoptr i64 %41 to ptr
  %43 = load ptr, ptr %4, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.MDBX_txn, ptr %43, i32 0, i32 10
  store ptr %42, ptr %44, align 8, !tbaa !11
  %45 = load ptr, ptr %4, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.MDBX_txn, ptr %45, i32 0, i32 10
  %47 = load ptr, ptr %46, align 8, !tbaa !11
  %48 = ptrtoint ptr %47 to i64
  %49 = load i64, ptr %3, align 8, !tbaa !5
  %50 = mul i64 %49, 48
  %51 = add nsw i64 %48, %50
  %52 = inttoptr i64 %51 to ptr
  %53 = load ptr, ptr %4, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.MDBX_txn, ptr %53, i32 0, i32 14
  store ptr %52, ptr %54, align 8, !tbaa !17
  %55 = load ptr, ptr %4, align 8, !tbaa !9
  %56 = getelementptr inbounds %struct.MDBX_txn, ptr %55, i32 0, i32 14
  %57 = load ptr, ptr %56, align 8, !tbaa !17
  %58 = ptrtoint ptr %57 to i64
  %59 = load i64, ptr %3, align 8, !tbaa !5
  %60 = mul i64 %59, 8
  %61 = add nsw i64 %58, %60
  %62 = inttoptr i64 %61 to ptr
  %63 = load ptr, ptr %4, align 8, !tbaa !9
  %64 = getelementptr inbounds %struct.MDBX_txn, ptr %63, i32 0, i32 13
  store ptr %62, ptr %64, align 8, !tbaa !18
  %65 = load ptr, ptr %4, align 8, !tbaa !9
  %66 = ptrtoint ptr %65 to i64
  %67 = load i64, ptr %7, align 8, !tbaa !5
  %68 = load i64, ptr %3, align 8, !tbaa !5
  %69 = mul i64 %68, 1
  %70 = sub i64 %67, %69
  %71 = add nsw i64 %66, %70
  %72 = inttoptr i64 %71 to ptr
  %73 = load ptr, ptr %4, align 8, !tbaa !9
  %74 = getelementptr inbounds %struct.MDBX_txn, ptr %73, i32 0, i32 12
  store ptr %72, ptr %74, align 8, !tbaa !19
  %75 = load ptr, ptr %4, align 8, !tbaa !9
  %76 = getelementptr inbounds %struct.MDBX_txn, ptr %75, i32 0, i32 12
  %77 = load ptr, ptr %76, align 8, !tbaa !19
  %78 = ptrtoint ptr %77 to i64
  %79 = load i64, ptr %5, align 8, !tbaa !5
  %80 = sub nsw i64 0, %79
  %81 = add nsw i64 %78, %80
  %82 = inttoptr i64 %81 to ptr
  %83 = load ptr, ptr %4, align 8, !tbaa !9
  %84 = getelementptr inbounds %struct.MDBX_txn, ptr %83, i32 0, i32 11
  store ptr %82, ptr %84, align 8, !tbaa !20
  %85 = load ptr, ptr %4, align 8, !tbaa !9
  %86 = getelementptr inbounds %struct.MDBX_txn, ptr %85, i32 0, i32 1
  store i32 1, ptr %86, align 4, !tbaa !21
  %87 = call ptr @pnl_alloc(i64 noundef 1018)
  %88 = load ptr, ptr %4, align 8, !tbaa !9
  %89 = getelementptr inbounds %struct.MDBX_txn, ptr %88, i32 0, i32 17
  %90 = getelementptr inbounds %struct.anon.2, ptr %89, i32 0, i32 8
  store ptr %87, ptr %90, align 8, !tbaa !22
  %91 = call ptr @pnl_alloc(i64 noundef 1018)
  %92 = load ptr, ptr %4, align 8, !tbaa !9
  %93 = getelementptr inbounds %struct.MDBX_txn, ptr %92, i32 0, i32 17
  %94 = getelementptr inbounds %struct.anon.2, ptr %93, i32 0, i32 1
  store ptr %91, ptr %94, align 8, !tbaa !22
  %95 = load ptr, ptr %4, align 8, !tbaa !9
  %96 = getelementptr inbounds %struct.MDBX_txn, ptr %95, i32 0, i32 17
  %97 = getelementptr inbounds %struct.anon.2, ptr %96, i32 0, i32 8
  %98 = load ptr, ptr %97, align 8, !tbaa !22
  %99 = icmp ne ptr %98, null
  br i1 %99, label %100, label %107

100:                                              ; preds = %30
  %101 = load ptr, ptr %4, align 8, !tbaa !9
  %102 = getelementptr inbounds %struct.MDBX_txn, ptr %101, i32 0, i32 17
  %103 = getelementptr inbounds %struct.anon.2, ptr %102, i32 0, i32 1
  %104 = load ptr, ptr %103, align 8, !tbaa !22
  %105 = icmp ne ptr %104, null
  %106 = xor i1 %105, true
  br label %107

107:                                              ; preds = %100, %30
  %108 = phi i1 [ true, %30 ], [ %106, %100 ]
  %109 = xor i1 %108, true
  %110 = xor i1 %109, true
  %111 = zext i1 %110 to i32
  %112 = sext i32 %111 to i64
  %113 = call i64 @llvm.expect.i64(i64 %112, i64 0)
  %114 = icmp ne i64 %113, 0
  br i1 %114, label %115, label %117

115:                                              ; preds = %107
  %116 = load ptr, ptr %4, align 8, !tbaa !9
  call void @txn_basal_destroy(ptr noundef %116)
  store ptr null, ptr %4, align 8, !tbaa !9
  br label %117

117:                                              ; preds = %115, %107
  %118 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %118, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %119

119:                                              ; preds = %117, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #6
  %120 = load ptr, ptr %2, align 8
  ret ptr %120
}

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

; Function Attrs: inlinehint nounwind willreturn memory(none) uwtable
declare hidden i64 @ceil_powerof2(i64 noundef, i64 noundef) #2

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #3

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

declare void @rkl_init(ptr noundef) #5

declare ptr @pnl_alloc(i64 noundef) #5

; Function Attrs: cold nounwind optsize uwtable
declare dso_local void @txn_basal_destroy(ptr 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 = { 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(argmem: readwrite) }
attributes #2 = { inlinehint nounwind willreturn memory(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" }
attributes #3 = { nounwind allocsize(0,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 #4 = { nocallback nofree nosync nounwind willreturn memory(none) }
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 = { nounwind }
attributes #7 = { nounwind willreturn memory(none) }
attributes #8 = { nounwind allocsize(0,1) }

!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 = !{!"long", !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, !10, i64 88}
!12 = !{!"MDBX_txn", !13, i64 0, !13, i64 4, !6, i64 8, !6, i64 16, !10, i64 24, !10, i64 32, !14, i64 40, !6, i64 64, !6, i64 72, !10, i64 80, !10, i64 88, !10, i64 96, !10, i64 104, !10, i64 112, !10, i64 120, !16, i64 128, !10, i64 160, !7, i64 168}
!13 = !{!"int", !7, i64 0}
!14 = !{!"geo", !15, i64 0, !15, i64 2, !13, i64 4, !13, i64 8, !7, i64 12, !7, i64 16}
!15 = !{!"short", !7, i64 0}
!16 = !{!"MDBX_canary", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!17 = !{!12, !10, i64 120}
!18 = !{!12, !10, i64 112}
!19 = !{!12, !10, i64 104}
!20 = !{!12, !10, i64 96}
!21 = !{!12, !13, i64 4}
!22 = !{!7, !7, i64 0}


; 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 nounwind optsize uwtable
define dso_local noundef ptr @txn_basal_create(i64 noundef %0) local_unnamed_addr #0 {
  %2 = tail call i64 @ceil_powerof2(i64 noundef %0, i64 noundef 32) #4
  %3 = lshr i64 %2, 3
  %4 = mul i64 %0, 61
  %5 = add i64 %4, 1368
  %6 = add i64 %5, %3
  %7 = tail call noalias ptr @calloc(i64 noundef 1, i64 noundef %6) #5
  %.not = icmp eq ptr %7, null
  br i1 %.not, label %37, label %8, !prof !5

8:                                                ; preds = %1
  %9 = getelementptr inbounds i8, ptr %7, i64 208
  tail call void @rkl_init(ptr noundef nonnull %9) #6
  %10 = getelementptr inbounds i8, ptr %7, i64 344
  tail call void @rkl_init(ptr noundef nonnull %10) #6
  %11 = ptrtoint ptr %7 to i64
  %12 = add nsw i64 %11, 1368
  %13 = inttoptr i64 %12 to ptr
  %14 = getelementptr inbounds i8, ptr %7, i64 88
  store ptr %13, ptr %14, align 8, !tbaa !6
  %15 = mul i64 %0, 48
  %16 = add nsw i64 %12, %15
  %17 = inttoptr i64 %16 to ptr
  %18 = getelementptr inbounds i8, ptr %7, i64 120
  store ptr %17, ptr %18, align 8, !tbaa !16
  %19 = shl i64 %0, 3
  %20 = add nsw i64 %16, %19
  %21 = inttoptr i64 %20 to ptr
  %22 = getelementptr inbounds i8, ptr %7, i64 112
  store ptr %21, ptr %22, align 8, !tbaa !17
  %23 = sub i64 %6, %0
  %24 = add nsw i64 %23, %11
  %25 = inttoptr i64 %24 to ptr
  %26 = getelementptr inbounds i8, ptr %7, i64 104
  store ptr %25, ptr %26, align 8, !tbaa !18
  %27 = sub i64 %24, %3
  %28 = inttoptr i64 %27 to ptr
  %29 = getelementptr inbounds i8, ptr %7, i64 96
  store ptr %28, ptr %29, align 8, !tbaa !19
  %30 = getelementptr inbounds i8, ptr %7, i64 4
  store i32 1, ptr %30, align 4, !tbaa !20
  %31 = tail call ptr @pnl_alloc(i64 noundef 1018) #6
  %32 = getelementptr inbounds i8, ptr %7, i64 504
  store ptr %31, ptr %32, align 8, !tbaa !21
  %33 = tail call ptr @pnl_alloc(i64 noundef 1018) #6
  %34 = getelementptr inbounds i8, ptr %7, i64 200
  store ptr %33, ptr %34, align 8, !tbaa !21
  %35 = load ptr, ptr %32, align 8, !tbaa !21
  %.not31 = icmp eq ptr %35, null
  %.not32 = icmp eq ptr %33, null
  %spec.select = select i1 %.not31, i1 true, i1 %.not32, !prof !5
  br i1 %spec.select, label %36, label %37, !prof !5

36:                                               ; preds = %8
  tail call void @txn_basal_destroy(ptr noundef nonnull %7)
  br label %37

37:                                               ; preds = %8, %36, %1
  %.0 = phi ptr [ null, %1 ], [ null, %36 ], [ %7, %8 ]
  ret ptr %.0
}

; Function Attrs: inlinehint mustprogress nofree nosync nounwind willreturn memory(none) uwtable
declare hidden i64 @ceil_powerof2(i64 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @calloc(i64 noundef, i64 noundef) local_unnamed_addr #2

declare void @rkl_init(ptr noundef) local_unnamed_addr #3

declare ptr @pnl_alloc(i64 noundef) local_unnamed_addr #3

; Function Attrs: cold nounwind optsize uwtable
declare dso_local void @txn_basal_destroy(ptr noundef) local_unnamed_addr #0

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 = { inlinehint mustprogress nofree nosync nounwind willreturn memory(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" }
attributes #2 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { "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 willreturn memory(none) }
attributes #5 = { nounwind allocsize(0,1) }
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 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!6 = !{!7, !12, i64 88}
!7 = !{!"MDBX_txn", !8, i64 0, !8, i64 4, !11, i64 8, !11, i64 16, !12, i64 24, !12, i64 32, !13, 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, !15, i64 128, !12, i64 160, !9, i64 168}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"long", !9, i64 0}
!12 = !{!"any pointer", !9, i64 0}
!13 = !{!"geo", !14, i64 0, !14, i64 2, !8, i64 4, !8, i64 8, !9, i64 12, !9, i64 16}
!14 = !{!"short", !9, i64 0}
!15 = !{!"MDBX_canary", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24}
!16 = !{!7, !12, i64 120}
!17 = !{!7, !12, i64 112}
!18 = !{!7, !12, i64 104}
!19 = !{!7, !12, i64 96}
!20 = !{!7, !8, i64 4}
!21 = !{!9, !9, i64 0}


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