; LLVM IR from erthink/libmdbx :: src (api-key-transform)

; 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.iovec = type { ptr, i64 }

@globals = external global %struct.libmdbx_globals, align 8
@.str.3 = external hidden unnamed_addr constant [15 x i8], align 1
@__func__.mdbx_jsonInteger_from_key = external hidden unnamed_addr constant [26 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [134 x i8], align 1

; Function Attrs: nounwind willreturn memory(none) uwtable
declare dso_local i64 @mdbx_key_from_jsonInteger(i64 noundef) #0

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

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

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

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

; Function Attrs: nounwind willreturn memory(read) uwtable
define dso_local i64 @mdbx_jsonInteger_from_key(ptr %0, i64 %1) #4 {
  %3 = alloca i64, align 8
  %4 = alloca %struct.iovec, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = getelementptr inbounds { ptr, i64 }, ptr %4, i32 0, i32 0
  store ptr %0, ptr %13, align 8
  %14 = getelementptr inbounds { ptr, i64 }, ptr %4, i32 0, i32 1
  store i64 %1, ptr %14, align 8
  br label %15

15:                                               ; preds = %2
  %16 = load i8, ptr getelementptr inbounds (%struct.libmdbx_globals, ptr @globals, i32 0, i32 4), align 1, !tbaa !5
  %17 = zext i8 %16 to i32
  %18 = and i32 %17, 1
  %19 = icmp ne i32 %18, 0
  %20 = xor i1 %19, true
  %21 = xor i1 %20, true
  %22 = zext i1 %21 to i32
  %23 = sext i32 %22 to i64
  %24 = call i64 @llvm.expect.i64(i64 %23, i64 1)
  %25 = icmp ne i64 %24, 0
  br i1 %25, label %26, label %41

26:                                               ; preds = %15
  br label %27

27:                                               ; preds = %26
  %28 = getelementptr inbounds %struct.iovec, ptr %4, i32 0, i32 1
  %29 = load i64, ptr %28, align 8, !tbaa !13
  %30 = icmp eq i64 %29, 8
  %31 = xor i1 %30, true
  %32 = xor i1 %31, true
  %33 = xor i1 %32, true
  %34 = zext i1 %33 to i32
  %35 = sext i32 %34 to i64
  %36 = call i64 @llvm.expect.i64(i64 %35, i64 0)
  %37 = icmp ne i64 %36, 0
  br i1 %37, label %38, label %39

38:                                               ; preds = %27
  call void @mdbx_assert_fail(ptr noundef null, ptr noundef @.str.3, ptr noundef @__func__.mdbx_jsonInteger_from_key, i32 noundef 160) #6
  unreachable

39:                                               ; preds = %27
  br label %40

40:                                               ; preds = %39
  br label %41

41:                                               ; preds = %40, %15
  br label %42

42:                                               ; preds = %41
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #7
  %43 = getelementptr inbounds %struct.iovec, ptr %4, i32 0, i32 0
  %44 = load ptr, ptr %43, align 8, !tbaa !15
  %45 = call i64 @unaligned_peek_u64(i64 noundef 2, ptr noundef %44) #8
  store i64 %45, ptr %5, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #7
  store i64 -9223372036854775808, ptr %6, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #7
  %46 = load i64, ptr %5, align 8, !tbaa !16
  %47 = icmp ugt i64 %46, -9223372036854775808
  br i1 %47, label %48, label %51

48:                                               ; preds = %42
  %49 = load i64, ptr %5, align 8, !tbaa !16
  %50 = sub i64 %49, -9223372036854775808
  br label %55

51:                                               ; preds = %42
  %52 = load i64, ptr %5, align 8, !tbaa !16
  %53 = sub i64 -9223372036854775808, %52
  %54 = sub i64 %53, 1
  br label %55

55:                                               ; preds = %51, %48
  %56 = phi i64 [ %50, %48 ], [ %54, %51 ]
  store i64 %56, ptr %7, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #7
  %57 = load i64, ptr %7, align 8, !tbaa !16
  %58 = lshr i64 %57, 52
  %59 = trunc i64 %58 to i32
  %60 = and i32 2047, %59
  %61 = sub nsw i32 1086, %60
  store i32 %61, ptr %8, align 4, !tbaa !17
  %62 = load i32, ptr %8, align 4, !tbaa !17
  %63 = icmp slt i32 %62, 1
  %64 = xor i1 %63, true
  %65 = xor i1 %64, true
  %66 = zext i1 %65 to i32
  %67 = sext i32 %66 to i64
  %68 = call i64 @llvm.expect.i64(i64 %67, i64 0)
  %69 = icmp ne i64 %68, 0
  br i1 %69, label %70, label %75

70:                                               ; preds = %55
  %71 = load i64, ptr %5, align 8, !tbaa !16
  %72 = icmp ult i64 %71, -9223372036854775808
  %73 = zext i1 %72 to i64
  %74 = select i1 %72, i64 -9223372036854775808, i64 9223372036854775807
  store i64 %74, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %151

75:                                               ; preds = %55
  %76 = load i32, ptr %8, align 4, !tbaa !17
  %77 = icmp sgt i32 %76, 63
  %78 = xor i1 %77, true
  %79 = xor i1 %78, true
  %80 = zext i1 %79 to i32
  %81 = sext i32 %80 to i64
  %82 = call i64 @llvm.expect.i64(i64 %81, i64 0)
  %83 = icmp ne i64 %82, 0
  br i1 %83, label %84, label %85

84:                                               ; preds = %75
  store i64 0, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %151

85:                                               ; preds = %75
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #7
  %86 = load i64, ptr %7, align 8, !tbaa !16
  %87 = and i64 %86, 4503599627370495
  %88 = shl i64 %87, 11
  %89 = add i64 %88, -9223372036854775808
  store i64 %89, ptr %10, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #7
  %90 = load i64, ptr %10, align 8, !tbaa !16
  %91 = load i32, ptr %8, align 4, !tbaa !17
  %92 = zext i32 %91 to i64
  %93 = lshr i64 %90, %92
  store i64 %93, ptr %11, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #7
  %94 = load i64, ptr %5, align 8, !tbaa !16
  %95 = icmp ult i64 %94, -9223372036854775808
  br i1 %95, label %96, label %99

96:                                               ; preds = %85
  %97 = load i64, ptr %11, align 8, !tbaa !16
  %98 = sub nsw i64 0, %97
  br label %101

99:                                               ; preds = %85
  %100 = load i64, ptr %11, align 8, !tbaa !16
  br label %101

101:                                              ; preds = %99, %96
  %102 = phi i64 [ %98, %96 ], [ %100, %99 ]
  store i64 %102, ptr %12, align 8, !tbaa !16
  br label %103

103:                                              ; preds = %101
  %104 = load i8, ptr getelementptr inbounds (%struct.libmdbx_globals, ptr @globals, i32 0, i32 4), align 1, !tbaa !5
  %105 = zext i8 %104 to i32
  %106 = and i32 %105, 1
  %107 = icmp ne i32 %106, 0
  %108 = xor i1 %107, true
  %109 = xor i1 %108, true
  %110 = zext i1 %109 to i32
  %111 = sext i32 %110 to i64
  %112 = call i64 @llvm.expect.i64(i64 %111, i64 1)
  %113 = icmp ne i64 %112, 0
  br i1 %113, label %114, label %147

114:                                              ; preds = %103
  br label %115

115:                                              ; preds = %114
  %116 = load i64, ptr %5, align 8, !tbaa !16
  %117 = load i64, ptr %12, align 8, !tbaa !16
  %118 = call i64 @mdbx_key_from_jsonInteger(i64 noundef %117) #9
  %119 = icmp eq i64 %116, %118
  br i1 %119, label %134, label %120

120:                                              ; preds = %115
  %121 = load i64, ptr %12, align 8, !tbaa !16
  %122 = sub nsw i64 %121, 1
  %123 = call i64 @mdbx_key_from_jsonInteger(i64 noundef %122) #9
  %124 = load i64, ptr %5, align 8, !tbaa !16
  %125 = icmp ult i64 %123, %124
  br i1 %125, label %126, label %132

126:                                              ; preds = %120
  %127 = load i64, ptr %5, align 8, !tbaa !16
  %128 = load i64, ptr %12, align 8, !tbaa !16
  %129 = add nsw i64 %128, 1
  %130 = call i64 @mdbx_key_from_jsonInteger(i64 noundef %129) #9
  %131 = icmp ult i64 %127, %130
  br label %132

132:                                              ; preds = %126, %120
  %133 = phi i1 [ false, %120 ], [ %131, %126 ]
  br label %134

134:                                              ; preds = %132, %115
  %135 = phi i1 [ true, %115 ], [ %133, %132 ]
  %136 = xor i1 %135, true
  %137 = xor i1 %136, true
  %138 = xor i1 %137, true
  %139 = zext i1 %138 to i32
  %140 = sext i32 %139 to i64
  %141 = call i64 @llvm.expect.i64(i64 %140, i64 0)
  %142 = icmp ne i64 %141, 0
  br i1 %142, label %143, label %144

143:                                              ; preds = %134
  call void @mdbx_assert_fail(ptr noundef null, ptr noundef @.str.4, ptr noundef @__func__.mdbx_jsonInteger_from_key, i32 noundef 175) #6
  unreachable

144:                                              ; preds = %134
  br label %145

145:                                              ; preds = %144
  br label %146

146:                                              ; preds = %145
  br label %147

147:                                              ; preds = %146, %103
  br label %148

148:                                              ; preds = %147
  br label %149

149:                                              ; preds = %148
  %150 = load i64, ptr %12, align 8, !tbaa !16
  store i64 %150, ptr %3, align 8
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #7
  br label %151

151:                                              ; preds = %149, %84, %70
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #7
  %152 = load i64, ptr %3, align 8
  ret i64 %152
}

