; LLVM IR from cruppstahl/upscaledb :: src/1os/os_posix.ll (ZN9upscaledb4File5preadEmPvm)

; 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.upscaledb::File" = type { i32, i32, i32 }

@.str.2 = external hidden unnamed_addr constant [74 x i8], align 1
@_ZTIN9upscaledb9ExceptionE = external dso_local constant { ptr, ptr }, align 8
@__FUNCTION__._ZN9upscaledb4File5preadEmPvm = external hidden unnamed_addr constant [6 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [39 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [42 x i8], align 1

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

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

declare i32 @__gxx_personality_v0(...)

declare ptr @__cxa_allocate_exception(i64)

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN9upscaledb9ExceptionC2Ei(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) unnamed_addr #1 align 2

declare void @__cxa_free_exception(ptr)

declare void @__cxa_throw(ptr, ptr, ptr)

declare void @_ZN9upscaledb11dbg_prepareEiPKciS1_S1_(i32 noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef) #2

declare void @_ZN9upscaledb7dbg_logEPKcz(ptr noundef, ...) #2

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #3

; Function Attrs: nounwind
declare ptr @strerror(i32 noundef) #4

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN9upscaledb4File5preadEmPvm(ptr noundef nonnull align 4 dereferenceable(12) %0, i64 noundef %1, ptr noundef %2, i64 noundef %3) #5 align 2 personality ptr @__gxx_personality_v0 {
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  store i64 %3, ptr %8, align 8, !tbaa !9
  %13 = load ptr, ptr %5, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #6
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %14

14:                                               ; preds = %53, %4
  %15 = load i64, ptr %10, align 8, !tbaa !9
  %16 = load i64, ptr %8, align 8, !tbaa !9
  %17 = icmp ult i64 %15, %16
  br i1 %17, label %18, label %58

18:                                               ; preds = %14
  %19 = getelementptr inbounds %"class.upscaledb::File", ptr %13, i32 0, i32 0
  %20 = load i32, ptr %19, align 4, !tbaa !11
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = load i64, ptr %10, align 8, !tbaa !9
  %23 = getelementptr inbounds i8, ptr %21, i64 %22
  %24 = load i64, ptr %8, align 8, !tbaa !9
  %25 = load i64, ptr %10, align 8, !tbaa !9
  %26 = sub i64 %24, %25
  %27 = load i64, ptr %6, align 8, !tbaa !9
  %28 = load i64, ptr %10, align 8, !tbaa !9
  %29 = add i64 %27, %28
  %30 = call i64 @pread64(i32 noundef %20, ptr noundef %23, i64 noundef %26, i64 noundef %29)
  %31 = trunc i64 %30 to i32
  store i32 %31, ptr %9, align 4, !tbaa !14
  %32 = load i32, ptr %9, align 4, !tbaa !14
  %33 = icmp slt i32 %32, 0
  br i1 %33, label %34, label %49

34:                                               ; preds = %18
  br label %35

35:                                               ; preds = %34
  call void @_ZN9upscaledb11dbg_prepareEiPKciS1_S1_(i32 noundef 1, ptr noundef @.str.2, i32 noundef 235, ptr noundef @__FUNCTION__._ZN9upscaledb4File5preadEmPvm, ptr noundef null)
  %36 = call ptr @__errno_location() #7
  %37 = load i32, ptr %36, align 4, !tbaa !14
  %38 = call ptr @__errno_location() #7
  %39 = load i32, ptr %38, align 4, !tbaa !14
  %40 = call ptr @strerror(i32 noundef %39) #6
  call void (ptr, ...) @_ZN9upscaledb7dbg_logEPKcz(ptr noundef @.str.4, i32 noundef %37, ptr noundef %40)
  br label %41

41:                                               ; preds = %35
  br label %42

42:                                               ; preds = %41
  %43 = call ptr @__cxa_allocate_exception(i64 4) #6
  invoke void @_ZN9upscaledb9ExceptionC2Ei(ptr noundef nonnull align 4 dereferenceable(4) %43, i32 noundef -18)
          to label %44 unwind label %45

44:                                               ; preds = %42
  call void @__cxa_throw(ptr %43, ptr @_ZTIN9upscaledb9ExceptionE, ptr null) #8
  unreachable

45:                                               ; preds = %42
  %46 = landingpad { ptr, i32 }
          cleanup
  %47 = extractvalue { ptr, i32 } %46, 0
  store ptr %47, ptr %11, align 8
  %48 = extractvalue { ptr, i32 } %46, 1
  store i32 %48, ptr %12, align 4
  call void @__cxa_free_exception(ptr %43) #6
  br label %76

49:                                               ; preds = %18
  %50 = load i32, ptr %9, align 4, !tbaa !14
  %51 = icmp eq i32 %50, 0
  br i1 %51, label %52, label %53

52:                                               ; preds = %49
  br label %58

53:                                               ; preds = %49
  %54 = load i32, ptr %9, align 4, !tbaa !14
  %55 = sext i32 %54 to i64
  %56 = load i64, ptr %10, align 8, !tbaa !9
  %57 = add i64 %56, %55
  store i64 %57, ptr %10, align 8, !tbaa !9
  br label %14, !llvm.loop !15

58:                                               ; preds = %52, %14
  %59 = load i64, ptr %10, align 8, !tbaa !9
  %60 = load i64, ptr %8, align 8, !tbaa !9
  %61 = icmp ne i64 %59, %60
  br i1 %61, label %62, label %75

62:                                               ; preds = %58
  br label %63

63:                                               ; preds = %62
  call void @_ZN9upscaledb11dbg_prepareEiPKciS1_S1_(i32 noundef 1, ptr noundef @.str.2, i32 noundef 244, ptr noundef @__FUNCTION__._ZN9upscaledb4File5preadEmPvm, ptr noundef null)
  %64 = call ptr @__errno_location() #7
  %65 = load i32, ptr %64, align 4, !tbaa !14
  %66 = call ptr @strerror(i32 noundef %65) #6
  call void (ptr, ...) @_ZN9upscaledb7dbg_logEPKcz(ptr noundef @.str.5, ptr noundef %66)
  br label %67

67:                                               ; preds = %63
  br label %68

68:                                               ; preds = %67
  %69 = call ptr @__cxa_allocate_exception(i64 4) #6
  invoke void @_ZN9upscaledb9ExceptionC2Ei(ptr noundef nonnull align 4 dereferenceable(4) %69, i32 noundef -18)
          to label %70 unwind label %71

70:                                               ; preds = %68
  call void @__cxa_throw(ptr %69, ptr @_ZTIN9upscaledb9ExceptionE, ptr null) #8
  unreachable

71:                                               ; preds = %68
  %72 = landingpad { ptr, i32 }
          cleanup
  %73 = extractvalue { ptr, i32 } %72, 0
  store ptr %73, ptr %11, align 8
  %74 = extractvalue { ptr, i32 } %72, 1
  store i32 %74, ptr %12, align 4
  call void @__cxa_free_exception(ptr %69) #6
  br label %76

75:                                               ; preds = %58
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #6
  ret void

76:                                               ; preds = %71, %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #6
  br label %77

77:                                               ; preds = %76
  %78 = load ptr, ptr %11, align 8
  %79 = load i32, ptr %12, align 4
  %80 = insertvalue { ptr, i32 } poison, ptr %78, 0
  %81 = insertvalue { ptr, i32 } %80, i32 %79, 1
  resume { ptr, i32 } %81
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { "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 willreturn memory(none) "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 "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 = { 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 #6 = { nounwind }
attributes #7 = { nounwind willreturn memory(none) }
attributes #8 = { noreturn }

!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 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"_ZTSN9upscaledb4FileE", !13, i64 0, !13, i64 4, !13, i64 8}
!13 = !{!"int", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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

@.str.2 = external hidden unnamed_addr constant [74 x i8], align 1
@_ZTIN9upscaledb9ExceptionE = external dso_local constant { ptr, ptr }, align 8
@__FUNCTION__._ZN9upscaledb4File5preadEmPvm = external hidden unnamed_addr constant [6 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [39 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [42 x i8], align 1

declare i32 @__gxx_personality_v0(...)

declare ptr @__cxa_allocate_exception(i64) local_unnamed_addr

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN9upscaledb9ExceptionC2Ei(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) unnamed_addr #0 align 2

declare void @__cxa_throw(ptr, ptr, ptr) local_unnamed_addr

declare void @_ZN9upscaledb11dbg_prepareEiPKciS1_S1_(i32 noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare void @_ZN9upscaledb7dbg_logEPKcz(ptr noundef, ...) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #2

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

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN9upscaledb4File5preadEmPvm(ptr nocapture noundef nonnull readonly align 4 dereferenceable(12) %0, i64 noundef %1, ptr noundef %2, i64 noundef %3) local_unnamed_addr #4 align 2 personality ptr @__gxx_personality_v0 {
  %.not20 = icmp eq i64 %3, 0
  br i1 %.not20, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %19
  %.017 = phi i64 [ %21, %19 ], [ 0, %4 ]
  %5 = load i32, ptr %0, align 4, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %2, i64 %.017
  %7 = sub i64 %3, %.017
  %8 = add i64 %.017, %1
  %9 = tail call i64 @pread64(i32 noundef %5, ptr noundef %6, i64 noundef %7, i64 noundef %8)
  %10 = trunc i64 %9 to i32
  %11 = icmp slt i32 %10, 0
  br i1 %11, label %12, label %17

12:                                               ; preds = %.lr.ph
  tail call void @_ZN9upscaledb11dbg_prepareEiPKciS1_S1_(i32 noundef 1, ptr noundef nonnull @.str.2, i32 noundef 235, ptr noundef nonnull @__FUNCTION__._ZN9upscaledb4File5preadEmPvm, ptr noundef null)
  %13 = tail call ptr @__errno_location() #5
  %14 = load i32, ptr %13, align 4, !tbaa !10
  %15 = tail call ptr @strerror(i32 noundef %14) #6
  tail call void (ptr, ...) @_ZN9upscaledb7dbg_logEPKcz(ptr noundef nonnull @.str.4, i32 noundef %14, ptr noundef %15)
  %16 = tail call ptr @__cxa_allocate_exception(i64 4) #6
  tail call void @_ZN9upscaledb9ExceptionC2Ei(ptr noundef nonnull align 4 dereferenceable(4) %16, i32 noundef -18)
  tail call void @__cxa_throw(ptr nonnull %16, ptr nonnull @_ZTIN9upscaledb9ExceptionE, ptr null) #7
  unreachable

17:                                               ; preds = %.lr.ph
  %18 = icmp eq i32 %10, 0
  br i1 %18, label %._crit_edge, label %19

19:                                               ; preds = %17
  %20 = and i64 %9, 2147483647
  %21 = add i64 %20, %.017
  %22 = icmp ult i64 %21, %3
  br i1 %22, label %.lr.ph, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %19, %17, %4
  %.0.lcssa = phi i64 [ 0, %4 ], [ %.017, %17 ], [ %21, %19 ]
  %.not = icmp eq i64 %.0.lcssa, %3
  br i1 %.not, label %28, label %23

23:                                               ; preds = %._crit_edge
  tail call void @_ZN9upscaledb11dbg_prepareEiPKciS1_S1_(i32 noundef 1, ptr noundef nonnull @.str.2, i32 noundef 244, ptr noundef nonnull @__FUNCTION__._ZN9upscaledb4File5preadEmPvm, ptr noundef null)
  %24 = tail call ptr @__errno_location() #5
  %25 = load i32, ptr %24, align 4, !tbaa !10
  %26 = tail call ptr @strerror(i32 noundef %25) #6
  tail call void (ptr, ...) @_ZN9upscaledb7dbg_logEPKcz(ptr noundef nonnull @.str.5, ptr noundef %26)
  %27 = tail call ptr @__cxa_allocate_exception(i64 4) #6
  tail call void @_ZN9upscaledb9ExceptionC2Ei(ptr noundef nonnull align 4 dereferenceable(4) %27, i32 noundef -18)
  tail call void @__cxa_throw(ptr nonnull %27, ptr nonnull @_ZTIN9upscaledb9ExceptionE, ptr null) #7
  unreachable

28:                                               ; preds = %._crit_edge
  ret void
}

declare i64 @pread64(i32 noundef, ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress 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 = { "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 nosync nounwind willreturn memory(none) "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 = { 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 #5 = { nounwind willreturn memory(none) }
attributes #6 = { nounwind }
attributes #7 = { noreturn }

!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 0}
!6 = !{!"_ZTSN9upscaledb4FileE", !7, i64 0, !7, i64 4, !7, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!7, !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass