; LLVM IR from chkn/logdb :: src (logdb_lease)

; 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.logdb_log_entry_t = type { i16 }
%struct.logdb_connection_t = type { i16, %union.pthread_rwlock_t, i32, i32, ptr, i32 }
%union.pthread_rwlock_t = type { %struct.__pthread_rwlock_arch_t }
%struct.__pthread_rwlock_arch_t = type { i32, i32, i32, i32, i32, i32, i32, i32, i8, [7 x i8], i64, i32 }
%struct.logdb_lease_t = type { ptr, i32, i64, i32, i32 }

; Function Attrs: nounwind uwtable
define dso_local i32 @logdb_lease_acqire_read(ptr noundef %0, ptr noundef %1, i32 noundef %2, i64 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca %struct.logdb_log_entry_t, align 2
  %11 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i64 %3, ptr %9, align 8, !tbaa !11
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = load ptr, ptr %7, align 8, !tbaa !5
  %14 = call i32 @logdb_lease_acquire_prelude(ptr noundef %12, ptr noundef %13)
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %4
  store i32 -1, ptr %5, align 4
  br label %62

17:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #4
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.logdb_connection_t, ptr %18, i32 0, i32 4
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  %21 = load i32, ptr %8, align 4, !tbaa !9
  %22 = call i64 @logdb_log_read_entry(ptr noundef %20, ptr noundef %10, i32 noundef %21)
  %23 = icmp eq i64 %22, -1
  br i1 %23, label %24, label %28

24:                                               ; preds = %17
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.logdb_connection_t, ptr %25, i32 0, i32 1
  %27 = call i32 @pthread_rwlock_unlock(ptr noundef %26) #4
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %61

28:                                               ; preds = %17
  %29 = load i64, ptr %9, align 8, !tbaa !11
  %30 = icmp slt i64 %29, 0
  br i1 %30, label %37, label %31

31:                                               ; preds = %28
  %32 = load i64, ptr %9, align 8, !tbaa !11
  %33 = getelementptr inbounds %struct.logdb_log_entry_t, ptr %10, i32 0, i32 0
  %34 = load i16, ptr %33, align 2, !tbaa !16
  %35 = zext i16 %34 to i64
  %36 = icmp sge i64 %32, %35
  br i1 %36, label %37, label %41

37:                                               ; preds = %31, %28
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.logdb_connection_t, ptr %38, i32 0, i32 1
  %40 = call i32 @pthread_rwlock_unlock(ptr noundef %39) #4
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %61

41:                                               ; preds = %31
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.logdb_lease_t, ptr %43, i32 0, i32 0
  store ptr %42, ptr %44, align 8, !tbaa !18
  %45 = load i32, ptr %8, align 4, !tbaa !9
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.logdb_lease_t, ptr %46, i32 0, i32 1
  store i32 %45, ptr %47, align 8, !tbaa !20
  %48 = load i64, ptr %9, align 8, !tbaa !11
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.logdb_lease_t, ptr %49, i32 0, i32 2
  store i64 %48, ptr %50, align 8, !tbaa !21
  %51 = getelementptr inbounds %struct.logdb_log_entry_t, ptr %10, i32 0, i32 0
  %52 = load i16, ptr %51, align 2, !tbaa !16
  %53 = zext i16 %52 to i64
  %54 = load i64, ptr %9, align 8, !tbaa !11
  %55 = sub nsw i64 %53, %54
  %56 = trunc i64 %55 to i32
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.logdb_lease_t, ptr %57, i32 0, i32 3
  store i32 %56, ptr %58, align 8, !tbaa !22
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.logdb_lease_t, ptr %59, i32 0, i32 4
  store i32 0, ptr %60, align 4, !tbaa !23
  store i32 0, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %61

61:                                               ; preds = %41, %37, %24
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #4
  br label %62

62:                                               ; preds = %61, %16
  %63 = load i32, ptr %5, align 4
  ret i32 %63
}

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

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

declare i64 @logdb_log_read_entry(ptr noundef, ptr noundef, i32 noundef) #2

; Function Attrs: nounwind
declare i32 @pthread_rwlock_unlock(ptr noundef) #3

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

