; LLVM IR from kmxz/overlayfs-tools :: logic (read_chunk)

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @read_chunk(i32 noundef %0, ptr noundef %1, i32 noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %11 = load i32, ptr %7, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  store i64 %12, ptr %9, align 8, !tbaa !11
  br label %13

13:                                               ; preds = %33, %31, %3
  %14 = load i64, ptr %9, align 8, !tbaa !11
  %15 = icmp sgt i64 %14, 0
  br i1 %15, label %16, label %22

16:                                               ; preds = %13
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = load ptr, ptr %6, align 8, !tbaa !9
  %19 = load i64, ptr %9, align 8, !tbaa !11
  %20 = call i64 @read(i32 noundef %17, ptr noundef %18, i64 noundef %19)
  store i64 %20, ptr %8, align 8, !tbaa !11
  %21 = icmp ne i64 %20, 0
  br label %22

22:                                               ; preds = %16, %13
  %23 = phi i1 [ false, %13 ], [ %21, %16 ]
  br i1 %23, label %24, label %40

24:                                               ; preds = %22
  %25 = load i64, ptr %8, align 8, !tbaa !11
  %26 = icmp eq i64 %25, -1
  br i1 %26, label %27, label %33

27:                                               ; preds = %24
  %28 = call ptr @__errno_location() #5
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = icmp eq i32 %29, 4
  br i1 %30, label %31, label %32

31:                                               ; preds = %27
  br label %13, !llvm.loop !13

32:                                               ; preds = %27
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %46

33:                                               ; preds = %24
  %34 = load i64, ptr %8, align 8, !tbaa !11
  %35 = load i64, ptr %9, align 8, !tbaa !11
  %36 = sub nsw i64 %35, %34
  store i64 %36, ptr %9, align 8, !tbaa !11
  %37 = load i64, ptr %8, align 8, !tbaa !11
  %38 = load ptr, ptr %6, align 8, !tbaa !9
  %39 = getelementptr inbounds i8, ptr %38, i64 %37
  store ptr %39, ptr %6, align 8, !tbaa !9
  br label %13, !llvm.loop !13

40:                                               ; preds = %22
  %41 = load i32, ptr %7, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = load i64, ptr %9, align 8, !tbaa !11
  %44 = sub nsw i64 %42, %43
  %45 = trunc i64 %44 to i32
  store i32 %45, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %46

46:                                               ; preds = %40, %32
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %47 = load i32, ptr %4, align 4
  ret i32 %47
}

declare i64 @read(i32 noundef, ptr noundef, i64 noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #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 }
attributes #5 = { 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, !6, i64 0}
!6 = !{!"int", !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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

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

; Function Attrs: nofree nounwind uwtable
define dso_local i32 @read_chunk(i32 noundef %0, ptr nocapture noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = sext i32 %2 to i64
  %5 = icmp sgt i32 %2, 0
  br i1 %5, label %.outer.split.preheader, label %.critedge.split

.outer.split.preheader:                           ; preds = %3, %.outer
  %.0.ph36 = phi i64 [ %11, %.outer ], [ %4, %3 ]
  %.013.ph35 = phi ptr [ %12, %.outer ], [ %1, %3 ]
  br label %.outer.split

.outer.split:                                     ; preds = %.outer.split.preheader, %7
  %6 = tail call i64 @read(i32 noundef %0, ptr noundef %.013.ph35, i64 noundef %.0.ph36) #3
  switch i64 %6, label %.outer [
    i64 0, label %.critedge.split
    i64 -1, label %7
  ]

7:                                                ; preds = %.outer.split
  %8 = tail call ptr @__errno_location() #4
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = icmp eq i32 %9, 4
  br i1 %10, label %.outer.split, label %.loopexit, !llvm.loop !9

.outer:                                           ; preds = %.outer.split
  %11 = sub nsw i64 %.0.ph36, %6
  %12 = getelementptr inbounds i8, ptr %.013.ph35, i64 %6
  %13 = icmp sgt i64 %11, 0
  br i1 %13, label %.outer.split.preheader, label %.critedge.split, !llvm.loop !9

.critedge.split:                                  ; preds = %.outer, %.outer.split, %3
  %.0.ph34 = phi i64 [ %4, %3 ], [ %.0.ph36, %.outer.split ], [ %11, %.outer ]
  %14 = trunc i64 %.0.ph34 to i32
  %15 = sub i32 %2, %14
  br label %.loopexit

.loopexit:                                        ; preds = %7, %.critedge.split
  %.012 = phi i32 [ %15, %.critedge.split ], [ -1, %7 ]
  ret i32 %.012
}

; Function Attrs: nofree
declare noundef i64 @read(i32 noundef, ptr nocapture noundef, i64 noundef) local_unnamed_addr #2

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass