; LLVM IR from Keith-Tachibana/Relational_Database_Management_System :: rbf/pfm.ll (ZN10FileHandle8readPageEjPv)

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

%class.FileHandle = type { i32, i32, i32, i32, i32, ptr }

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN10FileHandle13writeCountersEv(ptr noundef nonnull align 8 dereferenceable(32)) #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN10FileHandle8readPageEjPv(ptr noundef nonnull align 8 dereferenceable(32) %0, i32 noundef %1, ptr noundef %2) #0 align 2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  %8 = load ptr, ptr %5, align 8
  %9 = getelementptr inbounds %class.FileHandle, ptr %8, i32 0, i32 5
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  %11 = icmp ne ptr %10, null
  br i1 %11, label %16, label %12

12:                                               ; preds = %3
  %13 = load ptr, ptr %7, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %16, label %15

15:                                               ; preds = %12
  store i32 -1, ptr %4, align 4
  br label %41

16:                                               ; preds = %12, %3
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = add i32 %17, 1
  %19 = call noundef i32 @_ZN10FileHandle16getNumberOfPagesEv(ptr noundef nonnull align 8 dereferenceable(32) %8)
  %20 = icmp ugt i32 %18, %19
  br i1 %20, label %21, label %22

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

22:                                               ; preds = %16
  %23 = getelementptr inbounds %class.FileHandle, ptr %8, i32 0, i32 5
  %24 = load ptr, ptr %23, align 8, !tbaa !11
  %25 = load i32, ptr %6, align 4, !tbaa !9
  %26 = add i32 %25, 1
  %27 = mul i32 %26, 4096
  %28 = zext i32 %27 to i64
  %29 = call i32 @fseek(ptr noundef %24, i64 noundef %28, i32 noundef 0)
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %32

31:                                               ; preds = %22
  store i32 -1, ptr %4, align 4
  br label %41

32:                                               ; preds = %22
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = getelementptr inbounds %class.FileHandle, ptr %8, i32 0, i32 5
  %35 = load ptr, ptr %34, align 8, !tbaa !11
  %36 = call i64 @fread(ptr noundef %33, i64 noundef 4096, i64 noundef 1, ptr noundef %35)
  %37 = getelementptr inbounds %class.FileHandle, ptr %8, i32 0, i32 0
  %38 = load i32, ptr %37, align 8, !tbaa !13
  %39 = add i32 %38, 1
  store i32 %39, ptr %37, align 8, !tbaa !13
  %40 = call noundef i32 @_ZN10FileHandle13writeCountersEv(ptr noundef nonnull align 8 dereferenceable(32) %8)
  store i32 0, ptr %4, align 4
  br label %41

41:                                               ; preds = %32, %31, %21, %15
  %42 = load i32, ptr %4, align 4
  ret i32 %42
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN10FileHandle16getNumberOfPagesEv(ptr noundef nonnull align 8 dereferenceable(32)) #0 align 2

declare i32 @fseek(ptr noundef, i64 noundef, i32 noundef) #1

declare i64 @fread(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #1

attributes #0 = { mustprogress 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 = { "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 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 24}
!12 = !{!"_ZTS10FileHandle", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !6, i64 24}
!13 = !{!12, !10, 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: mustprogress uwtable
declare dso_local noundef i32 @_ZN10FileHandle13writeCountersEv(ptr noundef nonnull align 8 dereferenceable(32)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef range(i32 -1, 1) i32 @_ZN10FileHandle8readPageEjPv(ptr noundef nonnull align 8 dereferenceable(32) %0, i32 noundef %1, ptr noundef %2) local_unnamed_addr #0 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = icmp ne ptr %5, null
  %7 = icmp ne ptr %2, null
  %or.cond = or i1 %7, %6
  br i1 %or.cond, label %8, label %23

8:                                                ; preds = %3
  %9 = add i32 %1, 1
  %10 = tail call noundef i32 @_ZN10FileHandle16getNumberOfPagesEv(ptr noundef nonnull align 8 dereferenceable(32) %0)
  %11 = icmp ugt i32 %9, %10
  br i1 %11, label %23, label %12

12:                                               ; preds = %8
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = shl i32 %9, 12
  %15 = zext i32 %14 to i64
  %16 = tail call i32 @fseek(ptr noundef %13, i64 noundef %15, i32 noundef 0)
  %.not = icmp eq i32 %16, 0
  br i1 %.not, label %17, label %23

17:                                               ; preds = %12
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = tail call i64 @fread(ptr noundef %2, i64 noundef 4096, i64 noundef 1, ptr noundef %18)
  %20 = load i32, ptr %0, align 8, !tbaa !11
  %21 = add i32 %20, 1
  store i32 %21, ptr %0, align 8, !tbaa !11
  %22 = tail call noundef i32 @_ZN10FileHandle13writeCountersEv(ptr noundef nonnull align 8 dereferenceable(32) %0)
  br label %23

23:                                               ; preds = %12, %8, %3, %17
  %.0 = phi i32 [ 0, %17 ], [ -1, %3 ], [ -1, %8 ], [ -1, %12 ]
  ret i32 %.0
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN10FileHandle16getNumberOfPagesEv(ptr noundef nonnull align 8 dereferenceable(32)) local_unnamed_addr #0 align 2

; Function Attrs: nofree nounwind
declare noundef i32 @fseek(ptr nocapture noundef, i64 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i64 @fread(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #1

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

!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, !10, i64 24}
!6 = !{!"_ZTS10FileHandle", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !10, i64 24}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 0}


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