attributes #0 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 "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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !6, i64 72}
!14 = !{!"", !15, i64 0, !7, i64 8, !10, i64 64, !10, i64 68, !6, i64 72, !10, i64 80}
!15 = !{!"short", !7, i64 0}
!16 = !{!17, !15, i64 0}
!17 = !{!"", !15, i64 0}
!18 = !{!19, !6, i64 0}
!19 = !{!"", !6, i64 0, !10, i64 8, !12, i64 16, !10, i64 24, !10, i64 28}
!20 = !{!19, !10, i64 8}
!21 = !{!19, !12, i64 16}
!22 = !{!19, !10, i64 24}
!23 = !{!19, !10, i64 28}


; 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.logdb_log_entry_t = type { i16 }

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @logdb_lease_acqire_read(ptr noundef %0, ptr noundef %1, i32 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = alloca %struct.logdb_log_entry_t, align 2
  %6 = tail call i32 @logdb_lease_acquire_prelude(ptr noundef %0, ptr noundef %1)
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %7, label %30

7:                                                ; preds = %4
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %5) #4
  %8 = getelementptr inbounds i8, ptr %1, i64 72
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  %10 = call i64 @logdb_log_read_entry(ptr noundef %9, ptr noundef nonnull %5, i32 noundef %2) #4
  %11 = icmp eq i64 %10, -1
  br i1 %11, label %12, label %15

12:                                               ; preds = %7
  %13 = getelementptr inbounds i8, ptr %1, i64 8
  %14 = call i32 @pthread_rwlock_unlock(ptr noundef nonnull %13) #4
  br label %29

15:                                               ; preds = %7
  %16 = icmp sgt i64 %3, -1
  %17 = load i16, ptr %5, align 2
  %18 = zext i16 %17 to i64
  %.not18 = icmp ugt i64 %18, %3
  %or.cond = select i1 %16, i1 %.not18, i1 false
  br i1 %or.cond, label %22, label %19

19:                                               ; preds = %15
  %20 = getelementptr inbounds i8, ptr %1, i64 8
  %21 = call i32 @pthread_rwlock_unlock(ptr noundef nonnull %20) #4
  br label %29

22:                                               ; preds = %15
  store ptr %1, ptr %0, align 8, !tbaa !12
  %23 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 %2, ptr %23, align 8, !tbaa !15
  %24 = getelementptr inbounds i8, ptr %0, i64 16
  store i64 %3, ptr %24, align 8, !tbaa !16
  %25 = sub nsw i64 %18, %3
  %26 = trunc nsw i64 %25 to i32
  %27 = getelementptr inbounds i8, ptr %0, i64 24
  store i32 %26, ptr %27, align 8, !tbaa !17
  %28 = getelementptr inbounds i8, ptr %0, i64 28
  store i32 0, ptr %28, align 4, !tbaa !18
  br label %29

29:                                               ; preds = %22, %19, %12
  %.0 = phi i32 [ -1, %12 ], [ -1, %19 ], [ 0, %22 ]
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %5) #4
  br label %30

30:                                               ; preds = %4, %29
  %.1 = phi i32 [ %.0, %29 ], [ -1, %4 ]
  ret i32 %.1
}

; Function Attrs: nounwind uwtable
declare hidden i32 @logdb_lease_acquire_prelude(ptr noundef, ptr 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

declare i64 @logdb_log_read_entry(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare i32 @pthread_rwlock_unlock(ptr noundef) local_unnamed_addr #3

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

attributes #0 = { 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 "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, !11, i64 72}
!6 = !{!"", !7, i64 0, !8, i64 8, !10, i64 64, !10, i64 68, !11, i64 72, !10, i64 80}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!13, !11, i64 0}
!13 = !{!"", !11, i64 0, !10, i64 8, !14, i64 16, !10, i64 24, !10, i64 28}
!14 = !{!"long", !8, i64 0}
!15 = !{!13, !10, i64 8}
!16 = !{!13, !14, i64 16}
!17 = !{!13, !10, i64 24}
!18 = !{!13, !10, i64 28}


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