; Function Attrs: inlinehint nounwind willreturn memory(read) uwtable
declare hidden i64 @unaligned_peek_u64(i64 noundef, ptr noalias noundef) #5

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #3 = { 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 #4 = { nounwind willreturn memory(read) 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 = { inlinehint nounwind willreturn memory(read) 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 #6 = { noreturn }
attributes #7 = { nounwind }
attributes #8 = { nounwind willreturn memory(read) }
attributes #9 = { 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, !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 = !{!14, !11, i64 8}
!14 = !{!"iovec", !12, i64 0, !11, i64 8}
!15 = !{!14, !12, i64 0}
!16 = !{!11, !11, i64 0}
!17 = !{!9, !9, 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"

%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.3 = external hidden unnamed_addr constant [15 x i8], align 1
@__func__.mdbx_jsonInteger_from_key = external hidden unnamed_addr constant [26 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [134 x i8], align 1

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none) uwtable
declare dso_local i64 @mdbx_key_from_jsonInteger(i64 noundef) local_unnamed_addr #0

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(read) uwtable
define dso_local range(i64 -9223372036854774784, -9223372036854775807) i64 @mdbx_jsonInteger_from_key(ptr %0, i64 %1) local_unnamed_addr #2 {
  %3 = load i8, ptr getelementptr inbounds (i8, ptr @globals, i64 25), align 1, !tbaa !5
  %4 = and i8 %3, 1
  %.not = icmp eq i8 %4, 0
  %.not23 = icmp eq i64 %1, 8
  %or.cond = select i1 %.not, i1 true, i1 %.not23
  br i1 %or.cond, label %6, label %5, !prof !13

5:                                                ; preds = %2
  tail call void @mdbx_assert_fail(ptr noundef null, ptr noundef nonnull @.str.3, ptr noundef nonnull @__func__.mdbx_jsonInteger_from_key, i32 noundef 160) #4
  unreachable

6:                                                ; preds = %2
  %7 = tail call i64 @unaligned_peek_u64(i64 noundef 2, ptr noundef %0) #5
  %8 = icmp ugt i64 %7, -9223372036854775808
  %9 = sub i64 9223372036854775807, %7
  %10 = select i1 %8, i64 %7, i64 %9
  %11 = lshr i64 %10, 52
  %12 = trunc nuw nsw i64 %11 to i32
  %13 = and i32 %12, 2047
  %14 = sub nsw i32 1086, %13
  %15 = icmp ugt i32 %13, 1085
  br i1 %15, label %16, label %19, !prof !14

16:                                               ; preds = %6
  %17 = icmp sgt i64 %7, -1
  %18 = select i1 %17, i64 -9223372036854775808, i64 9223372036854775807
  br label %.critedge

19:                                               ; preds = %6
  %20 = icmp ult i32 %13, 1023
  br i1 %20, label %.critedge, label %21, !prof !14

21:                                               ; preds = %19
  %22 = shl i64 %10, 11
  %23 = or i64 %22, -9223372036854775808
  %24 = zext nneg i32 %14 to i64
  %25 = lshr i64 %23, %24
  %26 = sub nsw i64 0, %25
  %27 = icmp slt i64 %7, 0
  %28 = select i1 %27, i64 %25, i64 %26
  br i1 %.not, label %.critedge, label %29, !prof !14

29:                                               ; preds = %21
  %30 = tail call i64 @mdbx_key_from_jsonInteger(i64 noundef %28) #6
  %31 = icmp eq i64 %7, %30
  br i1 %31, label %.critedge, label %32

32:                                               ; preds = %29
  %33 = add nsw i64 %28, -1
  %34 = tail call i64 @mdbx_key_from_jsonInteger(i64 noundef %33) #6
  %35 = icmp ult i64 %34, %7
  br i1 %35, label %36, label %.critedge26

36:                                               ; preds = %32
  %37 = add nsw i64 %28, 1
  %38 = tail call i64 @mdbx_key_from_jsonInteger(i64 noundef %37) #6
  %.not27 = icmp ult i64 %7, %38
  br i1 %.not27, label %.critedge, label %.critedge26, !prof !15

.critedge26:                                      ; preds = %32, %36
  tail call void @mdbx_assert_fail(ptr noundef null, ptr noundef nonnull @.str.4, ptr noundef nonnull @__func__.mdbx_jsonInteger_from_key, i32 noundef 175) #4
  unreachable

.critedge:                                        ; preds = %21, %36, %29, %19, %16
  %.0 = phi i64 [ %18, %16 ], [ 0, %19 ], [ %28, %29 ], [ %28, %36 ], [ %28, %21 ]
  ret i64 %.0
}

; Function Attrs: inlinehint mustprogress nofree nounwind willreturn memory(read) uwtable
declare hidden i64 @unaligned_peek_u64(i64 noundef, ptr noalias noundef) local_unnamed_addr #3

attributes #0 = { 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 #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 = { mustprogress nofree nounwind willreturn memory(read) 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 mustprogress nofree nounwind willreturn memory(read) 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 = { noreturn nounwind }
attributes #5 = { nounwind willreturn memory(read) }
attributes #6 = { 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, !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", i32 4002001, i32 2000}
!14 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!15 = !{!"branch_weights", !"expected", i32 2000, i32 1}


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