; LLVM IR from artagnon/phoenixfs :: buffer (buffer_copy_bytes)

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

@buf = external hidden global [16384 x i8], align 16

; Function Attrs: nounwind uwtable
declare dso_local i64 @buffer_skip_bytes(ptr noundef, 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: nounwind
declare i32 @feof(ptr noundef) #2

; Function Attrs: nounwind
declare i32 @ferror(ptr noundef) #2

declare i64 @fread(ptr noundef, i64 noundef, i64 noundef, ptr 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 uwtable
define dso_local i64 @buffer_copy_bytes(ptr noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  store i64 0, ptr %9, align 8, !tbaa !9
  br label %11

11:                                               ; preds = %74, %3
  %12 = load i64, ptr %9, align 8, !tbaa !9
  %13 = load i64, ptr %7, align 8, !tbaa !9
  %14 = icmp ult i64 %12, %13
  br i1 %14, label %15, label %24

15:                                               ; preds = %11
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = call i32 @feof(ptr noundef %16) #4
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %24, label %19

19:                                               ; preds = %15
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = call i32 @ferror(ptr noundef %20) #4
  %22 = icmp ne i32 %21, 0
  %23 = xor i1 %22, true
  br label %24

24:                                               ; preds = %19, %15, %11
  %25 = phi i1 [ false, %15 ], [ false, %11 ], [ %23, %19 ]
  br i1 %25, label %26, label %75

26:                                               ; preds = %24
  %27 = load i64, ptr %7, align 8, !tbaa !9
  %28 = load i64, ptr %9, align 8, !tbaa !9
  %29 = sub i64 %27, %28
  %30 = icmp ult i64 %29, 16384
  br i1 %30, label %31, label %35

31:                                               ; preds = %26
  %32 = load i64, ptr %7, align 8, !tbaa !9
  %33 = load i64, ptr %9, align 8, !tbaa !9
  %34 = sub i64 %32, %33
  br label %36

35:                                               ; preds = %26
  br label %36

36:                                               ; preds = %35, %31
  %37 = phi i64 [ %34, %31 ], [ 16384, %35 ]
  store i64 %37, ptr %8, align 8, !tbaa !9
  %38 = load i64, ptr %8, align 8, !tbaa !9
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = call i64 @fread(ptr noundef @buf, i64 noundef 1, i64 noundef %38, ptr noundef %39)
  store i64 %40, ptr %8, align 8, !tbaa !9
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = call i32 @ferror(ptr noundef %41) #4
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %48, label %44

44:                                               ; preds = %36
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = call i32 @feof(ptr noundef %45) #4
  %47 = icmp ne i32 %46, 0
  br i1 %47, label %48, label %52

48:                                               ; preds = %44, %36
  %49 = load i64, ptr %9, align 8, !tbaa !9
  %50 = load i64, ptr %8, align 8, !tbaa !9
  %51 = add i64 %49, %50
  store i64 %51, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %77

52:                                               ; preds = %44
  %53 = load i64, ptr %8, align 8, !tbaa !9
  %54 = load i64, ptr %9, align 8, !tbaa !9
  %55 = add i64 %54, %53
  store i64 %55, ptr %9, align 8, !tbaa !9
  %56 = load i64, ptr %8, align 8, !tbaa !9
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = call i64 @fwrite(ptr noundef @buf, i64 noundef 1, i64 noundef %56, ptr noundef %57)
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = call i32 @ferror(ptr noundef %59) #4
  %61 = icmp ne i32 %60, 0
  br i1 %61, label %66, label %62

62:                                               ; preds = %52
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = call i32 @feof(ptr noundef %63) #4
  %65 = icmp ne i32 %64, 0
  br i1 %65, label %66, label %74

66:                                               ; preds = %62, %52
  %67 = load i64, ptr %9, align 8, !tbaa !9
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = load i64, ptr %7, align 8, !tbaa !9
  %70 = load i64, ptr %9, align 8, !tbaa !9
  %71 = sub i64 %69, %70
  %72 = call i64 @buffer_skip_bytes(ptr noundef %68, i64 noundef %71)
  %73 = add nsw i64 %67, %72
  store i64 %73, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %77

74:                                               ; preds = %62
  br label %11, !llvm.loop !11

75:                                               ; preds = %24
  %76 = load i64, ptr %9, align 8, !tbaa !9
  store i64 %76, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %77

77:                                               ; preds = %75, %66, %48
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %78 = load i64, ptr %4, align 8
  ret i64 %78
}

declare i64 @fwrite(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #3

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 = { 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 #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 }

!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 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

@buf = external hidden global [16384 x i8], align 16

; Function Attrs: nounwind uwtable
declare dso_local i64 @buffer_skip_bytes(ptr noundef, i64 noundef) local_unnamed_addr #0

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

; Function Attrs: nofree nounwind memory(read)
declare noundef i32 @ferror(ptr nocapture noundef) local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
define dso_local i64 @buffer_copy_bytes(ptr noundef %0, ptr nocapture noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  br label %4

4:                                                ; preds = %23, %3
  %.0 = phi i64 [ 0, %3 ], [ %20, %23 ]
  %5 = icmp ult i64 %.0, %2
  br i1 %5, label %6, label %.critedge

6:                                                ; preds = %4
  %7 = tail call i32 @feof(ptr noundef %0) #4
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %8, label %.critedge

8:                                                ; preds = %6
  %9 = tail call i32 @ferror(ptr noundef %0) #4
  %.not27 = icmp eq i32 %9, 0
  br i1 %.not27, label %10, label %.critedge

10:                                               ; preds = %8
  %11 = sub i64 %2, %.0
  %12 = tail call i64 @llvm.umin.i64(i64 %11, i64 16384)
  %13 = tail call i64 @fread(ptr noundef nonnull @buf, i64 noundef 1, i64 noundef %12, ptr noundef %0)
  %14 = tail call i32 @ferror(ptr noundef %0) #4
  %.not28 = icmp eq i32 %14, 0
  br i1 %.not28, label %15, label %17

15:                                               ; preds = %10
  %16 = tail call i32 @feof(ptr noundef %0) #4
  %.not29 = icmp eq i32 %16, 0
  br i1 %.not29, label %19, label %17

17:                                               ; preds = %15, %10
  %18 = add i64 %13, %.0
  br label %.critedge

19:                                               ; preds = %15
  %20 = add i64 %13, %.0
  %21 = tail call i64 @fwrite(ptr noundef nonnull @buf, i64 noundef 1, i64 noundef %13, ptr noundef %1)
  %22 = tail call i32 @ferror(ptr noundef %1) #4
  %.not30 = icmp eq i32 %22, 0
  br i1 %.not30, label %23, label %25

23:                                               ; preds = %19
  %24 = tail call i32 @feof(ptr noundef %1) #4
  %.not31 = icmp eq i32 %24, 0
  br i1 %.not31, label %4, label %25, !llvm.loop !5

25:                                               ; preds = %23, %19
  %26 = sub i64 %2, %20
  %27 = tail call i64 @buffer_skip_bytes(ptr noundef %0, i64 noundef %26)
  %28 = add nsw i64 %27, %20
  br label %.critedge

.critedge:                                        ; preds = %8, %4, %6, %25, %17
  %.025 = phi i64 [ %18, %17 ], [ %28, %25 ], [ %.0, %6 ], [ %.0, %4 ], [ %.0, %8 ]
  ret i64 %.025
}

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

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

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 = { 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" }
attributes #2 = { nofree nounwind memory(read) "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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