; LLVM IR from ebiggers/libdeflate :: programs (prog_util)

; 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.file_stream = type { i32, ptr, i8, ptr, ptr, i64 }

@.str.11 = external hidden unnamed_addr constant [22 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

; Function Attrs: nounwind uwtable
declare dso_local void @msg_errno(ptr noundef, ...) #1

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

; Function Attrs: nounwind uwtable
define dso_local i64 @xread(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = 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
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %12, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %13 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %13, ptr %9, align 8, !tbaa !9
  br label %14

14:                                               ; preds = %58, %56, %3
  %15 = load i64, ptr %7, align 8, !tbaa !9
  %16 = icmp ne i64 %15, 0
  br i1 %16, label %17, label %59

17:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.file_stream, ptr %18, i32 0, i32 0
  %20 = load i32, ptr %19, align 8, !tbaa !11
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = load i64, ptr %7, align 8, !tbaa !9
  %23 = icmp ule i64 %22, 2147483647
  br i1 %23, label %24, label %26

24:                                               ; preds = %17
  %25 = load i64, ptr %7, align 8, !tbaa !9
  br label %27

26:                                               ; preds = %17
  br label %27

27:                                               ; preds = %26, %24
  %28 = phi i64 [ %25, %24 ], [ 2147483647, %26 ]
  %29 = call i64 @read(i32 noundef %20, ptr noundef %21, i64 noundef %28)
  store i64 %29, ptr %10, align 8, !tbaa !9
  %30 = load i64, ptr %10, align 8, !tbaa !9
  %31 = icmp eq i64 %30, 0
  br i1 %31, label %32, label %33

32:                                               ; preds = %27
  store i32 3, ptr %11, align 4
  br label %56

33:                                               ; preds = %27
  %34 = load i64, ptr %10, align 8, !tbaa !9
  %35 = icmp slt i64 %34, 0
  br i1 %35, label %36, label %49

36:                                               ; preds = %33
  %37 = call ptr @__errno_location() #5
  %38 = load i32, ptr %37, align 4, !tbaa !15
  %39 = icmp eq i32 %38, 11
  br i1 %39, label %44, label %40

40:                                               ; preds = %36
  %41 = call ptr @__errno_location() #5
  %42 = load i32, ptr %41, align 4, !tbaa !15
  %43 = icmp eq i32 %42, 4
  br i1 %43, label %44, label %45

44:                                               ; preds = %40, %36
  store i32 2, ptr %11, align 4
  br label %56, !llvm.loop !16

45:                                               ; preds = %40
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.file_stream, ptr %46, i32 0, i32 1
  %48 = load ptr, ptr %47, align 8, !tbaa !18
  call void (ptr, ...) @msg_errno(ptr noundef @.str.11, ptr noundef %48)
  store i64 -1, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %56

49:                                               ; preds = %33
  %50 = load i64, ptr %10, align 8, !tbaa !9
  %51 = load ptr, ptr %8, align 8, !tbaa !5
  %52 = getelementptr inbounds i8, ptr %51, i64 %50
  store ptr %52, ptr %8, align 8, !tbaa !5
  %53 = load i64, ptr %10, align 8, !tbaa !9
  %54 = load i64, ptr %7, align 8, !tbaa !9
  %55 = sub i64 %54, %53
  store i64 %55, ptr %7, align 8, !tbaa !9
  store i32 0, ptr %11, align 4
  br label %56

56:                                               ; preds = %49, %45, %44, %32
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %57 = load i32, ptr %11, align 4
  switch i32 %57, label %63 [
    i32 0, label %58
    i32 3, label %59
    i32 2, label %14
  ]

58:                                               ; preds = %56
  br label %14, !llvm.loop !16

59:                                               ; preds = %56, %14
  %60 = load i64, ptr %9, align 8, !tbaa !9
  %61 = load i64, ptr %7, align 8, !tbaa !9
  %62 = sub i64 %60, %61
  store i64 %62, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %63

63:                                               ; preds = %59, %56
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %64 = load i64, ptr %4, align 8
  ret i64 %64
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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 = { "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 = !{!"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 = !{!12, !13, i64 0}
!12 = !{!"file_stream", !13, i64 0, !6, i64 8, !14, i64 16, !6, i64 24, !6, i64 32, !10, i64 40}
!13 = !{!"int", !7, i64 0}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{!13, !13, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!12, !6, i64 8}


; 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.11 = external hidden unnamed_addr constant [22 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @msg_errno(ptr noundef, ...) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local i64 @xread(ptr nocapture noundef readonly %0, ptr nocapture noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %.not32 = icmp eq i64 %2, 0
  br i1 %.not32, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %3, %18
  %.01834 = phi i64 [ %.119, %18 ], [ %2, %3 ]
  %.02133 = phi ptr [ %.122, %18 ], [ %1, %3 ]
  %4 = load i32, ptr %0, align 8, !tbaa !5
  %5 = tail call i64 @llvm.umin.i64(i64 %.01834, i64 2147483647)
  %6 = tail call i64 @read(i32 noundef %4, ptr noundef %.02133, i64 noundef %5) #4
  %7 = icmp eq i64 %6, 0
  br i1 %7, label %.thread, label %8

8:                                                ; preds = %.lr.ph
  %9 = icmp slt i64 %6, 0
  br i1 %9, label %10, label %15

10:                                               ; preds = %8
  %11 = tail call ptr @__errno_location() #5
  %12 = load i32, ptr %11, align 4, !tbaa !13
  switch i32 %12, label %.thread28 [
    i32 11, label %18
    i32 4, label %18
  ], !llvm.loop !14

.thread28:                                        ; preds = %10
  %13 = getelementptr inbounds i8, ptr %0, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !16
  tail call void (ptr, ...) @msg_errno(ptr noundef nonnull @.str.11, ptr noundef %14)
  br label %20

15:                                               ; preds = %8
  %16 = getelementptr inbounds i8, ptr %.02133, i64 %6
  %17 = sub i64 %.01834, %6
  br label %18

18:                                               ; preds = %10, %10, %15
  %.122 = phi ptr [ %16, %15 ], [ %.02133, %10 ], [ %.02133, %10 ]
  %.119 = phi i64 [ %17, %15 ], [ %.01834, %10 ], [ %.01834, %10 ]
  %.not = icmp eq i64 %.119, 0
  br i1 %.not, label %.thread, label %.lr.ph

.thread:                                          ; preds = %18, %.lr.ph, %3
  %.220 = phi i64 [ 0, %3 ], [ %.01834, %.lr.ph ], [ 0, %18 ]
  %19 = sub i64 %2, %.220
  br label %20

20:                                               ; preds = %.thread28, %.thread
  %.2 = phi i64 [ %19, %.thread ], [ -1, %.thread28 ]
  ret i64 %.2
}

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

; 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 = { 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 #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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !7, i64 0}
!6 = !{!"file_stream", !7, i64 0, !10, i64 8, !11, i64 16, !10, i64 24, !10, i64 32, !12, i64 40}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"_Bool", !8, i64 0}
!12 = !{!"long", !8, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!6, !10, i64 8}


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