; LLVM IR from blat-blatnik/Painted-Portal-Ray-Tracing :: src/utils.ll (Z13readWholeFilePKcPmd)

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

@.str = external hidden unnamed_addr constant [3 x i8], align 1

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

15:                                               ; preds = %32, %3
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = call noalias ptr @fopen(ptr noundef %16, ptr noundef @.str)
  store ptr %17, ptr %8, align 8, !tbaa !5
  %18 = call i64 @glfwGetTimerValue()
  store i64 %18, ptr %10, align 8, !tbaa !11
  br label %19

19:                                               ; preds = %15
  %20 = load ptr, ptr %8, align 8, !tbaa !5
  %21 = icmp ne ptr %20, null
  br i1 %21, label %32, label %22

22:                                               ; preds = %19
  %23 = load i64, ptr %10, align 8, !tbaa !11
  %24 = load i64, ptr %9, align 8, !tbaa !11
  %25 = sub i64 %23, %24
  %26 = uitofp i64 %25 to double
  %27 = call i64 @glfwGetTimerFrequency()
  %28 = uitofp i64 %27 to double
  %29 = fdiv double %26, %28
  %30 = load double, ptr %7, align 8, !tbaa !9
  %31 = fcmp olt double %29, %30
  br label %32

32:                                               ; preds = %22, %19
  %33 = phi i1 [ false, %19 ], [ %31, %22 ]
  br i1 %33, label %15, label %34, !llvm.loop !13

34:                                               ; preds = %32
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  %36 = icmp ne ptr %35, null
  br i1 %36, label %37, label %64

37:                                               ; preds = %34
  %38 = load ptr, ptr %8, align 8, !tbaa !5
  %39 = call i32 @fseek(ptr noundef %38, i64 noundef 0, i32 noundef 2)
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %40 = load ptr, ptr %8, align 8, !tbaa !5
  %41 = call i64 @ftell(ptr noundef %40)
  store i64 %41, ptr %11, align 8, !tbaa !11
  %42 = load ptr, ptr %8, align 8, !tbaa !5
  %43 = call i32 @fseek(ptr noundef %42, i64 noundef 0, i32 noundef 0)
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %44 = load i64, ptr %11, align 8, !tbaa !11
  %45 = add i64 %44, 1
  %46 = call noalias ptr @malloc(i64 noundef %45) #5
  store ptr %46, ptr %12, align 8, !tbaa !5
  %47 = load ptr, ptr %12, align 8, !tbaa !5
  %48 = icmp ne ptr %47, null
  br i1 %48, label %50, label %49

49:                                               ; preds = %37
  store ptr null, ptr %4, align 8
  store i32 1, ptr %13, align 4
  br label %63

50:                                               ; preds = %37
  %51 = load ptr, ptr %12, align 8, !tbaa !5
  %52 = load i64, ptr %11, align 8, !tbaa !11
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  %54 = call i64 @fread(ptr noundef %51, i64 noundef 1, i64 noundef %52, ptr noundef %53)
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  %56 = call i32 @fclose(ptr noundef %55)
  %57 = load ptr, ptr %12, align 8, !tbaa !5
  %58 = load i64, ptr %11, align 8, !tbaa !11
  %59 = getelementptr inbounds i8, ptr %57, i64 %58
  store i8 0, ptr %59, align 1, !tbaa !15
  %60 = load i64, ptr %11, align 8, !tbaa !11
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  store i64 %60, ptr %61, align 8, !tbaa !11
  %62 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %62, ptr %4, align 8
  store i32 1, ptr %13, align 4
  br label %63

63:                                               ; preds = %50, %49
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  br label %65

64:                                               ; preds = %34
  store ptr null, ptr %4, align 8
  store i32 1, ptr %13, align 4
  br label %65

65:                                               ; preds = %64, %63
  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 8, ptr %8) #4
  %66 = load ptr, ptr %4, align 8
  ret ptr %66
}

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

declare i64 @glfwGetTimerValue() #2

declare noalias ptr @fopen(ptr noundef, ptr noundef) #2

declare i64 @glfwGetTimerFrequency() #2

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

declare i64 @ftell(ptr noundef) #2

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #3

declare i64 @fread(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #2

declare i32 @fclose(ptr noundef) #2

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

attributes #0 = { mustprogress 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 = { "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 allocsize(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 }
attributes #5 = { nounwind allocsize(0) }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!7, !7, i64 0}


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

; Function Attrs: mustprogress uwtable
define dso_local noalias noundef ptr @_Z13readWholeFilePKcPmd(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1, double noundef %2) local_unnamed_addr #0 {
  %4 = tail call i64 @glfwGetTimerValue()
  br label %5

5:                                                ; preds = %8, %3
  %6 = tail call noalias ptr @fopen(ptr noundef %0, ptr noundef nonnull @.str)
  %7 = tail call i64 @glfwGetTimerValue()
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %8, label %.critedge23

8:                                                ; preds = %5
  %9 = sub i64 %7, %4
  %10 = uitofp i64 %9 to double
  %11 = tail call i64 @glfwGetTimerFrequency()
  %12 = uitofp i64 %11 to double
  %13 = fdiv double %10, %12
  %14 = fcmp olt double %13, %2
  br i1 %14, label %5, label %.critedge, !llvm.loop !5

.critedge23:                                      ; preds = %5
  %15 = tail call i32 @fseek(ptr noundef nonnull %6, i64 noundef 0, i32 noundef 2)
  %16 = tail call i64 @ftell(ptr noundef nonnull %6)
  %17 = tail call i32 @fseek(ptr noundef nonnull %6, i64 noundef 0, i32 noundef 0)
  %18 = add i64 %16, 1
  %19 = tail call noalias ptr @malloc(i64 noundef %18) #4
  %.not22 = icmp eq ptr %19, null
  br i1 %.not22, label %.critedge, label %20

20:                                               ; preds = %.critedge23
  %21 = tail call i64 @fread(ptr noundef nonnull %19, i64 noundef 1, i64 noundef %16, ptr noundef nonnull %6)
  %22 = tail call i32 @fclose(ptr noundef nonnull %6)
  %23 = getelementptr inbounds i8, ptr %19, i64 %16
  store i8 0, ptr %23, align 1, !tbaa !7
  store i64 %16, ptr %1, align 8, !tbaa !10
  br label %.critedge

.critedge:                                        ; preds = %8, %20, %.critedge23
  %.1 = phi ptr [ %19, %20 ], [ null, %.critedge23 ], [ null, %8 ]
  ret ptr %.1
}

declare i64 @glfwGetTimerValue() local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #2

declare i64 @glfwGetTimerFrequency() local_unnamed_addr #1

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

; Function Attrs: nofree nounwind
declare noundef i64 @ftell(ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #3

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

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

attributes #0 = { mustprogress 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 = { "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 "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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(0) }

!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"}
!7 = !{!8, !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !8, i64 0}


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