; LLVM IR from InductiveComputerScience/infracore :: drivers/disk-linux-file (disk-linux-file)

; 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.DiskStructure = type { ptr }
%struct.DiskStructureLinux = type { i32, %union.sem_t, ptr, i64, i64 }
%union.sem_t = type { i64, [24 x i8] }
%struct.ByteArrayReference = type { ptr, i64 }

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

declare i64 @sys_lseek(i32 noundef, i64 noundef, i32 noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @Read(ptr noundef %0, double noundef %1, ptr noundef %2) #3 {
  %4 = alloca ptr, align 8
  %5 = alloca double, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i8, align 1
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store double %1, ptr %5, align 8, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.DiskStructure, ptr %14, i32 0, i32 0
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  store ptr %16, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.DiskStructureLinux, ptr %17, i32 0, i32 4
  %19 = load i64, ptr %18, align 8, !tbaa !13
  store i64 %19, ptr %13, align 8, !tbaa !17
  %20 = load double, ptr %5, align 8, !tbaa !9
  %21 = fptoui double %20 to i64
  store i64 %21, ptr %12, align 8, !tbaa !17
  store i8 1, ptr %8, align 1, !tbaa !18
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.DiskStructureLinux, ptr %22, i32 0, i32 1
  %24 = call i32 @sem_wait(ptr noundef %23)
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.DiskStructureLinux, ptr %25, i32 0, i32 0
  %27 = load i32, ptr %26, align 8, !tbaa !20
  %28 = load i64, ptr %12, align 8, !tbaa !17
  %29 = load i64, ptr %13, align 8, !tbaa !17
  %30 = mul i64 %28, %29
  %31 = call i64 @sys_lseek(i32 noundef %27, i64 noundef %30, i32 noundef 0)
  store i64 %31, ptr %11, align 8, !tbaa !17
  %32 = load i64, ptr %11, align 8, !tbaa !17
  %33 = icmp ne i64 %32, -1
  br i1 %33, label %34, label %69

34:                                               ; preds = %3
  store i64 0, ptr %10, align 8, !tbaa !17
  br label %35

35:                                               ; preds = %67, %34
  %36 = load i64, ptr %10, align 8, !tbaa !17
  %37 = load i64, ptr %13, align 8, !tbaa !17
  %38 = icmp ult i64 %36, %37
  br i1 %38, label %39, label %42

39:                                               ; preds = %35
  %40 = load i8, ptr %8, align 1, !tbaa !18, !range !21, !noundef !22
  %41 = trunc i8 %40 to i1
  br label %42

42:                                               ; preds = %39, %35
  %43 = phi i1 [ false, %35 ], [ %41, %39 ]
  br i1 %43, label %44, label %68

44:                                               ; preds = %42
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.DiskStructureLinux, ptr %45, i32 0, i32 0
  %47 = load i32, ptr %46, align 8, !tbaa !20
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.ByteArrayReference, ptr %48, i32 0, i32 0
  %50 = load ptr, ptr %49, align 8, !tbaa !23
  %51 = load i64, ptr %10, align 8, !tbaa !17
  %52 = getelementptr inbounds i8, ptr %50, i64 %51
  %53 = load i64, ptr %13, align 8, !tbaa !17
  %54 = load i64, ptr %10, align 8, !tbaa !17
  %55 = sub i64 %53, %54
  %56 = call i64 @sys_read(i32 noundef %47, ptr noundef %52, i64 noundef %55)
  store i64 %56, ptr %9, align 8, !tbaa !17
  %57 = load i64, ptr %9, align 8, !tbaa !17
  %58 = icmp ne i64 %57, 0
  br i1 %58, label %59, label %66

59:                                               ; preds = %44
  %60 = load i64, ptr %9, align 8, !tbaa !17
  %61 = icmp slt i64 %60, 0
  br i1 %61, label %62, label %66

62:                                               ; preds = %59
  %63 = load i64, ptr %9, align 8, !tbaa !17
  %64 = load i64, ptr %10, align 8, !tbaa !17
  %65 = add nsw i64 %64, %63
  store i64 %65, ptr %10, align 8, !tbaa !17
  br label %67

66:                                               ; preds = %59, %44
  store i8 0, ptr %8, align 1, !tbaa !18
  br label %67

67:                                               ; preds = %66, %62
  br label %35, !llvm.loop !25

68:                                               ; preds = %42
  br label %70

69:                                               ; preds = %3
  store i8 0, ptr %8, align 1, !tbaa !18
  br label %70

70:                                               ; preds = %69, %68
  %71 = load ptr, ptr %7, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.DiskStructureLinux, ptr %71, i32 0, i32 1
  %73 = call i32 @sem_post(ptr noundef %72) #4
  %74 = load i8, ptr %8, align 1, !tbaa !18, !range !21, !noundef !22
  %75 = trunc i8 %74 to i1
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret i1 %75
}

