; LLVM IR from AlgoGenesis/C :: Miscellaneous Algorithms/Adaptive Disk Reordering Algorithm (ADR)

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

@.str = external hidden unnamed_addr constant [18 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [22 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [25 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @abs_diff(i32 noundef, i32 noundef) #0

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

16:                                               ; preds = %69, %3
  %17 = load i32, ptr %9, align 4, !tbaa !9
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %16
  store i32 2, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %72

21:                                               ; preds = %16
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = load i32, ptr %5, align 4, !tbaa !9
  %24 = load i32, ptr %7, align 4, !tbaa !9
  call void @reorder_requests(ptr noundef %22, i32 noundef %23, i32 noundef %24)
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %64, %21
  %26 = load i32, ptr %11, align 4, !tbaa !9
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = icmp slt i32 %26, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %25
  store i32 5, ptr %10, align 4
  br label %67

30:                                               ; preds = %25
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = load i32, ptr %11, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds %struct.DiskRequest, ptr %31, i64 %33
  %35 = getelementptr inbounds %struct.DiskRequest, ptr %34, i32 0, i32 1
  %36 = load i32, ptr %35, align 4, !tbaa !11
  %37 = icmp ne i32 %36, 0
  br i1 %37, label %63, label %38

38:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %39 = load i32, ptr %7, align 4, !tbaa !9
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = load i32, ptr %11, align 4, !tbaa !9
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds %struct.DiskRequest, ptr %40, i64 %42
  %44 = getelementptr inbounds %struct.DiskRequest, ptr %43, i32 0, i32 0
  %45 = load i32, ptr %44, align 4, !tbaa !13
  %46 = call i32 @abs_diff(i32 noundef %39, i32 noundef %45)
  store i32 %46, ptr %12, align 4, !tbaa !9
  %47 = load i32, ptr %12, align 4, !tbaa !9
  %48 = load i32, ptr %8, align 4, !tbaa !9
  %49 = add nsw i32 %48, %47
  store i32 %49, ptr %8, align 4, !tbaa !9
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = load i32, ptr %11, align 4, !tbaa !9
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds %struct.DiskRequest, ptr %50, i64 %52
  %54 = getelementptr inbounds %struct.DiskRequest, ptr %53, i32 0, i32 0
  %55 = load i32, ptr %54, align 4, !tbaa !13
  store i32 %55, ptr %7, align 4, !tbaa !9
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = load i32, ptr %11, align 4, !tbaa !9
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds %struct.DiskRequest, ptr %56, i64 %58
  %60 = getelementptr inbounds %struct.DiskRequest, ptr %59, i32 0, i32 1
  store i32 1, ptr %60, align 4, !tbaa !11
  %61 = load i32, ptr %7, align 4, !tbaa !9
  %62 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %61)
  store i32 5, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %67

63:                                               ; preds = %30
  br label %64

64:                                               ; preds = %63
  %65 = load i32, ptr %11, align 4, !tbaa !9
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %11, align 4, !tbaa !9
  br label %25, !llvm.loop !14

67:                                               ; preds = %38, %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %68

68:                                               ; preds = %67
  br label %69

69:                                               ; preds = %68
  %70 = load i32, ptr %9, align 4, !tbaa !9
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %9, align 4, !tbaa !9
  br label %16, !llvm.loop !16

72:                                               ; preds = %20
  %73 = load i32, ptr %8, align 4, !tbaa !9
  %74 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %73)
  %75 = load i32, ptr %8, align 4, !tbaa !9
  %76 = sitofp i32 %75 to float
  %77 = load i32, ptr %5, align 4, !tbaa !9
  %78 = sitofp i32 %77 to float
  %79 = fdiv float %76, %78
  %80 = fpext float %79 to double
  %81 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, double noundef %80)
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret void
}

declare i32 @printf(ptr noundef, ...) #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 = { 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 }

!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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 4}
!12 = !{!"", !10, i64 0, !10, i64 4}
!13 = !{!12, !10, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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

%struct.DiskRequest = type { i32, i32 }

@.str = external hidden unnamed_addr constant [18 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [22 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [25 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @abs_diff(i32 noundef, i32 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local void @adaptive_disk_reordering(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %2)
  %5 = icmp sgt i32 %1, 0
  br i1 %5, label %.lr.ph.us.preheader, label %._crit_edge

.lr.ph.us.preheader:                              ; preds = %3
  %wide.trip.count = zext nneg i32 %1 to i64
  br label %.lr.ph.us

.lr.ph.us:                                        ; preds = %.lr.ph.us.preheader, %..loopexit_crit_edge.us
  %.036.us = phi i32 [ %.1.us, %..loopexit_crit_edge.us ], [ %2, %.lr.ph.us.preheader ]
  %.02635.us = phi i32 [ %18, %..loopexit_crit_edge.us ], [ 0, %.lr.ph.us.preheader ]
  %.02734.us = phi i32 [ %.128.us, %..loopexit_crit_edge.us ], [ 0, %.lr.ph.us.preheader ]
  tail call void @reorder_requests(ptr noundef %0, i32 noundef %1, i32 noundef %.036.us)
  br label %7

6:                                                ; preds = %7
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %..loopexit_crit_edge.us, label %7, !llvm.loop !5

7:                                                ; preds = %.lr.ph.us, %6
  %indvars.iv = phi i64 [ 0, %.lr.ph.us ], [ %indvars.iv.next, %6 ]
  %8 = getelementptr inbounds %struct.DiskRequest, ptr %0, i64 %indvars.iv
  %9 = getelementptr inbounds i8, ptr %8, i64 4
  %10 = load i32, ptr %9, align 4, !tbaa !7
  %.not.us = icmp eq i32 %10, 0
  br i1 %.not.us, label %11, label %6

11:                                               ; preds = %7
  %12 = getelementptr inbounds i8, ptr %8, i64 4
  %13 = load i32, ptr %8, align 4, !tbaa !12
  %14 = tail call i32 @abs_diff(i32 noundef %.036.us, i32 noundef %13)
  %15 = add nsw i32 %14, %.02734.us
  %16 = load i32, ptr %8, align 4, !tbaa !12
  store i32 1, ptr %12, align 4, !tbaa !7
  %17 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %16)
  br label %..loopexit_crit_edge.us

..loopexit_crit_edge.us:                          ; preds = %6, %11
  %.128.us = phi i32 [ %15, %11 ], [ %.02734.us, %6 ]
  %.1.us = phi i32 [ %16, %11 ], [ %.036.us, %6 ]
  %18 = add nuw nsw i32 %.02635.us, 1
  %exitcond40.not = icmp eq i32 %18, %1
  br i1 %exitcond40.not, label %._crit_edge, label %.lr.ph.us, !llvm.loop !13

._crit_edge:                                      ; preds = %..loopexit_crit_edge.us, %3
  %.027.lcssa = phi i32 [ 0, %3 ], [ %.128.us, %..loopexit_crit_edge.us ]
  %19 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %.027.lcssa)
  %20 = sitofp i32 %.027.lcssa to float
  %21 = sitofp i32 %1 to float
  %22 = fdiv float %20, %21
  %23 = fpext float %22 to double
  %24 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, double noundef %23)
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

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

!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, !9, i64 4}
!8 = !{!"", !9, i64 0, !9, i64 4}
!9 = !{!"int", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C/C++ TBAA"}
!12 = !{!8, !9, i64 0}
!13 = distinct !{!13, !6}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass