; LLVM IR from kala13x/libxutils :: src/sys (xfs)

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

; 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 zeroext i8 @XFile_IsOpen(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @XFile_Read(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  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
  %9 = load ptr, ptr %5, align 8, !tbaa !5
  %10 = call zeroext i8 @XFile_IsOpen(ptr noundef %9)
  %11 = icmp ne i8 %10, 0
  br i1 %11, label %13, label %12

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

13:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store i64 0, ptr %8, align 8, !tbaa !9
  br label %14

14:                                               ; preds = %28, %13
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.XFile, ptr %15, i32 0, i32 7
  %17 = load i32, ptr %16, align 8, !tbaa !11
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = load i64, ptr %7, align 8, !tbaa !9
  %20 = call i64 @read(i32 noundef %17, ptr noundef %18, i64 noundef %19)
  store i64 %20, ptr %8, align 8, !tbaa !9
  br label %21

21:                                               ; preds = %14
  %22 = load i64, ptr %8, align 8, !tbaa !9
  %23 = icmp slt i64 %22, 0
  br i1 %23, label %24, label %28

24:                                               ; preds = %21
  %25 = call ptr @__errno_location() #5
  %26 = load i32, ptr %25, align 4, !tbaa !14
  %27 = icmp eq i32 %26, 4
  br label %28

28:                                               ; preds = %24, %21
  %29 = phi i1 [ false, %21 ], [ %27, %24 ]
  br i1 %29, label %14, label %30, !llvm.loop !15

30:                                               ; preds = %28
  %31 = load i64, ptr %8, align 8, !tbaa !9
  %32 = icmp sle i64 %31, 0
  br i1 %32, label %33, label %40

33:                                               ; preds = %30
  %34 = call ptr @__errno_location() #5
  %35 = load i32, ptr %34, align 4, !tbaa !14
  %36 = icmp ne i32 %35, 11
  br i1 %36, label %37, label %40

37:                                               ; preds = %33
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.XFile, ptr %38, i32 0, i32 4
  store i8 1, ptr %39, align 8, !tbaa !17
  br label %40

40:                                               ; preds = %37, %33, %30
  %41 = load i64, ptr %8, align 8, !tbaa !9
  %42 = trunc i64 %41 to i32
  store i32 %42, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %43

43:                                               ; preds = %40, %12
  %44 = load i32, ptr %4, align 4
  ret i32 %44
}

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

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #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 = { "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 }
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 40}
!12 = !{!"XFile", !10, i64 0, !13, i64 8, !10, i64 16, !10, i64 24, !7, i64 32, !7, i64 33, !13, i64 36, !13, i64 40}
!13 = !{!"int", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!12, !7, i64 32}


; 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: nounwind uwtable
declare dso_local zeroext i8 @XFile_IsOpen(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @XFile_Read(ptr noundef %0, ptr nocapture noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = tail call zeroext i8 @XFile_IsOpen(ptr noundef %0)
  %.not = icmp eq i8 %4, 0
  br i1 %.not, label %20, label %.preheader

.preheader:                                       ; preds = %3
  %5 = getelementptr inbounds i8, ptr %0, i64 40
  br label %6

6:                                                ; preds = %.preheader, %10
  %7 = load i32, ptr %5, align 8, !tbaa !5
  %8 = tail call i64 @read(i32 noundef %7, ptr noundef %1, i64 noundef %2) #3
  %9 = icmp slt i64 %8, 0
  br i1 %9, label %10, label %.critedge

10:                                               ; preds = %6
  %11 = tail call ptr @__errno_location() #4
  %12 = load i32, ptr %11, align 4, !tbaa !11
  %13 = icmp eq i32 %12, 4
  br i1 %13, label %6, label %.critedge.thread, !llvm.loop !12

.critedge:                                        ; preds = %6
  %14 = icmp eq i64 %8, 0
  br i1 %14, label %.critedge..critedge.thread_crit_edge, label %18

.critedge..critedge.thread_crit_edge:             ; preds = %.critedge
  %.pre = tail call ptr @__errno_location() #4
  %.pr = load i32, ptr %.pre, align 4, !tbaa !11
  br label %.critedge.thread

.critedge.thread:                                 ; preds = %10, %.critedge..critedge.thread_crit_edge
  %15 = phi i32 [ %.pr, %.critedge..critedge.thread_crit_edge ], [ %12, %10 ]
  %.not8 = icmp eq i32 %15, 11
  br i1 %.not8, label %18, label %16

16:                                               ; preds = %.critedge.thread
  %17 = getelementptr inbounds i8, ptr %0, i64 32
  store i8 1, ptr %17, align 8, !tbaa !14
  br label %18

18:                                               ; preds = %16, %.critedge.thread, %.critedge
  %19 = trunc i64 %8 to i32
  br label %20

20:                                               ; preds = %3, %18
  %.0 = phi i32 [ %19, %18 ], [ -1, %3 ]
  ret i32 %.0
}

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

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

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 "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 }
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, !10, i64 40}
!6 = !{!"XFile", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 24, !8, i64 32, !8, i64 33, !10, i64 36, !10, i64 40}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!10, !10, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!6, !8, i64 32}


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