declare i64 @sys_read(i32 noundef, ptr noundef, i64 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = { 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 #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 = !{!"double", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"DiskStructure", !6, i64 0}
!13 = !{!14, !16, i64 56}
!14 = !{!"DiskStructureLinux", !15, i64 0, !7, i64 8, !6, i64 40, !16, i64 48, !16, i64 56}
!15 = !{!"int", !7, i64 0}
!16 = !{!"long", !7, i64 0}
!17 = !{!16, !16, i64 0}
!18 = !{!19, !19, i64 0}
!19 = !{!"_Bool", !7, i64 0}
!20 = !{!14, !15, i64 0}
!21 = !{i8 0, i8 2}
!22 = !{}
!23 = !{!24, !6, i64 0}
!24 = !{!"ByteArrayReference", !6, i64 0, !16, i64 8}
!25 = distinct !{!25, !26}
!26 = !{!"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"

declare i32 @sem_wait(ptr noundef) local_unnamed_addr #0

declare i64 @sys_lseek(i32 noundef, i64 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind
declare i32 @sem_post(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @Read(ptr nocapture noundef readonly %0, double noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #2 {
  %4 = load ptr, ptr %0, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %4, i64 56
  %6 = load i64, ptr %5, align 8, !tbaa !10
  %7 = fptoui double %1 to i64
  %8 = getelementptr inbounds i8, ptr %4, i64 8
  %9 = tail call i32 @sem_wait(ptr noundef nonnull %8) #4
  %10 = load i32, ptr %4, align 8, !tbaa !14
  %11 = mul i64 %6, %7
  %12 = tail call i64 @sys_lseek(i32 noundef %10, i64 noundef %11, i32 noundef 0) #4
  %.not = icmp eq i64 %12, -1
  br i1 %.not, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %3
  %.not28 = icmp eq i64 %6, 0
  br i1 %.not28, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.027 = phi i8 [ %.1, %.lr.ph ], [ 1, %.preheader ]
  %.02226 = phi i64 [ %.123, %.lr.ph ], [ 0, %.preheader ]
  %13 = load i32, ptr %4, align 8, !tbaa !14
  %14 = load ptr, ptr %2, align 8, !tbaa !15
  %15 = getelementptr inbounds i8, ptr %14, i64 %.02226
  %16 = sub i64 %6, %.02226
  %17 = tail call i64 @sys_read(i32 noundef %13, ptr noundef %15, i64 noundef %16) #4
  %18 = icmp slt i64 %17, 0
  %19 = tail call i64 @llvm.smin.i64(i64 %17, i64 0)
  %.123 = add nsw i64 %19, %.02226
  %.1 = select i1 %18, i8 %.027, i8 0
  %20 = icmp ult i64 %.123, %6
  %21 = trunc nuw i8 %.1 to i1
  %22 = select i1 %20, i1 %21, i1 false
  br i1 %22, label %.lr.ph, label %.loopexit.loopexit, !llvm.loop !17

.loopexit.loopexit:                               ; preds = %.lr.ph
  %23 = trunc nuw i8 %.1 to i1
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit, %.preheader, %3
  %.2 = phi i1 [ false, %3 ], [ true, %.preheader ], [ %23, %.loopexit.loopexit ]
  %24 = tail call i32 @sem_post(ptr noundef nonnull %8) #4
  ret i1 %.2
}

declare i64 @sys_read(i32 noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

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

attributes #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 = { 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 = { 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 = { 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 = !{!6, !7, i64 0}
!6 = !{!"DiskStructure", !7, i64 0}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !13, i64 56}
!11 = !{!"DiskStructureLinux", !12, i64 0, !8, i64 8, !7, i64 40, !13, i64 48, !13, i64 56}
!12 = !{!"int", !8, i64 0}
!13 = !{!"long", !8, i64 0}
!14 = !{!11, !12, i64 0}
!15 = !{!16, !7, i64 0}
!16 = !{!"ByteArrayReference", !7, i64 0, !13, i64 8}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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